blob: ab9b39c280e9de22bec6d057ca84428d6265be4c [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
Kenny Rootd01bb412019-11-22 09:34:03 -080019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertSame;
23import static org.junit.Assert.assertTrue;
24import static org.junit.Assert.fail;
25import static org.mockito.ArgumentMatchers.eq;
Rubin Xu1de89b32016-11-30 20:03:13 +000026import static org.mockito.Mockito.mock;
27import static org.mockito.Mockito.when;
28
Andrew Scullf49794b2018-04-13 12:01:25 +010029import android.app.KeyguardManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000030import android.app.NotificationManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000031import android.app.admin.DevicePolicyManager;
Rubin Xu16c823e2017-06-27 14:44:58 +010032import android.app.trust.TrustManager;
Adrian Roose5424992014-11-07 21:47:17 +010033import android.content.pm.UserInfo;
34import android.database.sqlite.SQLiteDatabase;
35import android.os.FileUtils;
Adrian Roos8527b232019-05-13 09:35:21 -070036import android.os.SystemClock;
Adrian Roose5424992014-11-07 21:47:17 +010037import android.os.UserManager;
Rubin Xuaa32d152017-04-27 17:01:05 +010038import android.os.storage.StorageManager;
Pavel Grafov57f1b662019-03-27 14:55:38 +000039import android.platform.test.annotations.Presubmit;
Adrian Roosb2375942018-01-19 22:31:28 +010040import android.util.Log;
41import android.util.Log.TerribleFailure;
42import android.util.Log.TerribleFailureHandler;
Adrian Roose5424992014-11-07 21:47:17 +010043
Kenny Rootd01bb412019-11-22 09:34:03 -080044import androidx.test.InstrumentationRegistry;
Pavel Grafov57f1b662019-03-27 14:55:38 +000045import androidx.test.filters.SmallTest;
Kenny Rootd01bb412019-11-22 09:34:03 -080046import androidx.test.runner.AndroidJUnit4;
Pavel Grafov57f1b662019-03-27 14:55:38 +000047
Rubin Xu1de89b32016-11-30 20:03:13 +000048import com.android.internal.widget.LockPatternUtils;
Adrian Roosb2375942018-01-19 22:31:28 +010049import com.android.server.PersistentDataBlockManagerInternal;
Andrew Scull507d11c2017-05-03 17:19:01 +010050import com.android.server.locksettings.LockSettingsStorage.CredentialHash;
Adrian Roos7374d3a2017-03-31 14:14:53 -070051import com.android.server.locksettings.LockSettingsStorage.PersistentData;
52
Kenny Rootd01bb412019-11-22 09:34:03 -080053import org.junit.After;
54import org.junit.Before;
55import org.junit.Test;
56import org.junit.runner.RunWith;
57
Adrian Roose5424992014-11-07 21:47:17 +010058import java.io.File;
59import java.util.ArrayList;
60import java.util.Arrays;
61import java.util.List;
62import java.util.concurrent.CountDownLatch;
63
Rubin Xu1de89b32016-11-30 20:03:13 +000064/**
Kenny Rootd01bb412019-11-22 09:34:03 -080065 * atest FrameworksServicesTests:LockSettingsStorageTests
Rubin Xu1de89b32016-11-30 20:03:13 +000066 */
Pavel Grafov57f1b662019-03-27 14:55:38 +000067@SmallTest
68@Presubmit
Kenny Rootd01bb412019-11-22 09:34:03 -080069@RunWith(AndroidJUnit4.class)
70public class LockSettingsStorageTests {
Adrian Roos7374d3a2017-03-31 14:14:53 -070071 private static final int SOME_USER_ID = 1034;
Rubin Xu1de89b32016-11-30 20:03:13 +000072 private final byte[] PASSWORD_0 = "thepassword0".getBytes();
73 private final byte[] PASSWORD_1 = "password1".getBytes();
74 private final byte[] PATTERN_0 = "123654".getBytes();
75 private final byte[] PATTERN_1 = "147852369".getBytes();
76
Adrian Roos7374d3a2017-03-31 14:14:53 -070077 public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33};
78
Adrian Roosb2375942018-01-19 22:31:28 +010079 LockSettingsStorageTestable mStorage;
Adrian Roose5424992014-11-07 21:47:17 +010080 File mStorageDir;
81
82 private File mDb;
83
Kenny Rootd01bb412019-11-22 09:34:03 -080084 @Before
85 public void setUp() throws Exception {
86 mStorageDir = new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings");
87 mDb = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
Adrian Roose5424992014-11-07 21:47:17 +010088
89 assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
90 assertTrue(FileUtils.deleteContents(mStorageDir));
91 assertTrue(!mDb.exists() || mDb.delete());
92
Rubin Xu1de89b32016-11-30 20:03:13 +000093 final UserManager mockUserManager = mock(UserManager.class);
94 // User 2 is a profile of user 1.
95 when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0));
96 // User 3 is a profile of user 0.
97 when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0));
Adrian Roose5424992014-11-07 21:47:17 +010098
Kenny Rootd01bb412019-11-22 09:34:03 -080099 MockLockSettingsContext context = new MockLockSettingsContext(
100 InstrumentationRegistry.getContext(), mockUserManager,
Rubin Xuaa32d152017-04-27 17:01:05 +0100101 mock(NotificationManager.class), mock(DevicePolicyManager.class),
Andrew Scullf49794b2018-04-13 12:01:25 +0100102 mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000103 mStorage = new LockSettingsStorageTestable(context,
Kenny Rootd01bb412019-11-22 09:34:03 -0800104 new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000105 mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() {
106 @Override
107 public void initialize(SQLiteDatabase db) {
108 mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
109 }
110 });
Adrian Roose5424992014-11-07 21:47:17 +0100111 }
112
Kenny Rootd01bb412019-11-22 09:34:03 -0800113 @After
114 public void tearDown() throws Exception {
Adrian Roose5424992014-11-07 21:47:17 +0100115 mStorage.closeDatabase();
116 }
117
Kenny Rootd01bb412019-11-22 09:34:03 -0800118 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100119 public void testKeyValue_InitializeWorked() {
120 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
121 mStorage.clearCache();
122 assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
123 }
124
Kenny Rootd01bb412019-11-22 09:34:03 -0800125 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100126 public void testKeyValue_WriteThenRead() {
127 mStorage.writeKeyValue("key", "value", 0);
128 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
129 mStorage.clearCache();
130 assertEquals("value", mStorage.readKeyValue("key", "default", 0));
131 }
132
Kenny Rootd01bb412019-11-22 09:34:03 -0800133 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100134 public void testKeyValue_DefaultValue() {
135 assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
136 assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
137 }
138
Kenny Rootd01bb412019-11-22 09:34:03 -0800139 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100140 public void testKeyValue_Concurrency() {
141 final Object monitor = new Object();
142 List<Thread> threads = new ArrayList<>();
143 for (int i = 0; i < 100; i++) {
144 final int threadId = i;
Adrian Roos8527b232019-05-13 09:35:21 -0700145 threads.add(new Thread("testKeyValue_Concurrency_" + i) {
Adrian Roose5424992014-11-07 21:47:17 +0100146 @Override
147 public void run() {
148 synchronized (monitor) {
149 try {
150 monitor.wait();
151 } catch (InterruptedException e) {
152 return;
153 }
Adrian Roose5424992014-11-07 21:47:17 +0100154 }
Adrian Roos8527b232019-05-13 09:35:21 -0700155 mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
156 mStorage.readKeyValue("key", "default", 0);
157 mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
158 mStorage.readKeyValue("key", "default", 0);
159 mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
160 mStorage.readKeyValue("key", "default", 0);
161 mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
162 mStorage.readKeyValue("key", "default", 0);
163 mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
164 mStorage.readKeyValue("key", "default", 0);
Adrian Roose5424992014-11-07 21:47:17 +0100165 }
166 });
167 threads.get(i).start();
168 }
169 mStorage.writeKeyValue("key", "initalValue", 0);
170 synchronized (monitor) {
171 monitor.notifyAll();
172 }
Adrian Roos8527b232019-05-13 09:35:21 -0700173 joinAll(threads, 10000);
Adrian Roose5424992014-11-07 21:47:17 +0100174 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
175 mStorage.clearCache();
176 assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0));
177 }
178
Kenny Rootd01bb412019-11-22 09:34:03 -0800179 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100180 public void testKeyValue_CacheStarvedWriter() {
181 final CountDownLatch latch = new CountDownLatch(1);
182 List<Thread> threads = new ArrayList<>();
183 for (int i = 0; i < 100; i++) {
184 final int threadId = i;
185 threads.add(new Thread() {
186 @Override
187 public void run() {
188 try {
189 latch.await();
190 } catch (InterruptedException e) {
191 return;
192 }
193 if (threadId == 50) {
194 mStorage.writeKeyValue("starvedWriterKey", "value", 0);
195 } else {
196 mStorage.readKeyValue("starvedWriterKey", "default", 0);
197 }
198 }
199 });
200 threads.get(i).start();
201 }
202 latch.countDown();
203 for (int i = 0; i < threads.size(); i++) {
204 try {
205 threads.get(i).join();
206 } catch (InterruptedException e) {
207 }
208 }
209 String cached = mStorage.readKeyValue("key", "default", 0);
210 mStorage.clearCache();
211 String storage = mStorage.readKeyValue("key", "default", 0);
212 assertEquals("Cached value didn't match stored value", storage, cached);
213 }
214
Kenny Rootd01bb412019-11-22 09:34:03 -0800215 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100216 public void testRemoveUser() {
217 mStorage.writeKeyValue("key", "value", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000218 writePasswordBytes(PASSWORD_0, 0);
219 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100220
221 mStorage.writeKeyValue("key", "value", 1);
Rubin Xu1de89b32016-11-30 20:03:13 +0000222 writePasswordBytes(PASSWORD_1, 1);
223 writePatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100224
225 mStorage.removeUser(0);
226
227 assertEquals("value", mStorage.readKeyValue("key", "default", 1));
228 assertEquals("default", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000229 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type);
230 assertPatternBytes(PATTERN_1, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100231 }
232
Kenny Rootd01bb412019-11-22 09:34:03 -0800233 @Test
Rubin Xu1de89b32016-11-30 20:03:13 +0000234 public void testCredential_Default() {
235 assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE);
Adrian Roose5424992014-11-07 21:47:17 +0100236 }
237
Kenny Rootd01bb412019-11-22 09:34:03 -0800238 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100239 public void testPassword_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000240 writePasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100241
Rubin Xu1de89b32016-11-30 20:03:13 +0000242 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100243 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000244 assertPasswordBytes(PASSWORD_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100245 }
246
Kenny Rootd01bb412019-11-22 09:34:03 -0800247 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100248 public void testPassword_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000249 writePasswordBytes(PASSWORD_0, 1);
250 writePasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100251
Rubin Xu1de89b32016-11-30 20:03:13 +0000252 assertPasswordBytes(PASSWORD_0, 1);
253 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100254 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000255 assertPasswordBytes(PASSWORD_0, 1);
256 assertPasswordBytes(PASSWORD_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100257 }
258
Kenny Rootd01bb412019-11-22 09:34:03 -0800259 @Test
Ricky Waia46b40f2016-03-31 16:48:29 +0100260 public void testLockType_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000261 writePasswordBytes(PASSWORD_0, 10);
262 writePatternBytes(PATTERN_0, 20);
Ricky Waia46b40f2016-03-31 16:48:29 +0100263
Rubin Xu1de89b32016-11-30 20:03:13 +0000264 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
265 mStorage.readCredentialHash(10).type);
266 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
267 mStorage.readCredentialHash(20).type);
Ricky Waia46b40f2016-03-31 16:48:29 +0100268 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000269 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD,
270 mStorage.readCredentialHash(10).type);
271 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN,
272 mStorage.readCredentialHash(20).type);
273 }
274
Kenny Rootd01bb412019-11-22 09:34:03 -0800275 @Test
Rubin Xu1de89b32016-11-30 20:03:13 +0000276 public void testPassword_WriteParentWritesProfile() {
277 writePasswordBytes(PASSWORD_0, 2);
278 writePasswordBytes(PASSWORD_1, 1);
279
280 assertPasswordBytes(PASSWORD_1, 1);
281 assertPasswordBytes(PASSWORD_0, 2);
282 mStorage.clearCache();
283 assertPasswordBytes(PASSWORD_1, 1);
284 assertPasswordBytes(PASSWORD_0, 2);
Ricky Waia46b40f2016-03-31 16:48:29 +0100285 }
286
Kenny Rootd01bb412019-11-22 09:34:03 -0800287 @Test
Ricky Waia46b40f2016-03-31 16:48:29 +0100288 public void testProfileLock_ReadWriteChildProfileLock() {
289 assertFalse(mStorage.hasChildProfileLock(20));
Rubin Xu1de89b32016-11-30 20:03:13 +0000290 mStorage.writeChildProfileLock(20, PASSWORD_0);
291 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100292 assertTrue(mStorage.hasChildProfileLock(20));
293 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000294 assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20));
Ricky Waia46b40f2016-03-31 16:48:29 +0100295 assertTrue(mStorage.hasChildProfileLock(20));
296 }
297
Kenny Rootd01bb412019-11-22 09:34:03 -0800298 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100299 public void testPattern_Write() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000300 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100301
Rubin Xu1de89b32016-11-30 20:03:13 +0000302 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100303 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000304 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100305 }
306
Kenny Rootd01bb412019-11-22 09:34:03 -0800307 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100308 public void testPattern_WriteProfileWritesParent() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000309 writePatternBytes(PATTERN_0, 1);
310 writePatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100311
Rubin Xu1de89b32016-11-30 20:03:13 +0000312 assertPatternBytes(PATTERN_0, 1);
313 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100314 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000315 assertPatternBytes(PATTERN_0, 1);
316 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100317 }
318
Kenny Rootd01bb412019-11-22 09:34:03 -0800319 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100320 public void testPattern_WriteParentWritesProfile() {
Rubin Xu1de89b32016-11-30 20:03:13 +0000321 writePatternBytes(PATTERN_1, 2);
322 writePatternBytes(PATTERN_0, 1);
Adrian Roose5424992014-11-07 21:47:17 +0100323
Rubin Xu1de89b32016-11-30 20:03:13 +0000324 assertPatternBytes(PATTERN_0, 1);
325 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100326 mStorage.clearCache();
Rubin Xu1de89b32016-11-30 20:03:13 +0000327 assertPatternBytes(PATTERN_0, 1);
328 assertPatternBytes(PATTERN_1, 2);
Adrian Roose5424992014-11-07 21:47:17 +0100329 }
330
Kenny Rootd01bb412019-11-22 09:34:03 -0800331 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100332 public void testPrefetch() {
333 mStorage.writeKeyValue("key", "toBeFetched", 0);
Rubin Xu1de89b32016-11-30 20:03:13 +0000334 writePatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100335
336 mStorage.clearCache();
337 mStorage.prefetchUser(0);
338
339 assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
Rubin Xu1de89b32016-11-30 20:03:13 +0000340 assertPatternBytes(PATTERN_0, 0);
Adrian Roose5424992014-11-07 21:47:17 +0100341 }
342
Kenny Rootd01bb412019-11-22 09:34:03 -0800343 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100344 public void testFileLocation_Owner() {
Kenny Rootd01bb412019-11-22 09:34:03 -0800345 LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100346
Rubin Xu1de89b32016-11-30 20:03:13 +0000347 assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0));
348 assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0));
349 assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0));
350 assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0));
Adrian Roose5424992014-11-07 21:47:17 +0100351 }
352
Kenny Rootd01bb412019-11-22 09:34:03 -0800353 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100354 public void testFileLocation_SecondaryUser() {
Kenny Rootd01bb412019-11-22 09:34:03 -0800355 LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100356
Rubin Xu1de89b32016-11-30 20:03:13 +0000357 assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1));
358 assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1));
Adrian Roose5424992014-11-07 21:47:17 +0100359 }
360
Kenny Rootd01bb412019-11-22 09:34:03 -0800361 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100362 public void testFileLocation_ProfileToSecondary() {
Kenny Rootd01bb412019-11-22 09:34:03 -0800363 LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100364
Rubin Xu1de89b32016-11-30 20:03:13 +0000365 assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2));
366 assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2));
Adrian Roose5424992014-11-07 21:47:17 +0100367 }
368
Kenny Rootd01bb412019-11-22 09:34:03 -0800369 @Test
Adrian Roose5424992014-11-07 21:47:17 +0100370 public void testFileLocation_ProfileToOwner() {
Kenny Rootd01bb412019-11-22 09:34:03 -0800371 LockSettingsStorage storage = new LockSettingsStorage(InstrumentationRegistry.getContext());
Adrian Roose5424992014-11-07 21:47:17 +0100372
Rubin Xu1de89b32016-11-30 20:03:13 +0000373 assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3));
374 assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3));
Adrian Roose5424992014-11-07 21:47:17 +0100375 }
376
Kenny Rootd01bb412019-11-22 09:34:03 -0800377 @Test
Rubin Xu3bf722a2016-12-15 16:07:38 +0000378 public void testSyntheticPasswordState() {
379 final byte[] data = {1,2,3,4};
380 mStorage.writeSyntheticPasswordState(10, 1234L, "state", data);
381 assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
382 assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state"));
383
Rubin Xuaa32d152017-04-27 17:01:05 +0100384 mStorage.deleteSyntheticPasswordState(10, 1234L, "state");
Rubin Xu3bf722a2016-12-15 16:07:38 +0000385 assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state"));
386 }
387
Kenny Rootd01bb412019-11-22 09:34:03 -0800388 @Test
Adrian Roosb2375942018-01-19 22:31:28 +0100389 public void testPersistentDataBlock_unavailable() {
390 mStorage.mPersistentDataBlock = null;
391
392 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
393 }
394
Kenny Rootd01bb412019-11-22 09:34:03 -0800395 @Test
Adrian Roosb2375942018-01-19 22:31:28 +0100396 public void testPersistentDataBlock_empty() {
397 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
398
399 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
400 }
401
Kenny Rootd01bb412019-11-22 09:34:03 -0800402 @Test
Adrian Roosb2375942018-01-19 22:31:28 +0100403 public void testPersistentDataBlock_withData() {
404 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
405 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
406 .thenReturn(PersistentData.toBytes(PersistentData.TYPE_SP_WEAVER, SOME_USER_ID,
407 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD));
408
409 PersistentData data = mStorage.readPersistentDataBlock();
410
411 assertEquals(PersistentData.TYPE_SP_WEAVER, data.type);
412 assertEquals(SOME_USER_ID, data.userId);
413 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, data.qualityForUi);
414 assertArrayEquals(PAYLOAD, data.payload);
415 }
416
Kenny Rootd01bb412019-11-22 09:34:03 -0800417 @Test
Adrian Roosb2375942018-01-19 22:31:28 +0100418 public void testPersistentDataBlock_exception() {
419 mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class);
420 when(mStorage.mPersistentDataBlock.getFrpCredentialHandle())
421 .thenThrow(new IllegalStateException("oops"));
422 assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock());
423 }
424
Kenny Rootd01bb412019-11-22 09:34:03 -0800425 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700426 public void testPersistentData_serializeUnserialize() {
Andrew Scull971f2942017-07-12 15:09:45 +0100427 byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID,
Adrian Roos7374d3a2017-03-31 14:14:53 -0700428 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD);
429 PersistentData deserialized = PersistentData.fromBytes(serialized);
430
Andrew Scull971f2942017-07-12 15:09:45 +0100431 assertEquals(PersistentData.TYPE_SP, deserialized.type);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700432 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi);
433 assertArrayEquals(PAYLOAD, deserialized.payload);
434 }
435
Kenny Rootd01bb412019-11-22 09:34:03 -0800436 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700437 public void testPersistentData_unserializeNull() {
438 PersistentData deserialized = PersistentData.fromBytes(null);
439 assertSame(PersistentData.NONE, deserialized);
440 }
441
Kenny Rootd01bb412019-11-22 09:34:03 -0800442 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700443 public void testPersistentData_unserializeEmptyArray() {
444 PersistentData deserialized = PersistentData.fromBytes(new byte[0]);
445 assertSame(PersistentData.NONE, deserialized);
446 }
447
Kenny Rootd01bb412019-11-22 09:34:03 -0800448 @Test
Adrian Roosb2375942018-01-19 22:31:28 +0100449 public void testPersistentData_unserializeInvalid() {
450 assertNotNull(suppressAndReturnWtf(() -> {
451 PersistentData deserialized = PersistentData.fromBytes(new byte[]{5});
452 assertSame(PersistentData.NONE, deserialized);
453 }));
454 }
455
Kenny Rootd01bb412019-11-22 09:34:03 -0800456 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700457 public void testPersistentData_unserialize_version1() {
458 // This test ensures that we can read serialized VERSION_1 PersistentData even if we change
459 // the wire format in the future.
460 byte[] serializedVersion1 = new byte[] {
461 1, /* PersistentData.VERSION_1 */
Andrew Scull971f2942017-07-12 15:09:45 +0100462 1, /* PersistentData.TYPE_SP */
Adrian Roos7374d3a2017-03-31 14:14:53 -0700463 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */
464 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */
465 1, 2, -1, -2, 33, /* PAYLOAD */
466 };
467 PersistentData deserialized = PersistentData.fromBytes(serializedVersion1);
468 assertEquals(PersistentData.TYPE_SP, deserialized.type);
469 assertEquals(SOME_USER_ID, deserialized.userId);
470 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
471 deserialized.qualityForUi);
472 assertArrayEquals(PAYLOAD, deserialized.payload);
473
474 // Make sure the constants we use on the wire do not change.
475 assertEquals(0, PersistentData.TYPE_NONE);
Andrew Scull971f2942017-07-12 15:09:45 +0100476 assertEquals(1, PersistentData.TYPE_SP);
477 assertEquals(2, PersistentData.TYPE_SP_WEAVER);
Adrian Roos7374d3a2017-03-31 14:14:53 -0700478 }
479
Colin Cross1f4620a2019-12-18 17:16:36 -0800480 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700481 public void testCredentialHash_serializeUnserialize() {
482 byte[] serialized = CredentialHash.create(
483 PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes();
484 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
485
486 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
487 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
488 assertArrayEquals(PAYLOAD, deserialized.hash);
489 assertFalse(deserialized.isBaseZeroPattern);
490 }
491
Colin Cross1f4620a2019-12-18 17:16:36 -0800492 @Test
Adrian Roos7374d3a2017-03-31 14:14:53 -0700493 public void testCredentialHash_unserialize_versionGatekeeper() {
494 // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes
495 // even if we change the wire format in the future.
496 byte[] serialized = new byte[] {
497 1, /* VERSION_GATEKEEPER */
498 2, /* CREDENTIAL_TYPE_PASSWORD */
499 0, 0, 0, 5, /* hash length */
500 1, 2, -1, -2, 33, /* hash */
501 };
502 CredentialHash deserialized = CredentialHash.fromBytes(serialized);
503
504 assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version);
505 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
506 assertArrayEquals(PAYLOAD, deserialized.hash);
507 assertFalse(deserialized.isBaseZeroPattern);
508
509 // Make sure the constants we use on the wire do not change.
510 assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE);
511 assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
512 assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
513 }
514
Adrian Roose5424992014-11-07 21:47:17 +0100515 private static void assertArrayEquals(byte[] expected, byte[] actual) {
516 if (!Arrays.equals(expected, actual)) {
517 fail("expected:<" + Arrays.toString(expected) +
518 "> but was:<" + Arrays.toString(actual) + ">");
519 }
520 }
Rubin Xu1de89b32016-11-30 20:03:13 +0000521
522 private void writePasswordBytes(byte[] password, int userId) {
523 mStorage.writeCredentialHash(CredentialHash.create(
524 password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), userId);
525 }
526
527 private void writePatternBytes(byte[] pattern, int userId) {
528 mStorage.writeCredentialHash(CredentialHash.create(
529 pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN), userId);
530 }
531
532 private void assertPasswordBytes(byte[] password, int userId) {
533 CredentialHash cred = mStorage.readCredentialHash(userId);
534 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, cred.type);
535 assertArrayEquals(password, cred.hash);
536 }
537
538 private void assertPatternBytes(byte[] pattern, int userId) {
539 CredentialHash cred = mStorage.readCredentialHash(userId);
540 assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, cred.type);
541 assertArrayEquals(pattern, cred.hash);
542 }
Adrian Roosb2375942018-01-19 22:31:28 +0100543
544 /**
545 * Suppresses reporting of the WTF to system_server, so we don't pollute the dropbox with
546 * intentionally caused WTFs.
547 */
548 private TerribleFailure suppressAndReturnWtf(Runnable r) {
549 TerribleFailure[] captured = new TerribleFailure[1];
550 TerribleFailureHandler prevWtfHandler = Log.setWtfHandler((t, w, s) -> captured[0] = w);
551 try {
552 r.run();
553 } finally {
554 Log.setWtfHandler(prevWtfHandler);
555 }
556 return captured[0];
557 }
Adrian Roos8527b232019-05-13 09:35:21 -0700558
559 private static void joinAll(List<Thread> threads, long timeoutMillis) {
560 long deadline = SystemClock.uptimeMillis() + timeoutMillis;
561 for (Thread t : threads) {
562 try {
563 t.join(deadline - SystemClock.uptimeMillis());
564 if (t.isAlive()) {
565 t.interrupt();
566 throw new RuntimeException(
567 "Joining " + t + " timed out. Stack: \n" + getStack(t));
568 }
569 } catch (InterruptedException e) {
570 throw new RuntimeException("Interrupted while joining " + t, e);
571 }
572 }
573 }
574
575 private static String getStack(Thread t) {
576 StringBuilder sb = new StringBuilder();
577 sb.append(t.toString()).append('\n');
578 for (StackTraceElement ste : t.getStackTrace()) {
579 sb.append("\tat ").append(ste.toString()).append('\n');
580 }
581 return sb.toString();
582 }
Adrian Roose5424992014-11-07 21:47:17 +0100583}