blob: 6e1f35784c7d3a7947090c2ed48c94dafac5c673 [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
Andrew Scullf49794b2018-04-13 12:01:25 +010023import android.app.KeyguardManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000024import android.app.NotificationManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000025import android.app.admin.DevicePolicyManager;
Rubin Xu16c823e2017-06-27 14:44:58 +010026import android.app.trust.TrustManager;
Adrian Roose5424992014-11-07 21:47:17 +010027import android.content.pm.UserInfo;
28import android.database.sqlite.SQLiteDatabase;
29import android.os.FileUtils;
30import android.os.UserManager;
Rubin Xuaa32d152017-04-27 17:01:05 +010031import android.os.storage.StorageManager;
Adrian Roose5424992014-11-07 21:47:17 +010032import android.test.AndroidTestCase;
Adrian Roosb2375942018-01-19 22:31:28 +010033import android.util.Log;
34import android.util.Log.TerribleFailure;
35import android.util.Log.TerribleFailureHandler;
Adrian Roose5424992014-11-07 21:47:17 +010036
Rubin Xu1de89b32016-11-30 20:03:13 +000037import com.android.internal.widget.LockPatternUtils;
Adrian Roosb2375942018-01-19 22:31:28 +010038import com.android.server.PersistentDataBlockManagerInternal;
Andrew Scull507d11c2017-05-03 17:19:01 +010039import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
Adrian Roos7374d3a2017-03-31 14:14:53 -070040import com.android.server.locksettings.LockSettingsStorage.PersistentData;
41
Adrian Roose5424992014-11-07 21:47:17 +010042import java.io.File;
43import java.util.ArrayList;
44import java.util.Arrays;
45import java.util.List;
46import java.util.concurrent.CountDownLatch;
47
Rubin Xu1de89b32016-11-30 20:03:13 +000048/**
Andrew Scull507d11c2017-05-03 17:19:01 +010049 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests
Rubin Xu1de89b32016-11-30 20:03:13 +000050 */
Adrian Roose5424992014-11-07 21:47:17 +010051public class LockSettingsStorageTests extends AndroidTestCase {
Adrian Roos7374d3a2017-03-31 14:14:53 -070052 private static final int SOME_USER_ID = 1034;
Rubin Xu1de89b32016-11-30 20:03:13 +000053 private final byte[] PASSWORD_0 = "thepassword0".getBytes();
54 private final byte[] PASSWORD_1 = "password1".getBytes();
55 private final byte[] PATTERN_0 = "123654".getBytes();
56 private final byte[] PATTERN_1 = "147852369".getBytes();
57
Adrian Roos7374d3a2017-03-31 14:14:53 -070058 public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33};
59
Adrian Roosb2375942018-01-19 22:31:28 +010060 LockSettingsStorageTestable mStorage;
Adrian Roose5424992014-11-07 21:47:17 +010061 File mStorageDir;
62
63 private File mDb;
64
65 @Override
66 protected void setUp() throws Exception {
67 super.setUp();
68 mStorageDir = new File(getContext().getFilesDir(), "locksettings");
69 mDb = getContext().getDatabasePath("locksettings.db");
70
71 assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
72 assertTrue(FileUtils.deleteContents(mStorageDir));
73 assertTrue(!mDb.exists() || mDb.delete());
74
Rubin Xu1de89b32016-11-30 20:03:13 +000075 final UserManager mockUserManager = mock(UserManager.class);
76 // User 2 is a profile of user 1.
77 when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0));
78 // User 3 is a profile of user 0.
79 when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
Adrian Roose5424992014-11-07 21:47:17 +010080
Rubin Xu0cbc19e2016-12-09 14:00:21 +000081 MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager,
Rubin Xuaa32d152017-04-27 17:01:05 +010082 mock(NotificationManager.class), mock(DevicePolicyManager.class),
Andrew Scullf49794b2018-04-13 12:01:25 +010083 mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +000084 mStorage = new LockSettingsStorageTestable(context,
85 new File(getContext().getFilesDir(), "locksettings"));
86 mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
87 @Override
88 public void initialize(SQLiteDatabase db) {
89 mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
90 }
91 });
Adrian Roose5424992014-11-07 21:47:17 +010092 }
93
94 @Override
95 protected void tearDown() throws Exception {
96 super.tearDown();
97 mStorage.closeDatabase();
98 }
99
100 public void testKeyValue_InitializeWorked() {
101 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
102 mStorage.clearCache();
103 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
104 }
105
106 public void testKeyValue_WriteThenRead() {
107 mStorage.writeKeyValue("key", "value", 0);
108 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
109 mStorage.clearCache();
110 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
111 }
112
113 public void testKeyValue_DefaultValue() {
114 assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
115 assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
116 }
117
118 public void testKeyValue_Concurrency() {
119 final Object monitor = new Object();
120 List<Thread> threads = new ArrayList<>();
121 for (int i = 0; i < 100; i++) {
122 final int threadId = i;
123 threads.add(new Thread() {
124 @Override
125 public void run() {
126 synchronized (monitor) {
127 try {
128 monitor.wait();
129 } catch (InterruptedException e) {
130 return;
131 }
132 mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
133 mStorage.readKeyValue("key", "default", 0);
134 mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
135 mStorage.readKeyValue("key", "default", 0);
136 mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
137 mStorage.readKeyValue("key", "default", 0);
138 mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
139 mStorage.readKeyValue("key", "default", 0);
140 mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
141 mStorage.readKeyValue("key", "default", 0);
142 }
143 }
144 });
145 threads.get(i).start();
146 }
147 mStorage.writeKeyValue("key", "initalValue", 0);
148 synchronized (monitor) {
149 monitor.notifyAll();
150 }
151 for (int i = 0; i < threads.size(); i++) {
152 try {
153 threads.get(i).join();
154 } catch (InterruptedException e) {
155 }
156 }
157 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
158 mStorage.clearCache();
159 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
160 }
161
162 public void testKeyValue_CacheStarvedWriter() {
163 final CountDownLatch latch = new CountDownLatch(1);
164 List<Thread> threads = new ArrayList<>();
165 for (int i = 0; i < 100; i++) {
166 final int threadId = i;
167 threads.add(new Thread() {
168 @Override
169 public void run() {
170 try {
171 latch.await();
172 } catch (InterruptedException e) {
173 return;
174 }
175 if (threadId == 50) {
176 mStorage.writeKeyValue("starvedWriterKey", "value", 0);
177 } else {
178 mStorage.readKeyValue("starvedWriterKey", "default", 0);
179 }
180 }
181 });
182 threads.get(i).start();
183 }
184 latch.countDown();
185 for (int i = 0; i < threads.size(); i++) {
186 try {
187 threads.get(i).join();
188 } catch (InterruptedException e) {
189 }
190 }
191 String cached = mStorage.readKeyValue("key", "default", 0);
192 mStorage.clearCache();
193 String storage = mStorage.readKeyValue("key", "default", 0);
194 assertEquals("Cached value didn't match stored value", storage, cached);
195 }
196
197 public void testRemoveUser() {
198 mStorage.writeKeyValue("key", "value", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000199 writePasswordBytes(PASSWORD_0, 0);
200 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100201
202 mStorage.writeKeyValue("key", "value", 1);
Rubin Xu1de89b32016-11-30 20:03:13 +0000203 writePasswordBytes(PASSWORD_1, 1);
204 writePatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100205
206 mStorage.removeUser(0);
207
208 assertEquals("value", mStorage.readKeyValue("key", "default", 1));
209 assertEquals("default", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000210 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type);
211 assertPatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100212 }
213
Rubin Xu1de89b32016-11-30 20:03:13 +0000214 public void testCredential_Default() {
215 assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
Adrian Roose5424992014-11-07 21:47:17 +0100216 }
217
218 public void testPassword_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000219 writePasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100220
Rubin Xu1de89b32016-11-30 20:03:13 +0000221 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100222 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000223 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100224 }
225
226 public void testPassword_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000227 writePasswordBytes(PASSWORD_0, 1);
228 writePasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100229
Rubin Xu1de89b32016-11-30 20:03:13 +0000230 assertPasswordBytes(PASSWORD_0, 1);
231 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100232 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000233 assertPasswordBytes(PASSWORD_0, 1);
234 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100235 }
236
Ricky Waia46b40f2016-03-31 16:48:29 +0100237 public void testLockType_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000238 writePasswordBytes(PASSWORD_0, 10);
239 writePatternBytes(PATTERN_0, 20);
Ricky Waia46b40f2016-03-31 16:48:29 +0100240
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);
Ricky Waia46b40f2016-03-31 16:48:29 +0100245 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000246 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
247 mStorage.readCredentialHash(10).type);
248 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
249 mStorage.readCredentialHash(20).type);
250 }
251
252 public void testPassword_WriteParentWritesProfile() {
253 writePasswordBytes(PASSWORD_0, 2);
254 writePasswordBytes(PASSWORD_1, 1);
255
256 assertPasswordBytes(PASSWORD_1, 1);
257 assertPasswordBytes(PASSWORD_0, 2);
258 mStorage.clearCache();
259 assertPasswordBytes(PASSWORD_1, 1);
260 assertPasswordBytes(PASSWORD_0, 2);
Ricky Waia46b40f2016-03-31 16:48:29 +0100261 }
262
263 public void testProfileLock_ReadWriteChildProfileLock() {
264 assertFalse(mStorage.hasChildProfileLock(20));
Rubin Xu1de89b32016-11-30 20:03:13 +0000265 mStorage.writeChildProfileLock(20, PASSWORD_0);
266 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100267 assertTrue(mStorage.hasChildProfileLock(20));
268 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000269 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100270 assertTrue(mStorage.hasChildProfileLock(20));
271 }
272
Adrian Roose5424992014-11-07 21:47:17 +0100273 public void testPattern_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000274 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100275
Rubin Xu1de89b32016-11-30 20:03:13 +0000276 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100277 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000278 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100279 }
280
281 public void testPattern_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000282 writePatternBytes(PATTERN_0, 1);
283 writePatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100284
Rubin Xu1de89b32016-11-30 20:03:13 +0000285 assertPatternBytes(PATTERN_0, 1);
286 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100287 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000288 assertPatternBytes(PATTERN_0, 1);
289 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100290 }
291
292 public void testPattern_WriteParentWritesProfile() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000293 writePatternBytes(PATTERN_1, 2);
294 writePatternBytes(PATTERN_0, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100295
Rubin Xu1de89b32016-11-30 20:03:13 +0000296 assertPatternBytes(PATTERN_0, 1);
297 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100298 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000299 assertPatternBytes(PATTERN_0, 1);
300 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100301 }
302
303 public void testPrefetch() {
304 mStorage.writeKeyValue("key", "toBeFetched", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000305 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100306
307 mStorage.clearCache();
308 mStorage.prefetchUser(0);
309
310 assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000311 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100312 }
313
314 public void testFileLocation_Owner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000315 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100316
Rubin Xu1de89b32016-11-30 20:03:13 +0000317 assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
318 assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
319 assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0));
320 assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
Adrian Roose5424992014-11-07 21:47:17 +0100321 }
322
323 public void testFileLocation_SecondaryUser() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000324 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100325
Rubin Xu1de89b32016-11-30 20:03:13 +0000326 assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
327 assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
Adrian Roose5424992014-11-07 21:47:17 +0100328 }
329
330 public void testFileLocation_ProfileToSecondary() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000331 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100332
Rubin Xu1de89b32016-11-30 20:03:13 +0000333 assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
334 assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
Adrian Roose5424992014-11-07 21:47:17 +0100335 }
336
337 public void testFileLocation_ProfileToOwner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000338 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100339
Rubin Xu1de89b32016-11-30 20:03:13 +0000340 assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
341 assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
Adrian Roose5424992014-11-07 21:47:17 +0100342 }
343
Rubin Xu3bf722a2016-12-15 16:07:38 +0000344 public void testSyntheticPasswordState() {
345 final byte[] data = {1,2,3,4};
346 mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
347 assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
348 assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state"));
349
Rubin Xuaa32d152017-04-27 17:01:05 +0100350 mStorage.deleteSyntheticPasswordState(10, 1234L, "state");
Rubin Xu3bf722a2016-12-15 16:07:38 +0000351 assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
352 }
353
Adrian Roosb2375942018-01-19 22:31:28 +0100354 public void testPersistentDataBlock_unavailable() {
355 mStorage.mPersistentDataBlock = null;
356
357 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
358 }
359
360 public void testPersistentDataBlock_empty() {
361 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
362
363 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
364 }
365
366 public void testPersistentDataBlock_withData() {
367 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
368 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
369 .thenReturn(PersistentData.toBytes(PersistentData.TYPE_SP_WEAVER, SOME_USER_ID,
370 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD));
371
372 PersistentData data = mStorage.readPersistentDataBlock();
373
374 assertEquals(PersistentData.TYPE_SP_WEAVER, data.type);
375 assertEquals(SOME_USER_ID, data.userId);
376 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, data.qualityForUi);
377 assertArrayEquals(PAYLOAD, data.payload);
378 }
379
380 public void testPersistentDataBlock_exception() {
381 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
382 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
383 .thenThrow(new IllegalStateException("oops"));
384 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
385 }
386
Adrian Roos7374d3a2017-03-31 14:14:53 -0700387 public void testPersistentData_serializeUnserialize() {
Andrew Scull971f2942017-07-12 15:09:45 +0100388 byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
Adrian Roos7374d3a2017-03-31 14:14:53 -0700389 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
390 PersistentData deserialized = PersistentData.fromBytes(serialized);
391
Andrew Scull971f2942017-07-12 15:09:45 +0100392 assertEquals(PersistentData.TYPE_SP, deserialized.type);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700393 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi);
394 assertArrayEquals(PAYLOAD, deserialized.payload);
395 }
396
397 public void testPersistentData_unserializeNull() {
398 PersistentData deserialized = PersistentData.fromBytes(null);
399 assertSame(PersistentData.NONE, deserialized);
400 }
401
402 public void testPersistentData_unserializeEmptyArray() {
403 PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
404 assertSame(PersistentData.NONE, deserialized);
405 }
406
Adrian Roosb2375942018-01-19 22:31:28 +0100407 public void testPersistentData_unserializeInvalid() {
408 assertNotNull(suppressAndReturnWtf(() -> {
409 PersistentData deserialized = PersistentData.fromBytes(new byte[]{5});
410 assertSame(PersistentData.NONE, deserialized);
411 }));
412 }
413
Adrian Roos7374d3a2017-03-31 14:14:53 -0700414 public void testPersistentData_unserialize_version1() {
415 // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
416 // the wire format in the future.
417 byte[] serializedVersion1 = new byte[] {
418 1, /* PersistentData.VERSION_1 */
Andrew Scull971f2942017-07-12 15:09:45 +0100419 1, /* PersistentData.TYPE_SP */
Adrian Roos7374d3a2017-03-31 14:14:53 -0700420 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */
421 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */
422 1, 2, -1, -2, 33, /* PAYLOAD */
423 };
424 PersistentData deserialized = PersistentData.fromBytes(serializedVersion1);
425 assertEquals(PersistentData.TYPE_SP, deserialized.type);
426 assertEquals(SOME_USER_ID, deserialized.userId);
427 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
428 deserialized.qualityForUi);
429 assertArrayEquals(PAYLOAD, deserialized.payload);
430
431 // Make sure the constants we use on the wire do not change.
432 assertEquals(0, PersistentData.TYPE_NONE);
Andrew Scull971f2942017-07-12 15:09:45 +0100433 assertEquals(1, PersistentData.TYPE_SP);
434 assertEquals(2, PersistentData.TYPE_SP_WEAVER);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700435 }
436
437 public void testCredentialHash_serializeUnserialize() {
438 byte[] serialized = CredentialHash.create(
439 PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes();
440 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
441
442 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
443 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
444 assertArrayEquals(PAYLOAD, deserialized.hash);
445 assertFalse(deserialized.isBaseZeroPattern);
446 }
447
448 public void testCredentialHash_unserialize_versionGatekeeper() {
449 // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes
450 // even if we change the wire format in the future.
451 byte[] serialized = new byte[] {
452 1, /* VERSION_GATEKEEPER */
453 2, /* CREDENTIAL_TYPE_PASSWORD */
454 0, 0, 0, 5, /* hash length */
455 1, 2, -1, -2, 33, /* hash */
456 };
457 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
458
459 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
460 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
461 assertArrayEquals(PAYLOAD, deserialized.hash);
462 assertFalse(deserialized.isBaseZeroPattern);
463
464 // Make sure the constants we use on the wire do not change.
465 assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE);
466 assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
467 assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
468 }
469
Adrian Roose5424992014-11-07 21:47:17 +0100470 private static void assertArrayEquals(byte[] expected, byte[] actual) {
471 if (!Arrays.equals(expected, actual)) {
472 fail("expected:<" + Arrays.toString(expected) +
473 "> but was:<" + Arrays.toString(actual) + ">");
474 }
475 }
Rubin Xu1de89b32016-11-30 20:03:13 +0000476
477 private void writePasswordBytes(byte[] password, int userId) {
478 mStorage.writeCredentialHash(CredentialHash.create(
479 password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), userId);
480 }
481
482 private void writePatternBytes(byte[] pattern, int userId) {
483 mStorage.writeCredentialHash(CredentialHash.create(
484 pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN), userId);
485 }
486
487 private void assertPasswordBytes(byte[] password, int userId) {
488 CredentialHash cred = mStorage.readCredentialHash(userId);
489 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, cred.type);
490 assertArrayEquals(password, cred.hash);
491 }
492
493 private void assertPatternBytes(byte[] pattern, int userId) {
494 CredentialHash cred = mStorage.readCredentialHash(userId);
495 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, cred.type);
496 assertArrayEquals(pattern, cred.hash);
497 }
Adrian Roosb2375942018-01-19 22:31:28 +0100498
499 /**
500 * Suppresses reporting of the WTF to system_server, so we don't pollute the dropbox with
501 * intentionally caused WTFs.
502 */
503 private TerribleFailure suppressAndReturnWtf(Runnable r) {
504 TerribleFailure[] captured = new TerribleFailure[1];
505 TerribleFailureHandler prevWtfHandler = Log.setWtfHandler((t, w, s) -> captured[0] = w);
506 try {
507 r.run();
508 } finally {
509 Log.setWtfHandler(prevWtfHandler);
510 }
511 return captured[0];
512 }
Adrian Roose5424992014-11-07 21:47:17 +0100513}