Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 17 | package com.android.server.locksettings; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 18 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 19 | import static org.mockito.Matchers.eq; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 24 | import android.app.NotificationManager; |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 25 | import android.app.admin.DevicePolicyManager; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 26 | import android.app.trust.TrustManager; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 27 | import android.content.pm.UserInfo; |
| 28 | import android.database.sqlite.SQLiteDatabase; |
| 29 | import android.os.FileUtils; |
Adrian Roos | 8527b23 | 2019-05-13 09:35:21 -0700 | [diff] [blame^] | 30 | import android.os.SystemClock; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 31 | import android.os.UserManager; |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 32 | import android.os.storage.StorageManager; |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 33 | import android.platform.test.annotations.Presubmit; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 34 | import android.test.AndroidTestCase; |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.util.Log.TerribleFailure; |
| 37 | import android.util.Log.TerribleFailureHandler; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 38 | |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 39 | import androidx.test.filters.SmallTest; |
| 40 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 41 | import com.android.internal.widget.LockPatternUtils; |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 42 | import com.android.server.PersistentDataBlockManagerInternal; |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 43 | import com.android.server.locksettings.LockSettingsStorage.CredentialHash; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 44 | import com.android.server.locksettings.LockSettingsStorage.PersistentData; |
| 45 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 46 | import java.io.File; |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Arrays; |
| 49 | import java.util.List; |
| 50 | import java.util.concurrent.CountDownLatch; |
| 51 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 52 | /** |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 53 | * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 54 | */ |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 55 | @SmallTest |
| 56 | @Presubmit |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 57 | public class LockSettingsStorageTests extends AndroidTestCase { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 58 | private static final int SOME_USER_ID = 1034; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 59 | private final byte[] PASSWORD_0 = "thepassword0".getBytes(); |
| 60 | private final byte[] PASSWORD_1 = "password1".getBytes(); |
| 61 | private final byte[] PATTERN_0 = "123654".getBytes(); |
| 62 | private final byte[] PATTERN_1 = "147852369".getBytes(); |
| 63 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 64 | public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33}; |
| 65 | |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 66 | LockSettingsStorageTestable mStorage; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 67 | File mStorageDir; |
| 68 | |
| 69 | private File mDb; |
| 70 | |
| 71 | @Override |
| 72 | protected void setUp() throws Exception { |
| 73 | super.setUp(); |
| 74 | mStorageDir = new File(getContext().getFilesDir(), "locksettings"); |
| 75 | mDb = getContext().getDatabasePath("locksettings.db"); |
| 76 | |
| 77 | assertTrue(mStorageDir.exists() || mStorageDir.mkdirs()); |
| 78 | assertTrue(FileUtils.deleteContents(mStorageDir)); |
| 79 | assertTrue(!mDb.exists() || mDb.delete()); |
| 80 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 81 | final UserManager mockUserManager = mock(UserManager.class); |
| 82 | // User 2 is a profile of user 1. |
| 83 | when(mockUserManager.getProfileParent(eq(2))).thenReturn(new UserInfo(1, "name", 0)); |
| 84 | // User 3 is a profile of user 0. |
| 85 | when(mockUserManager.getProfileParent(eq(3))).thenReturn(new UserInfo(0, "name", 0)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 86 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 87 | MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager, |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 88 | mock(NotificationManager.class), mock(DevicePolicyManager.class), |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 89 | mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class)); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 90 | mStorage = new LockSettingsStorageTestable(context, |
| 91 | new File(getContext().getFilesDir(), "locksettings")); |
| 92 | mStorage.setDatabaseOnCreateCallback(new LockSettingsStorage.Callback() { |
| 93 | @Override |
| 94 | public void initialize(SQLiteDatabase db) { |
| 95 | mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0); |
| 96 | } |
| 97 | }); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void tearDown() throws Exception { |
| 102 | super.tearDown(); |
| 103 | mStorage.closeDatabase(); |
| 104 | } |
| 105 | |
| 106 | public void testKeyValue_InitializeWorked() { |
| 107 | assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0)); |
| 108 | mStorage.clearCache(); |
| 109 | assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0)); |
| 110 | } |
| 111 | |
| 112 | public void testKeyValue_WriteThenRead() { |
| 113 | mStorage.writeKeyValue("key", "value", 0); |
| 114 | assertEquals("value", mStorage.readKeyValue("key", "default", 0)); |
| 115 | mStorage.clearCache(); |
| 116 | assertEquals("value", mStorage.readKeyValue("key", "default", 0)); |
| 117 | } |
| 118 | |
| 119 | public void testKeyValue_DefaultValue() { |
| 120 | assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0)); |
| 121 | assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0)); |
| 122 | } |
| 123 | |
| 124 | public void testKeyValue_Concurrency() { |
| 125 | final Object monitor = new Object(); |
| 126 | List<Thread> threads = new ArrayList<>(); |
| 127 | for (int i = 0; i < 100; i++) { |
| 128 | final int threadId = i; |
Adrian Roos | 8527b23 | 2019-05-13 09:35:21 -0700 | [diff] [blame^] | 129 | threads.add(new Thread("testKeyValue_Concurrency_" + i) { |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 130 | @Override |
| 131 | public void run() { |
| 132 | synchronized (monitor) { |
| 133 | try { |
| 134 | monitor.wait(); |
| 135 | } catch (InterruptedException e) { |
| 136 | return; |
| 137 | } |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 138 | } |
Adrian Roos | 8527b23 | 2019-05-13 09:35:21 -0700 | [diff] [blame^] | 139 | mStorage.writeKeyValue("key", "1 from thread " + threadId, 0); |
| 140 | mStorage.readKeyValue("key", "default", 0); |
| 141 | mStorage.writeKeyValue("key", "2 from thread " + threadId, 0); |
| 142 | mStorage.readKeyValue("key", "default", 0); |
| 143 | mStorage.writeKeyValue("key", "3 from thread " + threadId, 0); |
| 144 | mStorage.readKeyValue("key", "default", 0); |
| 145 | mStorage.writeKeyValue("key", "4 from thread " + threadId, 0); |
| 146 | mStorage.readKeyValue("key", "default", 0); |
| 147 | mStorage.writeKeyValue("key", "5 from thread " + threadId, 0); |
| 148 | mStorage.readKeyValue("key", "default", 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 149 | } |
| 150 | }); |
| 151 | threads.get(i).start(); |
| 152 | } |
| 153 | mStorage.writeKeyValue("key", "initalValue", 0); |
| 154 | synchronized (monitor) { |
| 155 | monitor.notifyAll(); |
| 156 | } |
Adrian Roos | 8527b23 | 2019-05-13 09:35:21 -0700 | [diff] [blame^] | 157 | joinAll(threads, 10000); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 158 | assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0)); |
| 159 | mStorage.clearCache(); |
| 160 | assertEquals('5', mStorage.readKeyValue("key", "default", 0).charAt(0)); |
| 161 | } |
| 162 | |
| 163 | public void testKeyValue_CacheStarvedWriter() { |
| 164 | final CountDownLatch latch = new CountDownLatch(1); |
| 165 | List<Thread> threads = new ArrayList<>(); |
| 166 | for (int i = 0; i < 100; i++) { |
| 167 | final int threadId = i; |
| 168 | threads.add(new Thread() { |
| 169 | @Override |
| 170 | public void run() { |
| 171 | try { |
| 172 | latch.await(); |
| 173 | } catch (InterruptedException e) { |
| 174 | return; |
| 175 | } |
| 176 | if (threadId == 50) { |
| 177 | mStorage.writeKeyValue("starvedWriterKey", "value", 0); |
| 178 | } else { |
| 179 | mStorage.readKeyValue("starvedWriterKey", "default", 0); |
| 180 | } |
| 181 | } |
| 182 | }); |
| 183 | threads.get(i).start(); |
| 184 | } |
| 185 | latch.countDown(); |
| 186 | for (int i = 0; i < threads.size(); i++) { |
| 187 | try { |
| 188 | threads.get(i).join(); |
| 189 | } catch (InterruptedException e) { |
| 190 | } |
| 191 | } |
| 192 | String cached = mStorage.readKeyValue("key", "default", 0); |
| 193 | mStorage.clearCache(); |
| 194 | String storage = mStorage.readKeyValue("key", "default", 0); |
| 195 | assertEquals("Cached value didn't match stored value", storage, cached); |
| 196 | } |
| 197 | |
| 198 | public void testRemoveUser() { |
| 199 | mStorage.writeKeyValue("key", "value", 0); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 200 | writePasswordBytes(PASSWORD_0, 0); |
| 201 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 202 | |
| 203 | mStorage.writeKeyValue("key", "value", 1); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 204 | writePasswordBytes(PASSWORD_1, 1); |
| 205 | writePatternBytes(PATTERN_1, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 206 | |
| 207 | mStorage.removeUser(0); |
| 208 | |
| 209 | assertEquals("value", mStorage.readKeyValue("key", "default", 1)); |
| 210 | assertEquals("default", mStorage.readKeyValue("key", "default", 0)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 211 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type); |
| 212 | assertPatternBytes(PATTERN_1, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 215 | public void testCredential_Default() { |
| 216 | assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | public void testPassword_Write() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 220 | writePasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 221 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 222 | assertPasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 223 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 224 | assertPasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | public void testPassword_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 228 | writePasswordBytes(PASSWORD_0, 1); |
| 229 | writePasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 230 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 231 | assertPasswordBytes(PASSWORD_0, 1); |
| 232 | assertPasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 233 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 234 | assertPasswordBytes(PASSWORD_0, 1); |
| 235 | assertPasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 238 | public void testLockType_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 239 | writePasswordBytes(PASSWORD_0, 10); |
| 240 | writePatternBytes(PATTERN_0, 20); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 241 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 242 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, |
| 243 | mStorage.readCredentialHash(10).type); |
| 244 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, |
| 245 | mStorage.readCredentialHash(20).type); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 246 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 247 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, |
| 248 | mStorage.readCredentialHash(10).type); |
| 249 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, |
| 250 | mStorage.readCredentialHash(20).type); |
| 251 | } |
| 252 | |
| 253 | public void testPassword_WriteParentWritesProfile() { |
| 254 | writePasswordBytes(PASSWORD_0, 2); |
| 255 | writePasswordBytes(PASSWORD_1, 1); |
| 256 | |
| 257 | assertPasswordBytes(PASSWORD_1, 1); |
| 258 | assertPasswordBytes(PASSWORD_0, 2); |
| 259 | mStorage.clearCache(); |
| 260 | assertPasswordBytes(PASSWORD_1, 1); |
| 261 | assertPasswordBytes(PASSWORD_0, 2); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | public void testProfileLock_ReadWriteChildProfileLock() { |
| 265 | assertFalse(mStorage.hasChildProfileLock(20)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 266 | mStorage.writeChildProfileLock(20, PASSWORD_0); |
| 267 | assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20)); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 268 | assertTrue(mStorage.hasChildProfileLock(20)); |
| 269 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 270 | assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20)); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 271 | assertTrue(mStorage.hasChildProfileLock(20)); |
| 272 | } |
| 273 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 274 | public void testPattern_Write() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 275 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 276 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 277 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 278 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 279 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | public void testPattern_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 283 | writePatternBytes(PATTERN_0, 1); |
| 284 | writePatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 285 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 286 | assertPatternBytes(PATTERN_0, 1); |
| 287 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 288 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 289 | assertPatternBytes(PATTERN_0, 1); |
| 290 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | public void testPattern_WriteParentWritesProfile() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 294 | writePatternBytes(PATTERN_1, 2); |
| 295 | writePatternBytes(PATTERN_0, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 296 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 297 | assertPatternBytes(PATTERN_0, 1); |
| 298 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 299 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 300 | assertPatternBytes(PATTERN_0, 1); |
| 301 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | public void testPrefetch() { |
| 305 | mStorage.writeKeyValue("key", "toBeFetched", 0); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 306 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 307 | |
| 308 | mStorage.clearCache(); |
| 309 | mStorage.prefetchUser(0); |
| 310 | |
| 311 | assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 312 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | public void testFileLocation_Owner() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 316 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 317 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 318 | assertEquals("/data/system/gesture.key", storage.getLegacyLockPatternFilename(0)); |
| 319 | assertEquals("/data/system/password.key", storage.getLegacyLockPasswordFilename(0)); |
| 320 | assertEquals("/data/system/gatekeeper.pattern.key", storage.getLockPatternFilename(0)); |
| 321 | assertEquals("/data/system/gatekeeper.password.key", storage.getLockPasswordFilename(0)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | public void testFileLocation_SecondaryUser() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 325 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 326 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 327 | assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1)); |
| 328 | assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | public void testFileLocation_ProfileToSecondary() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 332 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 333 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 334 | assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2)); |
| 335 | assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | public void testFileLocation_ProfileToOwner() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 339 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 340 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 341 | assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3)); |
| 342 | assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 345 | public void testSyntheticPasswordState() { |
| 346 | final byte[] data = {1,2,3,4}; |
| 347 | mStorage.writeSyntheticPasswordState(10, 1234L, "state", data); |
| 348 | assertArrayEquals(data, mStorage.readSyntheticPasswordState(10, 1234L, "state")); |
| 349 | assertEquals(null, mStorage.readSyntheticPasswordState(0, 1234L, "state")); |
| 350 | |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 351 | mStorage.deleteSyntheticPasswordState(10, 1234L, "state"); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 352 | assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state")); |
| 353 | } |
| 354 | |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 355 | public void testPersistentDataBlock_unavailable() { |
| 356 | mStorage.mPersistentDataBlock = null; |
| 357 | |
| 358 | assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); |
| 359 | } |
| 360 | |
| 361 | public void testPersistentDataBlock_empty() { |
| 362 | mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); |
| 363 | |
| 364 | assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); |
| 365 | } |
| 366 | |
| 367 | public void testPersistentDataBlock_withData() { |
| 368 | mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); |
| 369 | when(mStorage.mPersistentDataBlock.getFrpCredentialHandle()) |
| 370 | .thenReturn(PersistentData.toBytes(PersistentData.TYPE_SP_WEAVER, SOME_USER_ID, |
| 371 | DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD)); |
| 372 | |
| 373 | PersistentData data = mStorage.readPersistentDataBlock(); |
| 374 | |
| 375 | assertEquals(PersistentData.TYPE_SP_WEAVER, data.type); |
| 376 | assertEquals(SOME_USER_ID, data.userId); |
| 377 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, data.qualityForUi); |
| 378 | assertArrayEquals(PAYLOAD, data.payload); |
| 379 | } |
| 380 | |
| 381 | public void testPersistentDataBlock_exception() { |
| 382 | mStorage.mPersistentDataBlock = mock(PersistentDataBlockManagerInternal.class); |
| 383 | when(mStorage.mPersistentDataBlock.getFrpCredentialHandle()) |
| 384 | .thenThrow(new IllegalStateException("oops")); |
| 385 | assertSame(PersistentData.NONE, mStorage.readPersistentDataBlock()); |
| 386 | } |
| 387 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 388 | public void testPersistentData_serializeUnserialize() { |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 389 | byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID, |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 390 | DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD); |
| 391 | PersistentData deserialized = PersistentData.fromBytes(serialized); |
| 392 | |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 393 | assertEquals(PersistentData.TYPE_SP, deserialized.type); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 394 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, deserialized.qualityForUi); |
| 395 | assertArrayEquals(PAYLOAD, deserialized.payload); |
| 396 | } |
| 397 | |
| 398 | public void testPersistentData_unserializeNull() { |
| 399 | PersistentData deserialized = PersistentData.fromBytes(null); |
| 400 | assertSame(PersistentData.NONE, deserialized); |
| 401 | } |
| 402 | |
| 403 | public void testPersistentData_unserializeEmptyArray() { |
| 404 | PersistentData deserialized = PersistentData.fromBytes(new byte[0]); |
| 405 | assertSame(PersistentData.NONE, deserialized); |
| 406 | } |
| 407 | |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 408 | public void testPersistentData_unserializeInvalid() { |
| 409 | assertNotNull(suppressAndReturnWtf(() -> { |
| 410 | PersistentData deserialized = PersistentData.fromBytes(new byte[]{5}); |
| 411 | assertSame(PersistentData.NONE, deserialized); |
| 412 | })); |
| 413 | } |
| 414 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 415 | public void testPersistentData_unserialize_version1() { |
| 416 | // This test ensures that we can read serialized VERSION_1 PersistentData even if we change |
| 417 | // the wire format in the future. |
| 418 | byte[] serializedVersion1 = new byte[] { |
| 419 | 1, /* PersistentData.VERSION_1 */ |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 420 | 1, /* PersistentData.TYPE_SP */ |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 421 | 0x00, 0x00, 0x04, 0x0A, /* SOME_USER_ID */ |
| 422 | 0x00, 0x03, 0x00, 0x00, /* PASSWORD_NUMERIC_COMPLEX */ |
| 423 | 1, 2, -1, -2, 33, /* PAYLOAD */ |
| 424 | }; |
| 425 | PersistentData deserialized = PersistentData.fromBytes(serializedVersion1); |
| 426 | assertEquals(PersistentData.TYPE_SP, deserialized.type); |
| 427 | assertEquals(SOME_USER_ID, deserialized.userId); |
| 428 | assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX, |
| 429 | deserialized.qualityForUi); |
| 430 | assertArrayEquals(PAYLOAD, deserialized.payload); |
| 431 | |
| 432 | // Make sure the constants we use on the wire do not change. |
| 433 | assertEquals(0, PersistentData.TYPE_NONE); |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 434 | assertEquals(1, PersistentData.TYPE_SP); |
| 435 | assertEquals(2, PersistentData.TYPE_SP_WEAVER); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | public void testCredentialHash_serializeUnserialize() { |
| 439 | byte[] serialized = CredentialHash.create( |
| 440 | PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes(); |
| 441 | CredentialHash deserialized = CredentialHash.fromBytes(serialized); |
| 442 | |
| 443 | assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version); |
| 444 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type); |
| 445 | assertArrayEquals(PAYLOAD, deserialized.hash); |
| 446 | assertFalse(deserialized.isBaseZeroPattern); |
| 447 | } |
| 448 | |
| 449 | public void testCredentialHash_unserialize_versionGatekeeper() { |
| 450 | // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes |
| 451 | // even if we change the wire format in the future. |
| 452 | byte[] serialized = new byte[] { |
| 453 | 1, /* VERSION_GATEKEEPER */ |
| 454 | 2, /* CREDENTIAL_TYPE_PASSWORD */ |
| 455 | 0, 0, 0, 5, /* hash length */ |
| 456 | 1, 2, -1, -2, 33, /* hash */ |
| 457 | }; |
| 458 | CredentialHash deserialized = CredentialHash.fromBytes(serialized); |
| 459 | |
| 460 | assertEquals(CredentialHash.VERSION_GATEKEEPER, deserialized.version); |
| 461 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type); |
| 462 | assertArrayEquals(PAYLOAD, deserialized.hash); |
| 463 | assertFalse(deserialized.isBaseZeroPattern); |
| 464 | |
| 465 | // Make sure the constants we use on the wire do not change. |
| 466 | assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE); |
| 467 | assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN); |
| 468 | assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD); |
| 469 | } |
| 470 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 471 | private static void assertArrayEquals(byte[] expected, byte[] actual) { |
| 472 | if (!Arrays.equals(expected, actual)) { |
| 473 | fail("expected:<" + Arrays.toString(expected) + |
| 474 | "> but was:<" + Arrays.toString(actual) + ">"); |
| 475 | } |
| 476 | } |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 477 | |
| 478 | private void writePasswordBytes(byte[] password, int userId) { |
| 479 | mStorage.writeCredentialHash(CredentialHash.create( |
| 480 | password, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD), userId); |
| 481 | } |
| 482 | |
| 483 | private void writePatternBytes(byte[] pattern, int userId) { |
| 484 | mStorage.writeCredentialHash(CredentialHash.create( |
| 485 | pattern, LockPatternUtils.CREDENTIAL_TYPE_PATTERN), userId); |
| 486 | } |
| 487 | |
| 488 | private void assertPasswordBytes(byte[] password, int userId) { |
| 489 | CredentialHash cred = mStorage.readCredentialHash(userId); |
| 490 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, cred.type); |
| 491 | assertArrayEquals(password, cred.hash); |
| 492 | } |
| 493 | |
| 494 | private void assertPatternBytes(byte[] pattern, int userId) { |
| 495 | CredentialHash cred = mStorage.readCredentialHash(userId); |
| 496 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, cred.type); |
| 497 | assertArrayEquals(pattern, cred.hash); |
| 498 | } |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 499 | |
| 500 | /** |
| 501 | * Suppresses reporting of the WTF to system_server, so we don't pollute the dropbox with |
| 502 | * intentionally caused WTFs. |
| 503 | */ |
| 504 | private TerribleFailure suppressAndReturnWtf(Runnable r) { |
| 505 | TerribleFailure[] captured = new TerribleFailure[1]; |
| 506 | TerribleFailureHandler prevWtfHandler = Log.setWtfHandler((t, w, s) -> captured[0] = w); |
| 507 | try { |
| 508 | r.run(); |
| 509 | } finally { |
| 510 | Log.setWtfHandler(prevWtfHandler); |
| 511 | } |
| 512 | return captured[0]; |
| 513 | } |
Adrian Roos | 8527b23 | 2019-05-13 09:35:21 -0700 | [diff] [blame^] | 514 | |
| 515 | private static void joinAll(List<Thread> threads, long timeoutMillis) { |
| 516 | long deadline = SystemClock.uptimeMillis() + timeoutMillis; |
| 517 | for (Thread t : threads) { |
| 518 | try { |
| 519 | t.join(deadline - SystemClock.uptimeMillis()); |
| 520 | if (t.isAlive()) { |
| 521 | t.interrupt(); |
| 522 | throw new RuntimeException( |
| 523 | "Joining " + t + " timed out. Stack: \n" + getStack(t)); |
| 524 | } |
| 525 | } catch (InterruptedException e) { |
| 526 | throw new RuntimeException("Interrupted while joining " + t, e); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | private static String getStack(Thread t) { |
| 532 | StringBuilder sb = new StringBuilder(); |
| 533 | sb.append(t.toString()).append('\n'); |
| 534 | for (StackTraceElement ste : t.getStackTrace()) { |
| 535 | sb.append("\tat ").append(ste.toString()).append('\n'); |
| 536 | } |
| 537 | return sb.toString(); |
| 538 | } |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 539 | } |