blob: b0325cb372e21847f733adb684eab2cf77dcdc67 [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;
Rubin Xu16c823e2017-06-27 14:44:58 +010025import android.app.trust.TrustManager;
Adrian Roose5424992014-11-07 21:47:17 +010026import android.content.pm.UserInfo;
27import android.database.sqlite.SQLiteDatabase;
28import android.os.FileUtils;
29import android.os.UserManager;
Rubin Xuaa32d152017-04-27 17:01:05 +010030import android.os.storage.StorageManager;
Adrian Roose5424992014-11-07 21:47:17 +010031import android.test.AndroidTestCase;
32
Rubin Xu1de89b32016-11-30 20:03:13 +000033import com.android.internal.widget.LockPatternUtils;
Andrew Scull507d11c2017-05-03 17:19:01 +010034import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
Adrian Roos7374d3a2017-03-31 14:14:53 -070035import com.android.server.locksettings.LockSettingsStorage.PersistentData;
36
Adrian Roose5424992014-11-07 21:47:17 +010037import java.io.File;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.List;
41import java.util.concurrent.CountDownLatch;
42
Rubin Xu1de89b32016-11-30 20:03:13 +000043/**
Andrew Scull507d11c2017-05-03 17:19:01 +010044 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests
Rubin Xu1de89b32016-11-30 20:03:13 +000045 */
Adrian Roose5424992014-11-07 21:47:17 +010046public class LockSettingsStorageTests extends AndroidTestCase {
Adrian Roos7374d3a2017-03-31 14:14:53 -070047 private static final int SOME_USER_ID = 1034;
Rubin Xu1de89b32016-11-30 20:03:13 +000048 private final byte[] PASSWORD_0 = "thepassword0".getBytes();
49 private final byte[] PASSWORD_1 = "password1".getBytes();
50 private final byte[] PATTERN_0 = "123654".getBytes();
51 private final byte[] PATTERN_1 = "147852369".getBytes();
52
Adrian Roos7374d3a2017-03-31 14:14:53 -070053 public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33};
54
Adrian Roose5424992014-11-07 21:47:17 +010055 LockSettingsStorage mStorage;
56 File mStorageDir;
57
58 private File mDb;
59
60 @Override
61 protected void setUp() throws Exception {
62 super.setUp();
63 mStorageDir = new File(getContext().getFilesDir(), "locksettings");
64 mDb = getContext().getDatabasePath("locksettings.db");
65
66 assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
67 assertTrue(FileUtils.deleteContents(mStorageDir));
68 assertTrue(!mDb.exists() || mDb.delete());
69
Rubin Xu1de89b32016-11-30 20:03:13 +000070 final UserManager mockUserManager = mock(UserManager.class);
71 // User 2 is a profile of user 1.
72 when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0));
73 // User 3 is a profile of user 0.
74 when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
Adrian Roose5424992014-11-07 21:47:17 +010075
Rubin Xu0cbc19e2016-12-09 14:00:21 +000076 MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager,
Rubin Xuaa32d152017-04-27 17:01:05 +010077 mock(NotificationManager.class), mock(DevicePolicyManager.class),
Rubin Xu16c823e2017-06-27 14:44:58 +010078 mock(StorageManager.class), mock(TrustManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +000079 mStorage = new LockSettingsStorageTestable(context,
80 new File(getContext().getFilesDir(), "locksettings"));
81 mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
82 @Override
83 public void initialize(SQLiteDatabase db) {
84 mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
85 }
86 });
Adrian Roose5424992014-11-07 21:47:17 +010087 }
88
89 @Override
90 protected void tearDown() throws Exception {
91 super.tearDown();
92 mStorage.closeDatabase();
93 }
94
95 public void testKeyValue_InitializeWorked() {
96 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
97 mStorage.clearCache();
98 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
99 }
100
101 public void testKeyValue_WriteThenRead() {
102 mStorage.writeKeyValue("key", "value", 0);
103 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
104 mStorage.clearCache();
105 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
106 }
107
108 public void testKeyValue_DefaultValue() {
109 assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
110 assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
111 }
112
113 public void testKeyValue_Concurrency() {
114 final Object monitor = new Object();
115 List<Thread> threads = new ArrayList<>();
116 for (int i = 0; i < 100; i++) {
117 final int threadId = i;
118 threads.add(new Thread() {
119 @Override
120 public void run() {
121 synchronized (monitor) {
122 try {
123 monitor.wait();
124 } catch (InterruptedException e) {
125 return;
126 }
127 mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
128 mStorage.readKeyValue("key", "default", 0);
129 mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
130 mStorage.readKeyValue("key", "default", 0);
131 mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
132 mStorage.readKeyValue("key", "default", 0);
133 mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
134 mStorage.readKeyValue("key", "default", 0);
135 mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
136 mStorage.readKeyValue("key", "default", 0);
137 }
138 }
139 });
140 threads.get(i).start();
141 }
142 mStorage.writeKeyValue("key", "initalValue", 0);
143 synchronized (monitor) {
144 monitor.notifyAll();
145 }
146 for (int i = 0; i < threads.size(); i++) {
147 try {
148 threads.get(i).join();
149 } catch (InterruptedException e) {
150 }
151 }
152 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
153 mStorage.clearCache();
154 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
155 }
156
157 public void testKeyValue_CacheStarvedWriter() {
158 final CountDownLatch latch = new CountDownLatch(1);
159 List<Thread> threads = new ArrayList<>();
160 for (int i = 0; i < 100; i++) {
161 final int threadId = i;
162 threads.add(new Thread() {
163 @Override
164 public void run() {
165 try {
166 latch.await();
167 } catch (InterruptedException e) {
168 return;
169 }
170 if (threadId == 50) {
171 mStorage.writeKeyValue("starvedWriterKey", "value", 0);
172 } else {
173 mStorage.readKeyValue("starvedWriterKey", "default", 0);
174 }
175 }
176 });
177 threads.get(i).start();
178 }
179 latch.countDown();
180 for (int i = 0; i < threads.size(); i++) {
181 try {
182 threads.get(i).join();
183 } catch (InterruptedException e) {
184 }
185 }
186 String cached = mStorage.readKeyValue("key", "default", 0);
187 mStorage.clearCache();
188 String storage = mStorage.readKeyValue("key", "default", 0);
189 assertEquals("Cached value didn't match stored value", storage, cached);
190 }
191
192 public void testRemoveUser() {
193 mStorage.writeKeyValue("key", "value", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000194 writePasswordBytes(PASSWORD_0, 0);
195 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100196
197 mStorage.writeKeyValue("key", "value", 1);
Rubin Xu1de89b32016-11-30 20:03:13 +0000198 writePasswordBytes(PASSWORD_1, 1);
199 writePatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100200
201 mStorage.removeUser(0);
202
203 assertEquals("value", mStorage.readKeyValue("key", "default", 1));
204 assertEquals("default", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000205 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type);
206 assertPatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100207 }
208
Rubin Xu1de89b32016-11-30 20:03:13 +0000209 public void testCredential_Default() {
210 assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
Adrian Roose5424992014-11-07 21:47:17 +0100211 }
212
213 public void testPassword_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000214 writePasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100215
Rubin Xu1de89b32016-11-30 20:03:13 +0000216 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100217 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000218 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100219 }
220
221 public void testPassword_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000222 writePasswordBytes(PASSWORD_0, 1);
223 writePasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100224
Rubin Xu1de89b32016-11-30 20:03:13 +0000225 assertPasswordBytes(PASSWORD_0, 1);
226 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100227 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000228 assertPasswordBytes(PASSWORD_0, 1);
229 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100230 }
231
Ricky Waia46b40f2016-03-31 16:48:29 +0100232 public void testLockType_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000233 writePasswordBytes(PASSWORD_0, 10);
234 writePatternBytes(PATTERN_0, 20);
Ricky Waia46b40f2016-03-31 16:48:29 +0100235
Rubin Xu1de89b32016-11-30 20:03:13 +0000236 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
237 mStorage.readCredentialHash(10).type);
238 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
239 mStorage.readCredentialHash(20).type);
Ricky Waia46b40f2016-03-31 16:48:29 +0100240 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000241 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
242 mStorage.readCredentialHash(10).type);
243 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
244 mStorage.readCredentialHash(20).type);
245 }
246
247 public void testPassword_WriteParentWritesProfile() {
248 writePasswordBytes(PASSWORD_0, 2);
249 writePasswordBytes(PASSWORD_1, 1);
250
251 assertPasswordBytes(PASSWORD_1, 1);
252 assertPasswordBytes(PASSWORD_0, 2);
253 mStorage.clearCache();
254 assertPasswordBytes(PASSWORD_1, 1);
255 assertPasswordBytes(PASSWORD_0, 2);
Ricky Waia46b40f2016-03-31 16:48:29 +0100256 }
257
258 public void testProfileLock_ReadWriteChildProfileLock() {
259 assertFalse(mStorage.hasChildProfileLock(20));
Rubin Xu1de89b32016-11-30 20:03:13 +0000260 mStorage.writeChildProfileLock(20, PASSWORD_0);
261 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100262 assertTrue(mStorage.hasChildProfileLock(20));
263 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000264 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100265 assertTrue(mStorage.hasChildProfileLock(20));
266 }
267
Adrian Roose5424992014-11-07 21:47:17 +0100268 public void testPattern_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000269 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100270
Rubin Xu1de89b32016-11-30 20:03:13 +0000271 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100272 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000273 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100274 }
275
276 public void testPattern_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000277 writePatternBytes(PATTERN_0, 1);
278 writePatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100279
Rubin Xu1de89b32016-11-30 20:03:13 +0000280 assertPatternBytes(PATTERN_0, 1);
281 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100282 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000283 assertPatternBytes(PATTERN_0, 1);
284 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100285 }
286
287 public void testPattern_WriteParentWritesProfile() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000288 writePatternBytes(PATTERN_1, 2);
289 writePatternBytes(PATTERN_0, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100290
Rubin Xu1de89b32016-11-30 20:03:13 +0000291 assertPatternBytes(PATTERN_0, 1);
292 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100293 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000294 assertPatternBytes(PATTERN_0, 1);
295 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100296 }
297
298 public void testPrefetch() {
299 mStorage.writeKeyValue("key", "toBeFetched", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000300 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100301
302 mStorage.clearCache();
303 mStorage.prefetchUser(0);
304
305 assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000306 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100307 }
308
309 public void testFileLocation_Owner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000310 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100311
Rubin Xu1de89b32016-11-30 20:03:13 +0000312 assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
313 assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
314 assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0));
315 assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
Adrian Roose5424992014-11-07 21:47:17 +0100316 }
317
318 public void testFileLocation_SecondaryUser() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000319 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100320
Rubin Xu1de89b32016-11-30 20:03:13 +0000321 assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
322 assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
Adrian Roose5424992014-11-07 21:47:17 +0100323 }
324
325 public void testFileLocation_ProfileToSecondary() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000326 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100327
Rubin Xu1de89b32016-11-30 20:03:13 +0000328 assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
329 assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
Adrian Roose5424992014-11-07 21:47:17 +0100330 }
331
332 public void testFileLocation_ProfileToOwner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000333 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100334
Rubin Xu1de89b32016-11-30 20:03:13 +0000335 assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
336 assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
Adrian Roose5424992014-11-07 21:47:17 +0100337 }
338
Rubin Xu3bf722a2016-12-15 16:07:38 +0000339 public void testSyntheticPasswordState() {
340 final byte[] data = {1,2,3,4};
341 mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
342 assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
343 assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state"));
344
Rubin Xuaa32d152017-04-27 17:01:05 +0100345 mStorage.deleteSyntheticPasswordState(10, 1234L, "state");
Rubin Xu3bf722a2016-12-15 16:07:38 +0000346 assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
347 }
348
Adrian Roos7374d3a2017-03-31 14:14:53 -0700349 public void testPersistentData_serializeUnserialize() {
Andrew Scull971f2942017-07-12 15:09:45 +0100350 byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
Adrian Roos7374d3a2017-03-31 14:14:53 -0700351 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
352 PersistentData deserialized = PersistentData.fromBytes(serialized);
353
Andrew Scull971f2942017-07-12 15:09:45 +0100354 assertEquals(PersistentData.TYPE_SP, deserialized.type);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700355 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi);
356 assertArrayEquals(PAYLOAD, deserialized.payload);
357 }
358
359 public void testPersistentData_unserializeNull() {
360 PersistentData deserialized = PersistentData.fromBytes(null);
361 assertSame(PersistentData.NONE, deserialized);
362 }
363
364 public void testPersistentData_unserializeEmptyArray() {
365 PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
366 assertSame(PersistentData.NONE, deserialized);
367 }
368
369 public void testPersistentData_unserialize_version1() {
370 // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
371 // the wire format in the future.
372 byte[] serializedVersion1 = new byte[] {
373 1, /* PersistentData.VERSION_1 */
Andrew Scull971f2942017-07-12 15:09:45 +0100374 1, /* PersistentData.TYPE_SP */
Adrian Roos7374d3a2017-03-31 14:14:53 -0700375 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */
376 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */
377 1, 2, -1, -2, 33, /* PAYLOAD */
378 };
379 PersistentData deserialized = PersistentData.fromBytes(serializedVersion1);
380 assertEquals(PersistentData.TYPE_SP, deserialized.type);
381 assertEquals(SOME_USER_ID, deserialized.userId);
382 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
383 deserialized.qualityForUi);
384 assertArrayEquals(PAYLOAD, deserialized.payload);
385
386 // Make sure the constants we use on the wire do not change.
387 assertEquals(0, PersistentData.TYPE_NONE);
Andrew Scull971f2942017-07-12 15:09:45 +0100388 assertEquals(1, PersistentData.TYPE_SP);
389 assertEquals(2, PersistentData.TYPE_SP_WEAVER);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700390 }
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}