| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "google_apis/gcm/engine/checkin_request.h" | 8 #include "google_apis/gcm/engine/checkin_request.h" |
| 9 #include "google_apis/gcm/protocol/checkin.pb.h" | 9 #include "google_apis/gcm/protocol/checkin.pb.h" |
| 10 #include "net/base/backoff_entry.h" | 10 #include "net/base/backoff_entry.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 false, | 44 false, |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 } | 47 } |
| 48 | 48 |
| 49 const uint64 kAndroidId = 42UL; | 49 const uint64 kAndroidId = 42UL; |
| 50 const uint64 kBlankAndroidId = 999999UL; | 50 const uint64 kBlankAndroidId = 999999UL; |
| 51 const uint64 kBlankSecurityToken = 999999UL; | 51 const uint64 kBlankSecurityToken = 999999UL; |
| 52 const char kChromeVersion[] = "Version String"; | 52 const char kChromeVersion[] = "Version String"; |
| 53 const uint64 kSecurityToken = 77; | 53 const uint64 kSecurityToken = 77; |
| 54 const char kSettingsDigest[] = "settings_digest"; |
| 54 | 55 |
| 55 class CheckinRequestTest : public testing::Test { | 56 class CheckinRequestTest : public testing::Test { |
| 56 public: | 57 public: |
| 57 enum ResponseScenario { | 58 enum ResponseScenario { |
| 58 VALID_RESPONSE, // Both android_id and security_token set in response. | 59 VALID_RESPONSE, // Both android_id and security_token set in response. |
| 59 MISSING_ANDROID_ID, // android_id is missing. | 60 MISSING_ANDROID_ID, // android_id is missing. |
| 60 MISSING_SECURITY_TOKEN, // security_token is missing. | 61 MISSING_SECURITY_TOKEN, // security_token is missing. |
| 61 ANDROID_ID_IS_ZER0, // android_id is 0. | 62 ANDROID_ID_IS_ZER0, // android_id is 0. |
| 62 SECURITY_TOKEN_IS_ZERO // security_token is 0. | 63 SECURITY_TOKEN_IS_ZERO // security_token is 0. |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 CheckinRequestTest(); | 66 CheckinRequestTest(); |
| 66 virtual ~CheckinRequestTest(); | 67 virtual ~CheckinRequestTest(); |
| 67 | 68 |
| 68 void FetcherCallback(uint64 android_id, uint64 security_token); | 69 void FetcherCallback( |
| 70 const checkin_proto::AndroidCheckinResponse& response); |
| 69 | 71 |
| 70 void CreateRequest(uint64 android_id, uint64 security_token); | 72 void CreateRequest(uint64 android_id, uint64 security_token); |
| 71 | 73 |
| 72 void SetResponseStatusAndString( | 74 void SetResponseStatusAndString( |
| 73 net::HttpStatusCode status_code, | 75 net::HttpStatusCode status_code, |
| 74 const std::string& response_data); | 76 const std::string& response_data); |
| 75 | 77 |
| 76 void CompleteFetch(); | 78 void CompleteFetch(); |
| 77 | 79 |
| 78 void SetResponse(ResponseScenario response_scenario); | 80 void SetResponse(ResponseScenario response_scenario); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 95 android_id_(kBlankAndroidId), | 97 android_id_(kBlankAndroidId), |
| 96 security_token_(kBlankSecurityToken), | 98 security_token_(kBlankSecurityToken), |
| 97 checkin_device_type_(0), | 99 checkin_device_type_(0), |
| 98 url_request_context_getter_(new net::TestURLRequestContextGetter( | 100 url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 99 message_loop_.message_loop_proxy())) { | 101 message_loop_.message_loop_proxy())) { |
| 100 account_ids_.push_back("account_id"); | 102 account_ids_.push_back("account_id"); |
| 101 } | 103 } |
| 102 | 104 |
| 103 CheckinRequestTest::~CheckinRequestTest() {} | 105 CheckinRequestTest::~CheckinRequestTest() {} |
| 104 | 106 |
| 105 void CheckinRequestTest::FetcherCallback(uint64 android_id, | 107 void CheckinRequestTest::FetcherCallback( |
| 106 uint64 security_token) { | 108 const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| 107 callback_called_ = true; | 109 callback_called_ = true; |
| 108 android_id_ = android_id; | 110 if (checkin_response.has_android_id()) |
| 109 security_token_ = security_token; | 111 android_id_ = checkin_response.android_id(); |
| 112 if (checkin_response.has_security_token()) |
| 113 security_token_ = checkin_response.security_token(); |
| 110 } | 114 } |
| 111 | 115 |
| 112 void CheckinRequestTest::CreateRequest(uint64 android_id, | 116 void CheckinRequestTest::CreateRequest(uint64 android_id, |
| 113 uint64 security_token) { | 117 uint64 security_token) { |
| 114 // First setup a chrome_build protobuf. | 118 // First setup a chrome_build protobuf. |
| 115 chrome_build_proto_.set_platform( | 119 chrome_build_proto_.set_platform( |
| 116 checkin_proto::ChromeBuildProto::PLATFORM_LINUX); | 120 checkin_proto::ChromeBuildProto::PLATFORM_LINUX); |
| 117 chrome_build_proto_.set_channel( | 121 chrome_build_proto_.set_channel( |
| 118 checkin_proto::ChromeBuildProto::CHANNEL_CANARY); | 122 checkin_proto::ChromeBuildProto::CHANNEL_CANARY); |
| 119 chrome_build_proto_.set_chrome_version(kChromeVersion); | 123 chrome_build_proto_.set_chrome_version(kChromeVersion); |
| 124 |
| 125 CheckinRequest::RequestInfo request_info( |
| 126 android_id, |
| 127 security_token, |
| 128 kSettingsDigest, |
| 129 account_ids_, |
| 130 chrome_build_proto_); |
| 120 // Then create a request with that protobuf and specified android_id, | 131 // Then create a request with that protobuf and specified android_id, |
| 121 // security_token. | 132 // security_token. |
| 122 request_.reset(new CheckinRequest( | 133 request_.reset(new CheckinRequest( |
| 134 request_info, |
| 135 kDefaultBackoffPolicy, |
| 123 base::Bind(&CheckinRequestTest::FetcherCallback, base::Unretained(this)), | 136 base::Bind(&CheckinRequestTest::FetcherCallback, base::Unretained(this)), |
| 124 kDefaultBackoffPolicy, | |
| 125 chrome_build_proto_, | |
| 126 android_id, | |
| 127 security_token, | |
| 128 account_ids_, | |
| 129 url_request_context_getter_.get())); | 137 url_request_context_getter_.get())); |
| 130 | 138 |
| 131 // Setting android_id_ and security_token_ to blank value, not used elsewhere | 139 // Setting android_id_ and security_token_ to blank value, not used elsewhere |
| 132 // in the tests. | 140 // in the tests. |
| 133 callback_called_ = false; | 141 callback_called_ = false; |
| 134 android_id_ = kBlankAndroidId; | 142 android_id_ = kBlankAndroidId; |
| 135 security_token_ = kBlankSecurityToken; | 143 security_token_ = kBlankSecurityToken; |
| 136 } | 144 } |
| 137 | 145 |
| 138 void CheckinRequestTest::SetResponseStatusAndString( | 146 void CheckinRequestTest::SetResponseStatusAndString( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 EXPECT_EQ(chrome_build_proto_.channel(), | 199 EXPECT_EQ(chrome_build_proto_.channel(), |
| 192 request_proto.checkin().chrome_build().channel()); | 200 request_proto.checkin().chrome_build().channel()); |
| 193 | 201 |
| 194 #if defined(CHROME_OS) | 202 #if defined(CHROME_OS) |
| 195 EXPECT_EQ(checkin_proto::DEVICE_CHROME_OS, request_proto.checkin().type()); | 203 EXPECT_EQ(checkin_proto::DEVICE_CHROME_OS, request_proto.checkin().type()); |
| 196 #else | 204 #else |
| 197 EXPECT_EQ(checkin_proto::DEVICE_CHROME_BROWSER, | 205 EXPECT_EQ(checkin_proto::DEVICE_CHROME_BROWSER, |
| 198 request_proto.checkin().type()); | 206 request_proto.checkin().type()); |
| 199 #endif | 207 #endif |
| 200 | 208 |
| 209 EXPECT_EQ(kSettingsDigest, request_proto.digest()); |
| 201 EXPECT_EQ(1, request_proto.account_cookie_size()); | 210 EXPECT_EQ(1, request_proto.account_cookie_size()); |
| 202 EXPECT_EQ("[account_id]", request_proto.account_cookie(0)); | 211 EXPECT_EQ("[account_id]", request_proto.account_cookie(0)); |
| 203 } | 212 } |
| 204 | 213 |
| 205 TEST_F(CheckinRequestTest, ResponseBodyEmpty) { | 214 TEST_F(CheckinRequestTest, ResponseBodyEmpty) { |
| 206 CreateRequest(0u, 0u); | 215 CreateRequest(0u, 0u); |
| 207 request_->Start(); | 216 request_->Start(); |
| 208 | 217 |
| 209 SetResponseStatusAndString(net::HTTP_OK, std::string()); | 218 SetResponseStatusAndString(net::HTTP_OK, std::string()); |
| 210 CompleteFetch(); | 219 CompleteFetch(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 237 } | 246 } |
| 238 | 247 |
| 239 TEST_F(CheckinRequestTest, ResponseHttpStatusUnauthorized) { | 248 TEST_F(CheckinRequestTest, ResponseHttpStatusUnauthorized) { |
| 240 CreateRequest(0u, 0u); | 249 CreateRequest(0u, 0u); |
| 241 request_->Start(); | 250 request_->Start(); |
| 242 | 251 |
| 243 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, std::string()); | 252 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, std::string()); |
| 244 CompleteFetch(); | 253 CompleteFetch(); |
| 245 | 254 |
| 246 EXPECT_TRUE(callback_called_); | 255 EXPECT_TRUE(callback_called_); |
| 247 EXPECT_EQ(0u, android_id_); | 256 EXPECT_EQ(kBlankAndroidId, android_id_); |
| 248 EXPECT_EQ(0u, security_token_); | 257 EXPECT_EQ(kBlankSecurityToken, security_token_); |
| 249 } | 258 } |
| 250 | 259 |
| 251 TEST_F(CheckinRequestTest, ResponseHttpStatusBadRequest) { | 260 TEST_F(CheckinRequestTest, ResponseHttpStatusBadRequest) { |
| 252 CreateRequest(0u, 0u); | 261 CreateRequest(0u, 0u); |
| 253 request_->Start(); | 262 request_->Start(); |
| 254 | 263 |
| 255 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, std::string()); | 264 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, std::string()); |
| 256 CompleteFetch(); | 265 CompleteFetch(); |
| 257 | 266 |
| 258 EXPECT_TRUE(callback_called_); | 267 EXPECT_TRUE(callback_called_); |
| 259 EXPECT_EQ(0u, android_id_); | 268 EXPECT_EQ(kBlankAndroidId, android_id_); |
| 260 EXPECT_EQ(0u, security_token_); | 269 EXPECT_EQ(kBlankSecurityToken, security_token_); |
| 261 } | 270 } |
| 262 | 271 |
| 263 TEST_F(CheckinRequestTest, ResponseHttpStatusNotOK) { | 272 TEST_F(CheckinRequestTest, ResponseHttpStatusNotOK) { |
| 264 CreateRequest(0u, 0u); | 273 CreateRequest(0u, 0u); |
| 265 request_->Start(); | 274 request_->Start(); |
| 266 | 275 |
| 267 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, std::string()); | 276 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, std::string()); |
| 268 CompleteFetch(); | 277 CompleteFetch(); |
| 269 | 278 |
| 270 EXPECT_FALSE(callback_called_); | 279 EXPECT_FALSE(callback_called_); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 372 |
| 364 SetResponse(VALID_RESPONSE); | 373 SetResponse(VALID_RESPONSE); |
| 365 CompleteFetch(); | 374 CompleteFetch(); |
| 366 | 375 |
| 367 EXPECT_TRUE(callback_called_); | 376 EXPECT_TRUE(callback_called_); |
| 368 EXPECT_EQ(kAndroidId, android_id_); | 377 EXPECT_EQ(kAndroidId, android_id_); |
| 369 EXPECT_EQ(kSecurityToken, security_token_); | 378 EXPECT_EQ(kSecurityToken, security_token_); |
| 370 } | 379 } |
| 371 | 380 |
| 372 } // namespace gcm | 381 } // namespace gcm |
| OLD | NEW |