[AiS] [Views] adding stock source to AiS.
[chromium-blink-merge.git] / google_apis / gcm / engine / unregistration_request_unittest.cc
blob8d1bb33f5568faba2aa32f8f4334be5b66332cfd
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.
5 #include <map>
6 #include <string>
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h"
12 #include "google_apis/gcm/engine/instance_id_delete_token_request_handler.h"
13 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h"
14 #include "net/base/load_flags.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace gcm {
21 namespace {
22 const int kMaxRetries = 2;
23 const uint64 kAndroidId = 42UL;
24 const char kLoginHeader[] = "AidLogin";
25 const char kAppId[] = "TestAppId";
26 const char kDeletedAppId[] = "deleted=TestAppId";
27 const char kDeletedToken[] = "token=SomeToken";
28 const char kRegistrationURL[] = "http://foo.bar/register";
29 const uint64 kSecurityToken = 77UL;
30 const int kGCMVersion = 40;
31 const char kInstanceId[] = "IID1";
32 const char kDeveloperId[] = "Project1";
33 const char kScope[] = "GCM";
35 // Backoff policy for testing registration request.
36 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
37 // Number of initial errors (in sequence) to ignore before applying
38 // exponential back-off rules.
39 // Explicitly set to 2 to skip the delay on the first retry, as we are not
40 // trying to test the backoff itself, but rather the fact that retry happens.
43 // Initial delay for exponential back-off in ms.
44 15000, // 15 seconds.
46 // Factor by which the waiting time will be multiplied.
49 // Fuzzing percentage. ex: 10% will spread requests randomly
50 // between 90%-100% of the calculated time.
51 0.5, // 50%.
53 // Maximum amount of time we are willing to delay our request in ms.
54 1000 * 60 * 5, // 5 minutes.
56 // Time to keep an entry from being discarded even when it
57 // has no significant state, -1 to never discard.
58 -1,
60 // Don't use initial delay unless the last request was an error.
61 false,
63 } // namespace
65 class UnregistrationRequestTest : public testing::Test {
66 public:
67 UnregistrationRequestTest();
68 ~UnregistrationRequestTest() override;
70 void UnregistrationCallback(UnregistrationRequest::Status status);
72 void SetResponseStatusAndString(net::HttpStatusCode status_code,
73 const std::string& response_body);
74 void CompleteFetch();
76 int max_retry_count() const { return max_retry_count_; }
77 void set_max_retry_count(int max_retry_count) {
78 max_retry_count_ = max_retry_count;
81 protected:
82 int max_retry_count_;
83 bool callback_called_;
84 UnregistrationRequest::Status status_;
85 scoped_ptr<UnregistrationRequest> request_;
86 base::MessageLoop message_loop_;
87 net::TestURLFetcherFactory url_fetcher_factory_;
88 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
89 FakeGCMStatsRecorder recorder_;
92 UnregistrationRequestTest::UnregistrationRequestTest()
93 : max_retry_count_(kMaxRetries),
94 callback_called_(false),
95 status_(UnregistrationRequest::UNREGISTRATION_STATUS_COUNT),
96 url_request_context_getter_(new net::TestURLRequestContextGetter(
97 message_loop_.task_runner())) {}
99 UnregistrationRequestTest::~UnregistrationRequestTest() {}
101 void UnregistrationRequestTest::UnregistrationCallback(
102 UnregistrationRequest::Status status) {
103 callback_called_ = true;
104 status_ = status;
107 void UnregistrationRequestTest::SetResponseStatusAndString(
108 net::HttpStatusCode status_code,
109 const std::string& response_body) {
110 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
111 ASSERT_TRUE(fetcher);
112 fetcher->set_response_code(status_code);
113 fetcher->SetResponseString(response_body);
116 void UnregistrationRequestTest::CompleteFetch() {
117 status_ = UnregistrationRequest::UNREGISTRATION_STATUS_COUNT;
118 callback_called_ = false;
119 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
120 ASSERT_TRUE(fetcher);
121 fetcher->delegate()->OnURLFetchComplete(fetcher);
124 class GCMUnregistrationRequestTest : public UnregistrationRequestTest {
125 public:
126 GCMUnregistrationRequestTest();
127 ~GCMUnregistrationRequestTest() override;
129 void CreateRequest();
132 GCMUnregistrationRequestTest::GCMUnregistrationRequestTest() {
135 GCMUnregistrationRequestTest::~GCMUnregistrationRequestTest() {
138 void GCMUnregistrationRequestTest::CreateRequest() {
139 UnregistrationRequest::RequestInfo request_info(
140 kAndroidId, kSecurityToken, kAppId);
141 scoped_ptr<GCMUnregistrationRequestHandler> request_handler(
142 new GCMUnregistrationRequestHandler(kAppId));
143 request_.reset(new UnregistrationRequest(
144 GURL(kRegistrationURL),
145 request_info,
146 request_handler.Pass(),
147 kDefaultBackoffPolicy,
148 base::Bind(&UnregistrationRequestTest::UnregistrationCallback,
149 base::Unretained(this)),
150 max_retry_count_,
151 url_request_context_getter_.get(),
152 &recorder_,
153 std::string()));
156 TEST_F(GCMUnregistrationRequestTest, RequestDataPassedToFetcher) {
157 CreateRequest();
158 request_->Start();
160 // Get data sent by request.
161 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
162 ASSERT_TRUE(fetcher);
164 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
166 int flags = fetcher->GetLoadFlags();
167 EXPECT_TRUE(flags & net::LOAD_DO_NOT_SEND_COOKIES);
168 EXPECT_TRUE(flags & net::LOAD_DO_NOT_SAVE_COOKIES);
170 // Verify that authorization header was put together properly.
171 net::HttpRequestHeaders headers;
172 fetcher->GetExtraRequestHeaders(&headers);
173 std::string auth_header;
174 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
175 base::StringTokenizer auth_tokenizer(auth_header, " :");
176 ASSERT_TRUE(auth_tokenizer.GetNext());
177 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
178 ASSERT_TRUE(auth_tokenizer.GetNext());
179 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
180 ASSERT_TRUE(auth_tokenizer.GetNext());
181 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
182 std::string app_id_header;
183 headers.GetHeader("app", &app_id_header);
184 EXPECT_EQ(kAppId, app_id_header);
186 std::map<std::string, std::string> expected_pairs;
187 expected_pairs["app"] = kAppId;
188 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
189 expected_pairs["delete"] = "true";
190 expected_pairs["gcm_unreg_caller"] = "false";
192 // Verify data was formatted properly.
193 std::string upload_data = fetcher->upload_data();
194 base::StringTokenizer data_tokenizer(upload_data, "&=");
195 while (data_tokenizer.GetNext()) {
196 std::map<std::string, std::string>::iterator iter =
197 expected_pairs.find(data_tokenizer.token());
198 ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token();
199 ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token();
200 EXPECT_EQ(iter->second, data_tokenizer.token());
201 // Ensure that none of the keys appears twice.
202 expected_pairs.erase(iter);
205 EXPECT_EQ(0UL, expected_pairs.size());
208 TEST_F(GCMUnregistrationRequestTest, SuccessfulUnregistration) {
209 set_max_retry_count(0);
210 CreateRequest();
211 request_->Start();
213 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
214 CompleteFetch();
216 EXPECT_TRUE(callback_called_);
217 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
220 TEST_F(GCMUnregistrationRequestTest, ResponseHttpStatusNotOK) {
221 CreateRequest();
222 request_->Start();
224 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
225 CompleteFetch();
227 EXPECT_FALSE(callback_called_);
229 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
230 CompleteFetch();
232 EXPECT_TRUE(callback_called_);
233 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
236 TEST_F(GCMUnregistrationRequestTest, ResponseEmpty) {
237 CreateRequest();
238 request_->Start();
240 SetResponseStatusAndString(net::HTTP_OK, "");
241 CompleteFetch();
243 EXPECT_FALSE(callback_called_);
245 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
246 CompleteFetch();
248 EXPECT_TRUE(callback_called_);
249 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
252 TEST_F(GCMUnregistrationRequestTest, InvalidParametersError) {
253 CreateRequest();
254 request_->Start();
256 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
257 CompleteFetch();
259 EXPECT_TRUE(callback_called_);
260 EXPECT_EQ(UnregistrationRequest::INVALID_PARAMETERS, status_);
263 TEST_F(GCMUnregistrationRequestTest, UnkwnownError) {
264 CreateRequest();
265 request_->Start();
267 SetResponseStatusAndString(net::HTTP_OK, "Error=XXX");
268 CompleteFetch();
270 EXPECT_TRUE(callback_called_);
271 EXPECT_EQ(UnregistrationRequest::UNKNOWN_ERROR, status_);
274 TEST_F(GCMUnregistrationRequestTest, ServiceUnavailable) {
275 CreateRequest();
276 request_->Start();
278 SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, "");
279 CompleteFetch();
281 EXPECT_FALSE(callback_called_);
283 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
284 CompleteFetch();
286 EXPECT_TRUE(callback_called_);
287 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
290 TEST_F(GCMUnregistrationRequestTest, InternalServerError) {
291 CreateRequest();
292 request_->Start();
294 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, "");
295 CompleteFetch();
297 EXPECT_FALSE(callback_called_);
299 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
300 CompleteFetch();
302 EXPECT_TRUE(callback_called_);
303 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
306 TEST_F(GCMUnregistrationRequestTest, IncorrectAppId) {
307 CreateRequest();
308 request_->Start();
310 SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId");
311 CompleteFetch();
313 EXPECT_FALSE(callback_called_);
315 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
316 CompleteFetch();
318 EXPECT_TRUE(callback_called_);
319 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
322 TEST_F(GCMUnregistrationRequestTest, ResponseParsingFailed) {
323 CreateRequest();
324 request_->Start();
326 SetResponseStatusAndString(net::HTTP_OK, "some malformed response");
327 CompleteFetch();
329 EXPECT_FALSE(callback_called_);
331 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
332 CompleteFetch();
334 EXPECT_TRUE(callback_called_);
335 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
338 TEST_F(GCMUnregistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) {
339 set_max_retry_count(0);
340 CreateRequest();
341 request_->Start();
343 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "bad response");
344 CompleteFetch();
346 EXPECT_TRUE(callback_called_);
347 EXPECT_EQ(UnregistrationRequest::REACHED_MAX_RETRIES, status_);
350 TEST_F(GCMUnregistrationRequestTest, MaximumAttemptsReached) {
351 CreateRequest();
352 request_->Start();
354 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "bad response");
355 CompleteFetch();
357 EXPECT_FALSE(callback_called_);
359 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "bad response");
360 CompleteFetch();
362 EXPECT_FALSE(callback_called_);
364 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "bad response");
365 CompleteFetch();
367 EXPECT_TRUE(callback_called_);
368 EXPECT_EQ(UnregistrationRequest::REACHED_MAX_RETRIES, status_);
371 class InstaceIDDeleteTokenRequestTest : public UnregistrationRequestTest {
372 public:
373 InstaceIDDeleteTokenRequestTest();
374 ~InstaceIDDeleteTokenRequestTest() override;
376 void CreateRequest(const std::string& instance_id,
377 const std::string& authorized_entity,
378 const std::string& scope);
381 InstaceIDDeleteTokenRequestTest::InstaceIDDeleteTokenRequestTest() {
384 InstaceIDDeleteTokenRequestTest::~InstaceIDDeleteTokenRequestTest() {
387 void InstaceIDDeleteTokenRequestTest::CreateRequest(
388 const std::string& instance_id,
389 const std::string& authorized_entity,
390 const std::string& scope) {
391 UnregistrationRequest::RequestInfo request_info(
392 kAndroidId, kSecurityToken, kAppId);
393 scoped_ptr<InstanceIDDeleteTokenRequestHandler> request_handler(
394 new InstanceIDDeleteTokenRequestHandler(
395 instance_id, authorized_entity, scope, kGCMVersion));
396 request_.reset(new UnregistrationRequest(
397 GURL(kRegistrationURL),
398 request_info,
399 request_handler.Pass(),
400 kDefaultBackoffPolicy,
401 base::Bind(&UnregistrationRequestTest::UnregistrationCallback,
402 base::Unretained(this)),
403 max_retry_count(),
404 url_request_context_getter_.get(),
405 &recorder_,
406 std::string()));
409 TEST_F(InstaceIDDeleteTokenRequestTest, RequestDataPassedToFetcher) {
410 CreateRequest(kInstanceId, kDeveloperId, kScope);
411 request_->Start();
413 // Get data sent by request.
414 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
415 ASSERT_TRUE(fetcher);
417 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
419 // Verify that authorization header was put together properly.
420 net::HttpRequestHeaders headers;
421 fetcher->GetExtraRequestHeaders(&headers);
422 std::string auth_header;
423 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
424 base::StringTokenizer auth_tokenizer(auth_header, " :");
425 ASSERT_TRUE(auth_tokenizer.GetNext());
426 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
427 ASSERT_TRUE(auth_tokenizer.GetNext());
428 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
429 ASSERT_TRUE(auth_tokenizer.GetNext());
430 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
431 std::string app_id_header;
432 headers.GetHeader("app", &app_id_header);
433 EXPECT_EQ(kAppId, app_id_header);
435 std::map<std::string, std::string> expected_pairs;
436 expected_pairs["gmsv"] = base::IntToString(kGCMVersion);
437 expected_pairs["app"] = kAppId;
438 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
439 expected_pairs["delete"] = "true";
440 expected_pairs["appid"] = kInstanceId;
441 expected_pairs["sender"] = kDeveloperId;
442 expected_pairs["X-subtype"] = kDeveloperId;
443 expected_pairs["scope"] = kScope;
445 // Verify data was formatted properly.
446 std::string upload_data = fetcher->upload_data();
447 base::StringTokenizer data_tokenizer(upload_data, "&=");
448 while (data_tokenizer.GetNext()) {
449 std::map<std::string, std::string>::iterator iter =
450 expected_pairs.find(data_tokenizer.token());
451 ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token();
452 ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token();
453 EXPECT_EQ(iter->second, data_tokenizer.token());
454 // Ensure that none of the keys appears twice.
455 expected_pairs.erase(iter);
458 EXPECT_EQ(0UL, expected_pairs.size());
461 TEST_F(InstaceIDDeleteTokenRequestTest, SuccessfulUnregistration) {
462 CreateRequest(kInstanceId, kDeveloperId, kScope);
463 request_->Start();
465 SetResponseStatusAndString(net::HTTP_OK, kDeletedToken);
466 CompleteFetch();
468 EXPECT_TRUE(callback_called_);
469 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
472 TEST_F(InstaceIDDeleteTokenRequestTest, ResponseHttpStatusNotOK) {
473 CreateRequest(kInstanceId, kDeveloperId, kScope);
474 request_->Start();
476 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
477 CompleteFetch();
479 EXPECT_FALSE(callback_called_);
481 SetResponseStatusAndString(net::HTTP_OK, kDeletedToken);
482 CompleteFetch();
484 EXPECT_TRUE(callback_called_);
485 EXPECT_EQ(UnregistrationRequest::SUCCESS, status_);
488 TEST_F(InstaceIDDeleteTokenRequestTest, InvalidParametersError) {
489 CreateRequest(kInstanceId, kDeveloperId, kScope);
490 request_->Start();
492 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
493 CompleteFetch();
495 EXPECT_TRUE(callback_called_);
496 EXPECT_EQ(UnregistrationRequest::INVALID_PARAMETERS, status_);
499 TEST_F(InstaceIDDeleteTokenRequestTest, UnkwnownError) {
500 CreateRequest(kInstanceId, kDeveloperId, kScope);
501 request_->Start();
503 SetResponseStatusAndString(net::HTTP_OK, "Error=XXX");
504 CompleteFetch();
506 EXPECT_TRUE(callback_called_);
507 EXPECT_EQ(UnregistrationRequest::UNKNOWN_ERROR, status_);
510 } // namespace gcm