forked from google/google-authenticator-android
/
AuthenticatorActivityPart2Test.java
270 lines (224 loc) · 10.7 KB
/
AuthenticatorActivityPart2Test.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.authenticator;
import static org.mockito.MockitoAnnotations.initMocks;
import com.google.android.apps.authenticator.AccountDb.OtpType;
import com.google.android.apps.authenticator.dataimport.ImportController;
import com.google.android.apps.authenticator.testability.DependencyInjector;
import com.google.android.apps.authenticator2.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
import android.test.MoreAsserts;
import android.view.View;
import org.mockito.Mock;
import java.util.ArrayList;
import java.util.List;
/**
* Unit test for authenticator activity (part/shard 2).
*
* @author sarvar@google.com (Sarvar Patel)
*/
public class AuthenticatorActivityPart2Test
extends ActivityInstrumentationTestCase2<AuthenticatorActivity> {
private AccountDb mAccountDb;
@Mock private ImportController mMockDataImportController;
public AuthenticatorActivityPart2Test() {
super(TestUtilities.APP_PACKAGE_NAME, AuthenticatorActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
DependencyInjector.resetForIntegrationTesting(getInstrumentation().getTargetContext());
mAccountDb = DependencyInjector.getAccountDb();
initMocks(this);
DependencyInjector.setDataImportController(mMockDataImportController);
}
@Override
protected void tearDown() throws Exception {
// Stop the activity to avoid it using the DependencyInjector after it's been closed.
TestUtilities.invokeFinishActivityOnUiThread(getActivity());
DependencyInjector.close();
super.tearDown();
}
public void testAccountSetup_validTotpUriAccepted() throws Throwable {
getActivity();
String secret = "CQAHUXJ2VWDI7WFF";
String accountName = "9.99.99.999";
callOnActivityResultOnUiThreadWithScannedUri(
"otpauth://totp/" + accountName + "?secret=" + secret);
List<String> accountNames = new ArrayList<String>();
assertEquals(1, mAccountDb.getNames(accountNames));
assertEquals(accountName, accountNames.get(0));
assertEquals(secret, mAccountDb.getSecret(accountName));
assertEquals(OtpType.TOTP, mAccountDb.getType(accountName));
assertFalse(getActivity().isFinishing()); // AuthenticatorActivity should continue
}
public void testAccountSetup_validHotpUriWithoutCounterAccepted() throws Throwable {
getActivity();
String secret = "CQAHUXJ2VWDI7WFF";
String accountName = "9.99.99.999";
callOnActivityResultOnUiThreadWithScannedUri(
"otpauth://hotp/" + accountName + "?secret=" + secret);
List<String> accountNames = new ArrayList<String>();
assertEquals(1, mAccountDb.getNames(accountNames));
assertEquals(accountName, accountNames.get(0));
assertEquals(secret, mAccountDb.getSecret(accountName));
assertEquals(OtpType.HOTP, mAccountDb.getType(accountName));
assertEquals(new Integer(0), mAccountDb.getCounter(accountName));
assertFalse(getActivity().isFinishing()); // AuthenticatorActivity should continue
}
public void testAccountSetup_validHotpUriWithCounterAccepted() throws Throwable {
getActivity();
String secret = "CQAHUXJ2VWDI7WFF";
String accountName = "9.99.99.999";
callOnActivityResultOnUiThreadWithScannedUri(
"otpauth://hotp/" + accountName + "?secret=" + secret + "&counter=264");
List<String> accountNames = new ArrayList<String>();
assertEquals(1, mAccountDb.getNames(accountNames));
assertEquals(accountName, accountNames.get(0));
assertEquals(secret, mAccountDb.getSecret(accountName));
assertEquals(OtpType.HOTP, mAccountDb.getType(accountName));
assertEquals(new Integer(264), mAccountDb.getCounter(accountName));
assertFalse(getActivity().isFinishing()); // AuthenticatorActivity should continue
}
///////////////////// Tests with Scanned URIs returned in ActivityResult ////////////////
private void checkBadAccountSetup(String uri, int dialogId) throws Throwable {
getActivity();
callOnActivityResultOnUiThreadWithScannedUri(uri);
List<String> accountNames = new ArrayList<String>();
mAccountDb.getNames(accountNames);
MoreAsserts.assertEmpty(accountNames);
TestUtilities.assertDialogWasDisplayed(getActivity(), dialogId);
assertFalse(getActivity().isFinishing()); // AuthenticatorActivity should continue
}
public void testAccountSetup_uriWithMissingSecretRejected() throws Throwable {
checkBadAccountSetup("otpauth://totp/9.99.99.999?secret2=unused",
Utilities.INVALID_SECRET_IN_QR_CODE);
}
public void testAccountSetup_uriWithEmptySecretRejected() throws Throwable {
checkBadAccountSetup("otpauth://totp/9.99.99.999?secret=", Utilities.INVALID_SECRET_IN_QR_CODE);
}
public void testAccountSetup_uriWithInvalidSecretRejected() throws Throwable {
// The URI contains an invalid base32 characters: 1 and 8
checkBadAccountSetup("otpauth://totp/9.99.99.999?secret=CQ1HUXJ2VWDI8WFF",
Utilities.INVALID_SECRET_IN_QR_CODE);
}
public void testAccountSetup_withNullUri() throws Throwable {
checkBadAccountSetup(null, Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withNullScheme() throws Throwable {
checkBadAccountSetup("totp/9.99.99.999?secret=CQAHUXJ2VWDI7WFF", Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withBadScheme() throws Throwable {
checkBadAccountSetup("otpauth?//totp/9.99.99.999?secret=CQAHUXJ2VWDI7WFF",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withEmptyAuthority() throws Throwable {
checkBadAccountSetup("otpauth:///9.99.99.999?secret=CQAHUXJ2VWDI7WFF",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withBadAuthority() throws Throwable {
checkBadAccountSetup("otpauth://bad/9.99.99.999?secret=CQAHUXJ2VWDI7WFF",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withEmptyUserAccount() throws Throwable {
checkBadAccountSetup("otpauth://totp/?secret=CQAHUXJ2VWDI7WFF",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withWhiteSpaceForUserAccount() throws Throwable {
checkBadAccountSetup("otpauth://totp/ ?secret=CQAHUXJ2VWDI7WFF",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withCounterTooBig() throws Throwable {
checkBadAccountSetup("otpauth://hotp/?secret=CQAHUXJ2VWDI7WFF&counter=34359738368",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withNonNumericCounter() throws Throwable {
checkBadAccountSetup("otpauth://hotp/?secret=CQAHUXJ2VWDI7WFF&counter=abc",
Utilities.INVALID_QR_CODE);
}
public void testAccountSetup_withNullIntent() throws Throwable {
getActivity();
callOnActivityResultOnUiThread(AuthenticatorActivity.SCAN_REQUEST, Activity.RESULT_OK, null);
List<String> accountNames = new ArrayList<String>();
mAccountDb.getNames(accountNames);
MoreAsserts.assertEmpty(accountNames);
TestUtilities.assertDialogWasDisplayed(getActivity(), Utilities.INVALID_QR_CODE);
assertFalse(getActivity().isFinishing()); // AuthenticatorActivity should continue
}
/**
* Invokes the activity's {@code onActivityResult} as though it received the specified scanned
* URI.
*/
private void callOnActivityResultOnUiThreadWithScannedUri(String uri) throws Throwable {
Intent intent = new Intent(Intent.ACTION_VIEW, null);
intent.putExtra("SCAN_RESULT", uri);
callOnActivityResultOnUiThread(AuthenticatorActivity.SCAN_REQUEST, Activity.RESULT_OK, intent);
}
private void callOnActivityResultOnUiThread(
final int requestCode, final int resultCode, final Intent intent) throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run () {
getActivity().onActivityResult(requestCode, resultCode, intent);
}
});
}
public void testDirectAccountSetupWithConfirmationAccepted() throws Throwable {
// start AuthenticatorActivity with a valid Uri for account setup.
String secret = "CQAHUXJ2VWDI7WFF";
String accountName = "9.99.99.999";
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("otpauth://totp/" + accountName + "?secret=" + secret));
setActivityIntent(intent);
getActivity();
// check main screen does not have focus because of save confirmation dialog.
View contentView = getActivity().findViewById(R.id.content_no_accounts);
assertFalse(contentView.hasWindowFocus());
// click Ok on the dialog box which has focus.
TestUtilities.tapDialogPositiveButton(this);
// check main screen gets focus back after dialog window disappears.
TestUtilities.waitForWindowFocus(contentView);
// check update to database.
List<String> accountNames = new ArrayList<String>();
assertEquals(1, mAccountDb.getNames(accountNames));
assertEquals(accountName, accountNames.get(0));
assertEquals(secret, mAccountDb.getSecret(accountName));
assertEquals(OtpType.TOTP, mAccountDb.getType(accountName));
}
public void testDirectAccountSetupWithConfirmationRejected() throws Throwable {
// start AuthenticatorActivity with a valid Uri for account setup.
String secret = "CQAHUXJ2VWDI7WFF";
String accountName = "9.99.99.999";
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("otpauth://totp/" + accountName + "?secret=" + secret));
setActivityIntent(intent);
getActivity();
// check main screen does not have focus because of save confirmation dialog.
View contentView = getActivity().findViewById(R.id.content_no_accounts);
assertFalse(contentView.hasWindowFocus());
// click Cancel on the save confirmation dialog box.
TestUtilities.tapDialogNegativeButton(this);
// check main screen gets focus back after dialog window disappears.
TestUtilities.waitForWindowFocus(contentView);
// check database has not been updated.
List<String> accountNames = new ArrayList<String>();
assertEquals(0, mAccountDb.getNames(accountNames));
assertEquals(null, mAccountDb.getSecret(accountName));
}
}