| 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 "google_apis/gcm/gcm_client_impl.h" | 5 #include "google_apis/gcm/gcm_client_impl.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| 12 #include "components/os_crypt/os_crypt.h" | 11 #include "components/os_crypt/os_crypt.h" |
| 13 #include "google_apis/gcm/base/mcs_message.h" | 12 #include "google_apis/gcm/base/mcs_message.h" |
| 14 #include "google_apis/gcm/base/mcs_util.h" | 13 #include "google_apis/gcm/base/mcs_util.h" |
| 15 #include "google_apis/gcm/engine/fake_connection_factory.h" | 14 #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 16 #include "google_apis/gcm/engine/fake_connection_handler.h" | 15 #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 17 #include "google_apis/gcm/protocol/android_checkin.pb.h" | 16 #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 18 #include "google_apis/gcm/protocol/checkin.pb.h" | 17 #include "google_apis/gcm/protocol/checkin.pb.h" |
| 19 #include "google_apis/gcm/protocol/mcs.pb.h" | 18 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 LOADING_COMPLETED, | 30 LOADING_COMPLETED, |
| 32 REGISTRATION_COMPLETED, | 31 REGISTRATION_COMPLETED, |
| 33 UNREGISTRATION_COMPLETED, | 32 UNREGISTRATION_COMPLETED, |
| 34 MESSAGE_SEND_ERROR, | 33 MESSAGE_SEND_ERROR, |
| 35 MESSAGE_RECEIVED, | 34 MESSAGE_RECEIVED, |
| 36 MESSAGES_DELETED, | 35 MESSAGES_DELETED, |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 const uint64 kDeviceAndroidId = 54321; | 38 const uint64 kDeviceAndroidId = 54321; |
| 40 const uint64 kDeviceSecurityToken = 12345; | 39 const uint64 kDeviceSecurityToken = 12345; |
| 41 const int64 kSettingsCheckinInterval = 0; | |
| 42 const char kSettingsCheckinIntervalKey[] = "checkin_interval"; | |
| 43 const char kSettingsDefaultDigest[] = "default_digest"; | |
| 44 const char kAppId[] = "app_id"; | 40 const char kAppId[] = "app_id"; |
| 45 const char kSender[] = "project_id"; | 41 const char kSender[] = "project_id"; |
| 46 const char kSender2[] = "project_id2"; | 42 const char kSender2[] = "project_id2"; |
| 47 const char kSender3[] = "project_id3"; | 43 const char kSender3[] = "project_id3"; |
| 48 const char kRegistrationResponsePrefix[] = "token="; | 44 const char kRegistrationResponsePrefix[] = "token="; |
| 49 const char kUnregistrationResponsePrefix[] = "deleted="; | 45 const char kUnregistrationResponsePrefix[] = "deleted="; |
| 50 | 46 |
| 51 // Helper for building arbitrary data messages. | 47 // Helper for building arbitrary data messages. |
| 52 MCSMessage BuildDownstreamMessage( | 48 MCSMessage BuildDownstreamMessage( |
| 53 const std::string& project_id, | 49 const std::string& project_id, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 public GCMClient::Delegate { | 161 public GCMClient::Delegate { |
| 166 public: | 162 public: |
| 167 GCMClientImplTest(); | 163 GCMClientImplTest(); |
| 168 virtual ~GCMClientImplTest(); | 164 virtual ~GCMClientImplTest(); |
| 169 | 165 |
| 170 virtual void SetUp() OVERRIDE; | 166 virtual void SetUp() OVERRIDE; |
| 171 | 167 |
| 172 void BuildGCMClient(); | 168 void BuildGCMClient(); |
| 173 void InitializeGCMClient(); | 169 void InitializeGCMClient(); |
| 174 void ReceiveMessageFromMCS(const MCSMessage& message); | 170 void ReceiveMessageFromMCS(const MCSMessage& message); |
| 175 void CompleteCheckin( | 171 void CompleteCheckin(uint64 android_id, uint64 security_token); |
| 176 uint64 android_id, | |
| 177 uint64 security_token, | |
| 178 const std::string& digest, | |
| 179 const std::map<std::string, std::string>& settings); | |
| 180 void CompleteRegistration(const std::string& registration_id); | 172 void CompleteRegistration(const std::string& registration_id); |
| 181 void CompleteUnregistration(const std::string& app_id); | 173 void CompleteUnregistration(const std::string& app_id); |
| 182 | 174 |
| 183 bool ExistsRegistration(const std::string& app_id) const; | 175 bool ExistsRegistration(const std::string& app_id) const; |
| 184 void AddRegistration(const std::string& app_id, | 176 void AddRegistration(const std::string& app_id, |
| 185 const std::vector<std::string>& sender_ids, | 177 const std::vector<std::string>& sender_ids, |
| 186 const std::string& registration_id); | 178 const std::string& registration_id); |
| 187 | 179 |
| 188 // GCMClient::Delegate overrides (for verification). | 180 // GCMClient::Delegate overrides (for verification). |
| 189 virtual void OnRegisterFinished(const std::string& app_id, | 181 virtual void OnRegisterFinished(const std::string& app_id, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 203 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 195 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
| 204 virtual void OnGCMReady() OVERRIDE; | 196 virtual void OnGCMReady() OVERRIDE; |
| 205 | 197 |
| 206 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } | 198 GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
| 207 FakeMCSClient* mcs_client() const { | 199 FakeMCSClient* mcs_client() const { |
| 208 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); | 200 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
| 209 } | 201 } |
| 210 ConnectionFactory* connection_factory() const { | 202 ConnectionFactory* connection_factory() const { |
| 211 return gcm_client_->connection_factory_.get(); | 203 return gcm_client_->connection_factory_.get(); |
| 212 } | 204 } |
| 213 GServicesSettingsMap& services_settings() { | |
| 214 return gcm_client_->gservices_settings_; | |
| 215 } | |
| 216 const std::string& services_digest() { | |
| 217 return gcm_client_->gservices_digest_; | |
| 218 } | |
| 219 | 205 |
| 220 void reset_last_event() { | 206 void reset_last_event() { |
| 221 last_event_ = NONE; | 207 last_event_ = NONE; |
| 222 last_app_id_.clear(); | 208 last_app_id_.clear(); |
| 223 last_registration_id_.clear(); | 209 last_registration_id_.clear(); |
| 224 last_message_id_.clear(); | 210 last_message_id_.clear(); |
| 225 last_result_ = GCMClient::UNKNOWN_ERROR; | 211 last_result_ = GCMClient::UNKNOWN_ERROR; |
| 226 } | 212 } |
| 227 | 213 |
| 228 LastEvent last_event() const { return last_event_; } | 214 LastEvent last_event() const { return last_event_; } |
| 229 const std::string& last_app_id() const { return last_app_id_; } | 215 const std::string& last_app_id() const { return last_app_id_; } |
| 230 const std::string& last_registration_id() const { | 216 const std::string& last_registration_id() const { |
| 231 return last_registration_id_; | 217 return last_registration_id_; |
| 232 } | 218 } |
| 233 const std::string& last_message_id() const { return last_message_id_; } | 219 const std::string& last_message_id() const { return last_message_id_; } |
| 234 GCMClient::Result last_result() const { return last_result_; } | 220 GCMClient::Result last_result() const { return last_result_; } |
| 235 const GCMClient::IncomingMessage& last_message() const { | 221 const GCMClient::IncomingMessage& last_message() const { |
| 236 return last_message_; | 222 return last_message_; |
| 237 } | 223 } |
| 238 const GCMClient::SendErrorDetails& last_error_details() const { | 224 const GCMClient::SendErrorDetails& last_error_details() const { |
| 239 return last_error_details_; | 225 return last_error_details_; |
| 240 } | 226 } |
| 227 |
| 228 int64 CurrentTime(); |
| 229 |
| 230 private: |
| 231 // Tooling. |
| 232 void PumpLoop(); |
| 233 void PumpLoopUntilIdle(); |
| 234 void QuitLoop(); |
| 235 |
| 241 base::SimpleTestClock* clock() const { | 236 base::SimpleTestClock* clock() const { |
| 242 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get()); | 237 return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get()); |
| 243 } | 238 } |
| 244 | 239 |
| 245 int64 CurrentTime(); | |
| 246 | |
| 247 // Tooling. | |
| 248 void PumpLoop(); | |
| 249 void PumpLoopUntilIdle(); | |
| 250 void QuitLoop(); | |
| 251 void ResetLoop(); | |
| 252 | |
| 253 bool CreateUniqueTempDir(); | |
| 254 | |
| 255 private: | |
| 256 // Variables used for verification. | 240 // Variables used for verification. |
| 257 LastEvent last_event_; | 241 LastEvent last_event_; |
| 258 std::string last_app_id_; | 242 std::string last_app_id_; |
| 259 std::string last_registration_id_; | 243 std::string last_registration_id_; |
| 260 std::string last_message_id_; | 244 std::string last_message_id_; |
| 261 GCMClient::Result last_result_; | 245 GCMClient::Result last_result_; |
| 262 GCMClient::IncomingMessage last_message_; | 246 GCMClient::IncomingMessage last_message_; |
| 263 GCMClient::SendErrorDetails last_error_details_; | 247 GCMClient::SendErrorDetails last_error_details_; |
| 264 | 248 |
| 265 scoped_ptr<GCMClientImpl> gcm_client_; | 249 scoped_ptr<GCMClientImpl> gcm_client_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 276 GCMClientImplTest::GCMClientImplTest() | 260 GCMClientImplTest::GCMClientImplTest() |
| 277 : last_event_(NONE), | 261 : last_event_(NONE), |
| 278 last_result_(GCMClient::UNKNOWN_ERROR), | 262 last_result_(GCMClient::UNKNOWN_ERROR), |
| 279 url_request_context_getter_(new net::TestURLRequestContextGetter( | 263 url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 280 message_loop_.message_loop_proxy())) { | 264 message_loop_.message_loop_proxy())) { |
| 281 } | 265 } |
| 282 | 266 |
| 283 GCMClientImplTest::~GCMClientImplTest() {} | 267 GCMClientImplTest::~GCMClientImplTest() {} |
| 284 | 268 |
| 285 void GCMClientImplTest::SetUp() { | 269 void GCMClientImplTest::SetUp() { |
| 286 ASSERT_TRUE(CreateUniqueTempDir()); | 270 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 287 ResetLoop(); | 271 run_loop_.reset(new base::RunLoop); |
| 288 BuildGCMClient(); | 272 BuildGCMClient(); |
| 289 InitializeGCMClient(); | 273 InitializeGCMClient(); |
| 290 CompleteCheckin(kDeviceAndroidId, | 274 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken); |
| 291 kDeviceSecurityToken, | |
| 292 kSettingsDefaultDigest, | |
| 293 std::map<std::string, std::string>()); | |
| 294 } | 275 } |
| 295 | 276 |
| 296 void GCMClientImplTest::PumpLoop() { | 277 void GCMClientImplTest::PumpLoop() { |
| 297 run_loop_->Run(); | 278 run_loop_->Run(); |
| 298 run_loop_.reset(new base::RunLoop()); | 279 run_loop_.reset(new base::RunLoop()); |
| 299 } | 280 } |
| 300 | 281 |
| 301 void GCMClientImplTest::PumpLoopUntilIdle() { | 282 void GCMClientImplTest::PumpLoopUntilIdle() { |
| 302 run_loop_->RunUntilIdle(); | 283 run_loop_->RunUntilIdle(); |
| 303 run_loop_.reset(new base::RunLoop()); | 284 run_loop_.reset(new base::RunLoop()); |
| 304 } | 285 } |
| 305 | 286 |
| 306 void GCMClientImplTest::QuitLoop() { | 287 void GCMClientImplTest::QuitLoop() { |
| 307 if (run_loop_ && run_loop_->running()) | 288 if (run_loop_ && run_loop_->running()) |
| 308 run_loop_->Quit(); | 289 run_loop_->Quit(); |
| 309 } | 290 } |
| 310 | 291 |
| 311 void GCMClientImplTest::ResetLoop() { | |
| 312 run_loop_.reset(new base::RunLoop); | |
| 313 } | |
| 314 | |
| 315 bool GCMClientImplTest::CreateUniqueTempDir() { | |
| 316 return temp_directory_.CreateUniqueTempDir(); | |
| 317 } | |
| 318 | |
| 319 void GCMClientImplTest::BuildGCMClient() { | 292 void GCMClientImplTest::BuildGCMClient() { |
| 320 gcm_client_.reset(new GCMClientImpl( | 293 gcm_client_.reset(new GCMClientImpl( |
| 321 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder()))); | 294 make_scoped_ptr<GCMInternalsBuilder>(new FakeGCMInternalsBuilder()))); |
| 322 } | 295 } |
| 323 | 296 |
| 324 void GCMClientImplTest::CompleteCheckin( | 297 void GCMClientImplTest::CompleteCheckin(uint64 android_id, |
| 325 uint64 android_id, | 298 uint64 security_token) { |
| 326 uint64 security_token, | |
| 327 const std::string& digest, | |
| 328 const std::map<std::string, std::string>& settings) { | |
| 329 checkin_proto::AndroidCheckinResponse response; | 299 checkin_proto::AndroidCheckinResponse response; |
| 330 response.set_stats_ok(true); | 300 response.set_stats_ok(true); |
| 331 response.set_android_id(android_id); | 301 response.set_android_id(android_id); |
| 332 response.set_security_token(security_token); | 302 response.set_security_token(security_token); |
| 333 | 303 |
| 334 // For testing GServices settings. | |
| 335 response.set_digest(digest); | |
| 336 for (std::map<std::string, std::string>::const_iterator it = settings.begin(); | |
| 337 it != settings.end(); ++it) { | |
| 338 checkin_proto::GservicesSetting* setting = response.add_setting(); | |
| 339 setting->set_name(it->first); | |
| 340 setting->set_value(it->second); | |
| 341 } | |
| 342 | |
| 343 std::string response_string; | 304 std::string response_string; |
| 344 response.SerializeToString(&response_string); | 305 response.SerializeToString(&response_string); |
| 345 | 306 |
| 346 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 307 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 347 ASSERT_TRUE(fetcher); | 308 ASSERT_TRUE(fetcher); |
| 348 fetcher->set_response_code(net::HTTP_OK); | 309 fetcher->set_response_code(net::HTTP_OK); |
| 349 fetcher->SetResponseString(response_string); | 310 fetcher->SetResponseString(response_string); |
| 350 fetcher->delegate()->OnURLFetchComplete(fetcher); | 311 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 351 url_fetcher_factory_.RemoveFetcherFromMap(0); | 312 url_fetcher_factory_.RemoveFetcherFromMap(0); |
| 352 } | 313 } |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); | 588 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); |
| 628 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); | 589 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); |
| 629 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); | 590 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); |
| 630 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); | 591 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from()); |
| 631 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); | 592 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
| 632 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); | 593 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); |
| 633 EXPECT_EQ("value", | 594 EXPECT_EQ("value", |
| 634 mcs_client()->last_data_message_stanza().app_data(0).value()); | 595 mcs_client()->last_data_message_stanza().app_data(0).value()); |
| 635 } | 596 } |
| 636 | 597 |
| 637 class GCMClientImplCheckinTest : public GCMClientImplTest { | |
| 638 public: | |
| 639 GCMClientImplCheckinTest(); | |
| 640 virtual ~GCMClientImplCheckinTest(); | |
| 641 | |
| 642 virtual void SetUp() OVERRIDE; | |
| 643 | |
| 644 std::map<std::string, std::string> GenerateSettings(); | |
| 645 }; | |
| 646 | |
| 647 GCMClientImplCheckinTest::GCMClientImplCheckinTest() {} | |
| 648 | |
| 649 GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {} | |
| 650 | |
| 651 void GCMClientImplCheckinTest::SetUp() { | |
| 652 ASSERT_TRUE(CreateUniqueTempDir()); | |
| 653 ResetLoop(); | |
| 654 BuildGCMClient(); | |
| 655 InitializeGCMClient(); | |
| 656 } | |
| 657 | |
| 658 std::map<std::string, std::string> | |
| 659 GCMClientImplCheckinTest::GenerateSettings() { | |
| 660 std::map<std::string, std::string> settings; | |
| 661 settings[kSettingsCheckinIntervalKey] = | |
| 662 base::Int64ToString(kSettingsCheckinInterval); | |
| 663 return settings; | |
| 664 } | |
| 665 | |
| 666 TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) { | |
| 667 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 668 kSettingsDefaultDigest, GenerateSettings()); | |
| 669 EXPECT_EQ(base::Int64ToString(kSettingsCheckinInterval), | |
| 670 services_settings()[kSettingsCheckinIntervalKey]); | |
| 671 } | |
| 672 | |
| 673 // This test only checks that periodic checkin happens. | |
| 674 TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) { | |
| 675 std::map<std::string, std::string> settings = GenerateSettings(); | |
| 676 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 677 kSettingsDefaultDigest, settings); | |
| 678 PumpLoopUntilIdle(); | |
| 679 | |
| 680 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 681 kSettingsDefaultDigest, settings); | |
| 682 } | |
| 683 | |
| 684 // This test checks that checkin reponse with the same digest will not update | |
| 685 // G-services settings. | |
| 686 TEST_F(GCMClientImplCheckinTest, GServicesSettingsSameDigest) { | |
| 687 std::map<std::string, std::string> settings = GenerateSettings(); | |
| 688 settings["checkin_url"] = "http://checkin.google.com"; | |
| 689 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 690 kSettingsDefaultDigest, settings); | |
| 691 EXPECT_EQ(settings, services_settings()); | |
| 692 EXPECT_EQ(kSettingsDefaultDigest, services_digest()); | |
| 693 PumpLoopUntilIdle(); | |
| 694 | |
| 695 // Response will carry same digest and no settings. | |
| 696 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 697 kSettingsDefaultDigest, std::map<std::string, std::string>()); | |
| 698 EXPECT_EQ(settings, services_settings()); | |
| 699 EXPECT_EQ(kSettingsDefaultDigest, services_digest()); | |
| 700 } | |
| 701 | |
| 702 // Test that checkin response with a different digest will also update the | |
| 703 // G-services settings. | |
| 704 TEST_F(GCMClientImplCheckinTest, GServicesSettingsDifferentDigest) { | |
| 705 std::map<std::string, std::string> settings = GenerateSettings(); | |
| 706 settings["checkin_url"] = "http://checkin.google.com"; | |
| 707 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, | |
| 708 kSettingsDefaultDigest, settings); | |
| 709 EXPECT_EQ(settings, services_settings()); | |
| 710 EXPECT_EQ(kSettingsDefaultDigest, services_digest()); | |
| 711 PumpLoopUntilIdle(); | |
| 712 | |
| 713 settings.clear(); | |
| 714 settings["some_settings"] = "on second checkin"; | |
| 715 settings[kSettingsCheckinIntervalKey] = "2100"; | |
| 716 settings["checkin_url"] = "http://checkin.google.com"; | |
| 717 std::string new_digest = "some_other_digest"; | |
| 718 | |
| 719 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, new_digest, settings); | |
| 720 EXPECT_EQ(settings, services_settings()); | |
| 721 EXPECT_EQ(new_digest, services_digest()); | |
| 722 } | |
| 723 | |
| 724 } // namespace gcm | 598 } // namespace gcm |
| OLD | NEW |