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
3 // found in the LICENSE file.
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "google_apis/gcm/engine/registration_request.h"
12 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_status.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 const uint64 kAndroidId
= 42UL;
22 const char kAppId
[] = "TestAppId";
23 const char kDeveloperId
[] = "Project1";
24 const char kLoginHeader
[] = "AidLogin";
25 const char kRegistrationURL
[] = "http://foo.bar/register";
26 const uint64 kSecurityToken
= 77UL;
28 // Backoff policy for testing registration request.
29 const net::BackoffEntry::Policy kDefaultBackoffPolicy
= {
30 // Number of initial errors (in sequence) to ignore before applying
31 // exponential back-off rules.
32 // 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 // Initial delay for exponential back-off in ms.
39 // Factor by which the waiting time will be multiplied.
42 // Fuzzing percentage. ex: 10% will spread requests randomly
43 // between 90%-100% of the calculated time.
46 // Maximum amount of time we are willing to delay our request in ms.
47 1000 * 60 * 5, // 5 minutes.
49 // Time to keep an entry from being discarded even when it
50 // has no significant state, -1 to never discard.
53 // Don't use initial delay unless the last request was an error.
59 class RegistrationRequestTest
: public testing::Test
{
61 RegistrationRequestTest();
62 virtual ~RegistrationRequestTest();
64 void RegistrationCallback(RegistrationRequest::Status status
,
65 const std::string
& registration_id
);
67 void CreateRequest(const std::string
& sender_ids
);
68 void SetResponseStatusAndString(net::HttpStatusCode status_code
,
69 const std::string
& response_body
);
71 void set_max_retry_count(int max_retry_count
) {
72 max_retry_count_
= max_retry_count
;
77 RegistrationRequest::Status status_
;
78 std::string registration_id_
;
79 bool callback_called_
;
80 std::map
<std::string
, std::string
> extras_
;
81 scoped_ptr
<RegistrationRequest
> request_
;
82 base::MessageLoop message_loop_
;
83 net::TestURLFetcherFactory url_fetcher_factory_
;
84 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
85 GCMStatsRecorder recorder_
;
88 RegistrationRequestTest::RegistrationRequestTest()
89 : max_retry_count_(2),
90 status_(RegistrationRequest::SUCCESS
),
91 callback_called_(false),
92 url_request_context_getter_(new net::TestURLRequestContextGetter(
93 message_loop_
.message_loop_proxy())) {}
95 RegistrationRequestTest::~RegistrationRequestTest() {}
97 void RegistrationRequestTest::RegistrationCallback(
98 RegistrationRequest::Status status
,
99 const std::string
& registration_id
) {
101 registration_id_
= registration_id
;
102 callback_called_
= true;
105 void RegistrationRequestTest::CreateRequest(const std::string
& sender_ids
) {
106 std::vector
<std::string
> senders
;
107 base::StringTokenizer
tokenizer(sender_ids
, ",");
108 while (tokenizer
.GetNext())
109 senders
.push_back(tokenizer
.token());
111 request_
.reset(new RegistrationRequest(
112 GURL(kRegistrationURL
),
113 RegistrationRequest::RequestInfo(kAndroidId
,
117 kDefaultBackoffPolicy
,
118 base::Bind(&RegistrationRequestTest::RegistrationCallback
,
119 base::Unretained(this)),
121 url_request_context_getter_
.get(),
125 void RegistrationRequestTest::SetResponseStatusAndString(
126 net::HttpStatusCode status_code
,
127 const std::string
& response_body
) {
128 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
129 ASSERT_TRUE(fetcher
);
130 fetcher
->set_response_code(status_code
);
131 fetcher
->SetResponseString(response_body
);
134 void RegistrationRequestTest::CompleteFetch() {
135 registration_id_
.clear();
136 status_
= RegistrationRequest::SUCCESS
;
137 callback_called_
= false;
139 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
140 ASSERT_TRUE(fetcher
);
141 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
144 TEST_F(RegistrationRequestTest
, RequestSuccessful
) {
145 set_max_retry_count(0);
146 CreateRequest("sender1,sender2");
149 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
152 EXPECT_TRUE(callback_called_
);
153 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
154 EXPECT_EQ("2501", registration_id_
);
157 TEST_F(RegistrationRequestTest
, RequestDataAndURL
) {
158 CreateRequest(kDeveloperId
);
161 // Get data sent by request.
162 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
163 ASSERT_TRUE(fetcher
);
165 EXPECT_EQ(GURL(kRegistrationURL
), fetcher
->GetOriginalURL());
167 // Verify that authorization header was put together properly.
168 net::HttpRequestHeaders headers
;
169 fetcher
->GetExtraRequestHeaders(&headers
);
170 std::string auth_header
;
171 headers
.GetHeader(net::HttpRequestHeaders::kAuthorization
, &auth_header
);
172 base::StringTokenizer
auth_tokenizer(auth_header
, " :");
173 ASSERT_TRUE(auth_tokenizer
.GetNext());
174 EXPECT_EQ(kLoginHeader
, auth_tokenizer
.token());
175 ASSERT_TRUE(auth_tokenizer
.GetNext());
176 EXPECT_EQ(base::Uint64ToString(kAndroidId
), auth_tokenizer
.token());
177 ASSERT_TRUE(auth_tokenizer
.GetNext());
178 EXPECT_EQ(base::Uint64ToString(kSecurityToken
), auth_tokenizer
.token());
180 std::map
<std::string
, std::string
> expected_pairs
;
181 expected_pairs
["app"] = kAppId
;
182 expected_pairs
["sender"] = kDeveloperId
;
183 expected_pairs
["device"] = base::Uint64ToString(kAndroidId
);
185 // Verify data was formatted properly.
186 std::string upload_data
= fetcher
->upload_data();
187 base::StringTokenizer
data_tokenizer(upload_data
, "&=");
188 while (data_tokenizer
.GetNext()) {
189 std::map
<std::string
, std::string
>::iterator iter
=
190 expected_pairs
.find(data_tokenizer
.token());
191 ASSERT_TRUE(iter
!= expected_pairs
.end());
192 ASSERT_TRUE(data_tokenizer
.GetNext());
193 EXPECT_EQ(iter
->second
, data_tokenizer
.token());
194 // Ensure that none of the keys appears twice.
195 expected_pairs
.erase(iter
);
198 EXPECT_EQ(0UL, expected_pairs
.size());
201 TEST_F(RegistrationRequestTest
, RequestRegistrationWithMultipleSenderIds
) {
202 CreateRequest("sender1,sender2@gmail.com");
205 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
206 ASSERT_TRUE(fetcher
);
208 // Verify data was formatted properly.
209 std::string upload_data
= fetcher
->upload_data();
210 base::StringTokenizer
data_tokenizer(upload_data
, "&=");
212 // Skip all tokens until you hit entry for senders.
213 while (data_tokenizer
.GetNext() && data_tokenizer
.token() != "sender")
216 ASSERT_TRUE(data_tokenizer
.GetNext());
217 std::string
senders(net::UnescapeURLComponent(data_tokenizer
.token(),
218 net::UnescapeRule::URL_SPECIAL_CHARS
));
219 base::StringTokenizer
sender_tokenizer(senders
, ",");
220 ASSERT_TRUE(sender_tokenizer
.GetNext());
221 EXPECT_EQ("sender1", sender_tokenizer
.token());
222 ASSERT_TRUE(sender_tokenizer
.GetNext());
223 EXPECT_EQ("sender2@gmail.com", sender_tokenizer
.token());
226 TEST_F(RegistrationRequestTest
, ResponseParsing
) {
227 CreateRequest("sender1,sender2");
230 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
233 EXPECT_TRUE(callback_called_
);
234 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
235 EXPECT_EQ("2501", registration_id_
);
238 TEST_F(RegistrationRequestTest
, ResponseHttpStatusNotOK
) {
239 CreateRequest("sender1,sender2");
242 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
, "token=2501");
245 EXPECT_FALSE(callback_called_
);
247 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
250 EXPECT_TRUE(callback_called_
);
251 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
252 EXPECT_EQ("2501", registration_id_
);
255 TEST_F(RegistrationRequestTest
, ResponseMissingRegistrationId
) {
256 CreateRequest("sender1,sender2");
259 SetResponseStatusAndString(net::HTTP_OK
, "");
262 EXPECT_FALSE(callback_called_
);
264 SetResponseStatusAndString(net::HTTP_OK
, "some error in response");
267 EXPECT_FALSE(callback_called_
);
269 // Ensuring a retry happened and succeeds.
270 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
273 EXPECT_TRUE(callback_called_
);
274 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
275 EXPECT_EQ("2501", registration_id_
);
278 TEST_F(RegistrationRequestTest
, ResponseDeviceRegistrationError
) {
279 CreateRequest("sender1,sender2");
282 SetResponseStatusAndString(net::HTTP_OK
, "Error=PHONE_REGISTRATION_ERROR");
285 EXPECT_FALSE(callback_called_
);
287 // Ensuring a retry happened and succeeds.
288 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
291 EXPECT_TRUE(callback_called_
);
292 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
293 EXPECT_EQ("2501", registration_id_
);
296 TEST_F(RegistrationRequestTest
, ResponseAuthenticationError
) {
297 CreateRequest("sender1,sender2");
300 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
,
301 "Error=AUTHENTICATION_FAILED");
304 EXPECT_FALSE(callback_called_
);
306 // Ensuring a retry happened and succeeds.
307 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
310 EXPECT_TRUE(callback_called_
);
311 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
312 EXPECT_EQ("2501", registration_id_
);
315 TEST_F(RegistrationRequestTest
, ResponseInvalidParameters
) {
316 CreateRequest("sender1,sender2");
319 SetResponseStatusAndString(net::HTTP_OK
, "Error=INVALID_PARAMETERS");
322 EXPECT_TRUE(callback_called_
);
323 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS
, status_
);
324 EXPECT_EQ(std::string(), registration_id_
);
327 TEST_F(RegistrationRequestTest
, ResponseInvalidSender
) {
328 CreateRequest("sender1,sender2");
331 SetResponseStatusAndString(net::HTTP_OK
, "Error=INVALID_SENDER");
334 EXPECT_TRUE(callback_called_
);
335 EXPECT_EQ(RegistrationRequest::INVALID_SENDER
, status_
);
336 EXPECT_EQ(std::string(), registration_id_
);
339 TEST_F(RegistrationRequestTest
, ResponseInvalidSenderBadRequest
) {
340 CreateRequest("sender1");
343 SetResponseStatusAndString(net::HTTP_BAD_REQUEST
, "Error=INVALID_SENDER");
346 EXPECT_TRUE(callback_called_
);
347 EXPECT_EQ(RegistrationRequest::INVALID_SENDER
, status_
);
348 EXPECT_EQ(std::string(), registration_id_
);
351 TEST_F(RegistrationRequestTest
, RequestNotSuccessful
) {
352 CreateRequest("sender1,sender2");
355 net::URLRequestStatus
request_status(net::URLRequestStatus::FAILED
, 1);
356 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
357 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
358 ASSERT_TRUE(fetcher
);
359 fetcher
->set_status(request_status
);
363 EXPECT_FALSE(callback_called_
);
365 // Ensuring a retry happened and succeeded.
366 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
369 EXPECT_TRUE(callback_called_
);
370 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
371 EXPECT_EQ("2501", registration_id_
);
374 TEST_F(RegistrationRequestTest
, ResponseHttpNotOk
) {
375 CreateRequest("sender1,sender2");
378 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT
, "token=2501");
381 EXPECT_FALSE(callback_called_
);
383 // Ensuring a retry happened and succeeded.
384 SetResponseStatusAndString(net::HTTP_OK
, "token=2501");
387 EXPECT_TRUE(callback_called_
);
388 EXPECT_EQ(RegistrationRequest::SUCCESS
, status_
);
389 EXPECT_EQ("2501", registration_id_
);
392 TEST_F(RegistrationRequestTest
, MaximumAttemptsReachedWithZeroRetries
) {
393 set_max_retry_count(0);
394 CreateRequest("sender1,sender2");
397 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT
, "token=2501");
400 EXPECT_TRUE(callback_called_
);
401 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES
, status_
);
402 EXPECT_EQ(std::string(), registration_id_
);
405 TEST_F(RegistrationRequestTest
, MaximumAttemptsReached
) {
406 CreateRequest("sender1,sender2");
409 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT
, "token=2501");
412 EXPECT_FALSE(callback_called_
);
414 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT
, "token=2501");
417 EXPECT_FALSE(callback_called_
);
419 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT
, "token=2501");
422 EXPECT_TRUE(callback_called_
);
423 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES
, status_
);
424 EXPECT_EQ(std::string(), registration_id_
);