blob: 8af4edda3b8bedb4d8b4f3e8ccd1aa452e0a5c78 [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;
Pavel Grafov57f1b662019-03-27 14:55:38 +000032import android.platform.test.annotations.Presubmit;
Adrian Roose5424992014-11-07 21:47:17 +010033import android.test.AndroidTestCase;
Adrian Roosb2375942018-01-19 22:31:28 +010034import android.util.Log;
35import android.util.Log.TerribleFailure;
36import android.util.Log.TerribleFailureHandler;
Adrian Roose5424992014-11-07 21:47:17 +010037
Pavel Grafov57f1b662019-03-27 14:55:38 +000038import androidx.test.filters.SmallTest;
39
Rubin Xu1de89b32016-11-30 20:03:13 +000040import com.android.internal.widget.LockPatternUtils;
Adrian Roosb2375942018-01-19 22:31:28 +010041import com.android.server.PersistentDataBlockManagerInternal;
Andrew Scull507d11c2017-05-03 17:19:01 +010042import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
Adrian Roos7374d3a2017-03-31 14:14:53 -070043import com.android.server.locksettings.LockSettingsStorage.PersistentData;
44
Adrian Roose5424992014-11-07 21:47:17 +010045import java.io.File;
46import java.util.ArrayList;
47import java.util.Arrays;
48import java.util.List;
49import java.util.concurrent.CountDownLatch;
50
Rubin Xu1de89b32016-11-30 20:03:13 +000051/**
Andrew Scull507d11c2017-05-03 17:19:01 +010052 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests
Rubin Xu1de89b32016-11-30 20:03:13 +000053 */
Pavel Grafov57f1b662019-03-27 14:55:38 +000054@SmallTest
55@Presubmit
Adrian Roose5424992014-11-07 21:47:17 +010056public class LockSettingsStorageTests extends AndroidTestCase {
Adrian Roos7374d3a2017-03-31 14:14:53 -070057 private static final int SOME_USER_ID = 1034;
Rubin Xu1de89b32016-11-30 20:03:13 +000058 private final byte[] PASSWORD_0 = "thepassword0".getBytes();
59 private final byte[] PASSWORD_1 = "password1".getBytes();
60 private final byte[] PATTERN_0 = "123654".getBytes();
61 private final byte[] PATTERN_1 = "147852369".getBytes();
62
Adrian Roos7374d3a2017-03-31 14:14:53 -070063 public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33};
64
Adrian Roosb2375942018-01-19 22:31:28 +010065 LockSettingsStorageTestable mStorage;
Adrian Roose5424992014-11-07 21:47:17 +010066 File mStorageDir;
67
68 private File mDb;
69
70 @Override
71 protected void setUp() throws Exception {
72 super.setUp();
73 mStorageDir = new File(getContext().getFilesDir(), "locksettings");
74 mDb = getContext().getDatabasePath("locksettings.db");
75
76 assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
77 assertTrue(FileUtils.deleteContents(mStorageDir));
78 assertTrue(!mDb.exists() || mDb.delete());
79
Rubin Xu1de89b32016-11-30 20:03:13 +000080 final UserManager mockUserManager = mock(UserManager.class);
81 // User 2 is a profile of user 1.
82 when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0));
83 // User 3 is a profile of user 0.
84 when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
Adrian Roose5424992014-11-07 21:47:17 +010085
Rubin Xu0cbc19e2016-12-09 14:00:21 +000086 MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager,
Rubin Xuaa32d152017-04-27 17:01:05 +010087 mock(NotificationManager.class), mock(DevicePolicyManager.class),
Andrew Scullf49794b2018-04-13 12:01:25 +010088 mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +000089 mStorage = new LockSettingsStorageTestable(context,
90 new File(getContext().getFilesDir(), "locksettings"));
91 mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
92 @Override
93 public void initialize(SQLiteDatabase db) {
94 mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
95 }
96 });
Adrian Roose5424992014-11-07 21:47:17 +010097 }
98
99 @Override
100 protected void tearDown() throws Exception {
101 super.tearDown();
102 mStorage.closeDatabase();
103 }
104
105 public void testKeyValue_InitializeWorked() {
106 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
107 mStorage.clearCache();
108 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
109 }
110
111 public void testKeyValue_WriteThenRead() {
112 mStorage.writeKeyValue("key", "value", 0);
113 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
114 mStorage.clearCache();
115 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
116 }
117
118 public void testKeyValue_DefaultValue() {
119 assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
120 assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
121 }
122
123 public void testKeyValue_Concurrency() {
124 final Object monitor = new Object();
125 List<Thread> threads = new ArrayList<>();
126 for (int i = 0; i < 100; i++) {
127 final int threadId = i;
128 threads.add(new Thread() {
129 @Override
130 public void run() {
131 synchronized (monitor) {
132 try {
133 monitor.wait();
134 } catch (InterruptedException e) {
135 return;
136 }
137 mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
138 mStorage.readKeyValue("key", "default", 0);
139 mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
140 mStorage.readKeyValue("key", "default", 0);
141 mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
142 mStorage.readKeyValue("key", "default", 0);
143 mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
144 mStorage.readKeyValue("key", "default", 0);
145 mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
146 mStorage.readKeyValue("key", "default", 0);
147 }
148 }
149 });
150 threads.get(i).start();
151 }
152 mStorage.writeKeyValue("key", "initalValue", 0);
153 synchronized (monitor) {
154 monitor.notifyAll();
155 }
156 for (int i = 0; i < threads.size(); i++) {
157 try {
158 threads.get(i).join();
159 } catch (InterruptedException e) {
160 }
161 }
162 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
163 mStorage.clearCache();
164 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
165 }
166
167 public void testKeyValue_CacheStarvedWriter() {
168 final CountDownLatch latch = new CountDownLatch(1);
169 List<Thread> threads = new ArrayList<>();
170 for (int i = 0; i < 100; i++) {
171 final int threadId = i;
172 threads.add(new Thread() {
173 @Override
174 public void run() {
175 try {
176 latch.await();
177 } catch (InterruptedException e) {
178 return;
179 }
180 if (threadId == 50) {
181 mStorage.writeKeyValue("starvedWriterKey", "value", 0);
182 } else {
183 mStorage.readKeyValue("starvedWriterKey", "default", 0);
184 }
185 }
186 });
187 threads.get(i).start();
188 }
189 latch.countDown();
190 for (int i = 0; i < threads.size(); i++) {
191 try {
192 threads.get(i).join();
193 } catch (InterruptedException e) {
194 }
195 }
196 String cached = mStorage.readKeyValue("key", "default", 0);
197 mStorage.clearCache();
198 String storage = mStorage.readKeyValue("key", "default", 0);
199 assertEquals("Cached value didn't match stored value", storage, cached);
200 }
201
202 public void testRemoveUser() {
203 mStorage.writeKeyValue("key", "value", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000204 writePasswordBytes(PASSWORD_0, 0);
205 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100206
207 mStorage.writeKeyValue("key", "value", 1);
Rubin Xu1de89b32016-11-30 20:03:13 +0000208 writePasswordBytes(PASSWORD_1, 1);
209 writePatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100210
211 mStorage.removeUser(0);
212
213 assertEquals("value", mStorage.readKeyValue("key", "default", 1));
214 assertEquals("default", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000215 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type);
216 assertPatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100217 }
218
Rubin Xu1de89b32016-11-30 20:03:13 +0000219 public void testCredential_Default() {
220 assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
Adrian Roose5424992014-11-07 21:47:17 +0100221 }
222
223 public void testPassword_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000224 writePasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100225
Rubin Xu1de89b32016-11-30 20:03:13 +0000226 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100227 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000228 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100229 }
230
231 public void testPassword_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000232 writePasswordBytes(PASSWORD_0, 1);
233 writePasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100234
Rubin Xu1de89b32016-11-30 20:03:13 +0000235 assertPasswordBytes(PASSWORD_0, 1);
236 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100237 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000238 assertPasswordBytes(PASSWORD_0, 1);
239 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100240 }
241
Ricky Waia46b40f2016-03-31 16:48:29 +0100242 public void testLockType_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000243 writePasswordBytes(PASSWORD_0, 10);
244 writePatternBytes(PATTERN_0, 20);
Ricky Waia46b40f2016-03-31 16:48:29 +0100245
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);
Ricky Waia46b40f2016-03-31 16:48:29 +0100250 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000251 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
252 mStorage.readCredentialHash(10).type);
253 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
254 mStorage.readCredentialHash(20).type);
255 }
256
257 public void testPassword_WriteParentWritesProfile() {
258 writePasswordBytes(PASSWORD_0, 2);
259 writePasswordBytes(PASSWORD_1, 1);
260
261 assertPasswordBytes(PASSWORD_1, 1);
262 assertPasswordBytes(PASSWORD_0, 2);
263 mStorage.clearCache();
264 assertPasswordBytes(PASSWORD_1, 1);
265 assertPasswordBytes(PASSWORD_0, 2);
Ricky Waia46b40f2016-03-31 16:48:29 +0100266 }
267
268 public void testProfileLock_ReadWriteChildProfileLock() {
269 assertFalse(mStorage.hasChildProfileLock(20));
Rubin Xu1de89b32016-11-30 20:03:13 +0000270 mStorage.writeChildProfileLock(20, PASSWORD_0);
271 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100272 assertTrue(mStorage.hasChildProfileLock(20));
273 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000274 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100275 assertTrue(mStorage.hasChildProfileLock(20));
276 }
277
Adrian Roose5424992014-11-07 21:47:17 +0100278 public void testPattern_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000279 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100280
Rubin Xu1de89b32016-11-30 20:03:13 +0000281 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100282 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000283 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100284 }
285
286 public void testPattern_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000287 writePatternBytes(PATTERN_0, 1);
288 writePatternBytes(PATTERN_1, 2);
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 testPattern_WriteParentWritesProfile() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000298 writePatternBytes(PATTERN_1, 2);
299 writePatternBytes(PATTERN_0, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100300
Rubin Xu1de89b32016-11-30 20:03:13 +0000301 assertPatternBytes(PATTERN_0, 1);
302 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100303 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000304 assertPatternBytes(PATTERN_0, 1);
305 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100306 }
307
308 public void testPrefetch() {
309 mStorage.writeKeyValue("key", "toBeFetched", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000310 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100311
312 mStorage.clearCache();
313 mStorage.prefetchUser(0);
314
315 assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000316 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100317 }
318
319 public void testFileLocation_Owner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000320 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100321
Rubin Xu1de89b32016-11-30 20:03:13 +0000322 assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
323 assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
324 assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0));
325 assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
Adrian Roose5424992014-11-07 21:47:17 +0100326 }
327
328 public void testFileLocation_SecondaryUser() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000329 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100330
Rubin Xu1de89b32016-11-30 20:03:13 +0000331 assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
332 assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
Adrian Roose5424992014-11-07 21:47:17 +0100333 }
334
335 public void testFileLocation_ProfileToSecondary() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000336 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100337
Rubin Xu1de89b32016-11-30 20:03:13 +0000338 assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
339 assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
Adrian Roose5424992014-11-07 21:47:17 +0100340 }
341
342 public void testFileLocation_ProfileToOwner() {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000343 LockSettingsStorage storage = new LockSettingsStorage(getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100344
Rubin Xu1de89b32016-11-30 20:03:13 +0000345 assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
346 assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
Adrian Roose5424992014-11-07 21:47:17 +0100347 }
348
Rubin Xu3bf722a2016-12-15 16:07:38 +0000349 public void testSyntheticPasswordState() {
350 final byte[] data = {1,2,3,4};
351 mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
352 assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
353 assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state"));
354
Rubin Xuaa32d152017-04-27 17:01:05 +0100355 mStorage.deleteSyntheticPasswordState(10, 1234L, "state");
Rubin Xu3bf722a2016-12-15 16:07:38 +0000356 assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
357 }
358
Adrian Roosb2375942018-01-19 22:31:28 +0100359 public void testPersistentDataBlock_unavailable() {
360 mStorage.mPersistentDataBlock = null;
361
362 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
363 }
364
365 public void testPersistentDataBlock_empty() {
366 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
367
368 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
369 }
370
371 public void testPersistentDataBlock_withData() {
372 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
373 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
374 .thenReturn(PersistentData.toBytes(PersistentData.TYPE_SP_WEAVER, SOME_USER_ID,
375 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD));
376
377 PersistentData data = mStorage.readPersistentDataBlock();
378
379 assertEquals(PersistentData.TYPE_SP_WEAVER, data.type);
380 assertEquals(SOME_USER_ID, data.userId);
381 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, data.qualityForUi);
382 assertArrayEquals(PAYLOAD, data.payload);
383 }
384
385 public void testPersistentDataBlock_exception() {
386 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
387 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
388 .thenThrow(new IllegalStateException("oops"));
389 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
390 }
391
Adrian Roos7374d3a2017-03-31 14:14:53 -0700392 public void testPersistentData_serializeUnserialize() {
Andrew Scull971f2942017-07-12 15:09:45 +0100393 byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
Adrian Roos7374d3a2017-03-31 14:14:53 -0700394 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
395 PersistentData deserialized = PersistentData.fromBytes(serialized);
396
Andrew Scull971f2942017-07-12 15:09:45 +0100397 assertEquals(PersistentData.TYPE_SP, deserialized.type);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700398 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi);
399 assertArrayEquals(PAYLOAD, deserialized.payload);
400 }
401
402 public void testPersistentData_unserializeNull() {
403 PersistentData deserialized = PersistentData.fromBytes(null);
404 assertSame(PersistentData.NONE, deserialized);
405 }
406
407 public void testPersistentData_unserializeEmptyArray() {
408 PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
409 assertSame(PersistentData.NONE, deserialized);
410 }
411
Adrian Roosb2375942018-01-19 22:31:28 +0100412 public void testPersistentData_unserializeInvalid() {
413 assertNotNull(suppressAndReturnWtf(() -> {
414 PersistentData deserialized = PersistentData.fromBytes(new byte[]{5});
415 assertSame(PersistentData.NONE, deserialized);
416 }));
417 }
418
Adrian Roos7374d3a2017-03-31 14:14:53 -0700419 public void testPersistentData_unserialize_version1() {
420 // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
421 // the wire format in the future.
422 byte[] serializedVersion1 = new byte[] {
423 1, /* PersistentData.VERSION_1 */
Andrew Scull971f2942017-07-12 15:09:45 +0100424 1, /* PersistentData.TYPE_SP */
Adrian Roos7374d3a2017-03-31 14:14:53 -0700425 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */
426 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */
427 1, 2, -1, -2, 33, /* PAYLOAD */
428 };
429 PersistentData deserialized = PersistentData.fromBytes(serializedVersion1);
430 assertEquals(PersistentData.TYPE_SP, deserialized.type);
431 assertEquals(SOME_USER_ID, deserialized.userId);
432 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
433 deserialized.qualityForUi);
434 assertArrayEquals(PAYLOAD, deserialized.payload);
435
436 // Make sure the constants we use on the wire do not change.
437 assertEquals(0, PersistentData.TYPE_NONE);
Andrew Scull971f2942017-07-12 15:09:45 +0100438 assertEquals(1, PersistentData.TYPE_SP);
439 assertEquals(2, PersistentData.TYPE_SP_WEAVER);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700440 }
441
442 public void testCredentialHash_serializeUnserialize() {
443 byte[] serialized = CredentialHash.create(
444 PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes();
445 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
446
447 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
448 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
449 assertArrayEquals(PAYLOAD, deserialized.hash);
450 assertFalse(deserialized.isBaseZeroPattern);
451 }
452
453 public void testCredentialHash_unserialize_versionGatekeeper() {
454 // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes
455 // even if we change the wire format in the future.
456 byte[] serialized = new byte[] {
457 1, /* VERSION_GATEKEEPER */
458 2, /* CREDENTIAL_TYPE_PASSWORD */
459 0, 0, 0, 5, /* hash length */
460 1, 2, -1, -2, 33, /* hash */
461 };
462 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
463
464 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
465 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
466 assertArrayEquals(PAYLOAD, deserialized.hash);
467 assertFalse(deserialized.isBaseZeroPattern);
468
469 // Make sure the constants we use on the wire do not change.
470 assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE);
471 assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
472 assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
473 }
474
Adrian Roose5424992014-11-07 21:47:17 +0100475 private static void assertArrayEquals(byte[] expected, byte[] actual) {
476 if (!Arrays.equals(expected, actual)) {
477 fail("expected:<" + Arrays.toString(expected) +
478 "> but was:<" + Arrays.toString(actual) + ">");
479 }
480 }
Rubin Xu1de89b32016-11-30 20:03:13 +0000481
482 private void writePasswordBytes(byte[] password, int userId) {
483 mStorage.writeCredentialHash(CredentialHash.create(
484 password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), userId);
485 }
486
487 private void writePatternBytes(byte[] pattern, int userId) {
488 mStorage.writeCredentialHash(CredentialHash.create(
489 pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN), userId);
490 }
491
492 private void assertPasswordBytes(byte[] password, int userId) {
493 CredentialHash cred = mStorage.readCredentialHash(userId);
494 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, cred.type);
495 assertArrayEquals(password, cred.hash);
496 }
497
498 private void assertPatternBytes(byte[] pattern, int userId) {
499 CredentialHash cred = mStorage.readCredentialHash(userId);
500 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, cred.type);
501 assertArrayEquals(pattern, cred.hash);
502 }
Adrian Roosb2375942018-01-19 22:31:28 +0100503
504 /**
505 * Suppresses reporting of the WTF to system_server, so we don't pollute the dropbox with
506 * intentionally caused WTFs.
507 */
508 private TerribleFailure suppressAndReturnWtf(Runnable r) {
509 TerribleFailure[] captured = new TerribleFailure[1];
510 TerribleFailureHandler prevWtfHandler = Log.setWtfHandler((t, w, s) -> captured[0] = w);
511 try {
512 r.run();
513 } finally {
514 Log.setWtfHandler(prevWtfHandler);
515 }
516 return captured[0];
517 }
Adrian Roose5424992014-11-07 21:47:17 +0100518}