OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/crypto/gcm_key_store.h" | 5 #include "components/gcm_driver/crypto/gcm_key_store.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
12 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
13 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
14 #include "components/gcm_driver/crypto/p256_key_util.h" | 17 #include "components/gcm_driver/crypto/p256_key_util.h" |
| 18 #include "net/test/gtest_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
16 | 20 |
17 namespace gcm { | 21 namespace gcm { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 const char kFakeAppId[] = "my_app_id"; | 25 const char kFakeAppId[] = "my_app_id"; |
22 const char kSecondFakeAppId[] = "my_other_app_id"; | 26 const char kSecondFakeAppId[] = "my_other_app_id"; |
| 27 const char kFakeAuthorizedEntity[] = "my_sender_id"; |
| 28 const char kSecondFakeAuthorizedEntity[] = "my_other_sender_id"; |
23 | 29 |
24 class GCMKeyStoreTest : public ::testing::Test { | 30 class GCMKeyStoreTest : public ::testing::Test { |
25 public: | 31 public: |
26 GCMKeyStoreTest() {} | 32 GCMKeyStoreTest() {} |
27 ~GCMKeyStoreTest() override {} | 33 ~GCMKeyStoreTest() override {} |
28 | 34 |
29 void SetUp() override { | 35 void SetUp() override { |
30 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 36 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
31 CreateKeyStore(); | 37 CreateKeyStore(); |
32 } | 38 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 }; | 72 }; |
67 | 73 |
68 TEST_F(GCMKeyStoreTest, EmptyByDefault) { | 74 TEST_F(GCMKeyStoreTest, EmptyByDefault) { |
69 // The key store is initialized lazily, so this histogram confirms that | 75 // The key store is initialized lazily, so this histogram confirms that |
70 // calling the constructor does not in fact cause initialization. | 76 // calling the constructor does not in fact cause initialization. |
71 histogram_tester()->ExpectTotalCount( | 77 histogram_tester()->ExpectTotalCount( |
72 "GCM.Crypto.InitKeyStoreSuccessRate", 0); | 78 "GCM.Crypto.InitKeyStoreSuccessRate", 0); |
73 | 79 |
74 KeyPair pair; | 80 KeyPair pair; |
75 std::string auth_secret; | 81 std::string auth_secret; |
76 gcm_key_store()->GetKeys(kFakeAppId, | 82 gcm_key_store()->GetKeys( |
77 base::Bind(&GCMKeyStoreTest::GotKeys, | 83 kFakeAppId, kFakeAuthorizedEntity, |
78 base::Unretained(this), &pair, | 84 false /* fallback_to_empty_authorized_entity */, |
79 &auth_secret)); | 85 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
| 86 &auth_secret)); |
80 | 87 |
81 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
82 | 89 |
83 ASSERT_FALSE(pair.IsInitialized()); | 90 ASSERT_FALSE(pair.IsInitialized()); |
84 EXPECT_FALSE(pair.has_type()); | 91 EXPECT_FALSE(pair.has_type()); |
85 EXPECT_EQ(0u, auth_secret.size()); | 92 EXPECT_EQ(0u, auth_secret.size()); |
86 | 93 |
87 histogram_tester()->ExpectBucketCount( | 94 histogram_tester()->ExpectBucketCount( |
88 "GCM.Crypto.GetKeySuccessRate", 0, 1); // failure | 95 "GCM.Crypto.GetKeySuccessRate", 0, 1); // failure |
89 } | 96 } |
90 | 97 |
91 TEST_F(GCMKeyStoreTest, CreateAndGetKeys) { | 98 TEST_F(GCMKeyStoreTest, CreateAndGetKeys) { |
92 KeyPair pair; | 99 KeyPair pair; |
93 std::string auth_secret; | 100 std::string auth_secret; |
94 gcm_key_store()->CreateKeys(kFakeAppId, | 101 gcm_key_store()->CreateKeys( |
95 base::Bind(&GCMKeyStoreTest::GotKeys, | 102 kFakeAppId, kFakeAuthorizedEntity, |
96 base::Unretained(this), &pair, | 103 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
97 &auth_secret)); | 104 &auth_secret)); |
98 | 105 |
99 base::RunLoop().RunUntilIdle(); | 106 base::RunLoop().RunUntilIdle(); |
100 | 107 |
101 ASSERT_TRUE(pair.IsInitialized()); | 108 ASSERT_TRUE(pair.IsInitialized()); |
102 ASSERT_TRUE(pair.has_private_key()); | 109 ASSERT_TRUE(pair.has_private_key()); |
103 ASSERT_TRUE(pair.has_public_key()); | 110 ASSERT_TRUE(pair.has_public_key()); |
104 | 111 |
105 EXPECT_GT(pair.public_key().size(), 0u); | 112 EXPECT_GT(pair.public_key().size(), 0u); |
106 EXPECT_GT(pair.private_key().size(), 0u); | 113 EXPECT_GT(pair.private_key().size(), 0u); |
107 | 114 |
108 ASSERT_GT(auth_secret.size(), 0u); | 115 ASSERT_GT(auth_secret.size(), 0u); |
109 | 116 |
110 histogram_tester()->ExpectBucketCount( | 117 histogram_tester()->ExpectBucketCount( |
111 "GCM.Crypto.CreateKeySuccessRate", 1, 1); // success | 118 "GCM.Crypto.CreateKeySuccessRate", 1, 1); // success |
112 | 119 |
113 KeyPair read_pair; | 120 KeyPair read_pair; |
114 std::string read_auth_secret; | 121 std::string read_auth_secret; |
115 gcm_key_store()->GetKeys(kFakeAppId, | 122 gcm_key_store()->GetKeys( |
116 base::Bind(&GCMKeyStoreTest::GotKeys, | 123 kFakeAppId, kFakeAuthorizedEntity, |
117 base::Unretained(this), &read_pair, | 124 false /* fallback_to_empty_authorized_entity */, |
118 &read_auth_secret)); | 125 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 126 &read_auth_secret)); |
119 | 127 |
120 base::RunLoop().RunUntilIdle(); | 128 base::RunLoop().RunUntilIdle(); |
121 | 129 |
122 ASSERT_TRUE(read_pair.IsInitialized()); | 130 ASSERT_TRUE(read_pair.IsInitialized()); |
123 | 131 |
124 EXPECT_EQ(pair.type(), read_pair.type()); | 132 EXPECT_EQ(pair.type(), read_pair.type()); |
125 EXPECT_EQ(pair.private_key(), read_pair.private_key()); | 133 EXPECT_EQ(pair.private_key(), read_pair.private_key()); |
126 EXPECT_EQ(pair.public_key(), read_pair.public_key()); | 134 EXPECT_EQ(pair.public_key(), read_pair.public_key()); |
127 | 135 |
128 EXPECT_EQ(auth_secret, read_auth_secret); | 136 EXPECT_EQ(auth_secret, read_auth_secret); |
129 | 137 |
130 histogram_tester()->ExpectBucketCount( | 138 histogram_tester()->ExpectBucketCount("GCM.Crypto.GetKeySuccessRate", 1, |
131 "GCM.Crypto.GetKeySuccessRate", 1, 1); // failure | 139 1); // success |
| 140 |
| 141 // GetKey should also succeed if fallback_to_empty_authorized_entity is true |
| 142 // (fallback should not occur, since an exact match is found). |
| 143 gcm_key_store()->GetKeys( |
| 144 kFakeAppId, kFakeAuthorizedEntity, |
| 145 true /* fallback_to_empty_authorized_entity */, |
| 146 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 147 &read_auth_secret)); |
| 148 |
| 149 base::RunLoop().RunUntilIdle(); |
| 150 |
| 151 ASSERT_TRUE(read_pair.IsInitialized()); |
| 152 |
| 153 EXPECT_EQ(pair.type(), read_pair.type()); |
| 154 EXPECT_EQ(pair.private_key(), read_pair.private_key()); |
| 155 EXPECT_EQ(pair.public_key(), read_pair.public_key()); |
| 156 |
| 157 EXPECT_EQ(auth_secret, read_auth_secret); |
| 158 |
| 159 histogram_tester()->ExpectBucketCount("GCM.Crypto.GetKeySuccessRate", 1, |
| 160 2); // another success |
| 161 } |
| 162 |
| 163 TEST_F(GCMKeyStoreTest, GetKeysFallback) { |
| 164 KeyPair pair; |
| 165 std::string auth_secret; |
| 166 gcm_key_store()->CreateKeys( |
| 167 kFakeAppId, "" /* empty authorized entity for non-InstanceID */, |
| 168 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
| 169 &auth_secret)); |
| 170 |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 |
| 173 ASSERT_TRUE(pair.IsInitialized()); |
| 174 ASSERT_TRUE(pair.has_private_key()); |
| 175 ASSERT_TRUE(pair.has_public_key()); |
| 176 |
| 177 EXPECT_GT(pair.public_key().size(), 0u); |
| 178 EXPECT_GT(pair.private_key().size(), 0u); |
| 179 |
| 180 ASSERT_GT(auth_secret.size(), 0u); |
| 181 |
| 182 histogram_tester()->ExpectBucketCount("GCM.Crypto.CreateKeySuccessRate", 1, |
| 183 1); // success |
| 184 |
| 185 // GetKeys should fail when fallback_to_empty_authorized_entity is false, as |
| 186 // there is not an exact match for kFakeAuthorizedEntity. |
| 187 KeyPair read_pair; |
| 188 std::string read_auth_secret; |
| 189 gcm_key_store()->GetKeys( |
| 190 kFakeAppId, kFakeAuthorizedEntity, |
| 191 false /* fallback_to_empty_authorized_entity */, |
| 192 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 193 &read_auth_secret)); |
| 194 |
| 195 base::RunLoop().RunUntilIdle(); |
| 196 |
| 197 ASSERT_FALSE(read_pair.IsInitialized()); |
| 198 EXPECT_FALSE(read_pair.has_type()); |
| 199 EXPECT_EQ(0u, read_auth_secret.size()); |
| 200 |
| 201 histogram_tester()->ExpectBucketCount("GCM.Crypto.GetKeySuccessRate", 0, |
| 202 1); // failure |
| 203 |
| 204 // GetKey should succeed when fallback_to_empty_authorized_entity is true, as |
| 205 // falling back to empty authorized entity will match the created key. |
| 206 gcm_key_store()->GetKeys( |
| 207 kFakeAppId, kFakeAuthorizedEntity, |
| 208 true /* fallback_to_empty_authorized_entity */, |
| 209 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 210 &read_auth_secret)); |
| 211 |
| 212 base::RunLoop().RunUntilIdle(); |
| 213 |
| 214 ASSERT_TRUE(read_pair.IsInitialized()); |
| 215 |
| 216 EXPECT_EQ(pair.type(), read_pair.type()); |
| 217 EXPECT_EQ(pair.private_key(), read_pair.private_key()); |
| 218 EXPECT_EQ(pair.public_key(), read_pair.public_key()); |
| 219 |
| 220 EXPECT_EQ(auth_secret, read_auth_secret); |
| 221 |
| 222 histogram_tester()->ExpectBucketCount("GCM.Crypto.GetKeySuccessRate", 1, |
| 223 1); // success |
132 } | 224 } |
133 | 225 |
134 TEST_F(GCMKeyStoreTest, KeysPersistenceBetweenInstances) { | 226 TEST_F(GCMKeyStoreTest, KeysPersistenceBetweenInstances) { |
135 KeyPair pair; | 227 KeyPair pair; |
136 std::string auth_secret; | 228 std::string auth_secret; |
137 gcm_key_store()->CreateKeys(kFakeAppId, | 229 gcm_key_store()->CreateKeys( |
138 base::Bind(&GCMKeyStoreTest::GotKeys, | 230 kFakeAppId, kFakeAuthorizedEntity, |
139 base::Unretained(this), &pair, | 231 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
140 &auth_secret)); | 232 &auth_secret)); |
141 | 233 |
142 base::RunLoop().RunUntilIdle(); | 234 base::RunLoop().RunUntilIdle(); |
143 | 235 |
144 ASSERT_TRUE(pair.IsInitialized()); | 236 ASSERT_TRUE(pair.IsInitialized()); |
145 | 237 |
146 histogram_tester()->ExpectBucketCount( | 238 histogram_tester()->ExpectBucketCount( |
147 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 1); // success | 239 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 1); // success |
148 histogram_tester()->ExpectBucketCount( | 240 histogram_tester()->ExpectBucketCount( |
149 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 1); // success | 241 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 1); // success |
150 | 242 |
151 // Create a new GCM Key Store instance. | 243 // Create a new GCM Key Store instance. |
152 CreateKeyStore(); | 244 CreateKeyStore(); |
153 | 245 |
154 KeyPair read_pair; | 246 KeyPair read_pair; |
155 std::string read_auth_secret; | 247 std::string read_auth_secret; |
156 gcm_key_store()->GetKeys(kFakeAppId, | 248 gcm_key_store()->GetKeys( |
157 base::Bind(&GCMKeyStoreTest::GotKeys, | 249 kFakeAppId, kFakeAuthorizedEntity, |
158 base::Unretained(this), &read_pair, | 250 false /* fallback_to_empty_authorized_entity */, |
159 &read_auth_secret)); | 251 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 252 &read_auth_secret)); |
160 | 253 |
161 base::RunLoop().RunUntilIdle(); | 254 base::RunLoop().RunUntilIdle(); |
162 | 255 |
163 ASSERT_TRUE(read_pair.IsInitialized()); | 256 ASSERT_TRUE(read_pair.IsInitialized()); |
164 EXPECT_TRUE(read_pair.has_type()); | 257 EXPECT_TRUE(read_pair.has_type()); |
165 EXPECT_GT(read_auth_secret.size(), 0u); | 258 EXPECT_GT(read_auth_secret.size(), 0u); |
166 | 259 |
167 histogram_tester()->ExpectBucketCount( | 260 histogram_tester()->ExpectBucketCount( |
168 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 2); // success | 261 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 2); // success |
169 histogram_tester()->ExpectBucketCount( | 262 histogram_tester()->ExpectBucketCount( |
170 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 2); // success | 263 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 2); // success |
171 } | 264 } |
172 | 265 |
173 TEST_F(GCMKeyStoreTest, CreateAndRemoveKeys) { | 266 TEST_F(GCMKeyStoreTest, CreateAndRemoveKeys) { |
174 KeyPair pair; | 267 KeyPair pair; |
175 std::string auth_secret; | 268 std::string auth_secret; |
176 gcm_key_store()->CreateKeys(kFakeAppId, | 269 gcm_key_store()->CreateKeys( |
177 base::Bind(&GCMKeyStoreTest::GotKeys, | 270 kFakeAppId, kFakeAuthorizedEntity, |
178 base::Unretained(this), &pair, | 271 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
179 &auth_secret)); | 272 &auth_secret)); |
180 | 273 |
181 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
182 | 275 |
183 ASSERT_TRUE(pair.IsInitialized()); | 276 ASSERT_TRUE(pair.IsInitialized()); |
184 | 277 |
185 KeyPair read_pair; | 278 KeyPair read_pair; |
186 std::string read_auth_secret; | 279 std::string read_auth_secret; |
187 gcm_key_store()->GetKeys(kFakeAppId, | 280 gcm_key_store()->GetKeys( |
188 base::Bind(&GCMKeyStoreTest::GotKeys, | 281 kFakeAppId, kFakeAuthorizedEntity, |
189 base::Unretained(this), &read_pair, | 282 false /* fallback_to_empty_authorized_entity */, |
190 &read_auth_secret)); | 283 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 284 &read_auth_secret)); |
191 | 285 |
192 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
193 | 287 |
194 ASSERT_TRUE(read_pair.IsInitialized()); | 288 ASSERT_TRUE(read_pair.IsInitialized()); |
195 EXPECT_TRUE(read_pair.has_type()); | 289 EXPECT_TRUE(read_pair.has_type()); |
196 | 290 |
197 gcm_key_store()->RemoveKeys(kFakeAppId, base::Bind(&base::DoNothing)); | 291 gcm_key_store()->RemoveKeys(kFakeAppId, kFakeAuthorizedEntity, |
| 292 base::Bind(&base::DoNothing)); |
198 | 293 |
199 base::RunLoop().RunUntilIdle(); | 294 base::RunLoop().RunUntilIdle(); |
200 | 295 |
201 histogram_tester()->ExpectBucketCount( | 296 histogram_tester()->ExpectBucketCount( |
202 "GCM.Crypto.RemoveKeySuccessRate", 1, 1); // success | 297 "GCM.Crypto.RemoveKeySuccessRate", 1, 1); // success |
203 | 298 |
204 gcm_key_store()->GetKeys(kFakeAppId, | 299 gcm_key_store()->GetKeys( |
205 base::Bind(&GCMKeyStoreTest::GotKeys, | 300 kFakeAppId, kFakeAuthorizedEntity, |
206 base::Unretained(this), &read_pair, | 301 false /* fallback_to_empty_authorized_entity */, |
207 &read_auth_secret)); | 302 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 303 &read_auth_secret)); |
208 | 304 |
209 base::RunLoop().RunUntilIdle(); | 305 base::RunLoop().RunUntilIdle(); |
210 | 306 |
211 ASSERT_FALSE(read_pair.IsInitialized()); | 307 ASSERT_FALSE(read_pair.IsInitialized()); |
212 } | 308 } |
213 | 309 |
214 TEST_F(GCMKeyStoreTest, CreateAndRemoveKeysSynchronously) { | 310 TEST_F(GCMKeyStoreTest, CreateGetAndRemoveKeysSynchronously) { |
215 KeyPair pair; | 311 KeyPair pair; |
216 std::string auth_secret; | 312 std::string auth_secret; |
217 gcm_key_store()->CreateKeys(kFakeAppId, | 313 gcm_key_store()->CreateKeys( |
218 base::Bind(&GCMKeyStoreTest::GotKeys, | 314 kFakeAppId, kFakeAuthorizedEntity, |
219 base::Unretained(this), &pair, | 315 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
220 &auth_secret)); | 316 &auth_secret)); |
221 | 317 |
222 // Continue synchronously, without running RunUntilIdle first. | 318 // Continue synchronously, without running RunUntilIdle first. |
223 gcm_key_store()->RemoveKeys(kFakeAppId, base::Bind(&base::DoNothing)); | 319 KeyPair pair_after_create; |
| 320 std::string auth_secret_after_create; |
| 321 gcm_key_store()->GetKeys( |
| 322 kFakeAppId, kFakeAuthorizedEntity, |
| 323 false /* fallback_to_empty_authorized_entity */, |
| 324 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 325 &pair_after_create, &auth_secret_after_create)); |
| 326 |
| 327 // Continue synchronously, without running RunUntilIdle first. |
| 328 gcm_key_store()->RemoveKeys(kFakeAppId, kFakeAuthorizedEntity, |
| 329 base::Bind(&base::DoNothing)); |
| 330 |
| 331 // Continue synchronously, without running RunUntilIdle first. |
| 332 KeyPair pair_after_remove; |
| 333 std::string auth_secret_after_remove; |
| 334 gcm_key_store()->GetKeys( |
| 335 kFakeAppId, kFakeAuthorizedEntity, |
| 336 false /* fallback_to_empty_authorized_entity */, |
| 337 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 338 &pair_after_remove, &auth_secret_after_remove)); |
| 339 |
| 340 base::RunLoop().RunUntilIdle(); |
| 341 |
| 342 histogram_tester()->ExpectBucketCount("GCM.Crypto.RemoveKeySuccessRate", 1, |
| 343 1); // success |
| 344 |
| 345 KeyPair pair_after_idle; |
| 346 std::string auth_secret_after_idle; |
| 347 gcm_key_store()->GetKeys( |
| 348 kFakeAppId, kFakeAuthorizedEntity, |
| 349 false /* fallback_to_empty_authorized_entity */, |
| 350 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 351 &pair_after_idle, &auth_secret_after_idle)); |
224 | 352 |
225 base::RunLoop().RunUntilIdle(); | 353 base::RunLoop().RunUntilIdle(); |
226 | 354 |
227 ASSERT_TRUE(pair.IsInitialized()); | 355 ASSERT_TRUE(pair.IsInitialized()); |
| 356 ASSERT_TRUE(pair_after_create.IsInitialized()); |
| 357 EXPECT_FALSE(pair_after_remove.IsInitialized()); |
| 358 EXPECT_FALSE(pair_after_idle.IsInitialized()); |
| 359 |
228 EXPECT_TRUE(pair.has_type()); | 360 EXPECT_TRUE(pair.has_type()); |
| 361 EXPECT_EQ(pair.type(), pair_after_create.type()); |
| 362 EXPECT_EQ(pair.private_key(), pair_after_create.private_key()); |
| 363 EXPECT_EQ(pair.public_key(), pair_after_create.public_key()); |
229 | 364 |
230 histogram_tester()->ExpectBucketCount( | 365 EXPECT_GT(auth_secret.size(), 0u); |
231 "GCM.Crypto.RemoveKeySuccessRate", 1, 1); // success | 366 EXPECT_EQ(auth_secret, auth_secret_after_create); |
| 367 EXPECT_EQ("", auth_secret_after_remove); |
| 368 EXPECT_EQ("", auth_secret_after_idle); |
| 369 } |
232 | 370 |
233 KeyPair read_pair; | 371 TEST_F(GCMKeyStoreTest, RemoveKeysWildcardAuthorizedEntity) { |
234 std::string read_auth_secret; | 372 KeyPair pair1, pair2, pair3; |
235 gcm_key_store()->GetKeys(kFakeAppId, | 373 std::string auth_secret1, auth_secret2, auth_secret3; |
236 base::Bind(&GCMKeyStoreTest::GotKeys, | 374 gcm_key_store()->CreateKeys( |
237 base::Unretained(this), &read_pair, | 375 kFakeAppId, kFakeAuthorizedEntity, |
238 &read_auth_secret)); | 376 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair1, |
| 377 &auth_secret1)); |
| 378 gcm_key_store()->CreateKeys( |
| 379 kFakeAppId, kSecondFakeAuthorizedEntity, |
| 380 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair2, |
| 381 &auth_secret2)); |
| 382 gcm_key_store()->CreateKeys( |
| 383 kSecondFakeAppId, kFakeAuthorizedEntity, |
| 384 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair3, |
| 385 &auth_secret3)); |
239 | 386 |
240 base::RunLoop().RunUntilIdle(); | 387 base::RunLoop().RunUntilIdle(); |
241 | 388 |
242 ASSERT_FALSE(read_pair.IsInitialized()); | 389 ASSERT_TRUE(pair1.IsInitialized()); |
| 390 ASSERT_TRUE(pair2.IsInitialized()); |
| 391 ASSERT_TRUE(pair3.IsInitialized()); |
| 392 |
| 393 KeyPair read_pair1, read_pair2, read_pair3; |
| 394 std::string read_auth_secret1, read_auth_secret2, read_auth_secret3; |
| 395 gcm_key_store()->GetKeys( |
| 396 kFakeAppId, kFakeAuthorizedEntity, |
| 397 false /* fallback_to_empty_authorized_entity */, |
| 398 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair1, |
| 399 &read_auth_secret1)); |
| 400 gcm_key_store()->GetKeys( |
| 401 kFakeAppId, kSecondFakeAuthorizedEntity, |
| 402 false /* fallback_to_empty_authorized_entity */, |
| 403 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair2, |
| 404 &read_auth_secret2)); |
| 405 gcm_key_store()->GetKeys( |
| 406 kSecondFakeAppId, kFakeAuthorizedEntity, |
| 407 false /* fallback_to_empty_authorized_entity */, |
| 408 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair3, |
| 409 &read_auth_secret3)); |
| 410 |
| 411 base::RunLoop().RunUntilIdle(); |
| 412 |
| 413 ASSERT_TRUE(read_pair1.IsInitialized()); |
| 414 EXPECT_TRUE(read_pair1.has_type()); |
| 415 ASSERT_TRUE(read_pair2.IsInitialized()); |
| 416 EXPECT_TRUE(read_pair2.has_type()); |
| 417 ASSERT_TRUE(read_pair3.IsInitialized()); |
| 418 EXPECT_TRUE(read_pair3.has_type()); |
| 419 |
| 420 gcm_key_store()->RemoveKeys(kFakeAppId, "*" /* authorized_entity */, |
| 421 base::Bind(&base::DoNothing)); |
| 422 |
| 423 base::RunLoop().RunUntilIdle(); |
| 424 |
| 425 histogram_tester()->ExpectBucketCount("GCM.Crypto.RemoveKeySuccessRate", 1, |
| 426 1); // success |
| 427 |
| 428 gcm_key_store()->GetKeys( |
| 429 kFakeAppId, kFakeAuthorizedEntity, |
| 430 false /* fallback_to_empty_authorized_entity */, |
| 431 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair1, |
| 432 &read_auth_secret1)); |
| 433 gcm_key_store()->GetKeys( |
| 434 kFakeAppId, kSecondFakeAuthorizedEntity, |
| 435 false /* fallback_to_empty_authorized_entity */, |
| 436 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair2, |
| 437 &read_auth_secret2)); |
| 438 gcm_key_store()->GetKeys( |
| 439 kSecondFakeAppId, kFakeAuthorizedEntity, |
| 440 false /* fallback_to_empty_authorized_entity */, |
| 441 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair3, |
| 442 &read_auth_secret3)); |
| 443 |
| 444 base::RunLoop().RunUntilIdle(); |
| 445 |
| 446 EXPECT_FALSE(read_pair1.IsInitialized()); |
| 447 EXPECT_FALSE(read_pair2.IsInitialized()); |
| 448 ASSERT_TRUE(read_pair3.IsInitialized()); |
| 449 EXPECT_TRUE(read_pair3.has_type()); |
243 } | 450 } |
244 | 451 |
245 TEST_F(GCMKeyStoreTest, GetKeysMultipleAppIds) { | 452 TEST_F(GCMKeyStoreTest, GetKeysMultipleAppIds) { |
246 KeyPair pair; | 453 KeyPair pair; |
247 std::string auth_secret; | 454 std::string auth_secret; |
248 gcm_key_store()->CreateKeys(kFakeAppId, | 455 gcm_key_store()->CreateKeys( |
249 base::Bind(&GCMKeyStoreTest::GotKeys, | 456 kFakeAppId, kFakeAuthorizedEntity, |
250 base::Unretained(this), &pair, | 457 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
251 &auth_secret)); | 458 &auth_secret)); |
252 | 459 |
253 base::RunLoop().RunUntilIdle(); | 460 base::RunLoop().RunUntilIdle(); |
254 | 461 |
255 ASSERT_TRUE(pair.IsInitialized()); | 462 ASSERT_TRUE(pair.IsInitialized()); |
256 | 463 |
257 gcm_key_store()->CreateKeys(kSecondFakeAppId, | 464 gcm_key_store()->CreateKeys( |
258 base::Bind(&GCMKeyStoreTest::GotKeys, | 465 kSecondFakeAppId, kSecondFakeAuthorizedEntity, |
259 base::Unretained(this), &pair, | 466 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
260 &auth_secret)); | 467 &auth_secret)); |
261 | 468 |
262 base::RunLoop().RunUntilIdle(); | 469 base::RunLoop().RunUntilIdle(); |
263 | 470 |
264 ASSERT_TRUE(pair.IsInitialized()); | 471 ASSERT_TRUE(pair.IsInitialized()); |
265 | 472 |
266 KeyPair read_pair; | 473 KeyPair read_pair; |
267 std::string read_auth_secret; | 474 std::string read_auth_secret; |
268 gcm_key_store()->GetKeys(kFakeAppId, | 475 gcm_key_store()->GetKeys( |
269 base::Bind(&GCMKeyStoreTest::GotKeys, | 476 kFakeAppId, kFakeAuthorizedEntity, |
270 base::Unretained(this), &read_pair, | 477 false /* fallback_to_empty_authorized_entity */, |
271 &read_auth_secret)); | 478 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 479 &read_auth_secret)); |
272 | 480 |
273 base::RunLoop().RunUntilIdle(); | 481 base::RunLoop().RunUntilIdle(); |
274 | 482 |
275 ASSERT_TRUE(read_pair.IsInitialized()); | 483 ASSERT_TRUE(read_pair.IsInitialized()); |
276 EXPECT_TRUE(read_pair.has_type()); | 484 EXPECT_TRUE(read_pair.has_type()); |
277 } | 485 } |
278 | 486 |
279 TEST_F(GCMKeyStoreTest, SuccessiveCallsBeforeInitialization) { | 487 TEST_F(GCMKeyStoreTest, SuccessiveCallsBeforeInitialization) { |
280 KeyPair pair; | 488 KeyPair pair; |
281 std::string auth_secret; | 489 std::string auth_secret; |
282 gcm_key_store()->CreateKeys(kFakeAppId, | 490 gcm_key_store()->CreateKeys( |
283 base::Bind(&GCMKeyStoreTest::GotKeys, | 491 kFakeAppId, kFakeAuthorizedEntity, |
284 base::Unretained(this), &pair, | 492 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &pair, |
285 &auth_secret)); | 493 &auth_secret)); |
286 | 494 |
287 // Deliberately do not run the message loop, so that the callback has not | 495 // Deliberately do not run the message loop, so that the callback has not |
288 // been resolved yet. The following EXPECT() ensures this. | 496 // been resolved yet. The following EXPECT() ensures this. |
289 EXPECT_FALSE(pair.IsInitialized()); | 497 EXPECT_FALSE(pair.IsInitialized()); |
290 | 498 |
291 KeyPair read_pair; | 499 KeyPair read_pair; |
292 std::string read_auth_secret; | 500 std::string read_auth_secret; |
293 gcm_key_store()->GetKeys(kFakeAppId, | 501 gcm_key_store()->GetKeys( |
294 base::Bind(&GCMKeyStoreTest::GotKeys, | 502 kFakeAppId, kFakeAuthorizedEntity, |
295 base::Unretained(this), &read_pair, | 503 false /* fallback_to_empty_authorized_entity */, |
296 &read_auth_secret)); | 504 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), &read_pair, |
| 505 &read_auth_secret)); |
297 | 506 |
298 EXPECT_FALSE(read_pair.IsInitialized()); | 507 EXPECT_FALSE(read_pair.IsInitialized()); |
299 | 508 |
300 // Now run the message loop. Both tasks should have finished executing. Due | 509 // Now run the message loop. Both tasks should have finished executing. Due |
301 // to the asynchronous nature of operations, however, we can't rely on the | 510 // to the asynchronous nature of operations, however, we can't rely on the |
302 // write to have finished before the read begins. | 511 // write to have finished before the read begins. |
303 base::RunLoop().RunUntilIdle(); | 512 base::RunLoop().RunUntilIdle(); |
304 | 513 |
305 EXPECT_TRUE(pair.IsInitialized()); | 514 EXPECT_TRUE(pair.IsInitialized()); |
306 } | 515 } |
307 | 516 |
| 517 #if !defined(NDEBUG) && DCHECK_IS_ON() |
| 518 |
| 519 TEST_F(GCMKeyStoreTest, CannotShareAppIdFromGCMToInstanceID) { |
| 520 KeyPair pair_unused; |
| 521 std::string auth_secret_unused; |
| 522 gcm_key_store()->CreateKeys( |
| 523 kFakeAppId, "" /* empty authorized entity for non-InstanceID */, |
| 524 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 525 &pair_unused, &auth_secret_unused)); |
| 526 |
| 527 base::RunLoop().RunUntilIdle(); |
| 528 |
| 529 static_assert(logging::LOG_DCHECK == logging::LOG_DFATAL, |
| 530 "Unless these are equal, EXPECT_DFATAL will miss the DCHECK"); |
| 531 EXPECT_DFATAL( |
| 532 { |
| 533 gcm_key_store()->CreateKeys( |
| 534 kFakeAppId, kFakeAuthorizedEntity, |
| 535 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 536 &pair_unused, &auth_secret_unused)); |
| 537 |
| 538 base::RunLoop().RunUntilIdle(); |
| 539 }, |
| 540 "Instance ID tokens cannot share an app_id with a non-InstanceID GCM " |
| 541 "registration"); |
| 542 } |
| 543 |
| 544 TEST_F(GCMKeyStoreTest, CannotShareAppIdFromInstanceIDToGCM) { |
| 545 KeyPair pair_unused; |
| 546 std::string auth_secret_unused; |
| 547 gcm_key_store()->CreateKeys( |
| 548 kFakeAppId, kFakeAuthorizedEntity, |
| 549 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 550 &pair_unused, &auth_secret_unused)); |
| 551 |
| 552 base::RunLoop().RunUntilIdle(); |
| 553 |
| 554 gcm_key_store()->CreateKeys( |
| 555 kFakeAppId, kSecondFakeAuthorizedEntity, |
| 556 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 557 &pair_unused, &auth_secret_unused)); |
| 558 |
| 559 base::RunLoop().RunUntilIdle(); |
| 560 |
| 561 static_assert(logging::LOG_DCHECK == logging::LOG_DFATAL, |
| 562 "Unless these are equal, EXPECT_DFATAL will miss the DCHECK"); |
| 563 EXPECT_DFATAL( |
| 564 { |
| 565 gcm_key_store()->CreateKeys( |
| 566 kFakeAppId, "" /* empty authorized entity for non-InstanceID */, |
| 567 base::Bind(&GCMKeyStoreTest::GotKeys, base::Unretained(this), |
| 568 &pair_unused, &auth_secret_unused)); |
| 569 |
| 570 base::RunLoop().RunUntilIdle(); |
| 571 }, |
| 572 "Instance ID tokens cannot share an app_id with a non-InstanceID GCM " |
| 573 "registration"); |
| 574 } |
| 575 |
| 576 #endif // !defined(NDEBUG) && DCHECK_IS_ON() |
| 577 |
308 } // namespace | 578 } // namespace |
309 | 579 |
310 } // namespace gcm | 580 } // namespace gcm |
OLD | NEW |