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.
8 #include "google_apis/gcm/engine/checkin_request.h"
9 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
10 #include "google_apis/gcm/protocol/checkin.pb.h"
11 #include "net/base/backoff_entry.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 const net::BackoffEntry::Policy kDefaultBackoffPolicy
= {
21 // Number of initial errors (in sequence) to ignore before applying
22 // exponential back-off rules.
23 // Explicitly set to 1 to skip the delay of the first Retry, as we are not
24 // trying to test the backoff itself, but rather the fact that retry happens.
27 // Initial delay for exponential back-off in ms.
30 // Factor by which the waiting time will be multiplied.
33 // Fuzzing percentage. ex: 10% will spread requests randomly
34 // between 90%-100% of the calculated time.
37 // Maximum amount of time we are willing to delay our request in ms.
38 1000 * 60 * 5, // 5 minutes.
40 // Time to keep an entry from being discarded even when it
41 // has no significant state, -1 to never discard.
44 // Don't use initial delay unless the last request was an error.
50 const uint64 kAndroidId
= 42UL;
51 const uint64 kBlankAndroidId
= 999999UL;
52 const uint64 kBlankSecurityToken
= 999999UL;
53 const char kCheckinURL
[] = "http://foo.bar/checkin";
54 const char kChromeVersion
[] = "Version String";
55 const uint64 kSecurityToken
= 77;
56 const char kSettingsDigest
[] = "settings_digest";
58 class CheckinRequestTest
: public testing::Test
{
60 enum ResponseScenario
{
61 VALID_RESPONSE
, // Both android_id and security_token set in response.
62 MISSING_ANDROID_ID
, // android_id is missing.
63 MISSING_SECURITY_TOKEN
, // security_token is missing.
64 ANDROID_ID_IS_ZER0
, // android_id is 0.
65 SECURITY_TOKEN_IS_ZERO
// security_token is 0.
69 virtual ~CheckinRequestTest();
72 const checkin_proto::AndroidCheckinResponse
& response
);
74 void CreateRequest(uint64 android_id
, uint64 security_token
);
76 void SetResponseStatusAndString(
77 net::HttpStatusCode status_code
,
78 const std::string
& response_data
);
82 void SetResponse(ResponseScenario response_scenario
);
85 bool callback_called_
;
87 uint64 security_token_
;
88 int checkin_device_type_
;
89 base::MessageLoop message_loop_
;
90 net::TestURLFetcherFactory url_fetcher_factory_
;
91 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
92 checkin_proto::ChromeBuildProto chrome_build_proto_
;
93 std::vector
<std::string
> account_ids_
;
94 scoped_ptr
<CheckinRequest
> request_
;
95 GCMStatsRecorder recorder_
;
98 CheckinRequestTest::CheckinRequestTest()
99 : callback_called_(false),
100 android_id_(kBlankAndroidId
),
101 security_token_(kBlankSecurityToken
),
102 checkin_device_type_(0),
103 url_request_context_getter_(new net::TestURLRequestContextGetter(
104 message_loop_
.message_loop_proxy())) {
105 account_ids_
.push_back("account_id");
108 CheckinRequestTest::~CheckinRequestTest() {}
110 void CheckinRequestTest::FetcherCallback(
111 const checkin_proto::AndroidCheckinResponse
& checkin_response
) {
112 callback_called_
= true;
113 if (checkin_response
.has_android_id())
114 android_id_
= checkin_response
.android_id();
115 if (checkin_response
.has_security_token())
116 security_token_
= checkin_response
.security_token();
119 void CheckinRequestTest::CreateRequest(uint64 android_id
,
120 uint64 security_token
) {
121 // First setup a chrome_build protobuf.
122 chrome_build_proto_
.set_platform(
123 checkin_proto::ChromeBuildProto::PLATFORM_LINUX
);
124 chrome_build_proto_
.set_channel(
125 checkin_proto::ChromeBuildProto::CHANNEL_CANARY
);
126 chrome_build_proto_
.set_chrome_version(kChromeVersion
);
128 CheckinRequest::RequestInfo
request_info(
133 chrome_build_proto_
);
134 // Then create a request with that protobuf and specified android_id,
136 request_
.reset(new CheckinRequest(
139 kDefaultBackoffPolicy
,
140 base::Bind(&CheckinRequestTest::FetcherCallback
, base::Unretained(this)),
141 url_request_context_getter_
.get(),
144 // Setting android_id_ and security_token_ to blank value, not used elsewhere
146 callback_called_
= false;
147 android_id_
= kBlankAndroidId
;
148 security_token_
= kBlankSecurityToken
;
151 void CheckinRequestTest::SetResponseStatusAndString(
152 net::HttpStatusCode status_code
,
153 const std::string
& response_data
) {
154 net::TestURLFetcher
* fetcher
=
155 url_fetcher_factory_
.GetFetcherByID(0);
156 ASSERT_TRUE(fetcher
);
157 fetcher
->set_response_code(status_code
);
158 fetcher
->SetResponseString(response_data
);
161 void CheckinRequestTest::CompleteFetch() {
162 net::TestURLFetcher
* fetcher
=
163 url_fetcher_factory_
.GetFetcherByID(0);
164 ASSERT_TRUE(fetcher
);
165 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
168 void CheckinRequestTest::SetResponse(ResponseScenario response_scenario
) {
169 checkin_proto::AndroidCheckinResponse response
;
170 response
.set_stats_ok(true);
172 uint64 android_id
= response_scenario
== ANDROID_ID_IS_ZER0
? 0 : kAndroidId
;
173 uint64 security_token
=
174 response_scenario
== SECURITY_TOKEN_IS_ZERO
? 0 : kSecurityToken
;
176 if (response_scenario
!= MISSING_ANDROID_ID
)
177 response
.set_android_id(android_id
);
179 if (response_scenario
!= MISSING_SECURITY_TOKEN
)
180 response
.set_security_token(security_token
);
182 std::string response_string
;
183 response
.SerializeToString(&response_string
);
184 SetResponseStatusAndString(net::HTTP_OK
, response_string
);
187 TEST_F(CheckinRequestTest
, FetcherDataAndURL
) {
188 CreateRequest(kAndroidId
, kSecurityToken
);
191 // Get data sent by request.
192 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
193 ASSERT_TRUE(fetcher
);
194 EXPECT_EQ(GURL(kCheckinURL
), fetcher
->GetOriginalURL());
196 checkin_proto::AndroidCheckinRequest request_proto
;
197 request_proto
.ParseFromString(fetcher
->upload_data());
198 EXPECT_EQ(kAndroidId
, static_cast<uint64
>(request_proto
.id()));
199 EXPECT_EQ(kSecurityToken
, request_proto
.security_token());
200 EXPECT_EQ(chrome_build_proto_
.platform(),
201 request_proto
.checkin().chrome_build().platform());
202 EXPECT_EQ(chrome_build_proto_
.chrome_version(),
203 request_proto
.checkin().chrome_build().chrome_version());
204 EXPECT_EQ(chrome_build_proto_
.channel(),
205 request_proto
.checkin().chrome_build().channel());
207 #if defined(CHROME_OS)
208 EXPECT_EQ(checkin_proto::DEVICE_CHROME_OS
, request_proto
.checkin().type());
210 EXPECT_EQ(checkin_proto::DEVICE_CHROME_BROWSER
,
211 request_proto
.checkin().type());
214 EXPECT_EQ(kSettingsDigest
, request_proto
.digest());
215 EXPECT_EQ(1, request_proto
.account_cookie_size());
216 EXPECT_EQ("[account_id]", request_proto
.account_cookie(0));
219 TEST_F(CheckinRequestTest
, ResponseBodyEmpty
) {
220 CreateRequest(0u, 0u);
223 SetResponseStatusAndString(net::HTTP_OK
, std::string());
226 EXPECT_FALSE(callback_called_
);
228 SetResponse(VALID_RESPONSE
);
231 EXPECT_TRUE(callback_called_
);
232 EXPECT_EQ(kAndroidId
, android_id_
);
233 EXPECT_EQ(kSecurityToken
, security_token_
);
236 TEST_F(CheckinRequestTest
, ResponseBodyCorrupted
) {
237 CreateRequest(0u, 0u);
240 SetResponseStatusAndString(net::HTTP_OK
, "Corrupted response body");
243 EXPECT_FALSE(callback_called_
);
245 SetResponse(VALID_RESPONSE
);
248 EXPECT_TRUE(callback_called_
);
249 EXPECT_EQ(kAndroidId
, android_id_
);
250 EXPECT_EQ(kSecurityToken
, security_token_
);
253 TEST_F(CheckinRequestTest
, ResponseHttpStatusUnauthorized
) {
254 CreateRequest(0u, 0u);
257 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
, std::string());
260 EXPECT_TRUE(callback_called_
);
261 EXPECT_EQ(kBlankAndroidId
, android_id_
);
262 EXPECT_EQ(kBlankSecurityToken
, security_token_
);
265 TEST_F(CheckinRequestTest
, ResponseHttpStatusBadRequest
) {
266 CreateRequest(0u, 0u);
269 SetResponseStatusAndString(net::HTTP_BAD_REQUEST
, std::string());
272 EXPECT_TRUE(callback_called_
);
273 EXPECT_EQ(kBlankAndroidId
, android_id_
);
274 EXPECT_EQ(kBlankSecurityToken
, security_token_
);
277 TEST_F(CheckinRequestTest
, ResponseHttpStatusNotOK
) {
278 CreateRequest(0u, 0u);
281 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR
, std::string());
284 EXPECT_FALSE(callback_called_
);
286 SetResponse(VALID_RESPONSE
);
289 EXPECT_TRUE(callback_called_
);
290 EXPECT_EQ(kAndroidId
, android_id_
);
291 EXPECT_EQ(kSecurityToken
, security_token_
);
294 TEST_F(CheckinRequestTest
, ResponseMissingAndroidId
) {
295 CreateRequest(0u, 0u);
298 SetResponse(MISSING_ANDROID_ID
);
301 EXPECT_FALSE(callback_called_
);
303 SetResponse(VALID_RESPONSE
);
306 EXPECT_TRUE(callback_called_
);
307 EXPECT_EQ(kAndroidId
, android_id_
);
308 EXPECT_EQ(kSecurityToken
, security_token_
);
311 TEST_F(CheckinRequestTest
, ResponseMissingSecurityToken
) {
312 CreateRequest(0u, 0u);
315 SetResponse(MISSING_SECURITY_TOKEN
);
318 EXPECT_FALSE(callback_called_
);
320 SetResponse(VALID_RESPONSE
);
323 EXPECT_TRUE(callback_called_
);
324 EXPECT_EQ(kAndroidId
, android_id_
);
325 EXPECT_EQ(kSecurityToken
, security_token_
);
328 TEST_F(CheckinRequestTest
, AndroidIdEqualsZeroInResponse
) {
329 CreateRequest(0u, 0u);
332 SetResponse(ANDROID_ID_IS_ZER0
);
335 EXPECT_FALSE(callback_called_
);
337 SetResponse(VALID_RESPONSE
);
340 EXPECT_TRUE(callback_called_
);
341 EXPECT_EQ(kAndroidId
, android_id_
);
342 EXPECT_EQ(kSecurityToken
, security_token_
);
345 TEST_F(CheckinRequestTest
, SecurityTokenEqualsZeroInResponse
) {
346 CreateRequest(0u, 0u);
349 SetResponse(SECURITY_TOKEN_IS_ZERO
);
352 EXPECT_FALSE(callback_called_
);
354 SetResponse(VALID_RESPONSE
);
357 EXPECT_TRUE(callback_called_
);
358 EXPECT_EQ(kAndroidId
, android_id_
);
359 EXPECT_EQ(kSecurityToken
, security_token_
);
362 TEST_F(CheckinRequestTest
, SuccessfulFirstTimeCheckin
) {
363 CreateRequest(0u, 0u);
366 SetResponse(VALID_RESPONSE
);
369 EXPECT_TRUE(callback_called_
);
370 EXPECT_EQ(kAndroidId
, android_id_
);
371 EXPECT_EQ(kSecurityToken
, security_token_
);
374 TEST_F(CheckinRequestTest
, SuccessfulSubsequentCheckin
) {
375 CreateRequest(kAndroidId
, kSecurityToken
);
378 SetResponse(VALID_RESPONSE
);
381 EXPECT_TRUE(callback_called_
);
382 EXPECT_EQ(kAndroidId
, android_id_
);
383 EXPECT_EQ(kSecurityToken
, security_token_
);