blob: a0578c9098dd111462ba9157ced24919390ad975 [file] [log] [blame]
Adrian Roose5424992014-11-07 21:47:17 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
Andrew Scull507d11c2017-05-03 17:19:01 +010017package com.android.server.locksettings;
Adrian Roose5424992014-11-07 21:47:17 +010018
Rubin Xu1de89b32016-11-30 20:03:13 +000019import static org.mockito.Matchers.eq;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.when;
22
Rubin Xu0cbc19e2016-12-09 14:00:21 +000023import android.app.NotificationManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000024import android.app.admin.DevicePolicyManager;
Adrian Roose5424992014-11-07 21:47:17 +010025import android.content.pm.UserInfo;
26import android.database.sqlite.SQLiteDatabase;
27import android.os.FileUtils;
28import android.os.UserManager;
Rubin Xuaa32d152017-04-27 17:01:05 +010029import android.os.storage.StorageManager;
Adrian Roose5424992014-11-07 21:47:17 +010030import android.test.AndroidTestCase;
31
Rubin Xu1de89b32016-11-30 20:03:13 +000032import com.android.internal.widget.LockPatternUtils;
Andrew Scull507d11c2017-05-03 17:19:01 +010033import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
Adrian Roos7374d3a2017-03-31 14:14:53 -070034import com.android.server.locksettings.LockSettingsStorage.PersistentData;
35
Adrian Roose5424992014-11-07 21:47:17 +010036import java.io.File;
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.List;
40import java.util.concurrent.CountDownLatch;
41
Rubin Xu1de89b32016-11-30 20:03:13 +000042/**
Andrew Scull507d11c2017-05-03 17:19:01 +010043 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests
Rubin Xu1de89b32016-11-30 20:03:13 +000044 */
Adrian Roose5424992014-11-07 21:47:17 +010045public class LockSettingsStorageTests extends AndroidTestCase {
Adrian Roos7374d3a2017-03-31 14:14:53 -070046 private static final int SOME_USER_ID = 1034;
Rubin Xu1de89b32016-11-30 20:03:13 +000047 private final byte[] PASSWORD_0 = "thepassword0".getBytes();
48 private final byte[] PASSWORD_1 = "password1".getBytes();
49 private final byte[] PATTERN_0 = "123654".getBytes();
50 private final byte[] PATTERN_1 = "147852369".getBytes();
51
Adrian Roos7374d3a2017-03-31 14:14:53 -070052 public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33};
53
Adrian Roose5424992014-11-07 21:47:17 +010054 LockSettingsStorage mStorage;
55 File mStorageDir;
56
57 private File mDb;
58
59 @Override
60 protected void setUp() throws Exception {
61 super.setUp();
62 mStorageDir = new File(getContext().getFilesDir(), "locksettings");
63 mDb = getContext().getDatabasePath("locksettings.db");
64
65 assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
66 assertTrue(FileUtils.deleteContents(mStorageDir));
67 assertTrue(!mDb.exists() || mDb.delete());
68
Rubin Xu1de89b32016-11-30 20:03:13 +000069 final UserManager mockUserManager = mock(UserManager.class);
70 // User 2 is a profile of user 1.
71 when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0));
72 // User 3 is a profile of user 0.
73 when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
Adrian Roose5424992014-11-07 21:47:17 +010074
Rubin Xu0cbc19e2016-12-09 14:00:21 +000075 MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager,
Rubin Xuaa32d152017-04-27 17:01:05 +010076 mock(NotificationManager.class), mock(DevicePolicyManager.class),
77 mock(StorageManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +000078 mStorage = new LockSettingsStorageTestable(context,
79 new File(getContext().getFilesDir(), "locksettings"));
80 mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
81 @Override
82 public void initialize(SQLiteDatabase db) {
83 mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
84 }
85 });
Adrian Roose5424992014-11-07 21:47:17 +010086 }
87
88 @Override
89 protected void tearDown() throws Exception {
90 super.tearDown();
91 mStorage.closeDatabase();
92 }
93
94 public void testKeyValue_InitializeWorked() {
95 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
96 mStorage.clearCache();
97 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
98 }
99
100 public void testKeyValue_WriteThenRead() {
101 mStorage.writeKeyValue("key", "value", 0);
102 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
103 mStorage.clearCache();
104 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
105 }
106
107 public void testKeyValue_DefaultValue() {
108 assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
109 assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
110 }
111
112 public void testKeyValue_Concurrency() {
113 final Object monitor = new Object();
114 List<Thread> threads = new ArrayList<>();
115 for (int i = 0; i < 100; i++) {
116 final int threadId = i;
117 threads.add(new Thread() {
118 @Override
119 public void run() {
120 synchronized (monitor) {
121 try {
122 monitor.wait();
123 } catch (InterruptedException e) {
124 return;
125 }
126 mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
127 mStorage.readKeyValue("key", "default", 0);
128 mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
129 mStorage.readKeyValue("key", "default", 0);
130 mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
131 mStorage.readKeyValue("key", "default", 0);
132 mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
133 mStorage.readKeyValue("key", "default", 0);
134 mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
135 mStorage.readKeyValue("key", "default", 0);
136 }
137 }
138 });
139 threads.get(i).start();
140 }
141 mStorage.writeKeyValue("key", "initalValue", 0);
142 synchronized (monitor) {
143 monitor.notifyAll();
144 }
145 for (int i = 0; i < threads.size(); i++) {
146 try {
147 threads.get(i).join();
148 } catch (InterruptedException e) {
149 }
150 }
151 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
152 mStorage.clearCache();
153 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
154 }
155
156 public void testKeyValue_CacheStarvedWriter() {
157 final CountDownLatch latch = new CountDownLatch(1);
158 List<Thread> threads = new ArrayList<>();
159 for (int i = 0; i < 100; i++) {
160 final int threadId = i;
161 threads.add(new Thread() {
162 @Override
163 public void run() {
164 try {
165 latch.await();
166 } catch (InterruptedException e) {
167 return;
168 }
169 if (threadId == 50) {
170 mStorage.writeKeyValue("starvedWriterKey", "value", 0);
171 } else {
172 mStorage.readKeyValue("starvedWriterKey", "default", 0);
173 }
174 }
175 });
176 threads.get(i).start();
177 }
178 latch.countDown();
179 for (int i = 0; i < threads.size(); i++) {
180 try {
181 threads.get(i).join();
182 } catch (InterruptedException e) {
183 }
184 }
185 String cached = mStorage.readKeyValue("key", "default", 0);
186 mStorage.clearCache();
187 String storage = mStorage.readKeyValue("key", "default", 0);
188 assertEquals("Cached value didn't match stored value", storage, cached);
189 }
190
191 public void testRemoveUser() {
192 mStorage.writeKeyValue("key", "value", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000193 writePasswordBytes(PASSWORD_0, 0);
194 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100195
196 mStorage.writeKeyValue("key", "value", 1);
Rubin Xu1de89b32016-11-30 20:03:13 +0000197 writePasswordBytes(PASSWORD_1, 1);
198 writePatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100199
200 mStorage.removeUser(0);
201
202 assertEquals("value", mStorage.readKeyValue("key", "default", 1));
203 assertEquals("default", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000204 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type);
205 assertPatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100206 }
207
Rubin Xu1de89b32016-11-30 20:03:13 +0000208 public void testCredential_Default() {
209 assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
Adrian Roose5424992014-11-07 21:47:17 +0100210 }
211
212 public void testPassword_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000213 writePasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100214
Rubin Xu1de89b32016-11-30 20:03:13 +0000215 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100216 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000217 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100218 }
219
220 public void testPassword_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000221 writePasswordBytes(PASSWORD_0, 1);
222 writePasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100223
Rubin Xu1de89b32016-11-30 20:03:13 +0000224 assertPasswordBytes(PASSWORD_0, 1);
225 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100226 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000227 assertPasswordBytes(PASSWORD_0, 1);
228 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100229 }
230
Ricky Waia46b40f2016-03-31 16:48:29 +0100231 public void testLockType_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000232 writePasswordBytes(PASSWORD_0, 10);
233 writePatternBytes(PATTERN_0, 20);
Ricky Waia46b40f2016-03-31 16:48:29 +0100234
Rubin Xu1de89b32016-11-30 20:03:13 +0000235 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
236 mStorage.readCredentialHash(10).type);
237 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
238 mStorage.readCredentialHash(20).type);
Ricky Waia46b40f2016-03-31 16:48:29 +0100239 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000240 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
241 mStorage.readCredentialHash(10).type);
242 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
243 mStorage.readCredentialHash(20).type);
244 }
245
246 public void testPassword_WriteParentWritesProfile() {
247 writePasswordBytes(PASSWORD_0, 2);
248 writePasswordBytes(PASSWORD_1, 1);
249
250 assertPasswordBytes(PASSWORD_1, 1);
251 assertPasswordBytes(PASSWORD_0, 2);
252 mStorage.clearCache();
253 assertPasswordBytes(PASSWORD_1, 1);
254 assertPasswordBytes(PASSWORD_0, 2);
Ricky Waia46b40f2016-03-31 16:48:29 +0100255 }
256
257 public void testProfileLock_ReadWriteChildProfileLock() {
258 assertFalse(mStorage.hasChildProfileLock(20));
Rubin Xu1de89b32016-11-30 20:03:13 +0000259 mStorage.writeChildProfileLock(20, PASSWORD_0);
260 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100261 assertTrue(mStorage.hasChildProfileLock(20));
262 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000263 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100264 assertTrue(mStorage.hasChildProfileLock(20));
265 }
266
Adrian Roose5424992014-11-07 21:47:17 +0100267 public void testPattern_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000268 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100269
Rubin Xu1de89b32016-11-30 20:03:13 +0000270 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100271 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000272 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100273 }
274
275 public void testPattern_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000276 writePatternBytes(PATTERN_0, 1);
277 writePatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100278
Rubin Xu1de89b32016-11-30 20:03:13 +0000279 assertPatternBytes(PATTERN_0, 1);
280 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100281 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000282 assertPatternBytes(PATTERN_0, 1);
283 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100284 }
285
286 public void testPattern_WriteParentWritesProfile() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000287 writePatternBytes(PATTERN_1, 2);
288 writePatternBytes(PATTERN_0, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100289
Rubin Xu1de89b32016-11-30 20:03:13 +0000290 assertPatternBytes(PATTERN_0, 1);
291 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100292 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000293 assertPatternBytes(PATTERN_0, 1);
294 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100295 }
296
297 public void testPrefetch() {
298 mStorage.writeKeyValue("key", "toBeFetched", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000299 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100300
301 mStorage.clearCache();
302 mStorage.prefetchUser(0);
303
304 assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000305 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100306 }
307
308 public void testFileLocation_Owner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000309 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100310
Rubin Xu1de89b32016-11-30 20:03:13 +0000311 assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
312 assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
313 assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0));
314 assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
Adrian Roose5424992014-11-07 21:47:17 +0100315 }
316
317 public void testFileLocation_SecondaryUser() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000318 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100319
Rubin Xu1de89b32016-11-30 20:03:13 +0000320 assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
321 assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
Adrian Roose5424992014-11-07 21:47:17 +0100322 }
323
324 public void testFileLocation_ProfileToSecondary() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000325 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100326
Rubin Xu1de89b32016-11-30 20:03:13 +0000327 assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
328 assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
Adrian Roose5424992014-11-07 21:47:17 +0100329 }
330
331 public void testFileLocation_ProfileToOwner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000332 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100333
Rubin Xu1de89b32016-11-30 20:03:13 +0000334 assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
335 assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
Adrian Roose5424992014-11-07 21:47:17 +0100336 }
337
Rubin Xu3bf722a2016-12-15 16:07:38 +0000338 public void testSyntheticPasswordState() {
339 final byte[] data = {1,2,3,4};
340 mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
341 assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
342 assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state"));
343
Rubin Xuaa32d152017-04-27 17:01:05 +0100344 mStorage.deleteSyntheticPasswordState(10, 1234L, "state");
Rubin Xu3bf722a2016-12-15 16:07:38 +0000345 assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
346 }
347
Adrian Roos7374d3a2017-03-31 14:14:53 -0700348 public void testPersistentData_serializeUnserialize() {
349 byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_GATEKEEPER, SOME_USER_ID,
350 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
351 PersistentData deserialized = PersistentData.fromBytes(serialized);
352
353 assertEquals(PersistentData.TYPE_GATEKEEPER, deserialized.type);
354 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi);
355 assertArrayEquals(PAYLOAD, deserialized.payload);
356 }
357
358 public void testPersistentData_unserializeNull() {
359 PersistentData deserialized = PersistentData.fromBytes(null);
360 assertSame(PersistentData.NONE, deserialized);
361 }
362
363 public void testPersistentData_unserializeEmptyArray() {
364 PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
365 assertSame(PersistentData.NONE, deserialized);
366 }
367
368 public void testPersistentData_unserialize_version1() {
369 // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
370 // the wire format in the future.
371 byte[] serializedVersion1 = new byte[] {
372 1, /* PersistentData.VERSION_1 */
373 2, /* PersistentData.TYPE_SP */
374 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */
375 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */
376 1, 2, -1, -2, 33, /* PAYLOAD */
377 };
378 PersistentData deserialized = PersistentData.fromBytes(serializedVersion1);
379 assertEquals(PersistentData.TYPE_SP, deserialized.type);
380 assertEquals(SOME_USER_ID, deserialized.userId);
381 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
382 deserialized.qualityForUi);
383 assertArrayEquals(PAYLOAD, deserialized.payload);
384
385 // Make sure the constants we use on the wire do not change.
386 assertEquals(0, PersistentData.TYPE_NONE);
387 assertEquals(1, PersistentData.TYPE_GATEKEEPER);
388 assertEquals(2, PersistentData.TYPE_SP);
389 assertEquals(3, PersistentData.TYPE_SP_WEAVER);
390 }
391
392 public void testCredentialHash_serializeUnserialize() {
393 byte[] serialized = CredentialHash.create(
394 PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes();
395 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
396
397 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
398 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
399 assertArrayEquals(PAYLOAD, deserialized.hash);
400 assertFalse(deserialized.isBaseZeroPattern);
401 }
402
403 public void testCredentialHash_unserialize_versionGatekeeper() {
404 // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes
405 // even if we change the wire format in the future.
406 byte[] serialized = new byte[] {
407 1, /* VERSION_GATEKEEPER */
408 2, /* CREDENTIAL_TYPE_PASSWORD */
409 0, 0, 0, 5, /* hash length */
410 1, 2, -1, -2, 33, /* hash */
411 };
412 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
413
414 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
415 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
416 assertArrayEquals(PAYLOAD, deserialized.hash);
417 assertFalse(deserialized.isBaseZeroPattern);
418
419 // Make sure the constants we use on the wire do not change.
420 assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE);
421 assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
422 assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
423 }
424
Adrian Roose5424992014-11-07 21:47:17 +0100425 private static void assertArrayEquals(byte[] expected, byte[] actual) {
426 if (!Arrays.equals(expected, actual)) {
427 fail("expected:<" + Arrays.toString(expected) +
428 "> but was:<" + Arrays.toString(actual) + ">");
429 }
430 }
Rubin Xu1de89b32016-11-30 20:03:13 +0000431
432 private void writePasswordBytes(byte[] password, int userId) {
433 mStorage.writeCredentialHash(CredentialHash.create(
434 password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), userId);
435 }
436
437 private void writePatternBytes(byte[] pattern, int userId) {
438 mStorage.writeCredentialHash(CredentialHash.create(
439 pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN), userId);
440 }
441
442 private void assertPasswordBytes(byte[] password, int userId) {
443 CredentialHash cred = mStorage.readCredentialHash(userId);
444 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, cred.type);
445 assertArrayEquals(password, cred.hash);
446 }
447
448 private void assertPatternBytes(byte[] pattern, int userId) {
449 CredentialHash cred = mStorage.readCredentialHash(userId);
450 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, cred.type);
451 assertArrayEquals(pattern, cred.hash);
452 }
Adrian Roose5424992014-11-07 21:47:17 +0100453}