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) { |
| 182 DCHECK(!async_operation_completed_); |
133 async_operation_completed_ = true; | 183 async_operation_completed_ = true; |
134 id_ = id; | 184 id_ = id; |
135 if (!async_operation_completed_callback_.is_null()) | 185 if (!async_operation_completed_callback_.is_null()) |
136 async_operation_completed_callback_.Run(); | 186 async_operation_completed_callback_.Run(); |
137 } | 187 } |
138 | 188 |
139 void InstanceIDDriverTest::GetCreationTimeCompleted( | 189 void InstanceIDDriverTest::GetCreationTimeCompleted( |
140 const base::Time& creation_time) { | 190 const base::Time& creation_time) { |
| 191 DCHECK(!async_operation_completed_); |
141 async_operation_completed_ = true; | 192 async_operation_completed_ = true; |
142 creation_time_ = creation_time; | 193 creation_time_ = creation_time; |
143 if (!async_operation_completed_callback_.is_null()) | 194 if (!async_operation_completed_callback_.is_null()) |
144 async_operation_completed_callback_.Run(); | 195 async_operation_completed_callback_.Run(); |
145 } | 196 } |
146 | 197 |
147 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { | 198 void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) { |
| 199 DCHECK(!async_operation_completed_); |
148 async_operation_completed_ = true; | 200 async_operation_completed_ = true; |
149 result_ = result; | 201 result_ = result; |
150 if (!async_operation_completed_callback_.is_null()) | 202 if (!async_operation_completed_callback_.is_null()) |
| 203 async_operation_completed_callback_.Run(); |
| 204 } |
| 205 |
| 206 void InstanceIDDriverTest::GetTokenCompleted( |
| 207 const std::string& token, InstanceID::Result result){ |
| 208 DCHECK(!async_operation_completed_); |
| 209 async_operation_completed_ = true; |
| 210 token_ = token; |
| 211 result_ = result; |
| 212 if (!async_operation_completed_callback_.is_null()) |
| 213 async_operation_completed_callback_.Run(); |
| 214 } |
| 215 |
| 216 void InstanceIDDriverTest::DeleteTokenCompleted(InstanceID::Result result) { |
| 217 DCHECK(!async_operation_completed_); |
| 218 async_operation_completed_ = true; |
| 219 result_ = result; |
| 220 if (!async_operation_completed_callback_.is_null()) |
151 async_operation_completed_callback_.Run(); | 221 async_operation_completed_callback_.Run(); |
152 } | 222 } |
153 | 223 |
154 TEST_F(InstanceIDDriverTest, NewID) { | 224 TEST_F(InstanceIDDriverTest, NewID) { |
155 // Creation time should not be set when the ID is not created. | 225 // Creation time should not be set when the ID is not created. |
156 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); | 226 InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1); |
157 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); | 227 EXPECT_TRUE(GetCreationTime(instance_id1).is_null()); |
158 | 228 |
159 // New ID is generated for the first time. | 229 // New ID is generated for the first time. |
160 std::string id1 = GetID(instance_id1); | 230 std::string id1 = GetID(instance_id1); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // New ID will be generated from GetID after calling DeleteID. | 283 // New ID will be generated from GetID after calling DeleteID. |
214 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); | 284 EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id)); |
215 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); | 285 EXPECT_TRUE(GetCreationTime(instance_id).is_null()); |
216 | 286 |
217 std::string id2 = GetID(instance_id); | 287 std::string id2 = GetID(instance_id); |
218 EXPECT_FALSE(id2.empty()); | 288 EXPECT_FALSE(id2.empty()); |
219 EXPECT_NE(id1, id2); | 289 EXPECT_NE(id1, id2); |
220 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); | 290 EXPECT_FALSE(GetCreationTime(instance_id).is_null()); |
221 } | 291 } |
222 | 292 |
| 293 TEST_F(InstanceIDDriverTest, GetToken) { |
| 294 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); |
| 295 std::map<std::string, std::string> options; |
| 296 std::string token1 = |
| 297 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); |
| 298 EXPECT_FALSE(token1.empty()); |
| 299 |
| 300 // Same token is returned for same authorized entity and scope. |
| 301 EXPECT_EQ(token1, |
| 302 GetToken(instance_id, kAuthorizedEntity1, kScope1, options)); |
| 303 |
| 304 // Different token is returned for different authorized entity or scope. |
| 305 std::string token2 = |
| 306 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
| 307 EXPECT_FALSE(token2.empty()); |
| 308 EXPECT_NE(token1, token2); |
| 309 |
| 310 std::string token3 = |
| 311 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); |
| 312 EXPECT_FALSE(token3.empty()); |
| 313 EXPECT_NE(token1, token3); |
| 314 EXPECT_NE(token2, token3); |
| 315 } |
| 316 |
| 317 TEST_F(InstanceIDDriverTest, DeleteToken) { |
| 318 InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1); |
| 319 std::map<std::string, std::string> options; |
| 320 |
| 321 // Gets 2 tokens. |
| 322 std::string token1 = |
| 323 GetToken(instance_id, kAuthorizedEntity1, kScope1, options); |
| 324 EXPECT_FALSE(token1.empty()); |
| 325 std::string token2 = |
| 326 GetToken(instance_id, kAuthorizedEntity2, kScope1, options); |
| 327 EXPECT_FALSE(token1.empty()); |
| 328 EXPECT_NE(token1, token2); |
| 329 |
| 330 // Different token is returned for same authorized entity and scope after |
| 331 // deletion. |
| 332 EXPECT_EQ(InstanceID::SUCCESS, |
| 333 DeleteToken(instance_id, kAuthorizedEntity1, kScope1)); |
| 334 std::string new_token1 = |
| 335 GetToken(instance_id, kAuthorizedEntity1, kScope2, options); |
| 336 EXPECT_FALSE(new_token1.empty()); |
| 337 EXPECT_NE(token1, new_token1); |
| 338 |
| 339 // The other token is not affected by the deletion. |
| 340 EXPECT_EQ(token2, |
| 341 GetToken(instance_id, kAuthorizedEntity2, kScope1, options)); |
| 342 } |
| 343 |
223 } // instance_id | 344 } // instance_id |
OLD | NEW |