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