Chromium Code Reviews| 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 "components/gcm_driver/gcm_client_impl.h" | 5 #include "components/gcm_driver/gcm_client_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <initializer_list> | |
| 9 #include <memory> | 10 #include <memory> |
| 10 | 11 |
| 11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/test/test_mock_time_task_runner.h" | 19 #include "base/test/test_mock_time_task_runner.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/clock.h" | 21 #include "base/time/clock.h" |
| 21 #include "base/timer/timer.h" | 22 #include "base/timer/timer.h" |
| 22 #include "google_apis/gcm/base/fake_encryptor.h" | 23 #include "google_apis/gcm/base/fake_encryptor.h" |
| 23 #include "google_apis/gcm/base/mcs_message.h" | 24 #include "google_apis/gcm/base/mcs_message.h" |
| 24 #include "google_apis/gcm/base/mcs_util.h" | 25 #include "google_apis/gcm/base/mcs_util.h" |
| 25 #include "google_apis/gcm/engine/fake_connection_factory.h" | 26 #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 26 #include "google_apis/gcm/engine/fake_connection_handler.h" | 27 #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 27 #include "google_apis/gcm/engine/gservices_settings.h" | 28 #include "google_apis/gcm/engine/gservices_settings.h" |
| 28 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" | 29 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
| 29 #include "google_apis/gcm/protocol/android_checkin.pb.h" | 30 #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 30 #include "google_apis/gcm/protocol/checkin.pb.h" | 31 #include "google_apis/gcm/protocol/checkin.pb.h" |
| 31 #include "google_apis/gcm/protocol/mcs.pb.h" | 32 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 33 #include "net/test/gtest_util.h" | |
| 34 #include "net/test/scoped_disable_exit_on_dfatal.h" | |
| 32 #include "net/url_request/test_url_fetcher_factory.h" | 35 #include "net/url_request/test_url_fetcher_factory.h" |
| 33 #include "net/url_request/url_fetcher_delegate.h" | 36 #include "net/url_request/url_fetcher_delegate.h" |
| 34 #include "net/url_request/url_request_test_util.h" | 37 #include "net/url_request/url_request_test_util.h" |
| 38 #include "testing/gtest/include/gtest/gtest-spi.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 40 |
| 37 namespace gcm { | 41 namespace gcm { |
| 38 | 42 |
| 39 namespace { | 43 namespace { |
| 40 | 44 |
| 41 enum LastEvent { | 45 enum LastEvent { |
| 42 NONE, | 46 NONE, |
| 43 LOADING_COMPLETED, | 47 LOADING_COMPLETED, |
| 44 REGISTRATION_COMPLETED, | 48 REGISTRATION_COMPLETED, |
| 45 UNREGISTRATION_COMPLETED, | 49 UNREGISTRATION_COMPLETED, |
| 46 MESSAGE_SEND_ERROR, | 50 MESSAGE_SEND_ERROR, |
| 47 MESSAGE_SEND_ACK, | 51 MESSAGE_SEND_ACK, |
| 48 MESSAGE_RECEIVED, | 52 MESSAGE_RECEIVED, |
| 49 MESSAGES_DELETED, | 53 MESSAGES_DELETED, |
| 50 }; | 54 }; |
| 51 | 55 |
| 52 const char kChromeVersion[] = "45.0.0.1"; | 56 const char kChromeVersion[] = "45.0.0.1"; |
| 53 const uint64_t kDeviceAndroidId = 54321; | 57 const uint64_t kDeviceAndroidId = 54321; |
| 54 const uint64_t kDeviceSecurityToken = 12345; | 58 const uint64_t kDeviceSecurityToken = 12345; |
| 55 const uint64_t kDeviceAndroidId2 = 11111; | 59 const uint64_t kDeviceAndroidId2 = 11111; |
| 56 const uint64_t kDeviceSecurityToken2 = 2222; | 60 const uint64_t kDeviceSecurityToken2 = 2222; |
| 57 const int64_t kSettingsCheckinInterval = 16 * 60 * 60; | 61 const int64_t kSettingsCheckinInterval = 16 * 60 * 60; |
| 62 const char kCategoryForSubtypes[] = "com.chrome.stable:macosx"; | |
| 58 const char kAppId[] = "app_id"; | 63 const char kAppId[] = "app_id"; |
| 59 const char kSender[] = "project_id"; | 64 const char kSender[] = "project_id"; |
| 60 const char kSender2[] = "project_id2"; | 65 const char kSender2[] = "project_id2"; |
| 61 const char kSender3[] = "project_id3"; | 66 const char kSender3[] = "project_id3"; |
| 62 const char kRegistrationResponsePrefix[] = "token="; | 67 const char kRegistrationResponsePrefix[] = "token="; |
| 63 const char kUnregistrationResponsePrefix[] = "deleted="; | 68 const char kUnregistrationResponsePrefix[] = "deleted="; |
| 64 const char kRawData[] = "example raw data"; | 69 const char kRawData[] = "example raw data"; |
| 65 | 70 |
| 66 const char kInstanceID[] = "iid_1"; | 71 const char kInstanceID[] = "iid_1"; |
| 67 const char kScope[] = "GCM"; | 72 const char kScope[] = "GCM"; |
| 68 const char kDeleteTokenResponse[] = "token=foo"; | 73 const char kDeleteTokenResponse[] = "token=foo"; |
| 69 | 74 |
| 70 // Helper for building arbitrary data messages. | 75 // Helper for building arbitrary data messages. |
| 71 MCSMessage BuildDownstreamMessage( | 76 MCSMessage BuildDownstreamMessage( |
| 72 const std::string& project_id, | 77 const std::string& project_id, |
| 73 const std::string& app_id, | 78 const std::string& category, |
| 79 const std::string& subtype, | |
| 74 const std::map<std::string, std::string>& data, | 80 const std::map<std::string, std::string>& data, |
| 75 const std::string& raw_data) { | 81 const std::string& raw_data) { |
| 76 mcs_proto::DataMessageStanza data_message; | 82 mcs_proto::DataMessageStanza data_message; |
| 77 data_message.set_from(project_id); | 83 data_message.set_from(project_id); |
| 78 data_message.set_category(app_id); | 84 data_message.set_category(category); |
| 79 for (std::map<std::string, std::string>::const_iterator iter = data.begin(); | 85 for (std::map<std::string, std::string>::const_iterator iter = data.begin(); |
| 80 iter != data.end(); | 86 iter != data.end(); |
| 81 ++iter) { | 87 ++iter) { |
| 82 mcs_proto::AppData* app_data = data_message.add_app_data(); | 88 mcs_proto::AppData* app_data = data_message.add_app_data(); |
| 83 app_data->set_key(iter->first); | 89 app_data->set_key(iter->first); |
| 84 app_data->set_value(iter->second); | 90 app_data->set_value(iter->second); |
| 85 } | 91 } |
| 92 if (!subtype.empty()) { | |
| 93 mcs_proto::AppData* app_data = data_message.add_app_data(); | |
| 94 app_data->set_key("subtype"); | |
| 95 app_data->set_value(subtype); | |
| 96 } | |
| 86 data_message.set_raw_data(raw_data); | 97 data_message.set_raw_data(raw_data); |
| 87 return MCSMessage(kDataMessageStanzaTag, data_message); | 98 return MCSMessage(kDataMessageStanzaTag, data_message); |
| 88 } | 99 } |
| 89 | 100 |
| 90 GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email, | 101 GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email, |
| 91 const std::string& token) { | 102 const std::string& token) { |
| 92 GCMClient::AccountTokenInfo account_token; | 103 GCMClient::AccountTokenInfo account_token; |
| 93 account_token.email = email; | 104 account_token.email = email; |
| 94 account_token.access_token = token; | 105 account_token.access_token = token; |
| 95 return account_token; | 106 return account_token; |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 registration->sender_ids = sender_ids; | 535 registration->sender_ids = sender_ids; |
| 525 gcm_client_->registrations_[registration] = registration_id; | 536 gcm_client_->registrations_[registration] = registration_id; |
| 526 } | 537 } |
| 527 | 538 |
| 528 void GCMClientImplTest::InitializeGCMClient() { | 539 void GCMClientImplTest::InitializeGCMClient() { |
| 529 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); | 540 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 530 | 541 |
| 531 // Actual initialization. | 542 // Actual initialization. |
| 532 GCMClient::ChromeBuildInfo chrome_build_info; | 543 GCMClient::ChromeBuildInfo chrome_build_info; |
| 533 chrome_build_info.version = kChromeVersion; | 544 chrome_build_info.version = kChromeVersion; |
| 545 chrome_build_info.category_for_subtypes = kCategoryForSubtypes; | |
| 534 gcm_client_->Initialize(chrome_build_info, gcm_store_path(), task_runner_, | 546 gcm_client_->Initialize(chrome_build_info, gcm_store_path(), task_runner_, |
| 535 url_request_context_getter_, | 547 url_request_context_getter_, |
| 536 base::WrapUnique<Encryptor>(new FakeEncryptor), this); | 548 base::WrapUnique<Encryptor>(new FakeEncryptor), this); |
| 537 } | 549 } |
| 538 | 550 |
| 539 void GCMClientImplTest::StartGCMClient() { | 551 void GCMClientImplTest::StartGCMClient() { |
| 540 // Start loading and check-in. | 552 // Start loading and check-in. |
| 541 gcm_client_->Start(GCMClient::IMMEDIATE_START); | 553 gcm_client_->Start(GCMClient::IMMEDIATE_START); |
| 542 | 554 |
| 543 PumpLoopUntilIdle(); | 555 PumpLoopUntilIdle(); |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 811 senders.push_back(kSender); | 823 senders.push_back(kSender); |
| 812 senders.push_back(kSender2); | 824 senders.push_back(kSender2); |
| 813 AddRegistration(kAppId, senders, "reg_id"); | 825 AddRegistration(kAppId, senders, "reg_id"); |
| 814 | 826 |
| 815 std::map<std::string, std::string> expected_data; | 827 std::map<std::string, std::string> expected_data; |
| 816 expected_data["message_type"] = "gcm"; | 828 expected_data["message_type"] = "gcm"; |
| 817 expected_data["key"] = "value"; | 829 expected_data["key"] = "value"; |
| 818 expected_data["key2"] = "value2"; | 830 expected_data["key2"] = "value2"; |
| 819 | 831 |
| 820 // Message for kSender will be received. | 832 // Message for kSender will be received. |
| 821 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data, | 833 MCSMessage message( |
| 822 std::string() /* raw_data */)); | 834 BuildDownstreamMessage(kSender, kAppId, std::string() /* subtype */, |
| 835 expected_data, std::string() /* raw_data */)); | |
| 823 EXPECT_TRUE(message.IsValid()); | 836 EXPECT_TRUE(message.IsValid()); |
| 824 ReceiveMessageFromMCS(message); | 837 ReceiveMessageFromMCS(message); |
| 825 | 838 |
| 826 expected_data.erase(expected_data.find("message_type")); | 839 expected_data.erase(expected_data.find("message_type")); |
| 827 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 840 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 828 EXPECT_EQ(kAppId, last_app_id()); | 841 EXPECT_EQ(kAppId, last_app_id()); |
| 829 EXPECT_EQ(expected_data.size(), last_message().data.size()); | 842 EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 830 EXPECT_EQ(expected_data, last_message().data); | 843 EXPECT_EQ(expected_data, last_message().data); |
| 831 EXPECT_EQ(kSender, last_message().sender_id); | 844 EXPECT_EQ(kSender, last_message().sender_id); |
| 832 | 845 |
| 833 reset_last_event(); | 846 reset_last_event(); |
| 834 | 847 |
| 835 // Message for kSender2 will be received. | 848 // Message for kSender2 will be received. |
| 836 MCSMessage message2(BuildDownstreamMessage(kSender2, kAppId, expected_data, | 849 MCSMessage message2( |
| 837 std::string() /* raw_data */)); | 850 BuildDownstreamMessage(kSender2, kAppId, std::string() /* subtype */, |
| 851 expected_data, std::string() /* raw_data */)); | |
| 838 EXPECT_TRUE(message2.IsValid()); | 852 EXPECT_TRUE(message2.IsValid()); |
| 839 ReceiveMessageFromMCS(message2); | 853 ReceiveMessageFromMCS(message2); |
| 840 | 854 |
| 841 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 855 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 842 EXPECT_EQ(kAppId, last_app_id()); | 856 EXPECT_EQ(kAppId, last_app_id()); |
| 843 EXPECT_EQ(expected_data.size(), last_message().data.size()); | 857 EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 844 EXPECT_EQ(expected_data, last_message().data); | 858 EXPECT_EQ(expected_data, last_message().data); |
| 845 EXPECT_EQ(kSender2, last_message().sender_id); | 859 EXPECT_EQ(kSender2, last_message().sender_id); |
| 846 | 860 |
| 847 reset_last_event(); | 861 reset_last_event(); |
| 848 | 862 |
| 849 // Message from kSender3 will be dropped. | 863 // Message from kSender3 will be dropped. |
| 850 MCSMessage message3(BuildDownstreamMessage(kSender3, kAppId, expected_data, | 864 MCSMessage message3( |
| 851 std::string() /* raw_data */)); | 865 BuildDownstreamMessage(kSender3, kAppId, std::string() /* subtype */, |
| 866 expected_data, std::string() /* raw_data */)); | |
| 852 EXPECT_TRUE(message3.IsValid()); | 867 EXPECT_TRUE(message3.IsValid()); |
| 853 ReceiveMessageFromMCS(message3); | 868 ReceiveMessageFromMCS(message3); |
| 854 | 869 |
| 855 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | 870 EXPECT_NE(MESSAGE_RECEIVED, last_event()); |
| 856 EXPECT_NE(kAppId, last_app_id()); | 871 EXPECT_NE(kAppId, last_app_id()); |
| 857 } | 872 } |
| 858 | 873 |
| 859 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { | 874 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { |
| 860 std::vector<std::string> senders(1, kSender); | 875 std::vector<std::string> senders(1, kSender); |
| 861 AddRegistration(kAppId, senders, "reg_id"); | 876 AddRegistration(kAppId, senders, "reg_id"); |
| 862 | 877 |
| 863 std::map<std::string, std::string> expected_data; | 878 std::map<std::string, std::string> expected_data; |
| 864 | 879 |
| 865 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data, | 880 MCSMessage message(BuildDownstreamMessage( |
| 866 kRawData)); | 881 kSender, kAppId, std::string() /* subtype */, expected_data, kRawData)); |
| 867 EXPECT_TRUE(message.IsValid()); | 882 EXPECT_TRUE(message.IsValid()); |
| 868 ReceiveMessageFromMCS(message); | 883 ReceiveMessageFromMCS(message); |
| 869 | 884 |
| 870 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 885 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 871 EXPECT_EQ(kAppId, last_app_id()); | 886 EXPECT_EQ(kAppId, last_app_id()); |
| 872 EXPECT_EQ(expected_data.size(), last_message().data.size()); | 887 EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 873 EXPECT_EQ(kSender, last_message().sender_id); | 888 EXPECT_EQ(kSender, last_message().sender_id); |
| 874 EXPECT_EQ(kRawData, last_message().raw_data); | 889 EXPECT_EQ(kRawData, last_message().raw_data); |
| 875 } | 890 } |
| 876 | 891 |
| 877 TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) { | 892 TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) { |
| 878 std::map<std::string, std::string> expected_data; | 893 std::map<std::string, std::string> expected_data; |
| 879 expected_data["message_type"] = "send_error"; | 894 expected_data["message_type"] = "send_error"; |
| 880 expected_data["google.message_id"] = "007"; | 895 expected_data["google.message_id"] = "007"; |
| 881 expected_data["error_details"] = "some details"; | 896 expected_data["error_details"] = "some details"; |
| 882 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data, | 897 MCSMessage message( |
| 883 std::string() /* raw_data */)); | 898 BuildDownstreamMessage(kSender, kAppId, std::string() /* subtype */, |
| 899 expected_data, std::string() /* raw_data */)); | |
| 884 EXPECT_TRUE(message.IsValid()); | 900 EXPECT_TRUE(message.IsValid()); |
| 885 ReceiveMessageFromMCS(message); | 901 ReceiveMessageFromMCS(message); |
| 886 | 902 |
| 887 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); | 903 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); |
| 888 EXPECT_EQ(kAppId, last_app_id()); | 904 EXPECT_EQ(kAppId, last_app_id()); |
| 889 EXPECT_EQ("007", last_error_details().message_id); | 905 EXPECT_EQ("007", last_error_details().message_id); |
| 890 EXPECT_EQ(1UL, last_error_details().additional_data.size()); | 906 EXPECT_EQ(1UL, last_error_details().additional_data.size()); |
| 891 MessageData::const_iterator iter = | 907 MessageData::const_iterator iter = |
| 892 last_error_details().additional_data.find("error_details"); | 908 last_error_details().additional_data.find("error_details"); |
| 893 EXPECT_TRUE(iter != last_error_details().additional_data.end()); | 909 EXPECT_TRUE(iter != last_error_details().additional_data.end()); |
| 894 EXPECT_EQ("some details", iter->second); | 910 EXPECT_EQ("some details", iter->second); |
| 895 } | 911 } |
| 896 | 912 |
| 897 TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { | 913 TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { |
| 898 std::map<std::string, std::string> expected_data; | 914 std::map<std::string, std::string> expected_data; |
| 899 expected_data["message_type"] = "deleted_messages"; | 915 expected_data["message_type"] = "deleted_messages"; |
| 900 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data, | 916 MCSMessage message( |
| 901 std::string() /* raw_data */)); | 917 BuildDownstreamMessage(kSender, kAppId, std::string() /* subtype */, |
| 918 expected_data, std::string() /* raw_data */)); | |
| 902 EXPECT_TRUE(message.IsValid()); | 919 EXPECT_TRUE(message.IsValid()); |
| 903 ReceiveMessageFromMCS(message); | 920 ReceiveMessageFromMCS(message); |
| 904 | 921 |
| 905 EXPECT_EQ(MESSAGES_DELETED, last_event()); | 922 EXPECT_EQ(MESSAGES_DELETED, last_event()); |
| 906 EXPECT_EQ(kAppId, last_app_id()); | 923 EXPECT_EQ(kAppId, last_app_id()); |
| 907 } | 924 } |
| 908 | 925 |
| 909 TEST_F(GCMClientImplTest, SendMessage) { | 926 TEST_F(GCMClientImplTest, SendMessage) { |
| 910 OutgoingMessage message; | 927 OutgoingMessage message; |
| 911 message.id = "007"; | 928 message.id = "007"; |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1385 | 1402 |
| 1386 class GCMClientInstanceIDTest : public GCMClientImplTest { | 1403 class GCMClientInstanceIDTest : public GCMClientImplTest { |
| 1387 public: | 1404 public: |
| 1388 GCMClientInstanceIDTest(); | 1405 GCMClientInstanceIDTest(); |
| 1389 ~GCMClientInstanceIDTest() override; | 1406 ~GCMClientInstanceIDTest() override; |
| 1390 | 1407 |
| 1391 void AddInstanceID(const std::string& app_id, | 1408 void AddInstanceID(const std::string& app_id, |
| 1392 const std::string& instance_id); | 1409 const std::string& instance_id); |
| 1393 void RemoveInstanceID(const std::string& app_id); | 1410 void RemoveInstanceID(const std::string& app_id); |
| 1394 void GetToken(const std::string& app_id, | 1411 void GetToken(const std::string& app_id, |
| 1412 bool use_subtype, | |
| 1395 const std::string& authorized_entity, | 1413 const std::string& authorized_entity, |
| 1396 const std::string& scope); | 1414 const std::string& scope); |
| 1397 void DeleteToken(const std::string& app_id, | 1415 void DeleteToken(const std::string& app_id, |
| 1416 bool use_subtype, | |
| 1398 const std::string& authorized_entity, | 1417 const std::string& authorized_entity, |
| 1399 const std::string& scope); | 1418 const std::string& scope); |
| 1400 void CompleteDeleteToken(); | 1419 void CompleteDeleteToken(); |
| 1401 bool ExistsToken(const std::string& app_id, | 1420 bool ExistsToken(const std::string& app_id, |
| 1421 bool use_subtype, | |
| 1402 const std::string& authorized_entity, | 1422 const std::string& authorized_entity, |
| 1403 const std::string& scope) const; | 1423 const std::string& scope) const; |
| 1404 }; | 1424 }; |
| 1405 | 1425 |
| 1406 GCMClientInstanceIDTest::GCMClientInstanceIDTest() { | 1426 GCMClientInstanceIDTest::GCMClientInstanceIDTest() { |
| 1407 } | 1427 } |
| 1408 | 1428 |
| 1409 GCMClientInstanceIDTest::~GCMClientInstanceIDTest() { | 1429 GCMClientInstanceIDTest::~GCMClientInstanceIDTest() { |
| 1410 } | 1430 } |
| 1411 | 1431 |
| 1412 void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id, | 1432 void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id, |
| 1413 const std::string& instance_id) { | 1433 const std::string& instance_id) { |
| 1414 gcm_client()->AddInstanceIDData(app_id, instance_id, "123"); | 1434 gcm_client()->AddInstanceIDData(app_id, instance_id, "123"); |
| 1415 } | 1435 } |
| 1416 | 1436 |
| 1417 void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) { | 1437 void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) { |
| 1418 gcm_client()->RemoveInstanceIDData(app_id); | 1438 gcm_client()->RemoveInstanceIDData(app_id); |
| 1419 } | 1439 } |
| 1420 | 1440 |
| 1421 void GCMClientInstanceIDTest::GetToken(const std::string& app_id, | 1441 void GCMClientInstanceIDTest::GetToken(const std::string& app_id, |
| 1442 bool use_subtype, | |
| 1422 const std::string& authorized_entity, | 1443 const std::string& authorized_entity, |
| 1423 const std::string& scope) { | 1444 const std::string& scope) { |
| 1424 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( | 1445 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1425 new InstanceIDTokenInfo); | 1446 new InstanceIDTokenInfo); |
| 1426 instance_id_info->app_id = app_id; | 1447 instance_id_info->app_id = app_id; |
| 1448 instance_id_info->use_subtype = use_subtype; | |
| 1427 instance_id_info->authorized_entity = authorized_entity; | 1449 instance_id_info->authorized_entity = authorized_entity; |
| 1428 instance_id_info->scope = scope; | 1450 instance_id_info->scope = scope; |
| 1429 gcm_client()->Register( | 1451 gcm_client()->Register( |
| 1430 make_linked_ptr<RegistrationInfo>(instance_id_info.release())); | 1452 make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1431 } | 1453 } |
| 1432 | 1454 |
| 1433 void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id, | 1455 void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id, |
| 1456 bool use_subtype, | |
| 1434 const std::string& authorized_entity, | 1457 const std::string& authorized_entity, |
| 1435 const std::string& scope) { | 1458 const std::string& scope) { |
| 1436 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( | 1459 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1437 new InstanceIDTokenInfo); | 1460 new InstanceIDTokenInfo); |
| 1438 instance_id_info->app_id = app_id; | 1461 instance_id_info->app_id = app_id; |
| 1462 instance_id_info->use_subtype = use_subtype; | |
| 1439 instance_id_info->authorized_entity = authorized_entity; | 1463 instance_id_info->authorized_entity = authorized_entity; |
| 1440 instance_id_info->scope = scope; | 1464 instance_id_info->scope = scope; |
| 1441 gcm_client()->Unregister( | 1465 gcm_client()->Unregister( |
| 1442 make_linked_ptr<RegistrationInfo>(instance_id_info.release())); | 1466 make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1443 } | 1467 } |
| 1444 | 1468 |
| 1445 void GCMClientInstanceIDTest::CompleteDeleteToken() { | 1469 void GCMClientInstanceIDTest::CompleteDeleteToken() { |
| 1446 std::string response(kDeleteTokenResponse); | 1470 std::string response(kDeleteTokenResponse); |
| 1447 net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0); | 1471 net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0); |
| 1448 ASSERT_TRUE(fetcher); | 1472 ASSERT_TRUE(fetcher); |
| 1449 fetcher->set_response_code(net::HTTP_OK); | 1473 fetcher->set_response_code(net::HTTP_OK); |
| 1450 fetcher->SetResponseString(response); | 1474 fetcher->SetResponseString(response); |
| 1451 fetcher->delegate()->OnURLFetchComplete(fetcher); | 1475 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 1452 // Give a chance for GCMStoreImpl::Backend to finish persisting data. | 1476 // Give a chance for GCMStoreImpl::Backend to finish persisting data. |
| 1453 PumpLoopUntilIdle(); | 1477 PumpLoopUntilIdle(); |
| 1454 } | 1478 } |
| 1455 | 1479 |
| 1456 bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id, | 1480 bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id, |
| 1481 bool use_subtype, | |
| 1457 const std::string& authorized_entity, | 1482 const std::string& authorized_entity, |
| 1458 const std::string& scope) const { | 1483 const std::string& scope) const { |
| 1459 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( | 1484 std::unique_ptr<InstanceIDTokenInfo> instance_id_info( |
| 1460 new InstanceIDTokenInfo); | 1485 new InstanceIDTokenInfo); |
| 1461 instance_id_info->app_id = app_id; | 1486 instance_id_info->app_id = app_id; |
| 1487 instance_id_info->use_subtype = use_subtype; | |
| 1462 instance_id_info->authorized_entity = authorized_entity; | 1488 instance_id_info->authorized_entity = authorized_entity; |
| 1463 instance_id_info->scope = scope; | 1489 instance_id_info->scope = scope; |
| 1464 return gcm_client()->registrations_.count( | 1490 RegistrationInfoMap::const_iterator registrations_iter = |
| 1465 make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0; | 1491 gcm_client()->registrations_.find( |
| 1492 make_linked_ptr<RegistrationInfo>(instance_id_info.release())); | |
| 1493 if (registrations_iter == gcm_client()->registrations_.end()) | |
| 1494 return false; | |
| 1495 const InstanceIDTokenInfo* instance_id_token_info = | |
| 1496 InstanceIDTokenInfo::FromRegistrationInfo( | |
| 1497 registrations_iter->first.get()); | |
| 1498 // Compare use_subtype since RegistrationInfoComparer::operator() does not. | |
| 1499 return instance_id_token_info && | |
| 1500 instance_id_token_info->use_subtype == use_subtype; | |
| 1466 } | 1501 } |
| 1467 | 1502 |
| 1468 TEST_F(GCMClientInstanceIDTest, GetToken) { | 1503 TEST_F(GCMClientInstanceIDTest, GetToken) { |
| 1469 AddInstanceID(kAppId, kInstanceID); | 1504 AddInstanceID(kAppId, kInstanceID); |
| 1470 | 1505 |
| 1471 // Get a token. | 1506 // Get a token. |
| 1472 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1507 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1473 GetToken(kAppId, kSender, kScope); | 1508 GetToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1474 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | 1509 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
| 1475 | 1510 |
| 1476 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1511 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1477 EXPECT_EQ(kAppId, last_app_id()); | 1512 EXPECT_EQ(kAppId, last_app_id()); |
| 1478 EXPECT_EQ("token1", last_registration_id()); | 1513 EXPECT_EQ("token1", last_registration_id()); |
| 1479 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1514 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1480 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1515 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1481 | 1516 |
| 1482 // Get another token. | 1517 // Get another token. |
| 1483 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); | 1518 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1484 GetToken(kAppId, kSender2, kScope); | 1519 GetToken(kAppId, false /* use_subtype */, kSender2, kScope); |
| 1485 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | 1520 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
| 1486 | 1521 |
| 1487 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1522 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1488 EXPECT_EQ(kAppId, last_app_id()); | 1523 EXPECT_EQ(kAppId, last_app_id()); |
| 1489 EXPECT_EQ("token2", last_registration_id()); | 1524 EXPECT_EQ("token2", last_registration_id()); |
| 1490 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1525 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1491 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); | 1526 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1492 // The 1st token still exists. | 1527 // The 1st token still exists. |
| 1493 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1528 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1529 } | |
| 1530 | |
| 1531 TEST_F(GCMClientInstanceIDTest, GetTokenWithAndWithoutSubtype) { | |
| 1532 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); | |
|
Peter Beverloo
2016/07/22 12:17:03
Sounds like an ASSERT_EQ?
johnme
2016/07/26 17:11:55
Done.
| |
| 1533 | |
| 1534 AddInstanceID(kAppId, kInstanceID); | |
| 1535 | |
| 1536 EXPECT_FALSE(ExistsToken(kAppId, false, kSender, kScope)); | |
| 1537 EXPECT_FALSE(ExistsToken(kAppId, true, kSender, kScope)); | |
| 1538 | |
| 1539 DLOG(INFO) << "THIS TEST USES EXPECT_DCHECK - IGNORE STACKTRACES LOGGED DUE " | |
| 1540 "TO \"Registering the same Instance ID token both with and " | |
| 1541 "without subtypes is not supported\" SINCE THOSE ARE EXPECTED"; | |
| 1542 | |
| 1543 for (bool use_subtype : {false, true}) { | |
| 1544 // Get a token. | |
| 1545 GetToken(kAppId, use_subtype, kSender, kScope); | |
| 1546 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | |
| 1547 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | |
| 1548 EXPECT_EQ(kAppId, last_app_id()); | |
| 1549 EXPECT_EQ("token1", last_registration_id()); | |
| 1550 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | |
| 1551 EXPECT_TRUE(ExistsToken(kAppId, use_subtype, kSender, kScope)); | |
| 1552 EXPECT_FALSE(ExistsToken(kAppId, !use_subtype, kSender, kScope)); | |
| 1553 | |
| 1554 // Getting the same token with opposite value of use_subtype should assert. | |
| 1555 EXPECT_DCHECK( | |
| 1556 { GetToken(kAppId, !use_subtype, kSender, kScope); }, | |
| 1557 "Registering the same Instance ID token both with and without subtypes " | |
| 1558 "is not supported"); | |
| 1559 | |
| 1560 // Deletion should work. Clean up for next loop iteration. | |
| 1561 DeleteToken(kAppId, use_subtype, kSender, kScope); | |
| 1562 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); | |
| 1563 EXPECT_FALSE(ExistsToken(kAppId, use_subtype, kSender, kScope)); | |
| 1564 EXPECT_FALSE(ExistsToken(kAppId, !use_subtype, kSender, kScope)); | |
| 1565 } | |
| 1494 } | 1566 } |
| 1495 | 1567 |
| 1496 TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) { | 1568 TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) { |
| 1497 AddInstanceID(kAppId, kInstanceID); | 1569 AddInstanceID(kAppId, kInstanceID); |
| 1498 | 1570 |
| 1499 // Delete an invalid token. | 1571 // Delete an invalid token. |
| 1500 DeleteToken(kAppId, "Foo@#$", kScope); | 1572 DeleteToken(kAppId, false /* use_subtype */, "Foo@#$", kScope); |
| 1501 PumpLoopUntilIdle(); | 1573 PumpLoopUntilIdle(); |
| 1502 | 1574 |
| 1503 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1575 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1504 EXPECT_EQ(kAppId, last_app_id()); | 1576 EXPECT_EQ(kAppId, last_app_id()); |
| 1505 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); | 1577 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
| 1506 | 1578 |
| 1507 reset_last_event(); | 1579 reset_last_event(); |
| 1508 | 1580 |
| 1509 // Delete a non-existing token. | 1581 // Delete a non-existing token. |
| 1510 DeleteToken(kAppId, kSender, kScope); | 1582 DeleteToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1511 PumpLoopUntilIdle(); | 1583 PumpLoopUntilIdle(); |
| 1512 | 1584 |
| 1513 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1585 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1514 EXPECT_EQ(kAppId, last_app_id()); | 1586 EXPECT_EQ(kAppId, last_app_id()); |
| 1515 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); | 1587 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
| 1516 } | 1588 } |
| 1517 | 1589 |
| 1518 TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) { | 1590 TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) { |
| 1519 AddInstanceID(kAppId, kInstanceID); | 1591 AddInstanceID(kAppId, kInstanceID); |
| 1520 | 1592 |
| 1521 // Get a token. | 1593 // Get a token. |
| 1522 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1594 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1523 GetToken(kAppId, kSender, kScope); | 1595 GetToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1524 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | 1596 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
| 1525 | 1597 |
| 1526 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1598 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1527 EXPECT_EQ(kAppId, last_app_id()); | 1599 EXPECT_EQ(kAppId, last_app_id()); |
| 1528 EXPECT_EQ("token1", last_registration_id()); | 1600 EXPECT_EQ("token1", last_registration_id()); |
| 1529 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1601 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1530 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1602 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1531 | 1603 |
| 1532 reset_last_event(); | 1604 reset_last_event(); |
| 1533 | 1605 |
| 1534 // Get another token. | 1606 // Get another token. |
| 1535 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); | 1607 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1536 GetToken(kAppId, kSender2, kScope); | 1608 GetToken(kAppId, false /* use_subtype */, kSender2, kScope); |
| 1537 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | 1609 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
| 1538 | 1610 |
| 1539 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1611 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1540 EXPECT_EQ(kAppId, last_app_id()); | 1612 EXPECT_EQ(kAppId, last_app_id()); |
| 1541 EXPECT_EQ("token2", last_registration_id()); | 1613 EXPECT_EQ("token2", last_registration_id()); |
| 1542 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1614 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1543 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); | 1615 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1544 // The 1st token still exists. | 1616 // The 1st token still exists. |
| 1545 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1617 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1546 | 1618 |
| 1547 reset_last_event(); | 1619 reset_last_event(); |
| 1548 | 1620 |
| 1549 // Delete the 2nd token. | 1621 // Delete the 2nd token. |
| 1550 DeleteToken(kAppId, kSender2, kScope); | 1622 DeleteToken(kAppId, false /* use_subtype */, kSender2, kScope); |
| 1551 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); | 1623 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
| 1552 | 1624 |
| 1553 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1625 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1554 EXPECT_EQ(kAppId, last_app_id()); | 1626 EXPECT_EQ(kAppId, last_app_id()); |
| 1555 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1627 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1556 // The 2nd token is gone while the 1st token still exists. | 1628 // The 2nd token is gone while the 1st token still exists. |
| 1557 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1629 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1558 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); | 1630 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1559 | 1631 |
| 1560 reset_last_event(); | 1632 reset_last_event(); |
| 1561 | 1633 |
| 1562 // Delete the 1st token. | 1634 // Delete the 1st token. |
| 1563 DeleteToken(kAppId, kSender, kScope); | 1635 DeleteToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1564 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); | 1636 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
| 1565 | 1637 |
| 1566 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1638 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1567 EXPECT_EQ(kAppId, last_app_id()); | 1639 EXPECT_EQ(kAppId, last_app_id()); |
| 1568 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1640 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1569 // Both tokens are gone now. | 1641 // Both tokens are gone now. |
| 1570 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1642 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1571 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1643 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1572 | 1644 |
| 1573 reset_last_event(); | 1645 reset_last_event(); |
| 1574 | 1646 |
| 1575 // Trying to delete the token again will get an error. | 1647 // Trying to delete the token again will get an error. |
| 1576 DeleteToken(kAppId, kSender, kScope); | 1648 DeleteToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1577 PumpLoopUntilIdle(); | 1649 PumpLoopUntilIdle(); |
| 1578 | 1650 |
| 1579 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1651 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1580 EXPECT_EQ(kAppId, last_app_id()); | 1652 EXPECT_EQ(kAppId, last_app_id()); |
| 1581 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); | 1653 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result()); |
| 1582 } | 1654 } |
| 1583 | 1655 |
| 1584 TEST_F(GCMClientInstanceIDTest, DeleteAllTokens) { | 1656 TEST_F(GCMClientInstanceIDTest, DeleteAllTokens) { |
| 1585 AddInstanceID(kAppId, kInstanceID); | 1657 AddInstanceID(kAppId, kInstanceID); |
| 1586 | 1658 |
| 1587 // Get a token. | 1659 // Get a token. |
| 1588 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1660 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1589 GetToken(kAppId, kSender, kScope); | 1661 GetToken(kAppId, false /* use_subtype */, kSender, kScope); |
| 1590 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | 1662 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); |
| 1591 | 1663 |
| 1592 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1664 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1593 EXPECT_EQ(kAppId, last_app_id()); | 1665 EXPECT_EQ(kAppId, last_app_id()); |
| 1594 EXPECT_EQ("token1", last_registration_id()); | 1666 EXPECT_EQ("token1", last_registration_id()); |
| 1595 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1667 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1596 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1668 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1597 | 1669 |
| 1598 reset_last_event(); | 1670 reset_last_event(); |
| 1599 | 1671 |
| 1600 // Get another token. | 1672 // Get another token. |
| 1601 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); | 1673 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1602 GetToken(kAppId, kSender2, kScope); | 1674 GetToken(kAppId, false /* use_subtype */, kSender2, kScope); |
| 1603 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | 1675 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); |
| 1604 | 1676 |
| 1605 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); | 1677 EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1606 EXPECT_EQ(kAppId, last_app_id()); | 1678 EXPECT_EQ(kAppId, last_app_id()); |
| 1607 EXPECT_EQ("token2", last_registration_id()); | 1679 EXPECT_EQ("token2", last_registration_id()); |
| 1608 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1680 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1609 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); | 1681 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1610 // The 1st token still exists. | 1682 // The 1st token still exists. |
| 1611 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); | 1683 EXPECT_TRUE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1612 | 1684 |
| 1613 reset_last_event(); | 1685 reset_last_event(); |
| 1614 | 1686 |
| 1615 // Delete all tokens. | 1687 // Delete all tokens. |
| 1616 DeleteToken(kAppId, "*", "*"); | 1688 DeleteToken(kAppId, false /* use_subtype */, "*", "*"); |
| 1617 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); | 1689 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken()); |
| 1618 | 1690 |
| 1619 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1691 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1620 EXPECT_EQ(kAppId, last_app_id()); | 1692 EXPECT_EQ(kAppId, last_app_id()); |
| 1621 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1693 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1622 // All tokens are gone now. | 1694 // All tokens are gone now. |
| 1623 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1695 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender, kScope)); |
| 1624 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); | 1696 EXPECT_FALSE(ExistsToken(kAppId, false /* use_subtype */, kSender2, kScope)); |
| 1625 } | 1697 } |
| 1626 | 1698 |
| 1627 TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) { | 1699 TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) { |
| 1628 AddInstanceID(kAppId, kInstanceID); | 1700 AddInstanceID(kAppId, kInstanceID); |
| 1629 | 1701 |
| 1630 // Delete all tokens without getting a token first. | 1702 // Delete all tokens without getting a token first. |
| 1631 DeleteToken(kAppId, "*", "*"); | 1703 DeleteToken(kAppId, false /* use_subtype */, "*", "*"); |
| 1632 // No need to call CompleteDeleteToken since unregistration request should | 1704 // No need to call CompleteDeleteToken since unregistration request should |
| 1633 // not be triggered. | 1705 // not be triggered. |
| 1634 PumpLoopUntilIdle(); | 1706 PumpLoopUntilIdle(); |
| 1635 | 1707 |
| 1636 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); | 1708 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1637 EXPECT_EQ(kAppId, last_app_id()); | 1709 EXPECT_EQ(kAppId, last_app_id()); |
| 1638 EXPECT_EQ(GCMClient::SUCCESS, last_result()); | 1710 EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1639 } | 1711 } |
| 1640 | 1712 |
| 1713 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) { | |
| 1714 AddInstanceID(kAppId, kInstanceID); | |
| 1715 GetToken(kAppId, false /* use_subtype */, kSender, kScope); | |
| 1716 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | |
| 1717 GetToken(kAppId, false /* use_subtype */, kSender2, kScope); | |
| 1718 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | |
| 1719 | |
| 1720 std::map<std::string, std::string> expected_data; | |
| 1721 | |
| 1722 // Message for kSender with a subtype will be dropped. | |
| 1723 MCSMessage message0(BuildDownstreamMessage( | |
| 1724 kSender, kCategoryForSubtypes, kAppId /* subtype */, expected_data, | |
| 1725 std::string() /* raw_data */)); | |
| 1726 EXPECT_TRUE(message0.IsValid()); | |
| 1727 ReceiveMessageFromMCS(message0); | |
| 1728 | |
| 1729 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | |
| 1730 | |
| 1731 reset_last_event(); | |
| 1732 | |
| 1733 // Message for kSender will be received. | |
| 1734 MCSMessage message1( | |
| 1735 BuildDownstreamMessage(kSender, kAppId, std::string() /* subtype */, | |
| 1736 expected_data, std::string() /* raw_data */)); | |
| 1737 EXPECT_TRUE(message1.IsValid()); | |
| 1738 ReceiveMessageFromMCS(message1); | |
| 1739 | |
| 1740 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | |
| 1741 EXPECT_EQ(kAppId, last_app_id()); | |
| 1742 EXPECT_EQ(expected_data.size(), last_message().data.size()); | |
| 1743 EXPECT_EQ(expected_data, last_message().data); | |
| 1744 EXPECT_EQ(kSender, last_message().sender_id); | |
| 1745 | |
| 1746 reset_last_event(); | |
| 1747 | |
| 1748 // Message for kSender2 will be received. | |
| 1749 MCSMessage message2( | |
| 1750 BuildDownstreamMessage(kSender2, kAppId, std::string() /* subtype */, | |
| 1751 expected_data, std::string() /* raw_data */)); | |
| 1752 EXPECT_TRUE(message2.IsValid()); | |
| 1753 ReceiveMessageFromMCS(message2); | |
| 1754 | |
| 1755 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | |
| 1756 EXPECT_EQ(kAppId, last_app_id()); | |
| 1757 EXPECT_EQ(expected_data.size(), last_message().data.size()); | |
| 1758 EXPECT_EQ(expected_data, last_message().data); | |
| 1759 EXPECT_EQ(kSender2, last_message().sender_id); | |
| 1760 | |
| 1761 reset_last_event(); | |
| 1762 | |
| 1763 // Message from kSender3 will be dropped. | |
| 1764 MCSMessage message3( | |
| 1765 BuildDownstreamMessage(kSender3, kAppId, std::string() /* subtype */, | |
| 1766 expected_data, std::string() /* raw_data */)); | |
| 1767 EXPECT_TRUE(message3.IsValid()); | |
| 1768 ReceiveMessageFromMCS(message3); | |
| 1769 | |
| 1770 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | |
| 1771 EXPECT_NE(kAppId, last_app_id()); | |
| 1772 } | |
| 1773 | |
| 1774 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) { | |
| 1775 AddInstanceID(kAppId, kInstanceID); | |
| 1776 GetToken(kAppId, true /* use_subtype */, kSender, kScope); | |
| 1777 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | |
| 1778 GetToken(kAppId, true /* use_subtype */, kSender2, kScope); | |
| 1779 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | |
| 1780 | |
| 1781 std::map<std::string, std::string> expected_data; | |
| 1782 | |
| 1783 // Message for kSender without a subtype will be dropped. | |
| 1784 MCSMessage message0( | |
| 1785 BuildDownstreamMessage(kSender, kAppId, std::string() /* subtype */, | |
| 1786 expected_data, std::string() /* raw_data */)); | |
| 1787 EXPECT_TRUE(message0.IsValid()); | |
| 1788 ReceiveMessageFromMCS(message0); | |
| 1789 | |
| 1790 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | |
| 1791 | |
| 1792 reset_last_event(); | |
| 1793 | |
| 1794 // Message for kSender will be received. | |
| 1795 MCSMessage message1(BuildDownstreamMessage( | |
| 1796 kSender, kCategoryForSubtypes, kAppId /* subtype */, expected_data, | |
| 1797 std::string() /* raw_data */)); | |
| 1798 EXPECT_TRUE(message1.IsValid()); | |
| 1799 ReceiveMessageFromMCS(message1); | |
| 1800 | |
| 1801 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | |
| 1802 EXPECT_EQ(kAppId, last_app_id()); | |
| 1803 EXPECT_EQ(expected_data.size(), last_message().data.size()); | |
| 1804 EXPECT_EQ(expected_data, last_message().data); | |
| 1805 EXPECT_EQ(kSender, last_message().sender_id); | |
| 1806 | |
| 1807 reset_last_event(); | |
| 1808 | |
| 1809 // Message for kSender2 will be received. | |
| 1810 MCSMessage message2(BuildDownstreamMessage( | |
| 1811 kSender2, kCategoryForSubtypes, kAppId /* subtype */, expected_data, | |
| 1812 std::string() /* raw_data */)); | |
| 1813 EXPECT_TRUE(message2.IsValid()); | |
| 1814 ReceiveMessageFromMCS(message2); | |
| 1815 | |
| 1816 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | |
| 1817 EXPECT_EQ(kAppId, last_app_id()); | |
| 1818 EXPECT_EQ(expected_data.size(), last_message().data.size()); | |
| 1819 EXPECT_EQ(expected_data, last_message().data); | |
| 1820 EXPECT_EQ(kSender2, last_message().sender_id); | |
| 1821 | |
| 1822 reset_last_event(); | |
| 1823 | |
| 1824 // Message from kSender3 will be dropped. | |
| 1825 MCSMessage message3(BuildDownstreamMessage( | |
| 1826 kSender3, kCategoryForSubtypes, kAppId /* subtype */, expected_data, | |
| 1827 std::string() /* raw_data */)); | |
| 1828 EXPECT_TRUE(message3.IsValid()); | |
| 1829 ReceiveMessageFromMCS(message3); | |
| 1830 | |
| 1831 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | |
| 1832 EXPECT_NE(kAppId, last_app_id()); | |
| 1833 } | |
| 1834 | |
| 1835 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithFakeSubtype) { | |
| 1836 AddInstanceID("victim_app_id", "iid_1"); | |
| 1837 GetToken("victim_app_id", true /* use_subtype */, kSender, kScope); | |
| 1838 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); | |
| 1839 | |
| 1840 AddInstanceID("evil_app_id", "iid_2"); | |
| 1841 GetToken("evil_app_id", false /* use_subtype */, kSender, kScope); | |
| 1842 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); | |
| 1843 | |
| 1844 std::map<std::string, std::string> expected_data; | |
| 1845 | |
| 1846 // Message for "evil_app_id" should be delivered to evil app rather than | |
| 1847 // victim app, despite the malicious subtype property attempting to | |
| 1848 // impersonate victim app. | |
| 1849 MCSMessage message(BuildDownstreamMessage( | |
| 1850 kSender, "evil_app_id" /* category */, "victim_app_id" /* subtype */, | |
| 1851 expected_data, std::string() /* raw_data */)); | |
| 1852 EXPECT_TRUE(message.IsValid()); | |
| 1853 ReceiveMessageFromMCS(message); | |
| 1854 | |
| 1855 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | |
| 1856 EXPECT_EQ("evil_app_id", last_app_id()); | |
| 1857 EXPECT_EQ(expected_data.size(), last_message().data.size()); | |
| 1858 EXPECT_EQ(expected_data, last_message().data); | |
| 1859 EXPECT_EQ(kSender, last_message().sender_id); | |
| 1860 } | |
| 1861 | |
| 1641 } // namespace gcm | 1862 } // namespace gcm |
| OLD | NEW |