aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/EncryptDecryptSymmetricTests.java
blob: 081bef9205909205151bfe82373ee21fcd3e9e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain;


import android.content.Intent;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.sufficientlysecure.keychain.ui.MainActivity;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
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.matches;
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.not;
import static org.sufficientlysecure.keychain.TestHelpers.checkSnackbar;
import static org.sufficientlysecure.keychain.TestHelpers.randomString;
import static org.sufficientlysecure.keychain.actions.CustomActions.actionOpenDrawer;
import static org.sufficientlysecure.keychain.matcher.DrawableMatcher.withDrawable;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(AndroidJUnit4.class)
@LargeTest
public class EncryptDecryptSymmetricTests {

    public static final String PASSPHRASE = randomString(5, 20);

    @Rule
    public final ActivityTestRule<MainActivity> mActivity
            = new ActivityTestRule<MainActivity>(MainActivity.class) {
        @Override
        protected Intent getActivityIntent() {
            Intent intent = super.getActivityIntent();
            intent.putExtra(MainActivity.EXTRA_SKIP_FIRST_TIME, true);
            return intent;
        }
    };

    @Test
    public void testSymmetricTextEncryptDecrypt() throws Exception {

        MainActivity activity = mActivity.getActivity();

        String text = randomString(10, 30);

        // navigate to encrypt/decrypt
        onView(withId(R.id.drawer_layout)).perform(actionOpenDrawer());
        onView(ViewMatchers.withText(R.string.nav_encrypt_decrypt)).perform(click());
        onView(withId(R.id.encrypt_text)).perform(click());

        {
            onView(withId(R.id.encrypt_text_text)).perform(typeText(text));

            openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
            onView(withText(R.string.label_symmetric)).perform(click());

            onView(withId(R.id.passphrase)).perform(typeText(PASSPHRASE));

            onView(withId(R.id.encrypt_copy)).perform(click());

            checkSnackbar(Style.ERROR, R.string.passphrases_do_not_match);

            onView(withId(R.id.passphraseAgain)).perform(typeText(PASSPHRASE));

            onView(withId(R.id.encrypt_text_text)).check(matches(withText(text)));

            onView(withId(R.id.encrypt_copy)).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());

        {
            onView(withId(R.id.passphrase_passphrase)).perform(typeText(PASSPHRASE));
            onView(withText(R.string.btn_unlock)).perform(click());

            onView(withId(R.id.decrypt_text_plaintext)).check(matches(
                    withText(text)));

            // TODO write generic status verifier

            onView(withId(R.id.result_encryption_text)).check(matches(
                    withText(R.string.decrypt_result_encrypted)));
            onView(withId(R.id.result_signature_text)).check(matches(
                    withText(R.string.decrypt_result_no_signature)));
            onView(withId(R.id.result_signature_layout)).check(matches(
                    not(isDisplayed())));

            onView(withId(R.id.result_encryption_icon)).check(matches(
                    withDrawable(R.drawable.status_lock_closed_24dp)));
            onView(withId(R.id.result_signature_icon)).check(matches(
                    withDrawable(R.drawable.status_signature_unknown_cutout_24dp)));

        }

    }

}