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" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 12 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
13 #include "components/gcm_driver/instance_id/instance_id.h" | 13 #include "components/gcm_driver/instance_id/instance_id.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace instance_id { | 16 namespace instance_id { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char kTestAppID1[] = "TestApp1"; | 20 const char kTestAppID1[] = "TestApp1"; |
21 const char kTestAppID2[] = "TestApp2"; | 21 const char kTestAppID2[] = "TestApp2"; |
22 const char kAuthorizedEntity1[] = "Sender 1"; | |
23 const char kAuthorizedEntity2[] = "Sender 2"; | |
24 const char kScope1[] = "GCM1"; | |
25 const char kScope2[] = "FooBar"; | |
22 | 26 |
23 bool VerifyInstanceID(const std::string& str) { | 27 bool VerifyInstanceID(const std::string& str) { |
24 // Checks the length. | 28 // Checks the length. |
25 if (str.length() != static_cast<size_t>( | 29 if (str.length() != static_cast<size_t>( |
26 std::ceil(InstanceID::kInstanceIDByteLength * 8 / 6.0))) | 30 std::ceil(InstanceID::kInstanceIDByteLength * 8 / 6.0))) |
27 return false; | 31 return false; |
28 | 32 |
29 // Checks if it is URL-safe base64 encoded. | 33 // Checks if it is URL-safe base64 encoded. |
30 for (auto ch : str) { | 34 for (auto ch : str) { |
31 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-') | 35 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_' && ch != '-') |
(...skipping 14 matching lines...) Expand all Loading... | |
46 | 50 |
47 void WaitForAsyncOperation(); | 51 void WaitForAsyncOperation(); |
48 | 52 |
49 // Recreates InstanceIDDriver to simulate restart. | 53 // Recreates InstanceIDDriver to simulate restart. |
50 void RecreateInstanceIDDriver(); | 54 void RecreateInstanceIDDriver(); |
51 | 55 |
52 // Sync wrappers for async version. | 56 // Sync wrappers for async version. |
53 std::string GetID(InstanceID* instance_id); | 57 std::string GetID(InstanceID* instance_id); |
54 base::Time GetCreationTime(InstanceID* instance_id); | 58 base::Time GetCreationTime(InstanceID* instance_id); |
55 InstanceID::Result DeleteID(InstanceID* instance_id); | 59 InstanceID::Result DeleteID(InstanceID* instance_id); |
60 std::string GetToken( | |
61 InstanceID* instance_id, | |
62 const std::string& authorized_entity, | |
63 const std::string& scope, | |
64 const std::map<std::string, std::string>& options); | |
65 InstanceID::Result DeleteToken( | |
66 InstanceID* instance_id, | |
67 const std::string& authorized_entity, | |
68 const std::string& scope); | |
56 | 69 |
57 InstanceIDDriver* driver() const { return driver_.get(); } | 70 InstanceIDDriver* driver() const { return driver_.get(); } |
58 | 71 |
59 private: | 72 private: |
60 void GetIDCompleted(const std::string& id); | 73 void GetIDCompleted(const std::string& id); |
61 void GetCreationTimeCompleted(const base::Time& creation_time); | 74 void GetCreationTimeCompleted(const base::Time& creation_time); |
62 void DeleteIDCompleted(InstanceID::Result result); | 75 void DeleteIDCompleted(InstanceID::Result result); |
76 void GetTokenCompleted(const std::string& token, InstanceID::Result result); | |
77 void DeleteTokenCompleted(InstanceID::Result result); | |
63 | 78 |
64 base::MessageLoopForUI message_loop_; | 79 base::MessageLoopForUI message_loop_; |
65 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_; | 80 scoped_ptr<FakeGCMDriverForInstanceID> gcm_driver_; |
66 scoped_ptr<InstanceIDDriver> driver_; | 81 scoped_ptr<InstanceIDDriver> driver_; |
67 | 82 |
68 std::string id_; | 83 std::string id_; |
69 base::Time creation_time_; | 84 base::Time creation_time_; |
85 std::string token_; | |
70 InstanceID::Result result_; | 86 InstanceID::Result result_; |
71 | 87 |
72 bool async_operation_completed_; | 88 bool async_operation_completed_; |
73 base::Closure async_operation_completed_callback_; | 89 base::Closure async_operation_completed_callback_; |
74 | 90 |
75 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); | 91 DISALLOW_COPY_AND_ASSIGN(InstanceIDDriverTest); |
76 }; | 92 }; |
77 | 93 |
78 InstanceIDDriverTest::InstanceIDDriverTest() | 94 InstanceIDDriverTest::InstanceIDDriverTest() |
79 : result_(InstanceID::UNKNOWN_ERROR), | 95 : result_(InstanceID::UNKNOWN_ERROR), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 138 |
123 InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) { | 139 InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) { |
124 async_operation_completed_ = false; | 140 async_operation_completed_ = false; |
125 result_ = InstanceID::UNKNOWN_ERROR;; | 141 result_ = InstanceID::UNKNOWN_ERROR;; |
126 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, | 142 instance_id->DeleteID(base::Bind(&InstanceIDDriverTest::DeleteIDCompleted, |
127 base::Unretained(this))); | 143 base::Unretained(this))); |
128 WaitForAsyncOperation(); | 144 WaitForAsyncOperation(); |
129 return result_; | 145 return result_; |
130 } | 146 } |
131 | 147 |
148 std::string InstanceIDDriverTest::GetToken( | |
149 InstanceID* instance_id, | |
150 const std::string& authorized_entity, | |
151 const std::string& scope, | |
152 const std::map<std::string, std::string>& options) { | |
153 async_operation_completed_ = false; | |
154 token_.clear(); | |
155 result_ = InstanceID::UNKNOWN_ERROR;; | |
156 instance_id->GetToken( | |
157 authorized_entity, | |
158 scope, | |
159 options, | |
160 base::Bind(&InstanceIDDriverTest::GetTokenCompleted, | |
161 base::Unretained(this))); | |
162 WaitForAsyncOperation(); | |
163 return token_; | |
164 } | |
165 | |
166 InstanceID::Result InstanceIDDriverTest::DeleteToken( | |
167 InstanceID* instance_id, | |
168 const std::string& authorized_entity, | |
169 const std::string& scope) { | |
170 async_operation_completed_ = false; | |
171 result_ = InstanceID::UNKNOWN_ERROR;; | |
172 instance_id->DeleteToken( | |
173 authorized_entity, | |
174 scope, | |
175 base::Bind(&InstanceIDDriverTest::DeleteTokenCompleted, | |
176 base::Unretained(this))); | |
177 WaitForAsyncOperation(); | |
178 return result_; | |
179 } | |
180 | |
132 void InstanceIDDriverTest::GetIDCompleted(const std::string& id) { | 181 void InstanceIDDriverTest::GetIDCompleted(const std::string& id) { |
133 async_operation_completed_ = true; | 182 async_operation_completed_ = true; |
134 id_ = id; | 183 id_ = id; |
135 if (!async_operation_completed_callback_.is_null()) | 184 if (!async_operation_completed_callback_.is_null()) |
136 async_operation_completed_callback_.Run(); | 185 async_operation_completed_callback_.Run(); |
137 } | 186 } |
138 | 187 |
139 void InstanceIDDriverTest::GetCreationTimeCompleted( | 188 void InstanceIDDriverTest::GetCreationTimeCompleted( |
140 const base::Time& creation_time) { | 189 const base::Time& creation_time) { |
141 async_operation_completed_ = true; | 190 async_operation_completed_ = true; |
142 creation_time_ = creation_time; | 191 creation_time_ = creation_time; |
143 if (!async_operation_completed_callback_.is_null()) | 192 if (!async_operation_completed_callback_.is_null()) |
144 async_operation_completed_callback_.Run(); | 193 async_operation_completed_callback_.Run(); |
145 } | 194 } |
146 | 195 |
147 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { | 196 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { |
148 async_operation_completed_ = true; | 197 async_operation_completed_ = true; |
149 result_ = result; | 198 result_ = result; |
150 if (!async_operation_completed_callback_.is_null()) | 199 if (!async_operation_completed_callback_.is_null()) |
151 async_operation_completed_callback_.Run(); | 200 async_operation_completed_callback_.Run(); |
152 } | 201 } |
153 | 202 |
203 void InstanceIDDriverTest::GetTokenCompleted( | |
204 const std::string& token, InstanceID::Result result){ | |
205 async_operation_completed_ = true; | |
206 token_ = token; | |
207 result_ = result; | |
208 if (!async_operation_completed_callback_.is_null()) | |
209 async_operation_completed_callback_.Run(); | |
210 } | |
211 | |
212 void InstanceIDDriverTest::DeleteTokenCompleted(InstanceID::Result result) { | |
213 async_operation_completed_ = true; | |
214 result_ = result; | |
215 if (!async_operation_completed_callback_.is_null()) | |
216 async_operation_completed_callback_.Run(); | |
217 } | |
218 | |
154 TEST_F(InstanceIDDriverTest, NewID) { | 219 TEST_F(InstanceIDDriverTest, NewID) { |
155 // Creation time should not be set when the ID is not created. | 220 // Creation time should not be set when the ID is not created. |
156 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); | 221 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); |
157 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); | 222 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); |
158 | 223 |
159 // New ID is generated for the first time. | 224 // New ID is generated for the first time. |
160 std::string id1 = GetID(instance_id1); | 225 std::string id1 = GetID(instance_id1); |
161 EXPECT_TRUE(VerifyInstanceID(id1)); | 226 EXPECT_TRUE(VerifyInstanceID(id1)); |
162 base::Time creation_time = GetCreationTime(instance_id1); | 227 base::Time creation_time = GetCreationTime(instance_id1); |
163 EXPECT_FALSE(creation_time.is_null()); | 228 EXPECT_FALSE(creation_time.is_null()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 // New ID will be generated from GetID after calling DeleteID. | 278 // New ID will be generated from GetID after calling DeleteID. |
214 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); | 279 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
215 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); | 280 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
216 | 281 |
217 std::string id2 = GetID(instance_id); | 282 std::string id2 = GetID(instance_id); |
218 EXPECT_FALSE(id2.empty()); | 283 EXPECT_FALSE(id2.empty()); |
219 EXPECT_NE(id1, id2); | 284 EXPECT_NE(id1, id2); |
220 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); | 285 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
221 } | 286 } |
222 | 287 |
288 TEST_F(InstanceIDDriverTest, GetToken) { | |
289 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | |
290 std::map<std::string, std::string> options; | |
291 std::string token1 = | |
292 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); | |
293 EXPECT_FALSE(token1.empty()); | |
294 | |
295 // Same token is returned for same authorized entity and scope. | |
296 EXPECT_EQ(token1, | |
297 GetToken(instance_id, kAuthorizedEntity1, kScope1, options)); | |
298 | |
299 // Different token is returned for different authorized entity or scope. | |
300 std::string token2 = | |
301 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | |
302 EXPECT_FALSE(token2.empty()); | |
303 EXPECT_NE(token1, token2); | |
304 | |
305 std::string token3 = | |
306 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); | |
307 EXPECT_FALSE(token3.empty()); | |
308 EXPECT_NE(token1, token3); | |
309 EXPECT_NE(token2, token3); | |
310 } | |
311 | |
312 TEST_F(InstanceIDDriverTest, DeleteToken) { | |
313 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); | |
314 std::map<std::string, std::string> options; | |
315 std::string token1 = | |
316 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); | |
317 EXPECT_FALSE(token1.empty()); | |
318 | |
fgorski
2015/05/13 18:32:39
add second token and verify that it is still there
jianli
2015/05/13 22:42:56
Done.
| |
319 // Same token is returned for same authorized entity and scope after deletion. | |
320 EXPECT_EQ(InstanceID::SUCCESS, | |
321 DeleteToken(instance_id, kAuthorizedEntity1, kScope1)); | |
322 std::string token2 = | |
323 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); | |
324 EXPECT_FALSE(token2.empty()); | |
325 EXPECT_NE(token1, token2); | |
326 } | |
327 | |
223 } // instance_id | 328 } // instance_id |
OLD | NEW |