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