Reland of Skip the Service Worker CORS fallback for same origin requests. [2/2 chromi...
[chromium-blink-merge.git] / google_apis / gcm / engine / checkin_request_unittest.cc
blob9939c2736c572fa2dc93a4629e135a269efa004f
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 <string>
7 #include "google_apis/gcm/engine/checkin_request.h"
8 #include "google_apis/gcm/engine/gcm_request_test_base.h"
9 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h"
10 #include "google_apis/gcm/protocol/checkin.pb.h"
12 namespace gcm {
14 const uint64 kAndroidId = 42UL;
15 const uint64 kBlankAndroidId = 999999UL;
16 const uint64 kBlankSecurityToken = 999999UL;
17 const char kCheckinURL[] = "http://foo.bar/checkin";
18 const char kChromeVersion[] = "Version String";
19 const uint64 kSecurityToken = 77;
20 const char kSettingsDigest[] = "settings_digest";
21 const char kEmailAddress[] = "test_user@gmail.com";
22 const char kTokenValue[] = "token_value";
24 class CheckinRequestTest : public GCMRequestTestBase {
25 public:
26 enum ResponseScenario {
27 VALID_RESPONSE, // Both android_id and security_token set in response.
28 MISSING_ANDROID_ID, // android_id is missing.
29 MISSING_SECURITY_TOKEN, // security_token is missing.
30 ANDROID_ID_IS_ZER0, // android_id is 0.
31 SECURITY_TOKEN_IS_ZERO // security_token is 0.
34 CheckinRequestTest();
35 ~CheckinRequestTest() override;
37 void FetcherCallback(
38 const checkin_proto::AndroidCheckinResponse& response);
40 void CreateRequest(uint64 android_id, uint64 security_token);
42 void SetResponseScenario(ResponseScenario response_scenario);
44 protected:
45 bool callback_called_;
46 uint64 android_id_;
47 uint64 security_token_;
48 int checkin_device_type_;
49 checkin_proto::ChromeBuildProto chrome_build_proto_;
50 scoped_ptr<CheckinRequest> request_;
51 FakeGCMStatsRecorder recorder_;
54 CheckinRequestTest::CheckinRequestTest()
55 : callback_called_(false),
56 android_id_(kBlankAndroidId),
57 security_token_(kBlankSecurityToken),
58 checkin_device_type_(0) {
61 CheckinRequestTest::~CheckinRequestTest() {}
63 void CheckinRequestTest::FetcherCallback(
64 const checkin_proto::AndroidCheckinResponse& checkin_response) {
65 callback_called_ = true;
66 if (checkin_response.has_android_id())
67 android_id_ = checkin_response.android_id();
68 if (checkin_response.has_security_token())
69 security_token_ = checkin_response.security_token();
72 void CheckinRequestTest::CreateRequest(uint64 android_id,
73 uint64 security_token) {
74 // First setup a chrome_build protobuf.
75 chrome_build_proto_.set_platform(
76 checkin_proto::ChromeBuildProto::PLATFORM_LINUX);
77 chrome_build_proto_.set_channel(
78 checkin_proto::ChromeBuildProto::CHANNEL_CANARY);
79 chrome_build_proto_.set_chrome_version(kChromeVersion);
81 std::map<std::string, std::string> account_tokens;
82 account_tokens[kEmailAddress] = kTokenValue;
84 CheckinRequest::RequestInfo request_info(android_id,
85 security_token,
86 account_tokens,
87 kSettingsDigest,
88 chrome_build_proto_);
89 // Then create a request with that protobuf and specified android_id,
90 // security_token.
91 request_.reset(new CheckinRequest(
92 GURL(kCheckinURL),
93 request_info,
94 GetBackoffPolicy(),
95 base::Bind(&CheckinRequestTest::FetcherCallback, base::Unretained(this)),
96 url_request_context_getter(),
97 &recorder_));
99 // Setting android_id_ and security_token_ to blank value, not used elsewhere
100 // in the tests.
101 callback_called_ = false;
102 android_id_ = kBlankAndroidId;
103 security_token_ = kBlankSecurityToken;
106 void CheckinRequestTest::SetResponseScenario(
107 ResponseScenario response_scenario) {
108 checkin_proto::AndroidCheckinResponse response;
109 response.set_stats_ok(true);
111 uint64 android_id = response_scenario == ANDROID_ID_IS_ZER0 ? 0 : kAndroidId;
112 uint64 security_token =
113 response_scenario == SECURITY_TOKEN_IS_ZERO ? 0 : kSecurityToken;
115 if (response_scenario != MISSING_ANDROID_ID)
116 response.set_android_id(android_id);
118 if (response_scenario != MISSING_SECURITY_TOKEN)
119 response.set_security_token(security_token);
121 std::string response_string;
122 response.SerializeToString(&response_string);
123 SetResponse(net::HTTP_OK, response_string);
126 TEST_F(CheckinRequestTest, FetcherDataAndURL) {
127 CreateRequest(kAndroidId, kSecurityToken);
128 request_->Start();
130 // Get data sent by request.
131 net::TestURLFetcher* fetcher = GetFetcher();
132 ASSERT_TRUE(fetcher);
133 EXPECT_EQ(GURL(kCheckinURL), fetcher->GetOriginalURL());
135 checkin_proto::AndroidCheckinRequest request_proto;
136 request_proto.ParseFromString(fetcher->upload_data());
137 EXPECT_EQ(kAndroidId, static_cast<uint64>(request_proto.id()));
138 EXPECT_EQ(kSecurityToken, request_proto.security_token());
139 EXPECT_EQ(chrome_build_proto_.platform(),
140 request_proto.checkin().chrome_build().platform());
141 EXPECT_EQ(chrome_build_proto_.chrome_version(),
142 request_proto.checkin().chrome_build().chrome_version());
143 EXPECT_EQ(chrome_build_proto_.channel(),
144 request_proto.checkin().chrome_build().channel());
145 EXPECT_EQ(2, request_proto.account_cookie_size());
146 EXPECT_EQ(kEmailAddress, request_proto.account_cookie(0));
147 EXPECT_EQ(kTokenValue, request_proto.account_cookie(1));
149 #if defined(CHROME_OS)
150 EXPECT_EQ(checkin_proto::DEVICE_CHROME_OS, request_proto.checkin().type());
151 #else
152 EXPECT_EQ(checkin_proto::DEVICE_CHROME_BROWSER,
153 request_proto.checkin().type());
154 #endif
156 EXPECT_EQ(kSettingsDigest, request_proto.digest());
159 TEST_F(CheckinRequestTest, ResponseBodyEmpty) {
160 CreateRequest(0u, 0u);
161 request_->Start();
163 SetResponse(net::HTTP_OK, std::string());
164 CompleteFetch();
166 EXPECT_FALSE(callback_called_);
168 SetResponseScenario(VALID_RESPONSE);
169 CompleteFetch();
171 EXPECT_TRUE(callback_called_);
172 EXPECT_EQ(kAndroidId, android_id_);
173 EXPECT_EQ(kSecurityToken, security_token_);
176 TEST_F(CheckinRequestTest, ResponseBodyCorrupted) {
177 CreateRequest(0u, 0u);
178 request_->Start();
180 SetResponse(net::HTTP_OK, "Corrupted response body");
181 CompleteFetch();
183 EXPECT_FALSE(callback_called_);
185 SetResponseScenario(VALID_RESPONSE);
186 CompleteFetch();
188 EXPECT_TRUE(callback_called_);
189 EXPECT_EQ(kAndroidId, android_id_);
190 EXPECT_EQ(kSecurityToken, security_token_);
193 TEST_F(CheckinRequestTest, ResponseHttpStatusUnauthorized) {
194 CreateRequest(0u, 0u);
195 request_->Start();
197 SetResponse(net::HTTP_UNAUTHORIZED, std::string());
198 CompleteFetch();
200 EXPECT_TRUE(callback_called_);
201 EXPECT_EQ(kBlankAndroidId, android_id_);
202 EXPECT_EQ(kBlankSecurityToken, security_token_);
205 TEST_F(CheckinRequestTest, ResponseHttpStatusBadRequest) {
206 CreateRequest(0u, 0u);
207 request_->Start();
209 SetResponse(net::HTTP_BAD_REQUEST, std::string());
210 CompleteFetch();
212 EXPECT_TRUE(callback_called_);
213 EXPECT_EQ(kBlankAndroidId, android_id_);
214 EXPECT_EQ(kBlankSecurityToken, security_token_);
217 TEST_F(CheckinRequestTest, ResponseHttpStatusNotOK) {
218 CreateRequest(0u, 0u);
219 request_->Start();
221 SetResponse(net::HTTP_INTERNAL_SERVER_ERROR, std::string());
222 CompleteFetch();
224 EXPECT_FALSE(callback_called_);
226 SetResponseScenario(VALID_RESPONSE);
227 CompleteFetch();
229 EXPECT_TRUE(callback_called_);
230 EXPECT_EQ(kAndroidId, android_id_);
231 EXPECT_EQ(kSecurityToken, security_token_);
234 TEST_F(CheckinRequestTest, ResponseMissingAndroidId) {
235 CreateRequest(0u, 0u);
236 request_->Start();
238 SetResponseScenario(MISSING_ANDROID_ID);
239 CompleteFetch();
241 EXPECT_FALSE(callback_called_);
243 SetResponseScenario(VALID_RESPONSE);
244 CompleteFetch();
246 EXPECT_TRUE(callback_called_);
247 EXPECT_EQ(kAndroidId, android_id_);
248 EXPECT_EQ(kSecurityToken, security_token_);
251 TEST_F(CheckinRequestTest, ResponseMissingSecurityToken) {
252 CreateRequest(0u, 0u);
253 request_->Start();
255 SetResponseScenario(MISSING_SECURITY_TOKEN);
256 CompleteFetch();
258 EXPECT_FALSE(callback_called_);
260 SetResponseScenario(VALID_RESPONSE);
261 CompleteFetch();
263 EXPECT_TRUE(callback_called_);
264 EXPECT_EQ(kAndroidId, android_id_);
265 EXPECT_EQ(kSecurityToken, security_token_);
268 TEST_F(CheckinRequestTest, AndroidIdEqualsZeroInResponse) {
269 CreateRequest(0u, 0u);
270 request_->Start();
272 SetResponseScenario(ANDROID_ID_IS_ZER0);
273 CompleteFetch();
275 EXPECT_FALSE(callback_called_);
277 SetResponseScenario(VALID_RESPONSE);
278 CompleteFetch();
280 EXPECT_TRUE(callback_called_);
281 EXPECT_EQ(kAndroidId, android_id_);
282 EXPECT_EQ(kSecurityToken, security_token_);
285 TEST_F(CheckinRequestTest, SecurityTokenEqualsZeroInResponse) {
286 CreateRequest(0u, 0u);
287 request_->Start();
289 SetResponseScenario(SECURITY_TOKEN_IS_ZERO);
290 CompleteFetch();
292 EXPECT_FALSE(callback_called_);
294 SetResponseScenario(VALID_RESPONSE);
295 CompleteFetch();
297 EXPECT_TRUE(callback_called_);
298 EXPECT_EQ(kAndroidId, android_id_);
299 EXPECT_EQ(kSecurityToken, security_token_);
302 TEST_F(CheckinRequestTest, SuccessfulFirstTimeCheckin) {
303 CreateRequest(0u, 0u);
304 request_->Start();
306 SetResponseScenario(VALID_RESPONSE);
307 CompleteFetch();
309 EXPECT_TRUE(callback_called_);
310 EXPECT_EQ(kAndroidId, android_id_);
311 EXPECT_EQ(kSecurityToken, security_token_);
314 TEST_F(CheckinRequestTest, SuccessfulSubsequentCheckin) {
315 CreateRequest(kAndroidId, kSecurityToken);
316 request_->Start();
318 SetResponseScenario(VALID_RESPONSE);
319 CompleteFetch();
321 EXPECT_TRUE(callback_called_);
322 EXPECT_EQ(kAndroidId, android_id_);
323 EXPECT_EQ(kSecurityToken, security_token_);
326 } // namespace gcm