| 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/instance_id/instance_id_driver.h" | 5 #include "components/gcm_driver/instance_id/instance_id_driver.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 DCHECK(!async_operation_completed_); | 241 DCHECK(!async_operation_completed_); |
| 242 async_operation_completed_ = true; | 242 async_operation_completed_ = true; |
| 243 result_ = result; | 243 result_ = result; |
| 244 if (!async_operation_completed_callback_.is_null()) | 244 if (!async_operation_completed_callback_.is_null()) |
| 245 async_operation_completed_callback_.Run(); | 245 async_operation_completed_callback_.Run(); |
| 246 } | 246 } |
| 247 | 247 |
| 248 TEST_F(InstanceIDDriverTest, GetAndRemoveInstanceID) { | 248 TEST_F(InstanceIDDriverTest, GetAndRemoveInstanceID) { |
| 249 EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1)); | 249 EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1)); |
| 250 | 250 |
| 251 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 251 InstanceID* instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 252 EXPECT_TRUE(instance_id); | 252 EXPECT_TRUE(instance_id); |
| 253 EXPECT_TRUE(driver()->ExistsInstanceID(kTestAppID1)); | 253 EXPECT_TRUE(driver()->ExistsInstanceID(kTestAppID1)); |
| 254 | 254 |
| 255 driver()->RemoveInstanceID(kTestAppID1); | 255 driver()->RemoveInstanceID(kTestAppID1); |
| 256 EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1)); | 256 EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1)); |
| 257 } | 257 } |
| 258 | 258 |
| 259 TEST_F(InstanceIDDriverTest, NewID) { | 259 TEST_F(InstanceIDDriverTest, NewID) { |
| 260 // Creation time should not be set when the ID is not created. | 260 // Creation time should not be set when the ID is not created. |
| 261 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); | 261 InstanceID* instance_id1 = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 262 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); | 262 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); |
| 263 | 263 |
| 264 // New ID is generated for the first time. | 264 // New ID is generated for the first time. |
| 265 std::string id1 = GetID(instance_id1); | 265 std::string id1 = GetID(instance_id1); |
| 266 EXPECT_TRUE(VerifyInstanceID(id1)); | 266 EXPECT_TRUE(VerifyInstanceID(id1)); |
| 267 base::Time creation_time = GetCreationTime(instance_id1); | 267 base::Time creation_time = GetCreationTime(instance_id1); |
| 268 EXPECT_FALSE(creation_time.is_null()); | 268 EXPECT_FALSE(creation_time.is_null()); |
| 269 | 269 |
| 270 // Same ID is returned for the same app. | 270 // Same ID is returned for the same app. |
| 271 EXPECT_EQ(id1, GetID(instance_id1)); | 271 EXPECT_EQ(id1, GetID(instance_id1)); |
| 272 EXPECT_EQ(creation_time, GetCreationTime(instance_id1)); | 272 EXPECT_EQ(creation_time, GetCreationTime(instance_id1)); |
| 273 | 273 |
| 274 // New ID is generated for another app. | 274 // New ID is generated for another app. |
| 275 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2); | 275 InstanceID* instance_id2 = driver()->GetInstanceIDForExtensions(kTestAppID2); |
| 276 std::string id2 = GetID(instance_id2); | 276 std::string id2 = GetID(instance_id2); |
| 277 EXPECT_TRUE(VerifyInstanceID(id2)); | 277 EXPECT_TRUE(VerifyInstanceID(id2)); |
| 278 EXPECT_NE(id1, id2); | 278 EXPECT_NE(id1, id2); |
| 279 EXPECT_FALSE(GetCreationTime(instance_id2).is_null()); | 279 EXPECT_FALSE(GetCreationTime(instance_id2).is_null()); |
| 280 } | 280 } |
| 281 | 281 |
| 282 TEST_F(InstanceIDDriverTest, PersistID) { | 282 TEST_F(InstanceIDDriverTest, PersistID) { |
| 283 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 283 InstanceID* instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 284 | 284 |
| 285 // Create the ID for the first time. The ID and creation time should be saved | 285 // Create the ID for the first time. The ID and creation time should be saved |
| 286 // to the store. | 286 // to the store. |
| 287 std::string id = GetID(instance_id); | 287 std::string id = GetID(instance_id); |
| 288 EXPECT_FALSE(id.empty()); | 288 EXPECT_FALSE(id.empty()); |
| 289 base::Time creation_time = GetCreationTime(instance_id); | 289 base::Time creation_time = GetCreationTime(instance_id); |
| 290 EXPECT_FALSE(creation_time.is_null()); | 290 EXPECT_FALSE(creation_time.is_null()); |
| 291 | 291 |
| 292 // Simulate restart by recreating InstanceIDDriver. Same ID and creation time | 292 // Simulate restart by recreating InstanceIDDriver. Same ID and creation time |
| 293 // should be expected. | 293 // should be expected. |
| 294 RecreateInstanceIDDriver(); | 294 RecreateInstanceIDDriver(); |
| 295 instance_id = driver()->GetInstanceID(kTestAppID1); | 295 instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 296 EXPECT_EQ(creation_time, GetCreationTime(instance_id)); | 296 EXPECT_EQ(creation_time, GetCreationTime(instance_id)); |
| 297 EXPECT_EQ(id, GetID(instance_id)); | 297 EXPECT_EQ(id, GetID(instance_id)); |
| 298 | 298 |
| 299 // Delete the ID. The ID and creation time should be removed from the store. | 299 // Delete the ID. The ID and creation time should be removed from the store. |
| 300 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); | 300 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
| 301 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); | 301 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
| 302 | 302 |
| 303 // Simulate restart by recreating InstanceIDDriver. Different ID should be | 303 // Simulate restart by recreating InstanceIDDriver. Different ID should be |
| 304 // expected. | 304 // expected. |
| 305 // Note that we do not check for different creation time since the test might | 305 // Note that we do not check for different creation time since the test might |
| 306 // be run at a very fast server. | 306 // be run at a very fast server. |
| 307 RecreateInstanceIDDriver(); | 307 RecreateInstanceIDDriver(); |
| 308 instance_id = driver()->GetInstanceID(kTestAppID1); | 308 instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 309 EXPECT_NE(id, GetID(instance_id)); | 309 EXPECT_NE(id, GetID(instance_id)); |
| 310 } | 310 } |
| 311 | 311 |
| 312 TEST_F(InstanceIDDriverTest, DeleteID) { | 312 TEST_F(InstanceIDDriverTest, DeleteID) { |
| 313 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 313 InstanceID* instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 314 std::string id1 = GetID(instance_id); | 314 std::string id1 = GetID(instance_id); |
| 315 EXPECT_FALSE(id1.empty()); | 315 EXPECT_FALSE(id1.empty()); |
| 316 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); | 316 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
| 317 | 317 |
| 318 // New ID will be generated from GetID after calling DeleteID. | 318 // New ID will be generated from GetID after calling DeleteID. |
| 319 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); | 319 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
| 320 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); | 320 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
| 321 | 321 |
| 322 std::string id2 = GetID(instance_id); | 322 std::string id2 = GetID(instance_id); |
| 323 EXPECT_FALSE(id2.empty()); | 323 EXPECT_FALSE(id2.empty()); |
| 324 EXPECT_NE(id1, id2); | 324 EXPECT_NE(id1, id2); |
| 325 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); | 325 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
| 326 } | 326 } |
| 327 | 327 |
| 328 TEST_F(InstanceIDDriverTest, GetToken) { | 328 TEST_F(InstanceIDDriverTest, GetToken) { |
| 329 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 329 InstanceID* instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 330 std::map<std::string, std::string> options; | 330 std::map<std::string, std::string> options; |
| 331 std::string token1 = | 331 std::string token1 = |
| 332 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); | 332 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); |
| 333 EXPECT_FALSE(token1.empty()); | 333 EXPECT_FALSE(token1.empty()); |
| 334 | 334 |
| 335 // Same token is returned for same authorized entity and scope. | 335 // Same token is returned for same authorized entity and scope. |
| 336 EXPECT_EQ(token1, | 336 EXPECT_EQ(token1, |
| 337 GetToken(instance_id, kAuthorizedEntity1, kScope1, options)); | 337 GetToken(instance_id, kAuthorizedEntity1, kScope1, options)); |
| 338 | 338 |
| 339 // Different token is returned for different authorized entity or scope. | 339 // Different token is returned for different authorized entity or scope. |
| 340 std::string token2 = | 340 std::string token2 = |
| 341 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | 341 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
| 342 EXPECT_FALSE(token2.empty()); | 342 EXPECT_FALSE(token2.empty()); |
| 343 EXPECT_NE(token1, token2); | 343 EXPECT_NE(token1, token2); |
| 344 | 344 |
| 345 std::string token3 = | 345 std::string token3 = |
| 346 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); | 346 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); |
| 347 EXPECT_FALSE(token3.empty()); | 347 EXPECT_FALSE(token3.empty()); |
| 348 EXPECT_NE(token1, token3); | 348 EXPECT_NE(token1, token3); |
| 349 EXPECT_NE(token2, token3); | 349 EXPECT_NE(token2, token3); |
| 350 } | 350 } |
| 351 | 351 |
| 352 TEST_F(InstanceIDDriverTest, DeleteToken) { | 352 TEST_F(InstanceIDDriverTest, DeleteToken) { |
| 353 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 353 InstanceID* instance_id = driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 354 std::map<std::string, std::string> options; | 354 std::map<std::string, std::string> options; |
| 355 | 355 |
| 356 // Gets 2 tokens. | 356 // Gets 2 tokens. |
| 357 std::string token1 = | 357 std::string token1 = |
| 358 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); | 358 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); |
| 359 EXPECT_FALSE(token1.empty()); | 359 EXPECT_FALSE(token1.empty()); |
| 360 std::string token2 = | 360 std::string token2 = |
| 361 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); | 361 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); |
| 362 EXPECT_FALSE(token1.empty()); | 362 EXPECT_FALSE(token1.empty()); |
| 363 EXPECT_NE(token1, token2); | 363 EXPECT_NE(token1, token2); |
| 364 | 364 |
| 365 // Different token is returned for same authorized entity and scope after | 365 // Different token is returned for same authorized entity and scope after |
| 366 // deletion. | 366 // deletion. |
| 367 EXPECT_EQ(InstanceID::SUCCESS, | 367 EXPECT_EQ(InstanceID::SUCCESS, |
| 368 DeleteToken(instance_id, kAuthorizedEntity1, kScope1)); | 368 DeleteToken(instance_id, kAuthorizedEntity1, kScope1)); |
| 369 std::string new_token1 = | 369 std::string new_token1 = |
| 370 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | 370 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
| 371 EXPECT_FALSE(new_token1.empty()); | 371 EXPECT_FALSE(new_token1.empty()); |
| 372 EXPECT_NE(token1, new_token1); | 372 EXPECT_NE(token1, new_token1); |
| 373 | 373 |
| 374 // The other token is not affected by the deletion. | 374 // The other token is not affected by the deletion. |
| 375 EXPECT_EQ(token2, | 375 EXPECT_EQ(token2, |
| 376 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); | 376 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); |
| 377 } | 377 } |
| 378 | 378 |
| 379 TEST_F(InstanceIDDriverTest, GetWithAndWithoutSubtype) { |
| 380 ASSERT_FALSE(driver()->ExistsInstanceID(kTestAppID1)); |
| 381 |
| 382 InstanceID* iid_without_subtype = |
| 383 driver()->GetInstanceIDForExtensions(kTestAppID1); |
| 384 ASSERT_TRUE(iid_without_subtype); |
| 385 ASSERT_TRUE(driver()->ExistsInstanceID(kTestAppID1)); |
| 386 |
| 387 // InstanceIDDriver's caching is currently oblivious to subtypes (ideally this |
| 388 // would DCHECK instead, since registering the same app_id in both ways is not |
| 389 // supported). |
| 390 InstanceID* same_iid = driver()->GetInstanceID(kTestAppID1); |
| 391 EXPECT_TRUE(same_iid); |
| 392 EXPECT_EQ(iid_without_subtype, same_iid); |
| 393 } |
| 394 |
| 379 } // namespace instance_id | 395 } // namespace instance_id |
| OLD | NEW |