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 <cmath> | 7 #include <cmath> |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 return false; | 27 return false; |
28 | 28 |
29 // Checks if it is URL-safe base64 encoded. | 29 // Checks if it is URL-safe base64 encoded. |
30 for (auto ch : str) { | 30 for (auto ch : str) { |
31 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-') | 31 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-') |
32 return false; | 32 return false; |
33 } | 33 } |
34 return true; | 34 return true; |
35 } | 35 } |
36 | 36 |
| 37 class FakeGCMDriverForInstanceID : public gcm::FakeGCMDriver, |
| 38 public gcm::InstanceIDStore { |
| 39 public: |
| 40 FakeGCMDriverForInstanceID(); |
| 41 ~FakeGCMDriverForInstanceID() override; |
| 42 |
| 43 // FakeGCMDriver overrides: |
| 44 gcm::InstanceIDStore* GetInstanceIDStore() override; |
| 45 |
| 46 // InstanceIDStore overrides: |
| 47 void AddInstanceIDData(const std::string& app_id, |
| 48 const std::string& instance_id_data) override; |
| 49 void RemoveInstanceIDData(const std::string& app_id) override; |
| 50 void GetInstanceIDData( |
| 51 const std::string& app_id, |
| 52 const gcm::InstanceIDStore::GetInstanceIDDataCallback& callback) override; |
| 53 |
| 54 private: |
| 55 std::map<std::string, std::string> instance_id_data_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(FakeGCMDriverForInstanceID); |
| 58 }; |
| 59 |
| 60 FakeGCMDriverForInstanceID::FakeGCMDriverForInstanceID() { |
| 61 } |
| 62 |
| 63 FakeGCMDriverForInstanceID::~FakeGCMDriverForInstanceID() { |
| 64 } |
| 65 |
| 66 gcm::InstanceIDStore* FakeGCMDriverForInstanceID::GetInstanceIDStore() { |
| 67 return this; |
| 68 } |
| 69 |
| 70 void FakeGCMDriverForInstanceID::AddInstanceIDData( |
| 71 const std::string& app_id, |
| 72 const std::string& instance_id_data) { |
| 73 instance_id_data_[app_id] = instance_id_data; |
| 74 } |
| 75 |
| 76 void FakeGCMDriverForInstanceID::RemoveInstanceIDData( |
| 77 const std::string& app_id) { |
| 78 instance_id_data_.erase(app_id); |
| 79 } |
| 80 |
| 81 void FakeGCMDriverForInstanceID::GetInstanceIDData( |
| 82 const std::string& app_id, |
| 83 const gcm::InstanceIDStore::GetInstanceIDDataCallback& callback) { |
| 84 std::string data; |
| 85 auto iter = instance_id_data_.find(app_id); |
| 86 if (iter != instance_id_data_.cend()) |
| 87 data = iter->second; |
| 88 base::MessageLoop::current()->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind(callback, data)); |
| 91 } |
| 92 |
37 } // namespace | 93 } // namespace |
38 | 94 |
39 class InstanceIDDriverTest : public testing::Test { | 95 class InstanceIDDriverTest : public testing::Test { |
40 public: | 96 public: |
41 InstanceIDDriverTest(); | 97 InstanceIDDriverTest(); |
42 ~InstanceIDDriverTest() override; | 98 ~InstanceIDDriverTest() override; |
43 | 99 |
44 // testing::Test: | 100 // testing::Test: |
45 void SetUp() override; | 101 void SetUp() override; |
46 | 102 |
47 void WaitForAsyncOperation(); | 103 void WaitForAsyncOperation(); |
48 | 104 |
| 105 // Recreates InstanceIDDriver to simulate restart. |
| 106 void RecreateInstanceIDDriver(); |
| 107 |
| 108 // Sync wrappers for async version. |
| 109 std::string GetID(InstanceID* instance_id); |
| 110 base::Time GetCreationTime(InstanceID* instance_id); |
| 111 InstanceID::Result DeleteID(InstanceID* instance_id); |
| 112 |
| 113 InstanceIDDriver* driver() const { return driver_.get(); } |
| 114 |
| 115 private: |
| 116 void GetIDCompleted(const std::string& id); |
| 117 void GetCreationTimeCompleted(const base::Time& creation_time); |
49 void DeleteIDCompleted(InstanceID::Result result); | 118 void DeleteIDCompleted(InstanceID::Result result); |
50 | 119 |
51 InstanceIDDriver* driver() const { return driver_.get(); } | 120 base::MessageLoopForUI message_loop_; |
52 InstanceID::Result delete_id_result() const { return delete_id_result_; } | 121 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_; |
| 122 scoped_ptr<InstanceIDDriver> driver_; |
53 | 123 |
54 private: | 124 std::string id_; |
55 base::MessageLoopForUI message_loop_; | 125 base::Time creation_time_; |
56 scoped_ptr<gcm::FakeGCMDriver> gcm_driver_; | 126 InstanceID::Result result_; |
57 scoped_ptr<InstanceIDDriver> driver_; | 127 |
58 InstanceID::Result delete_id_result_; | 128 bool async_operation_completed_; |
59 base::Closure async_operation_completed_callback_; | 129 base::Closure async_operation_completed_callback_; |
60 | 130 |
61 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); | 131 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
62 }; | 132 }; |
63 | 133 |
64 InstanceIDDriverTest::InstanceIDDriverTest() | 134 InstanceIDDriverTest::InstanceIDDriverTest() |
65 : delete_id_result_(InstanceID::UNKNOWN_ERROR) { | 135 : result_(InstanceID::UNKNOWN_ERROR), |
| 136 async_operation_completed_(false) { |
66 } | 137 } |
67 | 138 |
68 InstanceIDDriverTest::~InstanceIDDriverTest() { | 139 InstanceIDDriverTest::~InstanceIDDriverTest() { |
69 } | 140 } |
70 | 141 |
71 void InstanceIDDriverTest::SetUp() { | 142 void InstanceIDDriverTest::SetUp() { |
72 gcm_driver_.reset(new gcm::FakeGCMDriver); | 143 gcm_driver_.reset(new gcm::FakeGCMDriver); |
| 144 RecreateInstanceIDDriver(); |
| 145 } |
| 146 |
| 147 void InstanceIDDriverTest::RecreateInstanceIDDriver() { |
73 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); | 148 driver_.reset(new InstanceIDDriver(gcm_driver_.get())); |
74 } | 149 } |
75 | 150 |
76 void InstanceIDDriverTest::WaitForAsyncOperation() { | 151 void InstanceIDDriverTest::WaitForAsyncOperation() { |
| 152 // No need to wait if async operation is not needed. |
| 153 if (async_operation_completed_) |
| 154 return; |
77 base::RunLoop run_loop; | 155 base::RunLoop run_loop; |
78 async_operation_completed_callback_ = run_loop.QuitClosure(); | 156 async_operation_completed_callback_ = run_loop.QuitClosure(); |
79 run_loop.Run(); | 157 run_loop.Run(); |
80 } | 158 } |
81 | 159 |
82 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { | 160 std::string InstanceIDDriverTest::GetID(InstanceID* instance_id) { |
83 delete_id_result_ = result; | 161 async_operation_completed_ = false; |
| 162 id_.clear(); |
| 163 instance_id->GetID(base::Bind(&InstanceIDDriverTest::GetIDCompleted, |
| 164 base::Unretained(this))); |
| 165 WaitForAsyncOperation(); |
| 166 return id_; |
| 167 } |
| 168 |
| 169 base::Time InstanceIDDriverTest::GetCreationTime(InstanceID* instance_id) { |
| 170 async_operation_completed_ = false; |
| 171 creation_time_ = base::Time(); |
| 172 instance_id->GetCreationTime( |
| 173 base::Bind(&InstanceIDDriverTest::GetCreationTimeCompleted, |
| 174 base::Unretained(this))); |
| 175 WaitForAsyncOperation(); |
| 176 return creation_time_; |
| 177 } |
| 178 |
| 179 InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) { |
| 180 async_operation_completed_ = false; |
| 181 result_ = InstanceID::UNKNOWN_ERROR;; |
| 182 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, |
| 183 base::Unretained(this))); |
| 184 WaitForAsyncOperation(); |
| 185 return result_; |
| 186 } |
| 187 |
| 188 void InstanceIDDriverTest::GetIDCompleted(const std::string& id) { |
| 189 async_operation_completed_ = true; |
| 190 id_ = id; |
84 if (!async_operation_completed_callback_.is_null()) | 191 if (!async_operation_completed_callback_.is_null()) |
85 async_operation_completed_callback_.Run(); | 192 async_operation_completed_callback_.Run(); |
86 } | 193 } |
| 194 |
| 195 void InstanceIDDriverTest::GetCreationTimeCompleted( |
| 196 const base::Time& creation_time) { |
| 197 async_operation_completed_ = true; |
| 198 creation_time_ = creation_time; |
| 199 if (!async_operation_completed_callback_.is_null()) |
| 200 async_operation_completed_callback_.Run(); |
| 201 } |
| 202 |
| 203 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { |
| 204 async_operation_completed_ = true; |
| 205 result_ = result; |
| 206 if (!async_operation_completed_callback_.is_null()) |
| 207 async_operation_completed_callback_.Run(); |
| 208 } |
87 | 209 |
88 TEST_F(InstanceIDDriverTest, NewID) { | 210 TEST_F(InstanceIDDriverTest, NewID) { |
89 // Creation time should not be set when the ID is not created. | 211 // Creation time should not be set when the ID is not created. |
90 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); | 212 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); |
91 EXPECT_TRUE(instance_id1->GetCreationTime().is_null()); | 213 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); |
92 | 214 |
93 // New ID is generated for the first time. | 215 // New ID is generated for the first time. |
94 std::string id1 = instance_id1->GetID(); | 216 std::string id1 = GetID(instance_id1); |
95 EXPECT_FALSE(id1.empty()); | |
96 EXPECT_TRUE(VerifyInstanceID(id1)); | 217 EXPECT_TRUE(VerifyInstanceID(id1)); |
97 base::Time creation_time = instance_id1->GetCreationTime(); | 218 base::Time creation_time = GetCreationTime(instance_id1); |
98 EXPECT_FALSE(creation_time.is_null()); | 219 EXPECT_FALSE(creation_time.is_null()); |
99 | 220 |
100 // Same ID is returned for the same app. | 221 // Same ID is returned for the same app. |
101 EXPECT_EQ(id1, instance_id1->GetID()); | 222 EXPECT_EQ(id1, GetID(instance_id1)); |
102 EXPECT_EQ(creation_time, instance_id1->GetCreationTime()); | 223 EXPECT_EQ(creation_time, GetCreationTime(instance_id1)); |
103 | 224 |
104 // New ID is generated for another app. | 225 // New ID is generated for another app. |
105 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2); | 226 InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2); |
106 std::string id2 = instance_id2->GetID(); | 227 std::string id2 = GetID(instance_id2); |
107 EXPECT_FALSE(id2.empty()); | |
108 EXPECT_TRUE(VerifyInstanceID(id2)); | 228 EXPECT_TRUE(VerifyInstanceID(id2)); |
109 EXPECT_NE(id1, id2); | 229 EXPECT_NE(id1, id2); |
110 EXPECT_FALSE(instance_id2->GetCreationTime().is_null()); | 230 EXPECT_FALSE(GetCreationTime(instance_id2).is_null()); |
| 231 } |
| 232 |
| 233 TEST_F(InstanceIDDriverTest, PersistID) { |
| 234 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); |
| 235 |
| 236 // Create the ID for the first time. The ID and creation time should be saved |
| 237 // to the store. |
| 238 std::string id = GetID(instance_id); |
| 239 EXPECT_FALSE(id.empty()); |
| 240 base::Time creation_time = GetCreationTime(instance_id); |
| 241 EXPECT_FALSE(creation_time.is_null()); |
| 242 |
| 243 // Simulate restart by recreating InstanceIDDriver. Same ID and creation time |
| 244 // should be expected. |
| 245 RecreateInstanceIDDriver(); |
| 246 instance_id = driver()->GetInstanceID(kTestAppID1); |
| 247 EXPECT_EQ(creation_time, GetCreationTime(instance_id)); |
| 248 EXPECT_EQ(id, GetID(instance_id)); |
| 249 |
| 250 // Delete the ID. The ID and creation time should be removed from the store. |
| 251 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
| 252 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
| 253 |
| 254 // Simulate restart by recreating InstanceIDDriver. Different ID and creation |
| 255 // time should be expected. |
| 256 RecreateInstanceIDDriver(); |
| 257 instance_id = driver()->GetInstanceID(kTestAppID1); |
| 258 EXPECT_NE(id, GetID(instance_id)); |
| 259 EXPECT_NE(creation_time, GetCreationTime(instance_id)); |
111 } | 260 } |
112 | 261 |
113 TEST_F(InstanceIDDriverTest, DeleteID) { | 262 TEST_F(InstanceIDDriverTest, DeleteID) { |
114 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | 263 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); |
115 std::string id1 = instance_id->GetID(); | 264 std::string id1 = GetID(instance_id); |
116 EXPECT_FALSE(id1.empty()); | 265 EXPECT_FALSE(id1.empty()); |
117 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); | 266 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
118 | 267 |
119 // New ID will be generated from GetID after calling DeleteID. | 268 // New ID will be generated from GetID after calling DeleteID. |
120 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, | 269 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
121 base::Unretained(this))); | 270 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
122 WaitForAsyncOperation(); | |
123 EXPECT_EQ(InstanceID::SUCCESS, delete_id_result()); | |
124 EXPECT_TRUE(instance_id->GetCreationTime().is_null()); | |
125 | 271 |
126 std::string id2 = instance_id->GetID(); | 272 std::string id2 = GetID(instance_id); |
127 EXPECT_FALSE(id2.empty()); | 273 EXPECT_FALSE(id2.empty()); |
128 EXPECT_NE(id1, id2); | 274 EXPECT_NE(id1, id2); |
129 EXPECT_FALSE(instance_id->GetCreationTime().is_null()); | 275 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
130 } | 276 } |
131 | 277 |
132 } // instance_id | 278 } // instance_id |
OLD | NEW |