From 804a58e779e8bbb5c8c2d53211626dcfd5556ed7 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 22 Jun 2015 03:16:32 +0200 Subject: instrument: some updates to asymmetric decrypt tests --- OpenKeychain/src/androidTest/assets/ci.png | Bin 0 -> 7230 bytes OpenKeychain/src/androidTest/assets/pa.png | Bin 0 -> 4440 bytes OpenKeychain/src/androidTest/assets/re.png | Bin 0 -> 7085 bytes .../keychain/AsymmetricOperationTests.java | 243 --------------- .../sufficientlysecure/keychain/TestHelpers.java | 40 ++- .../keychain/actions/OrientationChangeAction.java | 74 +++++ .../keychain/ui/AsymmetricFileOperationTests.java | 347 +++++++++++++++++++++ .../keychain/ui/AsymmetricTextOperationTests.java | 243 +++++++++++++++ .../keychain/ui/MiscFileOperationTests.java | 151 +++++++++ 9 files changed, 854 insertions(+), 244 deletions(-) create mode 100644 OpenKeychain/src/androidTest/assets/ci.png create mode 100644 OpenKeychain/src/androidTest/assets/pa.png create mode 100644 OpenKeychain/src/androidTest/assets/re.png delete mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/AsymmetricOperationTests.java create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/OrientationChangeAction.java create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricFileOperationTests.java create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricTextOperationTests.java create mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/MiscFileOperationTests.java (limited to 'OpenKeychain/src/androidTest') diff --git a/OpenKeychain/src/androidTest/assets/ci.png b/OpenKeychain/src/androidTest/assets/ci.png new file mode 100644 index 000000000..3a6117082 Binary files /dev/null and b/OpenKeychain/src/androidTest/assets/ci.png differ diff --git a/OpenKeychain/src/androidTest/assets/pa.png b/OpenKeychain/src/androidTest/assets/pa.png new file mode 100644 index 000000000..3c6aa7fda Binary files /dev/null and b/OpenKeychain/src/androidTest/assets/pa.png differ diff --git a/OpenKeychain/src/androidTest/assets/re.png b/OpenKeychain/src/androidTest/assets/re.png new file mode 100644 index 000000000..a441bbc87 Binary files /dev/null and b/OpenKeychain/src/androidTest/assets/re.png differ diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/AsymmetricOperationTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/AsymmetricOperationTests.java deleted file mode 100644 index 246e2dd52..000000000 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/AsymmetricOperationTests.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright (C) 2015 Vincent Breitmoser - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain; - - -import android.app.Activity; -import android.content.Intent; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.test.suitebuilder.annotation.LargeTest; -import android.widget.AdapterView; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.sufficientlysecure.keychain.service.PassphraseCacheService; -import org.sufficientlysecure.keychain.ui.MainActivity; -import org.sufficientlysecure.keychain.ui.util.Notify.Style; - -import static android.support.test.espresso.Espresso.onData; -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.Espresso.pressBack; -import static android.support.test.espresso.action.ViewActions.click; -import static android.support.test.espresso.action.ViewActions.typeText; -import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; -import static android.support.test.espresso.assertion.ViewAssertions.matches; -import static android.support.test.espresso.contrib.DrawerActions.openDrawer; -import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; -import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; -import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA; -import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; -import static android.support.test.espresso.matcher.ViewMatchers.withId; -import static android.support.test.espresso.matcher.ViewMatchers.withText; -import static org.hamcrest.CoreMatchers.allOf; -import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar; -import static org.sufficientlysecure.keychain.TestHelpers.importKeysFromResource; -import static org.sufficientlysecure.keychain.TestHelpers.randomString; -import static org.sufficientlysecure.keychain.actions.CustomActions.tokenEncryptViewAddToken; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.isRecyclerItemView; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withDisplayedChild; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withEncryptionStatus; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withKeyItemId; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureMyKey; -import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureNone; - - -@RunWith(AndroidJUnit4.class) -@LargeTest -public class AsymmetricOperationTests { - - @Rule - public final ActivityTestRule mActivity - = new ActivityTestRule(MainActivity.class) { - @Override - protected Intent getActivityIntent() { - Intent intent = super.getActivityIntent(); - intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true); - return intent; - } - }; - - @Before - public void setUp() throws Exception { - Activity activity = mActivity.getActivity(); - - // import these two, make sure they're there - importKeysFromResource(activity, "x.sec.asc"); - - // make sure no passphrases are cached - PassphraseCacheService.clearCachedPassphrases(activity); - } - - @Test - public void testTextEncryptDecryptFromToken() throws Exception { - - // navigate to 'encrypt text' - openDrawer(R.id.drawer_layout); - onView(withText(R.string.nav_encrypt_decrypt)).perform(click()); - onView(withId(R.id.encrypt_text)).perform(click()); - - String cleartext = randomString(10, 30); - - { // encrypt - - // the EncryptKeyCompletionView is tested individually - onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(0))); - onView(withId(R.id.recipient_list)).perform(tokenEncryptViewAddToken(0x9D604D2F310716A3L)); - onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(1))); - - onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); - - onView(withId(R.id.encrypt_copy)).perform(click()); - } - - // go to decrypt from clipboard view - pressBack(); - onView(withId(R.id.decrypt_from_clipboard)).perform(click()); - - { // decrypt - onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); - onView(withText(R.string.btn_unlock)).perform(click()); - - - onView(isRecyclerItemView(R.id.decrypted_files_list, - hasDescendant(withText(R.string.filename_unknown_text)))) - .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); - - } - - } - - @Test - public void testTextEncryptDecryptFromKeyView() throws Exception { - - String cleartext = randomString(10, 30); - - { // encrypt - - // navigate to edit key dialog - onData(withKeyItemId(0x9D604D2F310716A3L)) - .inAdapterView(allOf(isAssignableFrom(AdapterView.class), - isDescendantOfA(withId(R.id.key_list_list)))) - .perform(click()); - onView(withId(R.id.view_key_action_encrypt_text)).perform(click()); - - // make sure the encrypt is correctly set - onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(1))); - - onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); - - onView(withId(R.id.encrypt_copy)).perform(click()); - } - - // go to decrypt from clipboard view - pressBack(); - pressBack(); - - openDrawer(R.id.drawer_layout); - onView(withText(R.string.nav_encrypt_decrypt)).perform(click()); - onView(withId(R.id.decrypt_from_clipboard)).perform(click()); - - { // decrypt - - onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); - onView(withText(R.string.btn_unlock)).perform(click()); - - onView(isRecyclerItemView(R.id.decrypted_files_list, - hasDescendant(withText(R.string.filename_unknown_text)))) - .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); - - } - - pressBack(); - onView(withId(R.id.decrypt_from_clipboard)).perform(click()); - - { // decrypt again, passphrase should be cached - - onView(isRecyclerItemView(R.id.decrypted_files_list, - hasDescendant(withText(R.string.filename_unknown_text)))) - .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); - - } - - } - - @Test - public void testSignVerify() throws Exception { - - String cleartext = randomString(10, 30); - - // navigate to 'encrypt text' - openDrawer(R.id.drawer_layout); - onView(withText(R.string.nav_encrypt_decrypt)).perform(click()); - onView(withId(R.id.encrypt_text)).perform(click()); - - { // sign - - onView(withId(R.id.encrypt_copy)).perform(click()); - checkSnackbar(Style.ERROR, R.string.error_empty_text); - - onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(0))); - onView(withId(R.id.sign)).perform(click()); - onData(withKeyItemId(0x9D604D2F310716A3L)) - .inAdapterView(isAssignableFrom(AdapterView.class)) - .perform(click()); - onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(1))); - - onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); - - onView(withId(R.id.encrypt_copy)).perform(click()); - - onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); - onView(withText(R.string.btn_unlock)).perform(click()); - - checkSnackbar(Style.OK, R.string.msg_se_success); - - } - - // go to decrypt from clipboard view - pressBack(); - - onView(withId(R.id.decrypt_from_clipboard)).perform(click()); - - { // decrypt - - onView(isRecyclerItemView(R.id.decrypted_files_list, - hasDescendant(withText(R.string.filename_unknown)))) - .check(matches(allOf(withEncryptionStatus(false), withSignatureMyKey()))); - - // open context menu - onView(allOf(isDescendantOfA(isRecyclerItemView(R.id.decrypted_files_list, - hasDescendant(withText(R.string.filename_unknown)))), - withId(R.id.context_menu))).perform(click()); - - // "delete file" shouldn't be there - onView(withText(R.string.btn_delete_original)).check(doesNotExist()); - - // check if log looks ok - onView(withText(R.string.view_log)).perform(click()); - onView(withText(R.string.msg_dc_clear_signature_ok)).check(matches(isDisplayed())); - pressBack(); - - } - - } - -} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java index 4c058940b..bbf69f73e 100644 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/TestHelpers.java @@ -18,6 +18,13 @@ package org.sufficientlysecure.keychain; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Random; import android.content.Context; @@ -55,7 +62,7 @@ public class TestHelpers { } - static void importKeysFromResource(Context context, String name) throws Exception { + public static void importKeysFromResource(Context context, String name) throws Exception { IteratorWithIOThrow stream = UncachedKeyRing.fromStream( getInstrumentation().getContext().getAssets().open(name)); @@ -71,6 +78,37 @@ public class TestHelpers { } + public static void copyFiles() throws IOException { + File cacheDir = getInstrumentation().getTargetContext().getFilesDir(); + byte[] buf = new byte[256]; + for (String filename : FILES) { + File outFile = new File(cacheDir, filename); + if (outFile.exists()) { + continue; + } + InputStream in = new BufferedInputStream(getInstrumentation().getContext().getAssets().open(filename)); + OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); + int len; + while( (len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + } + } + + public static final String[] FILES = new String[] { "pa.png", "re.png", "ci.png" }; + public static File[] getImageNames() { + File cacheDir = getInstrumentation().getTargetContext().getFilesDir(); + File[] ret = new File[FILES.length]; + for (int i = 0; i < ret.length; i++) { + ret[i] = new File(cacheDir, FILES[i]); + } + return ret; + } + + public static T pickRandom(T[] haystack) { + return haystack[new Random().nextInt(haystack.length)]; + } + public static String randomString(int min, int max) { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@#$%^&*()-_="; Random r = new Random(); diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/OrientationChangeAction.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/OrientationChangeAction.java new file mode 100644 index 000000000..cdded7d7f --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/actions/OrientationChangeAction.java @@ -0,0 +1,74 @@ +package org.sufficientlysecure.keychain.actions; + + +import java.util.Collection; + +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; +import android.content.pm.ActivityInfo; +import android.support.test.espresso.UiController; +import android.support.test.espresso.ViewAction; +import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; +import android.support.test.runner.lifecycle.Stage; +import android.view.View; + +import org.hamcrest.Matcher; + +import static android.support.test.espresso.matcher.ViewMatchers.isRoot; + +public class OrientationChangeAction implements ViewAction { + private final int orientation; + + private OrientationChangeAction(int orientation) { + this.orientation = orientation; + } + + @Override + public Matcher getConstraints() { + return isRoot(); + } + + @Override + public String getDescription() { + return "change orientation to " + orientation; + } + + @Override + public void perform(UiController uiController, View view) { + uiController.loopMainThreadUntilIdle(); + + final Activity activity = findActivity(view.getContext()); + if (activity == null){ + throw new IllegalStateException("Could not find the current activity"); + } + + activity.setRequestedOrientation(orientation); + + Collection resumedActivities = ActivityLifecycleMonitorRegistry + .getInstance().getActivitiesInStage(Stage.RESUMED); + + if (resumedActivities.isEmpty()) { + throw new RuntimeException("Could not change orientation"); + } + } + + private static Activity findActivity(Context context) { + if (context == null) + return null; + else if (context instanceof Activity) + return (Activity) context; + else if (context instanceof ContextWrapper) + return findActivity(((ContextWrapper) context).getBaseContext()); + + return null; + } + + public static ViewAction orientationLandscape() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } + + public static ViewAction orientationPortrait() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } +} \ No newline at end of file diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricFileOperationTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricFileOperationTests.java new file mode 100644 index 000000000..7bef6833f --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricFileOperationTests.java @@ -0,0 +1,347 @@ +/* + * Copyright (C) 2015 Vincent Breitmoser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + + +import java.io.File; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Instrumentation; +import android.app.Instrumentation.ActivityResult; +import android.content.Intent; +import android.net.Uri; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.support.test.InstrumentationRegistry; +import android.support.test.espresso.intent.Intents; +import android.support.test.espresso.intent.rule.IntentsTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.LargeTest; +import android.widget.AdapterView; + +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.TestHelpers; +import org.sufficientlysecure.keychain.service.PassphraseCacheService; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; + +import static android.support.test.InstrumentationRegistry.*; +import static android.support.test.espresso.Espresso.onData; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.Espresso.pressBack; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.action.ViewActions.typeText; +import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerActions.openDrawer; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasCategories; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtraWithKey; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasType; +import static android.support.test.espresso.matcher.ViewMatchers.assertThat; +import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; +import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; +import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar; +import static org.sufficientlysecure.keychain.TestHelpers.getImageNames; +import static org.sufficientlysecure.keychain.TestHelpers.importKeysFromResource; +import static org.sufficientlysecure.keychain.TestHelpers.pickRandom; +import static org.sufficientlysecure.keychain.TestHelpers.randomString; +import static org.sufficientlysecure.keychain.actions.CustomActions.tokenEncryptViewAddToken; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.isRecyclerItemView; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withDisplayedChild; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withEncryptionStatus; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withKeyItemId; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureMyKey; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureNone; + + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class AsymmetricFileOperationTests { + + @Rule + public final IntentsTestRule mActivity + = new IntentsTestRule(MainActivity.class) { + @Override + protected Intent getActivityIntent() { + Intent intent = super.getActivityIntent(); + intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true); + intent.putExtra(MainActivity.EXTRA_INIT_FRAG, MainActivity.ID_ENCRYPT_DECRYPT); + return intent; + } + }; + + @Before + public void setUp() throws Exception { + Activity activity = mActivity.getActivity(); + + TestHelpers.copyFiles(); + + // import these two, make sure they're there + importKeysFromResource(activity, "x.sec.asc"); + + // make sure no passphrases are cached + PassphraseCacheService.clearCachedPassphrases(activity); + } + + @Test + public void testTextEncryptDecryptFromToken() throws Exception { + + // navigate to 'encrypt text' + onView(withId(R.id.encrypt_files)).perform(click()); + + File file = pickRandom(getImageNames()); + File outputFile = new File(getInstrumentation().getTargetContext().getFilesDir(), "output-token.gpg"); + + { // encrypt + + // the EncryptKeyCompletionView is tested individually + onView(withId(R.id.recipient_list)).perform(tokenEncryptViewAddToken(0x9D604D2F310716A3L)); + + handleAddFileIntent(file); + onView(withId(R.id.file_list_entry_add)).perform(click()); + + handleSaveFileIntent(outputFile); + onView(withId(R.id.encrypt_save)).perform(click()); + + assertThat("output file has been written", true, CoreMatchers.is(outputFile.exists())); + + } + + // go to decrypt from clipboard view + pressBack(); + + handleOpenFileIntentKitKat(outputFile); + onView(withId(R.id.decrypt_files)).perform(click()); + + onView(withId(R.id.decrypt_files_action_decrypt)).perform(click()); + + { // decrypt + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(file.getName())))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + } + + private void handleAddFileIntent(File file) { + if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { + handleAddFileIntentKitKat(file); + } else { + handleAddFileIntentOlder(file); + } + } + + @TargetApi(VERSION_CODES.KITKAT) + private void handleAddFileIntentKitKat(File file) { + Intent data = new Intent(); + data.setData(Uri.fromFile(file)); + + Intents.intending(allOf( + hasAction(Intent.ACTION_OPEN_DOCUMENT), + hasType("*/*"), + hasCategories(hasItem(Intent.CATEGORY_OPENABLE)), + hasExtraWithKey(Intent.EXTRA_ALLOW_MULTIPLE) + )).respondWith( + new ActivityResult(Activity.RESULT_OK, data) + ); + } + + private void handleAddFileIntentOlder(File file) { + Intent data = new Intent(); + data.setData(Uri.fromFile(file)); + + Intents.intending(allOf( + hasAction(Intent.ACTION_GET_CONTENT), + hasType("*/*"), + hasCategories(hasItem(Intent.CATEGORY_OPENABLE)) + )).respondWith( + new ActivityResult(Activity.RESULT_OK, data) + ); + } + + @TargetApi(VERSION_CODES.KITKAT) + private void handleSaveFileIntent(File file) { + + try { + file.delete(); + } catch (Exception e) { + // nvm + } + + Intent data = new Intent(); + data.setData(Uri.fromFile(file)); + + Intents.intending(allOf( + hasAction(Intent.ACTION_CREATE_DOCUMENT), + hasType("*/*"), + hasExtra("android.content.extra.SHOW_ADVANCED", true), + hasCategories(hasItem(Intent.CATEGORY_OPENABLE)) + )).respondWith( + new ActivityResult(Activity.RESULT_OK, data) + ); + } + + @TargetApi(VERSION_CODES.KITKAT) + private void handleOpenFileIntentKitKat(File file) { + Intent data = new Intent(); + data.setData(Uri.fromFile(file)); + + Intents.intending(allOf( + hasAction(Intent.ACTION_OPEN_DOCUMENT), + hasType("*/*"), + hasCategories(hasItem(Intent.CATEGORY_OPENABLE)) + // hasExtraWithKey(Intent.EXTRA_ALLOW_MULTIPLE) + )).respondWith( + new ActivityResult(Activity.RESULT_OK, data) + ); + } + + @Test + public void testTextEncryptDecryptFromKeyView() throws Exception { + + String cleartext = randomString(10, 30); + + // navigate to key list + pressBack(); + + { // encrypt + + // navigate to edit key dialog + onData(withKeyItemId(0x9D604D2F310716A3L)) + .inAdapterView(allOf(isAssignableFrom(AdapterView.class), + isDescendantOfA(withId(R.id.key_list_list)))) + .perform(click()); + onView(withId(R.id.view_key_action_encrypt_text)).perform(click()); + + // make sure the encrypt is correctly set + onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(1))); + + onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); + + onView(withId(R.id.encrypt_copy)).perform(click()); + } + + // go to decrypt from clipboard view + pressBack(); + pressBack(); + + openDrawer(R.id.drawer_layout); + onView(withText(R.string.nav_encrypt_decrypt)).perform(click()); + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt + + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown_text)))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + pressBack(); + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt again, passphrase should be cached + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown_text)))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + } + + @Test + public void testSignVerify() throws Exception { + + String cleartext = randomString(10, 30); + + // navigate to 'encrypt text' + onView(withId(R.id.encrypt_text)).perform(click()); + + { // sign + + onView(withId(R.id.encrypt_copy)).perform(click()); + checkSnackbar(Style.ERROR, R.string.error_empty_text); + + onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(0))); + onView(withId(R.id.sign)).perform(click()); + onData(withKeyItemId(0x9D604D2F310716A3L)) + .inAdapterView(isAssignableFrom(AdapterView.class)) + .perform(click()); + onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(1))); + + onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); + + onView(withId(R.id.encrypt_copy)).perform(click()); + + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + checkSnackbar(Style.OK, R.string.msg_se_success); + + } + + // go to decrypt from clipboard view + pressBack(); + + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown)))) + .check(matches(allOf(withEncryptionStatus(false), withSignatureMyKey()))); + + // open context menu + onView(allOf(isDescendantOfA(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown)))), + withId(R.id.context_menu))).perform(click()); + + // "delete file" shouldn't be there + onView(withText(R.string.btn_delete_original)).check(doesNotExist()); + + // check if log looks ok + onView(withText(R.string.view_log)).perform(click()); + onView(withText(R.string.msg_dc_clear_signature_ok)).check(matches(isDisplayed())); + pressBack(); + + } + + } + +} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricTextOperationTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricTextOperationTests.java new file mode 100644 index 000000000..c85dfbaab --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/AsymmetricTextOperationTests.java @@ -0,0 +1,243 @@ +/* + * Copyright (C) 2015 Vincent Breitmoser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + + +import android.app.Activity; +import android.content.Intent; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.LargeTest; +import android.widget.AdapterView; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.PassphraseCacheService; +import org.sufficientlysecure.keychain.ui.MainActivity; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; + +import static android.support.test.espresso.Espresso.onData; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.Espresso.pressBack; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.action.ViewActions.typeText; +import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerActions.openDrawer; +import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; +import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; +import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.CoreMatchers.allOf; +import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar; +import static org.sufficientlysecure.keychain.TestHelpers.importKeysFromResource; +import static org.sufficientlysecure.keychain.TestHelpers.randomString; +import static org.sufficientlysecure.keychain.actions.CustomActions.tokenEncryptViewAddToken; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.isRecyclerItemView; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withDisplayedChild; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withEncryptionStatus; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withKeyItemId; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureMyKey; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureNone; + + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class AsymmetricTextOperationTests { + + @Rule + public final ActivityTestRule mActivity + = new ActivityTestRule(MainActivity.class) { + @Override + protected Intent getActivityIntent() { + Intent intent = super.getActivityIntent(); + intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true); + intent.putExtra(MainActivity.EXTRA_INIT_FRAG, MainActivity.ID_ENCRYPT_DECRYPT); + return intent; + } + }; + + @Before + public void setUp() throws Exception { + Activity activity = mActivity.getActivity(); + + // import these two, make sure they're there + importKeysFromResource(activity, "x.sec.asc"); + + // make sure no passphrases are cached + PassphraseCacheService.clearCachedPassphrases(activity); + } + + @Test + public void testTextEncryptDecryptFromToken() throws Exception { + + // navigate to 'encrypt text' + onView(withId(R.id.encrypt_text)).perform(click()); + + String cleartext = randomString(10, 30); + + { // encrypt + + // the EncryptKeyCompletionView is tested individually + onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(0))); + onView(withId(R.id.recipient_list)).perform(tokenEncryptViewAddToken(0x9D604D2F310716A3L)); + onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(1))); + + onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); + + onView(withId(R.id.encrypt_copy)).perform(click()); + } + + // go to decrypt from clipboard view + pressBack(); + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown_text)))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + } + + @Test + public void testTextEncryptDecryptFromKeyView() throws Exception { + + String cleartext = randomString(10, 30); + + pressBack(); + + { // encrypt + + // navigate to edit key dialog + onData(withKeyItemId(0x9D604D2F310716A3L)) + .inAdapterView(allOf(isAssignableFrom(AdapterView.class), + isDescendantOfA(withId(R.id.key_list_list)))) + .perform(click()); + onView(withId(R.id.view_key_action_encrypt_text)).perform(click()); + + // make sure the encrypt is correctly set + onView(withId(R.id.result_encryption_icon)).check(matches(withDisplayedChild(1))); + + onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); + + onView(withId(R.id.encrypt_copy)).perform(click()); + } + + // go to decrypt from clipboard view + pressBack(); + pressBack(); + + openDrawer(R.id.drawer_layout); + onView(withText(R.string.nav_encrypt_decrypt)).perform(click()); + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt + + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown_text)))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + pressBack(); + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt again, passphrase should be cached + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown_text)))) + .check(matches(allOf(withEncryptionStatus(true), withSignatureNone()))); + + } + + } + + @Test + public void testSignVerify() throws Exception { + + String cleartext = randomString(10, 30); + + // navigate to 'encrypt text' + onView(withId(R.id.encrypt_text)).perform(click()); + + { // sign + + onView(withId(R.id.encrypt_copy)).perform(click()); + checkSnackbar(Style.ERROR, R.string.error_empty_text); + + onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(0))); + onView(withId(R.id.sign)).perform(click()); + onData(withKeyItemId(0x9D604D2F310716A3L)) + .inAdapterView(isAssignableFrom(AdapterView.class)) + .perform(click()); + onView(withId(R.id.result_signature_icon)).check(matches(withDisplayedChild(1))); + + onView(withId(R.id.encrypt_text_text)).perform(typeText(cleartext)); + + onView(withId(R.id.encrypt_copy)).perform(click()); + + onView(withId(R.id.passphrase_passphrase)).perform(typeText("x")); + onView(withText(R.string.btn_unlock)).perform(click()); + + checkSnackbar(Style.OK, R.string.msg_se_success); + + } + + // go to decrypt from clipboard view + pressBack(); + + onView(withId(R.id.decrypt_from_clipboard)).perform(click()); + + { // decrypt + + onView(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown)))) + .check(matches(allOf(withEncryptionStatus(false), withSignatureMyKey()))); + + // open context menu + onView(allOf(isDescendantOfA(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(withText(R.string.filename_unknown)))), + withId(R.id.context_menu))).perform(click()); + + // "delete file" shouldn't be there + onView(withText(R.string.btn_delete_original)).check(doesNotExist()); + + // check if log looks ok + onView(withText(R.string.view_log)).perform(click()); + onView(withText(R.string.msg_dc_clear_signature_ok)).check(matches(isDisplayed())); + pressBack(); + + } + + } + +} diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/MiscFileOperationTests.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/MiscFileOperationTests.java new file mode 100644 index 000000000..8d8437561 --- /dev/null +++ b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/MiscFileOperationTests.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2015 Vincent Breitmoser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + + +import java.io.File; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Instrumentation.ActivityResult; +import android.content.Intent; +import android.net.Uri; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.support.test.espresso.intent.Intents; +import android.support.test.espresso.intent.rule.IntentsTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.LargeTest; +import android.widget.AdapterView; + +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.TestHelpers; +import org.sufficientlysecure.keychain.service.PassphraseCacheService; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onData; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.Espresso.pressBack; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.action.ViewActions.typeText; +import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.contrib.DrawerActions.openDrawer; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasCategories; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtraWithKey; +import static android.support.test.espresso.intent.matcher.IntentMatchers.hasType; +import static android.support.test.espresso.matcher.ViewMatchers.assertThat; +import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; +import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; +import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar; +import static org.sufficientlysecure.keychain.TestHelpers.getImageNames; +import static org.sufficientlysecure.keychain.TestHelpers.importKeysFromResource; +import static org.sufficientlysecure.keychain.TestHelpers.pickRandom; +import static org.sufficientlysecure.keychain.TestHelpers.randomString; +import static org.sufficientlysecure.keychain.actions.CustomActions.tokenEncryptViewAddToken; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.isRecyclerItemView; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withDisplayedChild; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withEncryptionStatus; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withKeyItemId; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureMyKey; +import static org.sufficientlysecure.keychain.matcher.CustomMatchers.withSignatureNone; +import static org.sufficientlysecure.keychain.matcher.DrawableMatcher.withDrawable; + + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class MiscFileOperationTests { + + @Rule + public final IntentsTestRule mActivity + = new IntentsTestRule(MainActivity.class) { + @Override + protected Intent getActivityIntent() { + Intent intent = super.getActivityIntent(); + intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true); + intent.putExtra(MainActivity.EXTRA_INIT_FRAG, MainActivity.ID_ENCRYPT_DECRYPT); + return intent; + } + }; + + @Before + public void setUp() throws Exception { + Activity activity = mActivity.getActivity(); + + TestHelpers.copyFiles(); + + // import these two, make sure they're there + importKeysFromResource(activity, "x.sec.asc"); + + // make sure no passphrases are cached + PassphraseCacheService.clearCachedPassphrases(activity); + } + + @Test + public void testDecryptNonPgpFile() throws Exception { + + // decrypt any non-pgp file + File file = pickRandom(getImageNames()); + handleOpenFileIntentKitKat(file); + onView(withId(R.id.decrypt_files)).perform(click()); + + onView(withId(R.id.decrypt_files_action_decrypt)).perform(click()); + + { // decrypt + + // open context menu + onView(allOf(isDescendantOfA(isRecyclerItemView(R.id.decrypted_files_list, + hasDescendant(allOf( + hasDescendant(withDrawable(R.drawable.status_signature_invalid_cutout_24dp, true)), + hasDescendant(withText(R.string.msg_dc_error_invalid_data)))))), + withId(R.id.result_error_log))).perform(click()); + + } + + } + + @TargetApi(VERSION_CODES.KITKAT) + private void handleOpenFileIntentKitKat(File file) { + Intent data = new Intent(); + data.setData(Uri.fromFile(file)); + + Intents.intending(allOf( + hasAction(Intent.ACTION_OPEN_DOCUMENT), + hasType("*/*"), + hasCategories(hasItem(Intent.CATEGORY_OPENABLE)) + // hasExtraWithKey(Intent.EXTRA_ALLOW_MULTIPLE) + )).respondWith( + new ActivityResult(Activity.RESULT_OK, data) + ); + } + +} -- cgit v1.2.3