| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/gcm_driver/instance_id/instance_id_driver.h" |
| 6 |
| 7 #include <cmath> |
| 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "components/gcm_driver/instance_id/instance_id.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace instance_id { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const char kTestAppID1[] = "TestApp1"; |
| 20 const char kTestAppID2[] = "TestApp2"; |
| 21 |
| 22 bool VerifyInstanceID(const std::string& str) { |
| 23 // Checks the length. |
| 24 if (str.length() != static_cast<size_t>( |
| 25 std::ceil(InstanceID::kInstanceIDByteLength * 8 / 6.0))) |
| 26 return false; |
| 27 |
| 28 // Checks if it is URL-safe base64 encoded. |
| 29 for (auto ch : str) { |
| 30 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-') |
| 31 return false; |
| 32 } |
| 33 return true; |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class InstanceIDDriverTest : public testing::Test { |
| 39 public: |
| 40 InstanceIDDriverTest(); |
| 41 ~InstanceIDDriverTest() override; |
| 42 |
| 43 // testing::Test: |
| 44 void SetUp() override; |
| 45 |
| 46 void WaitForAsyncOperation(); |
| 47 |
| 48 void DeleteIDCompleted(InstanceID* instance_id, InstanceID::Result result); |
| 49 |
| 50 InstanceIDDriver* driver() const { return driver_.get(); } |
| 51 InstanceID::Result delete_id_result() const { return delete_id_result_; } |
| 52 |
| 53 private: |
| 54 base::MessageLoopForUI message_loop_; |
| 55 scoped_ptr<InstanceIDDriver> driver_; |
| 56 InstanceID::Result delete_id_result_; |
| 57 base::Closure async_operation_completed_callback_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
| 60 }; |
| 61 |
| 62 InstanceIDDriverTest::InstanceIDDriverTest() |
| 63 : delete_id_result_(InstanceID::UNKNOWN_ERROR) { |
| 64 } |
| 65 |
| 66 InstanceIDDriverTest::~InstanceIDDriverTest() { |
| 67 } |
| 68 |
| 69 void InstanceIDDriverTest::SetUp() { |
| 70 driver_.reset(new InstanceIDDriver()); |
| 71 } |
| 72 |
| 73 void InstanceIDDriverTest::WaitForAsyncOperation() { |
| 74 base::RunLoop run_loop; |
| 75 async_operation_completed_callback_ = run_loop.QuitClosure(); |
| 76 run_loop.Run(); |
| 77 } |
| 78 |
| 79 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID* instance_id, |
| 80 InstanceID::Result result) { |
| 81 delete_id_result_ = result; |
| 82 if (!async_operation_completed_callback_.is_null()) |
| 83 async_operation_completed_callback_.Run(); |
| 84 } |
| 85 |
| 86 TEST_F(InstanceIDDriverTest, NewID) { |
| 87 // Creation time should not be set when the ID is not created. |
| 88 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); |
| 89 EXPECT_TRUE(instance_id1->GetCreationTime().is_null()); |
| 90 |
| 91 // New ID is generated for the first time. |
| 92 std::string id1 = instance_id1->GetID(); |
| 93 EXPECT_FALSE(id1.empty()); |
| 94 EXPECT_TRUE(VerifyInstanceID(id1)); |
| 95 base::Time creation_time = instance_id1->GetCreationTime(); |
| 96 EXPECT_FALSE(creation_time.is_null()); |
| 97 |
| 98 // Same ID is returned for the same app. |
| 99 EXPECT_EQ(id1, instance_id1->GetID()); |
| 100 EXPECT_EQ(creation_time, instance_id1->GetCreationTime()); |
| 101 |
| 102 // New ID is generated for another app. |
| 103 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2); |
| 104 std::string id2 = instance_id2->GetID(); |
| 105 EXPECT_FALSE(id2.empty()); |
| 106 EXPECT_TRUE(VerifyInstanceID(id2)); |
| 107 EXPECT_NE(id1, id2); |
| 108 EXPECT_FALSE(instance_id2->GetCreationTime().is_null()); |
| 109 } |
| 110 |
| 111 TEST_F(InstanceIDDriverTest, DeleteID) { |
| 112 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); |
| 113 std::string id1 = instance_id->GetID(); |
| 114 EXPECT_FALSE(id1.empty()); |
| 115 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); |
| 116 |
| 117 // New ID will be generated from GetID after calling DeleteID. |
| 118 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, |
| 119 base::Unretained(this))); |
| 120 WaitForAsyncOperation(); |
| 121 EXPECT_EQ(InstanceID::SUCCESS, delete_id_result()); |
| 122 EXPECT_TRUE(instance_id->GetCreationTime().is_null()); |
| 123 |
| 124 std::string id2 = instance_id->GetID(); |
| 125 EXPECT_FALSE(id2.empty()); |
| 126 EXPECT_NE(id1, id2); |
| 127 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); |
| 128 } |
| 129 |
| 130 } // instance_id |
| OLD | NEW |