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; |
| 30 | import android.os.UserManager; |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 31 | import android.os.storage.StorageManager; |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 32 | import android.platform.test.annotations.Presubmit; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 33 | import android.test.AndroidTestCase; |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | import android.util.Log.TerribleFailure; |
| 36 | import android.util.Log.TerribleFailureHandler; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 37 | |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 38 | import androidx.test.filters.SmallTest; |
| 39 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 40 | import com.android.internal.widget.LockPatternUtils; |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 41 | import com.android.server.PersistentDataBlockManagerInternal; |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 42 | import com.android.server.locksettings.LockSettingsStorage.CredentialHash; |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 43 | import com.android.server.locksettings.LockSettingsStorage.PersistentData; |
| 44 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 45 | import java.io.File; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Arrays; |
| 48 | import java.util.List; |
| 49 | import java.util.concurrent.CountDownLatch; |
| 50 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 51 | /** |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 52 | * runtest frameworks-services -c com.android.server.locksettings.LockSettingsStorageTests |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 53 | */ |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 54 | @SmallTest |
| 55 | @Presubmit |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 56 | public class LockSettingsStorageTests extends AndroidTestCase { |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 57 | private static final int SOME_USER_ID = 1034; |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 58 | 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 Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 63 | public static final byte[] PAYLOAD = new byte[] {1, 2, -1, -2, 33}; |
| 64 | |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 65 | LockSettingsStorageTestable mStorage; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 66 | 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 Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 80 | 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 Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 85 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 86 | MockLockSettingsContext context = new MockLockSettingsContext(getContext(), mockUserManager, |
Rubin Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 87 | mock(NotificationManager.class), mock(DevicePolicyManager.class), |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 88 | mock(StorageManager.class), mock(TrustManager.class), mock(KeyguardManager.class)); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 89 | 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 Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 97 | } |
| 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 Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 204 | writePasswordBytes(PASSWORD_0, 0); |
| 205 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 206 | |
| 207 | mStorage.writeKeyValue("key", "value", 1); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 208 | writePasswordBytes(PASSWORD_1, 1); |
| 209 | writePatternBytes(PATTERN_1, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 210 | |
| 211 | mStorage.removeUser(0); |
| 212 | |
| 213 | assertEquals("value", mStorage.readKeyValue("key", "default", 1)); |
| 214 | assertEquals("default", mStorage.readKeyValue("key", "default", 0)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 215 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_NONE, mStorage.readCredentialHash(0).type); |
| 216 | assertPatternBytes(PATTERN_1, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 219 | public void testCredential_Default() { |
| 220 | assertEquals(mStorage.readCredentialHash(0).type, LockPatternUtils.CREDENTIAL_TYPE_NONE); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | public void testPassword_Write() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 224 | writePasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 225 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 226 | assertPasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 227 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 228 | assertPasswordBytes(PASSWORD_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | public void testPassword_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 232 | writePasswordBytes(PASSWORD_0, 1); |
| 233 | writePasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 234 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 235 | assertPasswordBytes(PASSWORD_0, 1); |
| 236 | assertPasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 237 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 238 | assertPasswordBytes(PASSWORD_0, 1); |
| 239 | assertPasswordBytes(PASSWORD_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 242 | public void testLockType_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 243 | writePasswordBytes(PASSWORD_0, 10); |
| 244 | writePatternBytes(PATTERN_0, 20); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 245 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 246 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, |
| 247 | mStorage.readCredentialHash(10).type); |
| 248 | assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PATTERN, |
| 249 | mStorage.readCredentialHash(20).type); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 250 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 251 | 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 Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | public void testProfileLock_ReadWriteChildProfileLock() { |
| 269 | assertFalse(mStorage.hasChildProfileLock(20)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 270 | mStorage.writeChildProfileLock(20, PASSWORD_0); |
| 271 | assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20)); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 272 | assertTrue(mStorage.hasChildProfileLock(20)); |
| 273 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 274 | assertArrayEquals(PASSWORD_0, mStorage.readChildProfileLock(20)); |
Ricky Wai | a46b40f | 2016-03-31 16:48:29 +0100 | [diff] [blame] | 275 | assertTrue(mStorage.hasChildProfileLock(20)); |
| 276 | } |
| 277 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 278 | public void testPattern_Write() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 279 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 280 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 281 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 282 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 283 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | public void testPattern_WriteProfileWritesParent() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 287 | writePatternBytes(PATTERN_0, 1); |
| 288 | writePatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 289 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 290 | assertPatternBytes(PATTERN_0, 1); |
| 291 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 292 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 293 | assertPatternBytes(PATTERN_0, 1); |
| 294 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | public void testPattern_WriteParentWritesProfile() { |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 298 | writePatternBytes(PATTERN_1, 2); |
| 299 | writePatternBytes(PATTERN_0, 1); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 300 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 301 | assertPatternBytes(PATTERN_0, 1); |
| 302 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 303 | mStorage.clearCache(); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 304 | assertPatternBytes(PATTERN_0, 1); |
| 305 | assertPatternBytes(PATTERN_1, 2); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | public void testPrefetch() { |
| 309 | mStorage.writeKeyValue("key", "toBeFetched", 0); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 310 | writePatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 311 | |
| 312 | mStorage.clearCache(); |
| 313 | mStorage.prefetchUser(0); |
| 314 | |
| 315 | assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0)); |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 316 | assertPatternBytes(PATTERN_0, 0); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | public void testFileLocation_Owner() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 320 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 321 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 322 | 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 Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | public void testFileLocation_SecondaryUser() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 329 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 330 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 331 | assertEquals("/data/system/users/1/gatekeeper.pattern.key", storage.getLockPatternFilename(1)); |
| 332 | assertEquals("/data/system/users/1/gatekeeper.password.key", storage.getLockPasswordFilename(1)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | public void testFileLocation_ProfileToSecondary() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 336 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 337 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 338 | assertEquals("/data/system/users/2/gatekeeper.pattern.key", storage.getLockPatternFilename(2)); |
| 339 | assertEquals("/data/system/users/2/gatekeeper.password.key", storage.getLockPasswordFilename(2)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public void testFileLocation_ProfileToOwner() { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 343 | LockSettingsStorage storage = new LockSettingsStorage(getContext()); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 344 | |
Rubin Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 345 | assertEquals("/data/system/users/3/gatekeeper.pattern.key", storage.getLockPatternFilename(3)); |
| 346 | assertEquals("/data/system/users/3/gatekeeper.password.key", storage.getLockPasswordFilename(3)); |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 349 | 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 Xu | aa32d15 | 2017-04-27 17:01:05 +0100 | [diff] [blame] | 355 | mStorage.deleteSyntheticPasswordState(10, 1234L, "state"); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 356 | assertEquals(null, mStorage.readSyntheticPasswordState(10, 1234L, "state")); |
| 357 | } |
| 358 | |
Adrian Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 359 | 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 Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 392 | public void testPersistentData_serializeUnserialize() { |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 393 | byte[] serialized = PersistentData.toBytes(PersistentData.TYPE_SP, SOME_USER_ID, |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 394 | DevicePolicyManager.PASSWORD_QUALITY_COMPLEX, PAYLOAD); |
| 395 | PersistentData deserialized = PersistentData.fromBytes(serialized); |
| 396 | |
Andrew Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 397 | assertEquals(PersistentData.TYPE_SP, deserialized.type); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 398 | 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 Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 412 | public void testPersistentData_unserializeInvalid() { |
| 413 | assertNotNull(suppressAndReturnWtf(() -> { |
| 414 | PersistentData deserialized = PersistentData.fromBytes(new byte[]{5}); |
| 415 | assertSame(PersistentData.NONE, deserialized); |
| 416 | })); |
| 417 | } |
| 418 | |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 419 | 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 Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 424 | 1, /* PersistentData.TYPE_SP */ |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 425 | 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 Scull | 971f294 | 2017-07-12 15:09:45 +0100 | [diff] [blame] | 438 | assertEquals(1, PersistentData.TYPE_SP); |
| 439 | assertEquals(2, PersistentData.TYPE_SP_WEAVER); |
Adrian Roos | 7374d3a | 2017-03-31 14:14:53 -0700 | [diff] [blame] | 440 | } |
| 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 Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 475 | 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 Xu | 1de89b3 | 2016-11-30 20:03:13 +0000 | [diff] [blame] | 481 | |
| 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 Roos | b237594 | 2018-01-19 22:31:28 +0100 | [diff] [blame] | 503 | |
| 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 Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 518 | } |