OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_tokenizer.h" | 10 #include "base/strings/string_tokenizer.h" |
11 #include "google_apis/gcm/engine/registration_request.h" | 11 #include "google_apis/gcm/engine/registration_request.h" |
12 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" | 12 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" |
13 #include "net/url_request/test_url_fetcher_factory.h" | 13 #include "net/url_request/test_url_fetcher_factory.h" |
14 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
15 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace gcm { | 18 namespace gcm { |
19 | 19 |
20 namespace { | 20 namespace { |
21 const uint64 kAndroidId = 42UL; | 21 const uint64 kAndroidId = 42UL; |
22 const char kAppId[] = "TestAppId"; | 22 const char kAppId[] = "TestAppId"; |
23 const char kDeveloperId[] = "Project1"; | 23 const char kDeveloperId[] = "Project1"; |
24 const char kLoginHeader[] = "AidLogin"; | 24 const char kLoginHeader[] = "AidLogin"; |
25 const char kRegistrationURL[] = "http://foo.bar/register"; | 25 const char kRegistrationURL[] = "http://foo.bar/register"; |
26 const uint64 kSecurityToken = 77UL; | 26 const uint64 kSecurityToken = 77UL; |
| 27 const char kChromeVersion[] = "40.0.0.1"; |
| 28 const char kInstanceId[] = "IID1"; |
| 29 const char kScope[] = "GCM"; |
27 | 30 |
28 // Backoff policy for testing registration request. | 31 // Backoff policy for testing registration request. |
29 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { | 32 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { |
30 // Number of initial errors (in sequence) to ignore before applying | 33 // Number of initial errors (in sequence) to ignore before applying |
31 // exponential back-off rules. | 34 // exponential back-off rules. |
32 // Explicitly set to 2 to skip the delay on the first retry, as we are not | 35 // Explicitly set to 2 to skip the delay on the first retry, as we are not |
33 // trying to test the backoff itself, but rather the fact that retry happens. | 36 // trying to test the backoff itself, but rather the fact that retry happens. |
34 2, | 37 2, |
35 | 38 |
36 // Initial delay for exponential back-off in ms. | 39 // Initial delay for exponential back-off in ms. |
(...skipping 20 matching lines...) Expand all Loading... |
57 } // namespace | 60 } // namespace |
58 | 61 |
59 class RegistrationRequestTest : public testing::Test { | 62 class RegistrationRequestTest : public testing::Test { |
60 public: | 63 public: |
61 RegistrationRequestTest(); | 64 RegistrationRequestTest(); |
62 ~RegistrationRequestTest() override; | 65 ~RegistrationRequestTest() override; |
63 | 66 |
64 void RegistrationCallback(RegistrationRequest::Status status, | 67 void RegistrationCallback(RegistrationRequest::Status status, |
65 const std::string& registration_id); | 68 const std::string& registration_id); |
66 | 69 |
67 void CreateRequest(const std::string& sender_ids); | 70 void CreateRequestFromRequestInfo( |
| 71 scoped_ptr<RegistrationRequest::RequestInfo> request_info); |
68 void SetResponseStatusAndString(net::HttpStatusCode status_code, | 72 void SetResponseStatusAndString(net::HttpStatusCode status_code, |
69 const std::string& response_body); | 73 const std::string& response_body); |
70 void CompleteFetch(); | 74 void CompleteFetch(); |
71 void set_max_retry_count(int max_retry_count) { | 75 void set_max_retry_count(int max_retry_count) { |
72 max_retry_count_ = max_retry_count; | 76 max_retry_count_ = max_retry_count; |
73 } | 77 } |
74 | 78 |
75 protected: | 79 protected: |
76 int max_retry_count_; | 80 int max_retry_count_; |
77 RegistrationRequest::Status status_; | 81 RegistrationRequest::Status status_; |
(...skipping 17 matching lines...) Expand all Loading... |
95 RegistrationRequestTest::~RegistrationRequestTest() {} | 99 RegistrationRequestTest::~RegistrationRequestTest() {} |
96 | 100 |
97 void RegistrationRequestTest::RegistrationCallback( | 101 void RegistrationRequestTest::RegistrationCallback( |
98 RegistrationRequest::Status status, | 102 RegistrationRequest::Status status, |
99 const std::string& registration_id) { | 103 const std::string& registration_id) { |
100 status_ = status; | 104 status_ = status; |
101 registration_id_ = registration_id; | 105 registration_id_ = registration_id; |
102 callback_called_ = true; | 106 callback_called_ = true; |
103 } | 107 } |
104 | 108 |
105 void RegistrationRequestTest::CreateRequest(const std::string& sender_ids) { | 109 void RegistrationRequestTest::CreateRequestFromRequestInfo( |
106 std::vector<std::string> senders; | 110 scoped_ptr<RegistrationRequest::RequestInfo> request_info) { |
107 base::StringTokenizer tokenizer(sender_ids, ","); | |
108 while (tokenizer.GetNext()) | |
109 senders.push_back(tokenizer.token()); | |
110 | |
111 request_.reset(new RegistrationRequest( | 111 request_.reset(new RegistrationRequest( |
112 GURL(kRegistrationURL), | 112 GURL(kRegistrationURL), |
113 RegistrationRequest::RequestInfo(kAndroidId, | 113 request_info.Pass(), |
114 kSecurityToken, | |
115 kAppId, | |
116 senders), | |
117 kDefaultBackoffPolicy, | 114 kDefaultBackoffPolicy, |
118 base::Bind(&RegistrationRequestTest::RegistrationCallback, | 115 base::Bind(&RegistrationRequestTest::RegistrationCallback, |
119 base::Unretained(this)), | 116 base::Unretained(this)), |
120 max_retry_count_, | 117 max_retry_count_, |
121 url_request_context_getter_.get(), | 118 url_request_context_getter_.get(), |
122 &recorder_)); | 119 &recorder_)); |
123 } | 120 } |
124 | 121 |
125 void RegistrationRequestTest::SetResponseStatusAndString( | 122 void RegistrationRequestTest::SetResponseStatusAndString( |
126 net::HttpStatusCode status_code, | 123 net::HttpStatusCode status_code, |
127 const std::string& response_body) { | 124 const std::string& response_body) { |
128 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 125 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
129 ASSERT_TRUE(fetcher); | 126 ASSERT_TRUE(fetcher); |
130 fetcher->set_response_code(status_code); | 127 fetcher->set_response_code(status_code); |
131 fetcher->SetResponseString(response_body); | 128 fetcher->SetResponseString(response_body); |
132 } | 129 } |
133 | 130 |
134 void RegistrationRequestTest::CompleteFetch() { | 131 void RegistrationRequestTest::CompleteFetch() { |
135 registration_id_.clear(); | 132 registration_id_.clear(); |
136 status_ = RegistrationRequest::SUCCESS; | 133 status_ = RegistrationRequest::SUCCESS; |
137 callback_called_ = false; | 134 callback_called_ = false; |
138 | 135 |
139 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 136 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
140 ASSERT_TRUE(fetcher); | 137 ASSERT_TRUE(fetcher); |
141 fetcher->delegate()->OnURLFetchComplete(fetcher); | 138 fetcher->delegate()->OnURLFetchComplete(fetcher); |
142 } | 139 } |
143 | 140 |
144 TEST_F(RegistrationRequestTest, RequestSuccessful) { | 141 class GCMRegistrationRequestTest : public RegistrationRequestTest { |
| 142 public: |
| 143 GCMRegistrationRequestTest(); |
| 144 ~GCMRegistrationRequestTest() override; |
| 145 |
| 146 void CreateRequest(const std::string& sender_ids); |
| 147 }; |
| 148 |
| 149 GCMRegistrationRequestTest::GCMRegistrationRequestTest() { |
| 150 } |
| 151 |
| 152 GCMRegistrationRequestTest::~GCMRegistrationRequestTest() { |
| 153 } |
| 154 |
| 155 void GCMRegistrationRequestTest::CreateRequest(const std::string& sender_ids) { |
| 156 std::vector<std::string> senders; |
| 157 base::StringTokenizer tokenizer(sender_ids, ","); |
| 158 while (tokenizer.GetNext()) |
| 159 senders.push_back(tokenizer.token()); |
| 160 |
| 161 scoped_ptr<RegistrationRequest::GCMRequestInfo> request_info( |
| 162 new RegistrationRequest::GCMRequestInfo); |
| 163 request_info->set_android_id(kAndroidId); |
| 164 request_info->set_security_token(kSecurityToken); |
| 165 request_info->set_app_id(kAppId); |
| 166 request_info->set_sender_ids(senders); |
| 167 return CreateRequestFromRequestInfo(request_info.Pass()); |
| 168 } |
| 169 |
| 170 TEST_F(GCMRegistrationRequestTest, RequestSuccessful) { |
145 set_max_retry_count(0); | 171 set_max_retry_count(0); |
146 CreateRequest("sender1,sender2"); | 172 CreateRequest("sender1,sender2"); |
147 request_->Start(); | 173 request_->Start(); |
148 | 174 |
149 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 175 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
150 CompleteFetch(); | 176 CompleteFetch(); |
151 | 177 |
152 EXPECT_TRUE(callback_called_); | 178 EXPECT_TRUE(callback_called_); |
153 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 179 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
154 EXPECT_EQ("2501", registration_id_); | 180 EXPECT_EQ("2501", registration_id_); |
155 } | 181 } |
156 | 182 |
157 TEST_F(RegistrationRequestTest, RequestDataAndURL) { | 183 TEST_F(GCMRegistrationRequestTest, RequestDataAndURL) { |
158 CreateRequest(kDeveloperId); | 184 CreateRequest(kDeveloperId); |
159 request_->Start(); | 185 request_->Start(); |
160 | 186 |
161 // Get data sent by request. | 187 // Get data sent by request. |
162 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 188 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
163 ASSERT_TRUE(fetcher); | 189 ASSERT_TRUE(fetcher); |
164 | 190 |
165 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); | 191 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); |
166 | 192 |
167 // Verify that authorization header was put together properly. | 193 // Verify that authorization header was put together properly. |
(...skipping 23 matching lines...) Expand all Loading... |
191 ASSERT_TRUE(iter != expected_pairs.end()); | 217 ASSERT_TRUE(iter != expected_pairs.end()); |
192 ASSERT_TRUE(data_tokenizer.GetNext()); | 218 ASSERT_TRUE(data_tokenizer.GetNext()); |
193 EXPECT_EQ(iter->second, data_tokenizer.token()); | 219 EXPECT_EQ(iter->second, data_tokenizer.token()); |
194 // Ensure that none of the keys appears twice. | 220 // Ensure that none of the keys appears twice. |
195 expected_pairs.erase(iter); | 221 expected_pairs.erase(iter); |
196 } | 222 } |
197 | 223 |
198 EXPECT_EQ(0UL, expected_pairs.size()); | 224 EXPECT_EQ(0UL, expected_pairs.size()); |
199 } | 225 } |
200 | 226 |
201 TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { | 227 TEST_F(GCMRegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { |
202 CreateRequest("sender1,sender2@gmail.com"); | 228 CreateRequest("sender1,sender2@gmail.com"); |
203 request_->Start(); | 229 request_->Start(); |
204 | 230 |
205 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 231 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
206 ASSERT_TRUE(fetcher); | 232 ASSERT_TRUE(fetcher); |
207 | 233 |
208 // Verify data was formatted properly. | 234 // Verify data was formatted properly. |
209 std::string upload_data = fetcher->upload_data(); | 235 std::string upload_data = fetcher->upload_data(); |
210 base::StringTokenizer data_tokenizer(upload_data, "&="); | 236 base::StringTokenizer data_tokenizer(upload_data, "&="); |
211 | 237 |
212 // Skip all tokens until you hit entry for senders. | 238 // Skip all tokens until you hit entry for senders. |
213 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender") | 239 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender") |
214 continue; | 240 continue; |
215 | 241 |
216 ASSERT_TRUE(data_tokenizer.GetNext()); | 242 ASSERT_TRUE(data_tokenizer.GetNext()); |
217 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(), | 243 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(), |
218 net::UnescapeRule::URL_SPECIAL_CHARS)); | 244 net::UnescapeRule::URL_SPECIAL_CHARS)); |
219 base::StringTokenizer sender_tokenizer(senders, ","); | 245 base::StringTokenizer sender_tokenizer(senders, ","); |
220 ASSERT_TRUE(sender_tokenizer.GetNext()); | 246 ASSERT_TRUE(sender_tokenizer.GetNext()); |
221 EXPECT_EQ("sender1", sender_tokenizer.token()); | 247 EXPECT_EQ("sender1", sender_tokenizer.token()); |
222 ASSERT_TRUE(sender_tokenizer.GetNext()); | 248 ASSERT_TRUE(sender_tokenizer.GetNext()); |
223 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token()); | 249 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token()); |
224 } | 250 } |
225 | 251 |
226 TEST_F(RegistrationRequestTest, ResponseParsing) { | 252 TEST_F(GCMRegistrationRequestTest, ResponseParsing) { |
227 CreateRequest("sender1,sender2"); | 253 CreateRequest("sender1,sender2"); |
228 request_->Start(); | 254 request_->Start(); |
229 | 255 |
230 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 256 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
231 CompleteFetch(); | 257 CompleteFetch(); |
232 | 258 |
233 EXPECT_TRUE(callback_called_); | 259 EXPECT_TRUE(callback_called_); |
234 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 260 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
235 EXPECT_EQ("2501", registration_id_); | 261 EXPECT_EQ("2501", registration_id_); |
236 } | 262 } |
237 | 263 |
238 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { | 264 TEST_F(GCMRegistrationRequestTest, ResponseHttpStatusNotOK) { |
239 CreateRequest("sender1,sender2"); | 265 CreateRequest("sender1,sender2"); |
240 request_->Start(); | 266 request_->Start(); |
241 | 267 |
242 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); | 268 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); |
243 CompleteFetch(); | 269 CompleteFetch(); |
244 | 270 |
245 EXPECT_FALSE(callback_called_); | 271 EXPECT_FALSE(callback_called_); |
246 | 272 |
247 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 273 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
248 CompleteFetch(); | 274 CompleteFetch(); |
249 | 275 |
250 EXPECT_TRUE(callback_called_); | 276 EXPECT_TRUE(callback_called_); |
251 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 277 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
252 EXPECT_EQ("2501", registration_id_); | 278 EXPECT_EQ("2501", registration_id_); |
253 } | 279 } |
254 | 280 |
255 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { | 281 TEST_F(GCMRegistrationRequestTest, ResponseMissingRegistrationId) { |
256 CreateRequest("sender1,sender2"); | 282 CreateRequest("sender1,sender2"); |
257 request_->Start(); | 283 request_->Start(); |
258 | 284 |
259 SetResponseStatusAndString(net::HTTP_OK, ""); | 285 SetResponseStatusAndString(net::HTTP_OK, ""); |
260 CompleteFetch(); | 286 CompleteFetch(); |
261 | 287 |
262 EXPECT_FALSE(callback_called_); | 288 EXPECT_FALSE(callback_called_); |
263 | 289 |
264 SetResponseStatusAndString(net::HTTP_OK, "some error in response"); | 290 SetResponseStatusAndString(net::HTTP_OK, "some error in response"); |
265 CompleteFetch(); | 291 CompleteFetch(); |
266 | 292 |
267 EXPECT_FALSE(callback_called_); | 293 EXPECT_FALSE(callback_called_); |
268 | 294 |
269 // Ensuring a retry happened and succeeds. | 295 // Ensuring a retry happened and succeeds. |
270 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 296 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
271 CompleteFetch(); | 297 CompleteFetch(); |
272 | 298 |
273 EXPECT_TRUE(callback_called_); | 299 EXPECT_TRUE(callback_called_); |
274 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 300 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
275 EXPECT_EQ("2501", registration_id_); | 301 EXPECT_EQ("2501", registration_id_); |
276 } | 302 } |
277 | 303 |
278 TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { | 304 TEST_F(GCMRegistrationRequestTest, ResponseDeviceRegistrationError) { |
279 CreateRequest("sender1,sender2"); | 305 CreateRequest("sender1,sender2"); |
280 request_->Start(); | 306 request_->Start(); |
281 | 307 |
282 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR"); | 308 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR"); |
283 CompleteFetch(); | 309 CompleteFetch(); |
284 | 310 |
285 EXPECT_FALSE(callback_called_); | 311 EXPECT_FALSE(callback_called_); |
286 | 312 |
287 // Ensuring a retry happened and succeeds. | 313 // Ensuring a retry happened and succeeds. |
288 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 314 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
289 CompleteFetch(); | 315 CompleteFetch(); |
290 | 316 |
291 EXPECT_TRUE(callback_called_); | 317 EXPECT_TRUE(callback_called_); |
292 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 318 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
293 EXPECT_EQ("2501", registration_id_); | 319 EXPECT_EQ("2501", registration_id_); |
294 } | 320 } |
295 | 321 |
296 TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { | 322 TEST_F(GCMRegistrationRequestTest, ResponseAuthenticationError) { |
297 CreateRequest("sender1,sender2"); | 323 CreateRequest("sender1,sender2"); |
298 request_->Start(); | 324 request_->Start(); |
299 | 325 |
300 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, | 326 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, |
301 "Error=AUTHENTICATION_FAILED"); | 327 "Error=AUTHENTICATION_FAILED"); |
302 CompleteFetch(); | 328 CompleteFetch(); |
303 | 329 |
304 EXPECT_FALSE(callback_called_); | 330 EXPECT_FALSE(callback_called_); |
305 | 331 |
306 // Ensuring a retry happened and succeeds. | 332 // Ensuring a retry happened and succeeds. |
307 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 333 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
308 CompleteFetch(); | 334 CompleteFetch(); |
309 | 335 |
310 EXPECT_TRUE(callback_called_); | 336 EXPECT_TRUE(callback_called_); |
311 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 337 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
312 EXPECT_EQ("2501", registration_id_); | 338 EXPECT_EQ("2501", registration_id_); |
313 } | 339 } |
314 | 340 |
315 TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { | 341 TEST_F(GCMRegistrationRequestTest, ResponseInvalidParameters) { |
316 CreateRequest("sender1,sender2"); | 342 CreateRequest("sender1,sender2"); |
317 request_->Start(); | 343 request_->Start(); |
318 | 344 |
319 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); | 345 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); |
320 CompleteFetch(); | 346 CompleteFetch(); |
321 | 347 |
322 EXPECT_TRUE(callback_called_); | 348 EXPECT_TRUE(callback_called_); |
323 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_); | 349 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_); |
324 EXPECT_EQ(std::string(), registration_id_); | 350 EXPECT_EQ(std::string(), registration_id_); |
325 } | 351 } |
326 | 352 |
327 TEST_F(RegistrationRequestTest, ResponseInvalidSender) { | 353 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSender) { |
328 CreateRequest("sender1,sender2"); | 354 CreateRequest("sender1,sender2"); |
329 request_->Start(); | 355 request_->Start(); |
330 | 356 |
331 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); | 357 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); |
332 CompleteFetch(); | 358 CompleteFetch(); |
333 | 359 |
334 EXPECT_TRUE(callback_called_); | 360 EXPECT_TRUE(callback_called_); |
335 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); | 361 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); |
336 EXPECT_EQ(std::string(), registration_id_); | 362 EXPECT_EQ(std::string(), registration_id_); |
337 } | 363 } |
338 | 364 |
339 TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { | 365 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSenderBadRequest) { |
340 CreateRequest("sender1"); | 366 CreateRequest("sender1"); |
341 request_->Start(); | 367 request_->Start(); |
342 | 368 |
343 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER"); | 369 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER"); |
344 CompleteFetch(); | 370 CompleteFetch(); |
345 | 371 |
346 EXPECT_TRUE(callback_called_); | 372 EXPECT_TRUE(callback_called_); |
347 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); | 373 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); |
348 EXPECT_EQ(std::string(), registration_id_); | 374 EXPECT_EQ(std::string(), registration_id_); |
349 } | 375 } |
350 | 376 |
351 TEST_F(RegistrationRequestTest, RequestNotSuccessful) { | 377 TEST_F(GCMRegistrationRequestTest, RequestNotSuccessful) { |
352 CreateRequest("sender1,sender2"); | 378 CreateRequest("sender1,sender2"); |
353 request_->Start(); | 379 request_->Start(); |
354 | 380 |
355 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); | 381 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); |
356 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 382 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
357 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 383 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
358 ASSERT_TRUE(fetcher); | 384 ASSERT_TRUE(fetcher); |
359 fetcher->set_status(request_status); | 385 fetcher->set_status(request_status); |
360 | 386 |
361 CompleteFetch(); | 387 CompleteFetch(); |
362 | 388 |
363 EXPECT_FALSE(callback_called_); | 389 EXPECT_FALSE(callback_called_); |
364 | 390 |
365 // Ensuring a retry happened and succeeded. | 391 // Ensuring a retry happened and succeeded. |
366 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 392 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
367 CompleteFetch(); | 393 CompleteFetch(); |
368 | 394 |
369 EXPECT_TRUE(callback_called_); | 395 EXPECT_TRUE(callback_called_); |
370 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 396 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
371 EXPECT_EQ("2501", registration_id_); | 397 EXPECT_EQ("2501", registration_id_); |
372 } | 398 } |
373 | 399 |
374 TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { | 400 TEST_F(GCMRegistrationRequestTest, ResponseHttpNotOk) { |
375 CreateRequest("sender1,sender2"); | 401 CreateRequest("sender1,sender2"); |
376 request_->Start(); | 402 request_->Start(); |
377 | 403 |
378 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | 404 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); |
379 CompleteFetch(); | 405 CompleteFetch(); |
380 | 406 |
381 EXPECT_FALSE(callback_called_); | 407 EXPECT_FALSE(callback_called_); |
382 | 408 |
383 // Ensuring a retry happened and succeeded. | 409 // Ensuring a retry happened and succeeded. |
384 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | 410 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
385 CompleteFetch(); | 411 CompleteFetch(); |
386 | 412 |
387 EXPECT_TRUE(callback_called_); | 413 EXPECT_TRUE(callback_called_); |
388 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | 414 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
389 EXPECT_EQ("2501", registration_id_); | 415 EXPECT_EQ("2501", registration_id_); |
390 } | 416 } |
391 | 417 |
392 TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { | 418 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { |
393 set_max_retry_count(0); | 419 set_max_retry_count(0); |
394 CreateRequest("sender1,sender2"); | 420 CreateRequest("sender1,sender2"); |
395 request_->Start(); | 421 request_->Start(); |
396 | 422 |
397 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | 423 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); |
398 CompleteFetch(); | 424 CompleteFetch(); |
399 | 425 |
400 EXPECT_TRUE(callback_called_); | 426 EXPECT_TRUE(callback_called_); |
401 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); | 427 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); |
402 EXPECT_EQ(std::string(), registration_id_); | 428 EXPECT_EQ(std::string(), registration_id_); |
403 } | 429 } |
404 | 430 |
405 TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { | 431 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReached) { |
406 CreateRequest("sender1,sender2"); | 432 CreateRequest("sender1,sender2"); |
407 request_->Start(); | 433 request_->Start(); |
408 | 434 |
409 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | 435 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); |
410 CompleteFetch(); | 436 CompleteFetch(); |
411 | 437 |
412 EXPECT_FALSE(callback_called_); | 438 EXPECT_FALSE(callback_called_); |
413 | 439 |
414 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | 440 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); |
415 CompleteFetch(); | 441 CompleteFetch(); |
416 | 442 |
417 EXPECT_FALSE(callback_called_); | 443 EXPECT_FALSE(callback_called_); |
418 | 444 |
419 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | 445 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); |
420 CompleteFetch(); | 446 CompleteFetch(); |
421 | 447 |
422 EXPECT_TRUE(callback_called_); | 448 EXPECT_TRUE(callback_called_); |
423 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); | 449 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); |
424 EXPECT_EQ(std::string(), registration_id_); | 450 EXPECT_EQ(std::string(), registration_id_); |
425 } | 451 } |
426 | 452 |
| 453 class InstanceIDGetTokenRequestTest : public RegistrationRequestTest { |
| 454 public: |
| 455 InstanceIDGetTokenRequestTest(); |
| 456 ~InstanceIDGetTokenRequestTest() override; |
| 457 |
| 458 void CreateRequest(const std::string& instance_id, |
| 459 const std::string& authorized_entity, |
| 460 const std::string& scope); |
| 461 }; |
| 462 |
| 463 InstanceIDGetTokenRequestTest::InstanceIDGetTokenRequestTest() { |
| 464 } |
| 465 |
| 466 InstanceIDGetTokenRequestTest::~InstanceIDGetTokenRequestTest() { |
| 467 } |
| 468 |
| 469 void InstanceIDGetTokenRequestTest::CreateRequest( |
| 470 const std::string& instance_id, |
| 471 const std::string& authorized_entity, |
| 472 const std::string& scope) { |
| 473 scoped_ptr<RegistrationRequest::InstanceIDRequestInfo> request_info( |
| 474 new RegistrationRequest::InstanceIDRequestInfo); |
| 475 request_info->set_chrome_version(kChromeVersion); |
| 476 request_info->set_android_id(kAndroidId); |
| 477 request_info->set_security_token(kSecurityToken); |
| 478 request_info->set_app_id(kAppId); |
| 479 request_info->set_instance_id(instance_id); |
| 480 request_info->set_authorized_entity(authorized_entity); |
| 481 request_info->set_scope(scope); |
| 482 return CreateRequestFromRequestInfo(request_info.Pass()); |
| 483 } |
| 484 |
| 485 TEST_F(InstanceIDGetTokenRequestTest, RequestSuccessful) { |
| 486 set_max_retry_count(0); |
| 487 CreateRequest(kInstanceId, kDeveloperId, kScope); |
| 488 request_->Start(); |
| 489 |
| 490 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
| 491 CompleteFetch(); |
| 492 |
| 493 EXPECT_TRUE(callback_called_); |
| 494 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
| 495 EXPECT_EQ("2501", registration_id_); |
| 496 } |
| 497 TEST_F(InstanceIDGetTokenRequestTest, RequestDataAndURL) { |
| 498 CreateRequest(kInstanceId, kDeveloperId, kScope); |
| 499 request_->Start(); |
| 500 |
| 501 // Get data sent by request. |
| 502 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 503 ASSERT_TRUE(fetcher); |
| 504 |
| 505 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); |
| 506 |
| 507 // Verify that authorization header was put together properly. |
| 508 net::HttpRequestHeaders headers; |
| 509 fetcher->GetExtraRequestHeaders(&headers); |
| 510 std::string auth_header; |
| 511 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header); |
| 512 base::StringTokenizer auth_tokenizer(auth_header, " :"); |
| 513 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 514 EXPECT_EQ(kLoginHeader, auth_tokenizer.token()); |
| 515 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 516 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token()); |
| 517 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 518 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token()); |
| 519 |
| 520 std::map<std::string, std::string> expected_pairs; |
| 521 expected_pairs["gmsv"] = kChromeVersion; |
| 522 expected_pairs["app"] = kAppId; |
| 523 expected_pairs["sender"] = kDeveloperId; |
| 524 expected_pairs["device"] = base::Uint64ToString(kAndroidId); |
| 525 expected_pairs["appid"] = kInstanceId; |
| 526 expected_pairs["scope"] = kScope; |
| 527 |
| 528 // Verify data was formatted properly. |
| 529 std::string upload_data = fetcher->upload_data(); |
| 530 base::StringTokenizer data_tokenizer(upload_data, "&="); |
| 531 while (data_tokenizer.GetNext()) { |
| 532 std::map<std::string, std::string>::iterator iter = |
| 533 expected_pairs.find(data_tokenizer.token()); |
| 534 ASSERT_TRUE(iter != expected_pairs.end()); |
| 535 ASSERT_TRUE(data_tokenizer.GetNext()); |
| 536 EXPECT_EQ(iter->second, data_tokenizer.token()); |
| 537 // Ensure that none of the keys appears twice. |
| 538 expected_pairs.erase(iter); |
| 539 } |
| 540 |
| 541 EXPECT_EQ(0UL, expected_pairs.size()); |
| 542 } |
| 543 |
| 544 TEST_F(InstanceIDGetTokenRequestTest, ResponseHttpStatusNotOK) { |
| 545 CreateRequest(kInstanceId, kDeveloperId, kScope); |
| 546 request_->Start(); |
| 547 |
| 548 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); |
| 549 CompleteFetch(); |
| 550 |
| 551 EXPECT_FALSE(callback_called_); |
| 552 |
| 553 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
| 554 CompleteFetch(); |
| 555 |
| 556 EXPECT_TRUE(callback_called_); |
| 557 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
| 558 EXPECT_EQ("2501", registration_id_); |
| 559 } |
| 560 |
427 } // namespace gcm | 561 } // namespace gcm |
OLD | NEW |