Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.server.locksettings.recoverablekeystore.storage; |
| 18 | |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.assertArrayEquals; |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertNull; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | |
| 30 | import android.content.Context; |
Robert Berry | d9f11a9 | 2018-02-26 16:37:06 +0000 | [diff] [blame] | 31 | import android.security.keystore.recovery.RecoveryController; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 32 | import android.support.test.InstrumentationRegistry; |
| 33 | import android.support.test.filters.SmallTest; |
| 34 | import android.support.test.runner.AndroidJUnit4; |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 35 | |
| 36 | import com.android.server.locksettings.recoverablekeystore.TestData; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 37 | import com.android.server.locksettings.recoverablekeystore.WrappedKey; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.nio.charset.StandardCharsets; |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 41 | import java.security.KeyPairGenerator; |
| 42 | import java.security.PublicKey; |
| 43 | import java.security.spec.ECGenParameterSpec; |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 44 | import java.util.List; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 45 | import java.util.Map; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | public class RecoverableKeyStoreDbTest { |
| 50 | private static final String DATABASE_FILE_NAME = "recoverablekeystore.db"; |
| 51 | |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 52 | private static final String TEST_ROOT_CERT_ALIAS = "trusted_root"; |
| 53 | private static final String TEST_ROOT_CERT_ALIAS2 = "another_trusted_root"; |
| 54 | |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 55 | private RecoverableKeyStoreDb mRecoverableKeyStoreDb; |
| 56 | private File mDatabaseFile; |
| 57 | |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 58 | private static final byte[] SERVER_PARAMS = |
| 59 | new byte[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}; |
| 60 | |
| 61 | private static final byte[] SERVER_PARAMS2 = |
| 62 | new byte[]{1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; |
| 63 | |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 64 | @Before |
| 65 | public void setUp() { |
| 66 | Context context = InstrumentationRegistry.getTargetContext(); |
| 67 | mDatabaseFile = context.getDatabasePath(DATABASE_FILE_NAME); |
| 68 | mRecoverableKeyStoreDb = RecoverableKeyStoreDb.newInstance(context); |
| 69 | } |
| 70 | |
| 71 | @After |
| 72 | public void tearDown() { |
| 73 | mRecoverableKeyStoreDb.close(); |
| 74 | mDatabaseFile.delete(); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void insertKey_replacesOldKey() { |
| 79 | int userId = 12; |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 80 | int uid = 10009; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 81 | String alias = "test"; |
| 82 | WrappedKey oldWrappedKey = new WrappedKey( |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 83 | getUtf8Bytes("nonce1"), |
| 84 | getUtf8Bytes("keymaterial1"), |
Robert Berry | bc08840 | 2017-12-18 13:10:41 +0000 | [diff] [blame] | 85 | /*platformKeyGenerationId=*/ 1); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 86 | mRecoverableKeyStoreDb.insertKey( |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 87 | userId, uid, alias, oldWrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 88 | byte[] nonce = getUtf8Bytes("nonce2"); |
| 89 | byte[] keyMaterial = getUtf8Bytes("keymaterial2"); |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 90 | WrappedKey newWrappedKey = new WrappedKey( |
| 91 | nonce, keyMaterial, /*platformKeyGenerationId=*/2); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 92 | |
| 93 | mRecoverableKeyStoreDb.insertKey( |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 94 | userId, uid, alias, newWrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 95 | |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 96 | WrappedKey retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 97 | assertArrayEquals(nonce, retrievedKey.getNonce()); |
| 98 | assertArrayEquals(keyMaterial, retrievedKey.getKeyMaterial()); |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 99 | assertEquals(2, retrievedKey.getPlatformKeyGenerationId()); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
Robert Berry | bc08840 | 2017-12-18 13:10:41 +0000 | [diff] [blame] | 103 | public void insertKey_allowsTwoUidsToHaveSameAlias() { |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 104 | int userId = 6; |
Robert Berry | bc08840 | 2017-12-18 13:10:41 +0000 | [diff] [blame] | 105 | String alias = "pcoulton"; |
| 106 | WrappedKey key1 = new WrappedKey( |
| 107 | getUtf8Bytes("nonce1"), |
| 108 | getUtf8Bytes("key1"), |
| 109 | /*platformKeyGenerationId=*/ 1); |
| 110 | WrappedKey key2 = new WrappedKey( |
| 111 | getUtf8Bytes("nonce2"), |
| 112 | getUtf8Bytes("key2"), |
| 113 | /*platformKeyGenerationId=*/ 1); |
| 114 | |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 115 | mRecoverableKeyStoreDb.insertKey(userId, /*uid=*/ 1, alias, key1); |
| 116 | mRecoverableKeyStoreDb.insertKey(userId, /*uid=*/ 2, alias, key2); |
Robert Berry | bc08840 | 2017-12-18 13:10:41 +0000 | [diff] [blame] | 117 | |
| 118 | assertArrayEquals( |
| 119 | getUtf8Bytes("nonce1"), |
| 120 | mRecoverableKeyStoreDb.getKey(1, alias).getNonce()); |
| 121 | assertArrayEquals( |
| 122 | getUtf8Bytes("nonce2"), |
| 123 | mRecoverableKeyStoreDb.getKey(2, alias).getNonce()); |
| 124 | } |
| 125 | |
| 126 | @Test |
Robert Berry | 5daccec | 2018-01-06 19:16:25 +0000 | [diff] [blame] | 127 | public void removeKey_removesAKey() { |
| 128 | int userId = 6; |
| 129 | int uid = 60001; |
| 130 | String alias = "rupertbates"; |
| 131 | WrappedKey key = new WrappedKey( |
| 132 | getUtf8Bytes("nonce1"), |
| 133 | getUtf8Bytes("key1"), |
| 134 | /*platformKeyGenerationId=*/ 1); |
| 135 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, key); |
| 136 | |
| 137 | assertTrue(mRecoverableKeyStoreDb.removeKey(uid, alias)); |
| 138 | |
| 139 | assertNull(mRecoverableKeyStoreDb.getKey(uid, alias)); |
| 140 | } |
| 141 | |
| 142 | @Test |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 143 | public void getKey_returnsNullIfNoKey() { |
| 144 | WrappedKey key = mRecoverableKeyStoreDb.getKey( |
| 145 | /*userId=*/ 1, /*alias=*/ "hello"); |
| 146 | |
| 147 | assertNull(key); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void getKey_returnsInsertedKey() { |
| 152 | int userId = 12; |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 153 | int uid = 1009; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 154 | int generationId = 6; |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 155 | int status = 120; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 156 | String alias = "test"; |
| 157 | byte[] nonce = getUtf8Bytes("nonce"); |
| 158 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 159 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId, 120); |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 160 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 161 | |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 162 | WrappedKey retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 163 | |
| 164 | assertArrayEquals(nonce, retrievedKey.getNonce()); |
| 165 | assertArrayEquals(keyMaterial, retrievedKey.getKeyMaterial()); |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 166 | assertEquals(generationId, retrievedKey.getPlatformKeyGenerationId()); |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 167 | assertEquals(status,retrievedKey.getRecoveryStatus()); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void getAllKeys_getsKeysWithUserIdAndGenerationId() { |
| 172 | int userId = 12; |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 173 | int uid = 1009; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 174 | int generationId = 6; |
| 175 | String alias = "test"; |
| 176 | byte[] nonce = getUtf8Bytes("nonce"); |
| 177 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 178 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId); |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 179 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 180 | |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 181 | Map<String, WrappedKey> keys = mRecoverableKeyStoreDb.getAllKeys(userId, uid, generationId); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 182 | |
| 183 | assertEquals(1, keys.size()); |
| 184 | assertTrue(keys.containsKey(alias)); |
| 185 | WrappedKey retrievedKey = keys.get(alias); |
| 186 | assertArrayEquals(nonce, retrievedKey.getNonce()); |
| 187 | assertArrayEquals(keyMaterial, retrievedKey.getKeyMaterial()); |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 188 | assertEquals(generationId, retrievedKey.getPlatformKeyGenerationId()); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void getAllKeys_doesNotReturnKeysWithBadGenerationId() { |
| 193 | int userId = 12; |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 194 | int uid = 6000; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 195 | WrappedKey wrappedKey = new WrappedKey( |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 196 | getUtf8Bytes("nonce"), |
| 197 | getUtf8Bytes("keymaterial"), |
| 198 | /*platformKeyGenerationId=*/ 5); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 199 | mRecoverableKeyStoreDb.insertKey( |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 200 | userId, uid, /*alias=*/ "test", wrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 201 | |
| 202 | Map<String, WrappedKey> keys = mRecoverableKeyStoreDb.getAllKeys( |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 203 | userId, uid, /*generationId=*/ 7); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 204 | |
| 205 | assertTrue(keys.isEmpty()); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void getAllKeys_doesNotReturnKeysWithBadUserId() { |
| 210 | int generationId = 12; |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 211 | int uid = 10009; |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 212 | WrappedKey wrappedKey = new WrappedKey( |
Robert Berry | 67b228c | 2017-12-18 19:26:22 +0000 | [diff] [blame] | 213 | getUtf8Bytes("nonce"), getUtf8Bytes("keymaterial"), generationId); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 214 | mRecoverableKeyStoreDb.insertKey( |
Robert Berry | b7c06ea | 2017-12-21 13:37:23 +0000 | [diff] [blame] | 215 | /*userId=*/ 1, uid, /*alias=*/ "test", wrappedKey); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 216 | |
| 217 | Map<String, WrappedKey> keys = mRecoverableKeyStoreDb.getAllKeys( |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 218 | /*userId=*/ 2, uid, generationId); |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 219 | |
| 220 | assertTrue(keys.isEmpty()); |
| 221 | } |
| 222 | |
Robert Berry | bc08840 | 2017-12-18 13:10:41 +0000 | [diff] [blame] | 223 | @Test |
| 224 | public void getPlatformKeyGenerationId_returnsGenerationId() { |
| 225 | int userId = 42; |
| 226 | int generationId = 24; |
| 227 | mRecoverableKeyStoreDb.setPlatformKeyGenerationId(userId, generationId); |
| 228 | |
| 229 | assertEquals(generationId, mRecoverableKeyStoreDb.getPlatformKeyGenerationId(userId)); |
| 230 | } |
| 231 | |
| 232 | @Test |
| 233 | public void getPlatformKeyGenerationId_returnsMinusOneIfNoEntry() { |
| 234 | assertEquals(-1, mRecoverableKeyStoreDb.getPlatformKeyGenerationId(42)); |
| 235 | } |
| 236 | |
| 237 | @Test |
| 238 | public void setPlatformKeyGenerationId_replacesOldEntry() { |
| 239 | int userId = 42; |
| 240 | mRecoverableKeyStoreDb.setPlatformKeyGenerationId(userId, 1); |
| 241 | mRecoverableKeyStoreDb.setPlatformKeyGenerationId(userId, 2); |
| 242 | |
| 243 | assertEquals(2, mRecoverableKeyStoreDb.getPlatformKeyGenerationId(userId)); |
| 244 | } |
| 245 | |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 246 | @Test |
| 247 | public void setRecoveryStatus_withSingleKey() { |
| 248 | int userId = 12; |
| 249 | int uid = 1009; |
| 250 | int generationId = 6; |
| 251 | int status = 120; |
| 252 | int status2 = 121; |
| 253 | String alias = "test"; |
| 254 | byte[] nonce = getUtf8Bytes("nonce"); |
| 255 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
| 256 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId, status); |
| 257 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKey); |
| 258 | |
| 259 | WrappedKey retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 260 | assertThat(retrievedKey.getRecoveryStatus()).isEqualTo(status); |
| 261 | |
| 262 | mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias, status2); |
| 263 | |
| 264 | retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 265 | assertThat(retrievedKey.getRecoveryStatus()).isEqualTo(status2); |
| 266 | } |
| 267 | |
| 268 | @Test |
| 269 | public void getStatusForAllKeys_with3Keys() { |
| 270 | int userId = 12; |
| 271 | int uid = 1009; |
| 272 | int generationId = 6; |
| 273 | int status = 120; |
| 274 | int status2 = 121; |
| 275 | String alias = "test"; |
| 276 | String alias2 = "test2"; |
| 277 | String alias3 = "test3"; |
| 278 | byte[] nonce = getUtf8Bytes("nonce"); |
| 279 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
| 280 | |
| 281 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId, status); |
| 282 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias2, wrappedKey); |
| 283 | WrappedKey wrappedKey2 = new WrappedKey(nonce, keyMaterial, generationId, status); |
| 284 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias3, wrappedKey); |
| 285 | WrappedKey wrappedKeyWithDefaultStatus = new WrappedKey(nonce, keyMaterial, generationId); |
| 286 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKeyWithDefaultStatus); |
| 287 | |
| 288 | Map<String, Integer> statuses = mRecoverableKeyStoreDb.getStatusForAllKeys(uid); |
| 289 | assertThat(statuses).hasSize(3); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 290 | assertThat(statuses).containsEntry(alias, |
| 291 | RecoveryController.RECOVERY_STATUS_SYNC_IN_PROGRESS); |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 292 | assertThat(statuses).containsEntry(alias2, status); |
| 293 | assertThat(statuses).containsEntry(alias3, status); |
| 294 | |
| 295 | int updates = mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias, status2); |
| 296 | assertThat(updates).isEqualTo(1); |
| 297 | updates = mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias3, status2); |
| 298 | assertThat(updates).isEqualTo(1); |
| 299 | statuses = mRecoverableKeyStoreDb.getStatusForAllKeys(uid); |
| 300 | |
| 301 | assertThat(statuses).hasSize(3); |
| 302 | assertThat(statuses).containsEntry(alias, status2); // updated from default |
| 303 | assertThat(statuses).containsEntry(alias2, status); |
| 304 | assertThat(statuses).containsEntry(alias3, status2); // updated |
| 305 | } |
| 306 | |
| 307 | @Test |
| 308 | public void setRecoveryStatus_withEmptyDatabase() throws Exception{ |
| 309 | int uid = 1009; |
| 310 | String alias = "test"; |
| 311 | int status = 120; |
| 312 | int updates = mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias, status); |
| 313 | assertThat(updates).isEqualTo(0); // database was empty |
| 314 | } |
| 315 | |
| 316 | |
| 317 | @Test |
| 318 | public void getStatusForAllKeys_withEmptyDatabase() { |
| 319 | int uid = 1009; |
| 320 | Map<String, Integer> statuses = mRecoverableKeyStoreDb.getStatusForAllKeys(uid); |
| 321 | assertThat(statuses).hasSize(0); |
| 322 | } |
| 323 | |
Andreas Gampe | bd66641 | 2018-02-19 10:47:28 -0800 | [diff] [blame] | 324 | @Test |
Dmitry Dementyev | 3f2d171 | 2018-01-24 11:11:40 -0800 | [diff] [blame] | 325 | public void testInvalidateKeysWithOldGenerationId_withSingleKey() { |
| 326 | int userId = 12; |
| 327 | int uid = 1009; |
| 328 | int generationId = 6; |
| 329 | int status = 120; |
| 330 | int status2 = 121; |
| 331 | String alias = "test"; |
| 332 | byte[] nonce = getUtf8Bytes("nonce"); |
| 333 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
| 334 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId, status); |
| 335 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKey); |
| 336 | |
| 337 | WrappedKey retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 338 | assertThat(retrievedKey.getRecoveryStatus()).isEqualTo(status); |
| 339 | |
| 340 | mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias, status2); |
| 341 | mRecoverableKeyStoreDb.invalidateKeysWithOldGenerationId(userId, generationId + 1); |
| 342 | |
| 343 | retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 344 | assertThat(retrievedKey.getRecoveryStatus()) |
| 345 | .isEqualTo(RecoveryController.RECOVERY_STATUS_PERMANENT_FAILURE); |
| 346 | } |
| 347 | |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 348 | @Test |
Aseem Kumar | 3326da5 | 2018-03-12 18:05:16 -0700 | [diff] [blame] | 349 | public void testInvalidateKeysForUserIdOnCustomScreenLock() { |
| 350 | int userId = 12; |
| 351 | int uid = 1009; |
| 352 | int generationId = 6; |
| 353 | int status = 120; |
| 354 | int status2 = 121; |
| 355 | String alias = "test"; |
| 356 | byte[] nonce = getUtf8Bytes("nonce"); |
| 357 | byte[] keyMaterial = getUtf8Bytes("keymaterial"); |
| 358 | WrappedKey wrappedKey = new WrappedKey(nonce, keyMaterial, generationId, status); |
| 359 | mRecoverableKeyStoreDb.insertKey(userId, uid, alias, wrappedKey); |
| 360 | |
| 361 | WrappedKey retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 362 | assertThat(retrievedKey.getRecoveryStatus()).isEqualTo(status); |
| 363 | |
| 364 | mRecoverableKeyStoreDb.setRecoveryStatus(uid, alias, status2); |
| 365 | mRecoverableKeyStoreDb.invalidateKeysForUserIdOnCustomScreenLock(userId); |
| 366 | |
| 367 | retrievedKey = mRecoverableKeyStoreDb.getKey(uid, alias); |
| 368 | assertThat(retrievedKey.getRecoveryStatus()) |
| 369 | .isEqualTo(RecoveryController.RECOVERY_STATUS_PERMANENT_FAILURE); |
| 370 | } |
| 371 | |
| 372 | @Test |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 373 | public void setRecoveryServicePublicKey_replaceOldKey() throws Exception { |
| 374 | int userId = 12; |
| 375 | int uid = 10009; |
| 376 | PublicKey pubkey1 = genRandomPublicKey(); |
| 377 | PublicKey pubkey2 = genRandomPublicKey(); |
| 378 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey1); |
| 379 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey2); |
| 380 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
| 381 | pubkey2); |
| 382 | } |
| 383 | |
| 384 | @Test |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 385 | public void getRecoveryServicePublicKey_returnsNullIfNoKey() throws Exception { |
| 386 | int userId = 12; |
| 387 | int uid = 10009; |
| 388 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isNull(); |
| 389 | |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 390 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 391 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isNull(); |
| 392 | } |
| 393 | |
| 394 | @Test |
| 395 | public void getRecoveryServicePublicKey_returnsInsertedKey() throws Exception { |
| 396 | int userId = 12; |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 397 | int uid = 10009; |
| 398 | PublicKey pubkey = genRandomPublicKey(); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 399 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey); |
| 400 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 401 | pubkey); |
| 402 | } |
| 403 | |
Andreas Gampe | bd66641 | 2018-02-19 10:47:28 -0800 | [diff] [blame] | 404 | @Test |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 405 | public void setRecoveryServiceCertPath_replaceOldValue() throws Exception { |
| 406 | int userId = 12; |
| 407 | int uid = 10009; |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 408 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS, |
| 409 | TestData.CERT_PATH_1); |
| 410 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS, |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 411 | TestData.CERT_PATH_2); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 412 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 413 | TEST_ROOT_CERT_ALIAS)).isEqualTo(TestData.CERT_PATH_2); |
| 414 | } |
| 415 | |
| 416 | @Test |
| 417 | public void setRecoveryServiceCertPath_updateValuesForCorrectRootCert() throws Exception { |
| 418 | int userId = 12; |
| 419 | int uid = 10009; |
| 420 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS, |
| 421 | TestData.CERT_PATH_1); |
| 422 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS2, |
| 423 | TestData.CERT_PATH_1); |
| 424 | |
| 425 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 426 | TEST_ROOT_CERT_ALIAS)).isEqualTo(TestData.CERT_PATH_1); |
| 427 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 428 | TEST_ROOT_CERT_ALIAS2)).isEqualTo(TestData.CERT_PATH_1); |
| 429 | |
| 430 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS2, |
| 431 | TestData.CERT_PATH_2); |
| 432 | |
| 433 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 434 | TEST_ROOT_CERT_ALIAS)).isEqualTo(TestData.CERT_PATH_1); |
| 435 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 436 | TEST_ROOT_CERT_ALIAS2)).isEqualTo(TestData.CERT_PATH_2); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Test |
| 440 | public void getRecoveryServiceCertPath_returnsNullIfNoValue() throws Exception { |
| 441 | int userId = 12; |
| 442 | int uid = 10009; |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 443 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 444 | TEST_ROOT_CERT_ALIAS)).isNull(); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Test |
| 448 | public void getRecoveryServiceCertPath_returnsInsertedValue() throws Exception { |
| 449 | int userId = 12; |
| 450 | int uid = 10009; |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 451 | mRecoverableKeyStoreDb.setRecoveryServiceCertPath(userId, uid, TEST_ROOT_CERT_ALIAS, |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 452 | TestData.CERT_PATH_1); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 453 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertPath(userId, uid, |
| 454 | TEST_ROOT_CERT_ALIAS)).isEqualTo(TestData.CERT_PATH_1); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Test |
| 458 | public void setRecoveryServiceCertSerial_replaceOldValue() throws Exception { |
| 459 | int userId = 12; |
| 460 | int uid = 10009; |
| 461 | |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 462 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, TEST_ROOT_CERT_ALIAS, 1L); |
| 463 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, TEST_ROOT_CERT_ALIAS, 3L); |
| 464 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 465 | TEST_ROOT_CERT_ALIAS)).isEqualTo(3L); |
| 466 | } |
| 467 | |
| 468 | @Test |
| 469 | public void setRecoveryServiceCertSerial_updateValuesForCorrectRootCert() throws Exception { |
| 470 | int userId = 12; |
| 471 | int uid = 10009; |
| 472 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, TEST_ROOT_CERT_ALIAS, 1L); |
| 473 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, TEST_ROOT_CERT_ALIAS2, 1L); |
| 474 | |
| 475 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 476 | TEST_ROOT_CERT_ALIAS)).isEqualTo(1L); |
| 477 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 478 | TEST_ROOT_CERT_ALIAS2)).isEqualTo(1L); |
| 479 | |
| 480 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, TEST_ROOT_CERT_ALIAS2, 3L); |
| 481 | |
| 482 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 483 | TEST_ROOT_CERT_ALIAS)).isEqualTo(1L); |
| 484 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 485 | TEST_ROOT_CERT_ALIAS2)).isEqualTo(3L); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Test |
| 489 | public void getRecoveryServiceCertSerial_returnsNullIfNoValue() throws Exception { |
| 490 | int userId = 12; |
| 491 | int uid = 10009; |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 492 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 493 | TEST_ROOT_CERT_ALIAS)).isNull(); |
| 494 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 495 | TEST_ROOT_CERT_ALIAS2)).isNull(); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | @Test |
| 499 | public void getRecoveryServiceCertSerial_returnsInsertedValue() throws Exception { |
| 500 | int userId = 12; |
| 501 | int uid = 10009; |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 502 | mRecoverableKeyStoreDb.setRecoveryServiceCertSerial(userId, uid, |
| 503 | TEST_ROOT_CERT_ALIAS, 1234L); |
| 504 | assertThat(mRecoverableKeyStoreDb.getRecoveryServiceCertSerial(userId, uid, |
| 505 | TEST_ROOT_CERT_ALIAS)).isEqualTo(1234L); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 508 | @Test |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 509 | public void getRecoveryAgents_returnsUidIfSet() throws Exception { |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 510 | int userId = 12; |
| 511 | int uid = 190992; |
| 512 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, genRandomPublicKey()); |
| 513 | |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 514 | assertThat(mRecoverableKeyStoreDb.getRecoveryAgents(userId)).contains(uid); |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | @Test |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 518 | public void getRecoveryAgents_returnsEmptyListIfThereAreNoAgents() throws Exception { |
| 519 | int userId = 12; |
| 520 | assertThat(mRecoverableKeyStoreDb.getRecoveryAgents(userId)).isEmpty(); |
| 521 | assertThat(mRecoverableKeyStoreDb.getRecoveryAgents(userId)).isNotNull(); |
| 522 | } |
| 523 | |
| 524 | @Test |
| 525 | public void getRecoveryAgents_withTwoAgents() throws Exception { |
| 526 | int userId = 12; |
| 527 | int uid1 = 190992; |
| 528 | int uid2 = 190993; |
| 529 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid1, genRandomPublicKey()); |
| 530 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid2, genRandomPublicKey()); |
| 531 | List<Integer> agents = mRecoverableKeyStoreDb.getRecoveryAgents(userId); |
| 532 | |
| 533 | assertThat(agents).hasSize(2); |
| 534 | assertThat(agents).contains(uid1); |
| 535 | assertThat(agents).contains(uid2); |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Andreas Gampe | bd66641 | 2018-02-19 10:47:28 -0800 | [diff] [blame] | 538 | @Test |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 539 | public void setActiveRootOfTrust_emptyDefaultValue() throws Exception { |
| 540 | int userId = 12; |
| 541 | int uid = 10009; |
| 542 | assertThat(mRecoverableKeyStoreDb.getActiveRootOfTrust(userId, uid)).isEqualTo(null); |
| 543 | } |
| 544 | |
| 545 | @Test |
| 546 | public void setActiveRootOfTrust_updateValue() throws Exception { |
| 547 | int userId = 12; |
| 548 | int uid = 10009; |
| 549 | mRecoverableKeyStoreDb.setActiveRootOfTrust(userId, uid, "root"); |
| 550 | assertThat(mRecoverableKeyStoreDb.getActiveRootOfTrust(userId, uid)).isEqualTo("root"); |
| 551 | |
| 552 | mRecoverableKeyStoreDb.setActiveRootOfTrust(userId, uid, "root2"); |
| 553 | assertThat(mRecoverableKeyStoreDb.getActiveRootOfTrust(userId, uid)).isEqualTo("root2"); |
| 554 | } |
| 555 | |
| 556 | @Test |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 557 | public void setRecoverySecretTypes_emptyDefaultValue() throws Exception { |
| 558 | int userId = 12; |
| 559 | int uid = 10009; |
| 560 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 561 | new int[]{}); // default |
| 562 | } |
| 563 | |
| 564 | @Test |
| 565 | public void setRecoverySecretTypes_updateValue() throws Exception { |
| 566 | int userId = 12; |
| 567 | int uid = 10009; |
| 568 | int[] types1 = new int[]{1}; |
| 569 | int[] types2 = new int[]{2}; |
| 570 | |
| 571 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types1); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 572 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo(types1); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 573 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types2); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 574 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo(types2); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | @Test |
| 578 | public void setRecoverySecretTypes_withMultiElementArrays() throws Exception { |
| 579 | int userId = 12; |
| 580 | int uid = 10009; |
| 581 | int[] types1 = new int[]{11, 2000}; |
| 582 | int[] types2 = new int[]{1, 2, 3}; |
| 583 | int[] types3 = new int[]{}; |
| 584 | |
| 585 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types1); |
| 586 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 587 | types1); |
| 588 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types2); |
| 589 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 590 | types2); |
| 591 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types3); |
| 592 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 593 | types3); |
| 594 | } |
| 595 | |
| 596 | @Test |
| 597 | public void setRecoverySecretTypes_withDifferentUid() throws Exception { |
| 598 | int userId = 12; |
| 599 | int uid1 = 10011; |
| 600 | int uid2 = 10012; |
| 601 | int[] types1 = new int[]{1}; |
| 602 | int[] types2 = new int[]{2}; |
| 603 | |
| 604 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid1, types1); |
| 605 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid2, types2); |
| 606 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid1)).isEqualTo( |
| 607 | types1); |
| 608 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid2)).isEqualTo( |
| 609 | types2); |
| 610 | } |
| 611 | |
| 612 | @Test |
| 613 | public void setRecoveryServiceMetadataMethods() throws Exception { |
| 614 | int userId = 12; |
| 615 | int uid = 10009; |
| 616 | |
| 617 | PublicKey pubkey1 = genRandomPublicKey(); |
| 618 | int[] types1 = new int[]{1}; |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 619 | |
| 620 | PublicKey pubkey2 = genRandomPublicKey(); |
| 621 | int[] types2 = new int[]{2}; |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 622 | |
| 623 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey1); |
| 624 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types1); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 625 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 626 | |
| 627 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 628 | types1); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 629 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo( |
| 630 | SERVER_PARAMS); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 631 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
| 632 | pubkey1); |
| 633 | |
| 634 | // Check that the methods don't interfere with each other. |
| 635 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey2); |
| 636 | mRecoverableKeyStoreDb.setRecoverySecretTypes(userId, uid, types2); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 637 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS2); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 638 | |
| 639 | assertThat(mRecoverableKeyStoreDb.getRecoverySecretTypes(userId, uid)).isEqualTo( |
| 640 | types2); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 641 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo( |
| 642 | SERVER_PARAMS2); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 643 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
| 644 | pubkey2); |
| 645 | } |
| 646 | |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 647 | @Test |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 648 | public void setServerParams_replaceOldValue() throws Exception { |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 649 | int userId = 12; |
| 650 | int uid = 10009; |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 651 | |
| 652 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS); |
| 653 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS2); |
| 654 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo( |
| 655 | SERVER_PARAMS2); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | @Test |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 659 | public void getServerParams_returnsNullIfNoValue() throws Exception { |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 660 | int userId = 12; |
| 661 | int uid = 10009; |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 662 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isNull(); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 663 | |
| 664 | PublicKey pubkey = genRandomPublicKey(); |
| 665 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 666 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isNull(); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | @Test |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 670 | public void getServerParams_returnsInsertedValue() throws Exception { |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 671 | int userId = 12; |
| 672 | int uid = 10009; |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 673 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS); |
| 674 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo(SERVER_PARAMS); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | @Test |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 678 | public void setCounterId_defaultValueAndTwoUpdates() throws Exception { |
| 679 | int userId = 12; |
| 680 | int uid = 10009; |
| 681 | long value1 = 111L; |
| 682 | long value2 = 222L; |
| 683 | assertThat(mRecoverableKeyStoreDb.getCounterId(userId, uid)).isNull(); |
| 684 | |
| 685 | mRecoverableKeyStoreDb.setCounterId(userId, uid, value1); |
| 686 | assertThat(mRecoverableKeyStoreDb.getCounterId(userId, uid)).isEqualTo( |
| 687 | value1); |
| 688 | |
| 689 | mRecoverableKeyStoreDb.setCounterId(userId, uid, value2); |
| 690 | assertThat(mRecoverableKeyStoreDb.getCounterId(userId, uid)).isEqualTo( |
| 691 | value2); |
| 692 | } |
| 693 | |
| 694 | @Test |
| 695 | public void setSnapshotVersion_defaultValueAndTwoUpdates() throws Exception { |
| 696 | int userId = 12; |
| 697 | int uid = 10009; |
| 698 | long value1 = 111L; |
| 699 | long value2 = 222L; |
| 700 | assertThat(mRecoverableKeyStoreDb.getSnapshotVersion(userId, uid)).isNull(); |
| 701 | mRecoverableKeyStoreDb.setSnapshotVersion(userId, uid, value1); |
| 702 | assertThat(mRecoverableKeyStoreDb.getSnapshotVersion(userId, uid)).isEqualTo( |
| 703 | value1); |
| 704 | mRecoverableKeyStoreDb.setSnapshotVersion(userId, uid, value2); |
| 705 | assertThat(mRecoverableKeyStoreDb.getSnapshotVersion(userId, uid)).isEqualTo( |
| 706 | value2); |
| 707 | } |
| 708 | |
| 709 | @Test |
| 710 | public void setShouldCreateSnapshot_defaultValueAndTwoUpdates() throws Exception { |
| 711 | int userId = 12; |
| 712 | int uid = 10009; |
| 713 | boolean value1 = true; |
| 714 | boolean value2 = false; |
| 715 | assertThat(mRecoverableKeyStoreDb.getShouldCreateSnapshot(userId, uid)).isEqualTo(false); |
| 716 | mRecoverableKeyStoreDb.setShouldCreateSnapshot(userId, uid, value1); |
| 717 | assertThat(mRecoverableKeyStoreDb.getShouldCreateSnapshot(userId, uid)).isEqualTo(value1); |
| 718 | mRecoverableKeyStoreDb.setShouldCreateSnapshot(userId, uid, value2); |
| 719 | assertThat(mRecoverableKeyStoreDb.getShouldCreateSnapshot(userId, uid)).isEqualTo( |
| 720 | value2); |
| 721 | } |
| 722 | |
| 723 | @Test |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 724 | public void setRecoveryServiceMetadataEntry_allowsAUserToHaveTwoUids() throws Exception { |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 725 | int userId = 12; |
| 726 | int uid1 = 10009; |
| 727 | int uid2 = 20009; |
| 728 | PublicKey pubkey = genRandomPublicKey(); |
| 729 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid1, pubkey); |
| 730 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid2, pubkey); |
| 731 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid1)).isEqualTo( |
| 732 | pubkey); |
| 733 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid2)).isEqualTo( |
| 734 | pubkey); |
| 735 | } |
| 736 | |
| 737 | @Test |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 738 | public void setRecoveryServiceMetadataEntry_allowsTwoUsersToHaveTheSameUid() throws Exception { |
| 739 | int userId1 = 12; |
| 740 | int userId2 = 23; |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 741 | int uid = 10009; |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 742 | PublicKey pubkey = genRandomPublicKey(); |
| 743 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId1, uid, pubkey); |
| 744 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId2, uid, pubkey); |
| 745 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId1, uid)).isEqualTo( |
| 746 | pubkey); |
| 747 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId2, uid)).isEqualTo( |
| 748 | pubkey); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | @Test |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 752 | public void setRecoveryServiceMetadataEntry_updatesColumnsSeparately() throws Exception { |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 753 | int userId = 12; |
| 754 | int uid = 10009; |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 755 | PublicKey pubkey1 = genRandomPublicKey(); |
| 756 | PublicKey pubkey2 = genRandomPublicKey(); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 757 | |
| 758 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey1); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 759 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 760 | pubkey1); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 761 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isNull(); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 762 | |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 763 | mRecoverableKeyStoreDb.setServerParams(userId, uid, SERVER_PARAMS); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 764 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
| 765 | pubkey1); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 766 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo(SERVER_PARAMS); |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 767 | |
| 768 | mRecoverableKeyStoreDb.setRecoveryServicePublicKey(userId, uid, pubkey2); |
| 769 | assertThat(mRecoverableKeyStoreDb.getRecoveryServicePublicKey(userId, uid)).isEqualTo( |
| 770 | pubkey2); |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 771 | assertThat(mRecoverableKeyStoreDb.getServerParams(userId, uid)).isEqualTo(SERVER_PARAMS); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 774 | private static byte[] getUtf8Bytes(String s) { |
| 775 | return s.getBytes(StandardCharsets.UTF_8); |
| 776 | } |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 777 | |
| 778 | private static PublicKey genRandomPublicKey() throws Exception { |
| 779 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC"); |
| 780 | keyPairGenerator.initialize(new ECGenParameterSpec("secp256r1")); |
| 781 | return keyPairGenerator.generateKeyPair().getPublic(); |
| 782 | } |
Robert Berry | 76cf083 | 2017-12-15 23:01:22 +0000 | [diff] [blame] | 783 | } |