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