| 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 <stdint.h> |
| 6 |
| 5 #include <string> | 7 #include <string> |
| 6 | 8 |
| 7 #include "google_apis/gcm/engine/checkin_request.h" | 9 #include "google_apis/gcm/engine/checkin_request.h" |
| 8 #include "google_apis/gcm/engine/gcm_request_test_base.h" | 10 #include "google_apis/gcm/engine/gcm_request_test_base.h" |
| 9 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" | 11 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" |
| 10 #include "google_apis/gcm/protocol/checkin.pb.h" | 12 #include "google_apis/gcm/protocol/checkin.pb.h" |
| 11 | 13 |
| 12 namespace gcm { | 14 namespace gcm { |
| 13 | 15 |
| 14 const uint64 kAndroidId = 42UL; | 16 const uint64_t kAndroidId = 42UL; |
| 15 const uint64 kBlankAndroidId = 999999UL; | 17 const uint64_t kBlankAndroidId = 999999UL; |
| 16 const uint64 kBlankSecurityToken = 999999UL; | 18 const uint64_t kBlankSecurityToken = 999999UL; |
| 17 const char kCheckinURL[] = "http://foo.bar/checkin"; | 19 const char kCheckinURL[] = "http://foo.bar/checkin"; |
| 18 const char kChromeVersion[] = "Version String"; | 20 const char kChromeVersion[] = "Version String"; |
| 19 const uint64 kSecurityToken = 77; | 21 const uint64_t kSecurityToken = 77; |
| 20 const char kSettingsDigest[] = "settings_digest"; | 22 const char kSettingsDigest[] = "settings_digest"; |
| 21 const char kEmailAddress[] = "test_user@gmail.com"; | 23 const char kEmailAddress[] = "test_user@gmail.com"; |
| 22 const char kTokenValue[] = "token_value"; | 24 const char kTokenValue[] = "token_value"; |
| 23 | 25 |
| 24 class CheckinRequestTest : public GCMRequestTestBase { | 26 class CheckinRequestTest : public GCMRequestTestBase { |
| 25 public: | 27 public: |
| 26 enum ResponseScenario { | 28 enum ResponseScenario { |
| 27 VALID_RESPONSE, // Both android_id and security_token set in response. | 29 VALID_RESPONSE, // Both android_id and security_token set in response. |
| 28 MISSING_ANDROID_ID, // android_id is missing. | 30 MISSING_ANDROID_ID, // android_id is missing. |
| 29 MISSING_SECURITY_TOKEN, // security_token is missing. | 31 MISSING_SECURITY_TOKEN, // security_token is missing. |
| 30 ANDROID_ID_IS_ZER0, // android_id is 0. | 32 ANDROID_ID_IS_ZER0, // android_id is 0. |
| 31 SECURITY_TOKEN_IS_ZERO // security_token is 0. | 33 SECURITY_TOKEN_IS_ZERO // security_token is 0. |
| 32 }; | 34 }; |
| 33 | 35 |
| 34 CheckinRequestTest(); | 36 CheckinRequestTest(); |
| 35 ~CheckinRequestTest() override; | 37 ~CheckinRequestTest() override; |
| 36 | 38 |
| 37 void FetcherCallback( | 39 void FetcherCallback( |
| 38 const checkin_proto::AndroidCheckinResponse& response); | 40 const checkin_proto::AndroidCheckinResponse& response); |
| 39 | 41 |
| 40 void CreateRequest(uint64 android_id, uint64 security_token); | 42 void CreateRequest(uint64_t android_id, uint64_t security_token); |
| 41 | 43 |
| 42 void SetResponseScenario(ResponseScenario response_scenario); | 44 void SetResponseScenario(ResponseScenario response_scenario); |
| 43 | 45 |
| 44 protected: | 46 protected: |
| 45 bool callback_called_; | 47 bool callback_called_; |
| 46 uint64 android_id_; | 48 uint64_t android_id_; |
| 47 uint64 security_token_; | 49 uint64_t security_token_; |
| 48 int checkin_device_type_; | 50 int checkin_device_type_; |
| 49 checkin_proto::ChromeBuildProto chrome_build_proto_; | 51 checkin_proto::ChromeBuildProto chrome_build_proto_; |
| 50 scoped_ptr<CheckinRequest> request_; | 52 scoped_ptr<CheckinRequest> request_; |
| 51 FakeGCMStatsRecorder recorder_; | 53 FakeGCMStatsRecorder recorder_; |
| 52 }; | 54 }; |
| 53 | 55 |
| 54 CheckinRequestTest::CheckinRequestTest() | 56 CheckinRequestTest::CheckinRequestTest() |
| 55 : callback_called_(false), | 57 : callback_called_(false), |
| 56 android_id_(kBlankAndroidId), | 58 android_id_(kBlankAndroidId), |
| 57 security_token_(kBlankSecurityToken), | 59 security_token_(kBlankSecurityToken), |
| 58 checkin_device_type_(0) { | 60 checkin_device_type_(0) { |
| 59 } | 61 } |
| 60 | 62 |
| 61 CheckinRequestTest::~CheckinRequestTest() {} | 63 CheckinRequestTest::~CheckinRequestTest() {} |
| 62 | 64 |
| 63 void CheckinRequestTest::FetcherCallback( | 65 void CheckinRequestTest::FetcherCallback( |
| 64 const checkin_proto::AndroidCheckinResponse& checkin_response) { | 66 const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| 65 callback_called_ = true; | 67 callback_called_ = true; |
| 66 if (checkin_response.has_android_id()) | 68 if (checkin_response.has_android_id()) |
| 67 android_id_ = checkin_response.android_id(); | 69 android_id_ = checkin_response.android_id(); |
| 68 if (checkin_response.has_security_token()) | 70 if (checkin_response.has_security_token()) |
| 69 security_token_ = checkin_response.security_token(); | 71 security_token_ = checkin_response.security_token(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void CheckinRequestTest::CreateRequest(uint64 android_id, | 74 void CheckinRequestTest::CreateRequest(uint64_t android_id, |
| 73 uint64 security_token) { | 75 uint64_t security_token) { |
| 74 // First setup a chrome_build protobuf. | 76 // First setup a chrome_build protobuf. |
| 75 chrome_build_proto_.set_platform( | 77 chrome_build_proto_.set_platform( |
| 76 checkin_proto::ChromeBuildProto::PLATFORM_LINUX); | 78 checkin_proto::ChromeBuildProto::PLATFORM_LINUX); |
| 77 chrome_build_proto_.set_channel( | 79 chrome_build_proto_.set_channel( |
| 78 checkin_proto::ChromeBuildProto::CHANNEL_CANARY); | 80 checkin_proto::ChromeBuildProto::CHANNEL_CANARY); |
| 79 chrome_build_proto_.set_chrome_version(kChromeVersion); | 81 chrome_build_proto_.set_chrome_version(kChromeVersion); |
| 80 | 82 |
| 81 std::map<std::string, std::string> account_tokens; | 83 std::map<std::string, std::string> account_tokens; |
| 82 account_tokens[kEmailAddress] = kTokenValue; | 84 account_tokens[kEmailAddress] = kTokenValue; |
| 83 | 85 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 101 callback_called_ = false; | 103 callback_called_ = false; |
| 102 android_id_ = kBlankAndroidId; | 104 android_id_ = kBlankAndroidId; |
| 103 security_token_ = kBlankSecurityToken; | 105 security_token_ = kBlankSecurityToken; |
| 104 } | 106 } |
| 105 | 107 |
| 106 void CheckinRequestTest::SetResponseScenario( | 108 void CheckinRequestTest::SetResponseScenario( |
| 107 ResponseScenario response_scenario) { | 109 ResponseScenario response_scenario) { |
| 108 checkin_proto::AndroidCheckinResponse response; | 110 checkin_proto::AndroidCheckinResponse response; |
| 109 response.set_stats_ok(true); | 111 response.set_stats_ok(true); |
| 110 | 112 |
| 111 uint64 android_id = response_scenario == ANDROID_ID_IS_ZER0 ? 0 : kAndroidId; | 113 uint64_t android_id = |
| 112 uint64 security_token = | 114 response_scenario == ANDROID_ID_IS_ZER0 ? 0 : kAndroidId; |
| 115 uint64_t security_token = |
| 113 response_scenario == SECURITY_TOKEN_IS_ZERO ? 0 : kSecurityToken; | 116 response_scenario == SECURITY_TOKEN_IS_ZERO ? 0 : kSecurityToken; |
| 114 | 117 |
| 115 if (response_scenario != MISSING_ANDROID_ID) | 118 if (response_scenario != MISSING_ANDROID_ID) |
| 116 response.set_android_id(android_id); | 119 response.set_android_id(android_id); |
| 117 | 120 |
| 118 if (response_scenario != MISSING_SECURITY_TOKEN) | 121 if (response_scenario != MISSING_SECURITY_TOKEN) |
| 119 response.set_security_token(security_token); | 122 response.set_security_token(security_token); |
| 120 | 123 |
| 121 std::string response_string; | 124 std::string response_string; |
| 122 response.SerializeToString(&response_string); | 125 response.SerializeToString(&response_string); |
| 123 SetResponse(net::HTTP_OK, response_string); | 126 SetResponse(net::HTTP_OK, response_string); |
| 124 } | 127 } |
| 125 | 128 |
| 126 TEST_F(CheckinRequestTest, FetcherDataAndURL) { | 129 TEST_F(CheckinRequestTest, FetcherDataAndURL) { |
| 127 CreateRequest(kAndroidId, kSecurityToken); | 130 CreateRequest(kAndroidId, kSecurityToken); |
| 128 request_->Start(); | 131 request_->Start(); |
| 129 | 132 |
| 130 // Get data sent by request. | 133 // Get data sent by request. |
| 131 net::TestURLFetcher* fetcher = GetFetcher(); | 134 net::TestURLFetcher* fetcher = GetFetcher(); |
| 132 ASSERT_TRUE(fetcher); | 135 ASSERT_TRUE(fetcher); |
| 133 EXPECT_EQ(GURL(kCheckinURL), fetcher->GetOriginalURL()); | 136 EXPECT_EQ(GURL(kCheckinURL), fetcher->GetOriginalURL()); |
| 134 | 137 |
| 135 checkin_proto::AndroidCheckinRequest request_proto; | 138 checkin_proto::AndroidCheckinRequest request_proto; |
| 136 request_proto.ParseFromString(fetcher->upload_data()); | 139 request_proto.ParseFromString(fetcher->upload_data()); |
| 137 EXPECT_EQ(kAndroidId, static_cast<uint64>(request_proto.id())); | 140 EXPECT_EQ(kAndroidId, static_cast<uint64_t>(request_proto.id())); |
| 138 EXPECT_EQ(kSecurityToken, request_proto.security_token()); | 141 EXPECT_EQ(kSecurityToken, request_proto.security_token()); |
| 139 EXPECT_EQ(chrome_build_proto_.platform(), | 142 EXPECT_EQ(chrome_build_proto_.platform(), |
| 140 request_proto.checkin().chrome_build().platform()); | 143 request_proto.checkin().chrome_build().platform()); |
| 141 EXPECT_EQ(chrome_build_proto_.chrome_version(), | 144 EXPECT_EQ(chrome_build_proto_.chrome_version(), |
| 142 request_proto.checkin().chrome_build().chrome_version()); | 145 request_proto.checkin().chrome_build().chrome_version()); |
| 143 EXPECT_EQ(chrome_build_proto_.channel(), | 146 EXPECT_EQ(chrome_build_proto_.channel(), |
| 144 request_proto.checkin().chrome_build().channel()); | 147 request_proto.checkin().chrome_build().channel()); |
| 145 EXPECT_EQ(2, request_proto.account_cookie_size()); | 148 EXPECT_EQ(2, request_proto.account_cookie_size()); |
| 146 EXPECT_EQ(kEmailAddress, request_proto.account_cookie(0)); | 149 EXPECT_EQ(kEmailAddress, request_proto.account_cookie(0)); |
| 147 EXPECT_EQ(kTokenValue, request_proto.account_cookie(1)); | 150 EXPECT_EQ(kTokenValue, request_proto.account_cookie(1)); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 | 320 |
| 318 SetResponseScenario(VALID_RESPONSE); | 321 SetResponseScenario(VALID_RESPONSE); |
| 319 CompleteFetch(); | 322 CompleteFetch(); |
| 320 | 323 |
| 321 EXPECT_TRUE(callback_called_); | 324 EXPECT_TRUE(callback_called_); |
| 322 EXPECT_EQ(kAndroidId, android_id_); | 325 EXPECT_EQ(kAndroidId, android_id_); |
| 323 EXPECT_EQ(kSecurityToken, security_token_); | 326 EXPECT_EQ(kSecurityToken, security_token_); |
| 324 } | 327 } |
| 325 | 328 |
| 326 } // namespace gcm | 329 } // namespace gcm |
| OLD | NEW |