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 <initializer_list> |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/metrics/field_trial.h" | |
19 #include "base/metrics/field_trial_param_associator.h" | |
20 #include "base/metrics/field_trial_params.h" | |
18 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
19 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
23 #include "base/test/mock_entropy_provider.h" | |
24 #include "base/test/scoped_feature_list.h" | |
20 #include "base/test/test_mock_time_task_runner.h" | 25 #include "base/test/test_mock_time_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/time/clock.h" | 27 #include "base/time/clock.h" |
23 #include "base/timer/timer.h" | 28 #include "base/timer/timer.h" |
24 #include "google_apis/gcm/base/fake_encryptor.h" | 29 #include "google_apis/gcm/base/fake_encryptor.h" |
25 #include "google_apis/gcm/base/mcs_message.h" | 30 #include "google_apis/gcm/base/mcs_message.h" |
26 #include "google_apis/gcm/base/mcs_util.h" | 31 #include "google_apis/gcm/base/mcs_util.h" |
27 #include "google_apis/gcm/engine/fake_connection_factory.h" | 32 #include "google_apis/gcm/engine/fake_connection_factory.h" |
28 #include "google_apis/gcm/engine/fake_connection_handler.h" | 33 #include "google_apis/gcm/engine/fake_connection_handler.h" |
29 #include "google_apis/gcm/engine/gservices_settings.h" | 34 #include "google_apis/gcm/engine/gservices_settings.h" |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 | 266 |
262 class GCMClientImplTest : public testing::Test, | 267 class GCMClientImplTest : public testing::Test, |
263 public GCMClient::Delegate { | 268 public GCMClient::Delegate { |
264 public: | 269 public: |
265 GCMClientImplTest(); | 270 GCMClientImplTest(); |
266 ~GCMClientImplTest() override; | 271 ~GCMClientImplTest() override; |
267 | 272 |
268 void SetUp() override; | 273 void SetUp() override; |
269 | 274 |
270 void SetUpUrlFetcherFactory(); | 275 void SetUpUrlFetcherFactory(); |
276 void InitializeFieldTrials(); | |
271 | 277 |
272 void BuildGCMClient(base::TimeDelta clock_step); | 278 void BuildGCMClient(base::TimeDelta clock_step); |
273 void InitializeGCMClient(); | 279 void InitializeGCMClient(); |
274 void StartGCMClient(); | 280 void StartGCMClient(); |
275 void Register(const std::string& app_id, | 281 void Register(const std::string& app_id, |
276 const std::vector<std::string>& senders); | 282 const std::vector<std::string>& senders); |
277 void Unregister(const std::string& app_id); | 283 void Unregister(const std::string& app_id); |
278 void ReceiveMessageFromMCS(const MCSMessage& message); | 284 void ReceiveMessageFromMCS(const MCSMessage& message); |
279 void ReceiveOnMessageSentToMCS( | 285 void ReceiveOnMessageSentToMCS( |
280 const std::string& app_id, | 286 const std::string& app_id, |
(...skipping 21 matching lines...) Expand all Loading... | |
302 // GCMClient::Delegate overrides (for verification). | 308 // GCMClient::Delegate overrides (for verification). |
303 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, | 309 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, |
304 const std::string& registration_id, | 310 const std::string& registration_id, |
305 GCMClient::Result result) override; | 311 GCMClient::Result result) override; |
306 void OnUnregisterFinished( | 312 void OnUnregisterFinished( |
307 const linked_ptr<RegistrationInfo>& registration_info, | 313 const linked_ptr<RegistrationInfo>& registration_info, |
308 GCMClient::Result result) override; | 314 GCMClient::Result result) override; |
309 void OnSendFinished(const std::string& app_id, | 315 void OnSendFinished(const std::string& app_id, |
310 const std::string& message_id, | 316 const std::string& message_id, |
311 GCMClient::Result result) override {} | 317 GCMClient::Result result) override {} |
312 void OnMessageReceived(const std::string& registration_id, | 318 void OnMessageReceived( |
313 const IncomingMessage& message) override; | 319 const std::string& registration_id, |
320 const IncomingMessage& message, | |
321 const MessageReceiptCallback& optional_receipt_callback) override; | |
314 void OnMessagesDeleted(const std::string& app_id) override; | 322 void OnMessagesDeleted(const std::string& app_id) override; |
315 void OnMessageSendError( | 323 void OnMessageSendError( |
316 const std::string& app_id, | 324 const std::string& app_id, |
317 const gcm::GCMClient::SendErrorDetails& send_error_details) override; | 325 const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
318 void OnSendAcknowledged(const std::string& app_id, | 326 void OnSendAcknowledged(const std::string& app_id, |
319 const std::string& message_id) override; | 327 const std::string& message_id) override; |
320 void OnGCMReady(const std::vector<AccountMapping>& account_mappings, | 328 void OnGCMReady(const std::vector<AccountMapping>& account_mappings, |
321 const base::Time& last_token_fetch_time) override; | 329 const base::Time& last_token_fetch_time) override; |
322 void OnActivityRecorded() override {} | 330 void OnActivityRecorded() override {} |
323 void OnConnected(const net::IPEndPoint& ip_endpoint) override {} | 331 void OnConnected(const net::IPEndPoint& ip_endpoint) override {} |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 // Variables used for verification. | 413 // Variables used for verification. |
406 LastEvent last_event_; | 414 LastEvent last_event_; |
407 std::string last_app_id_; | 415 std::string last_app_id_; |
408 std::string last_registration_id_; | 416 std::string last_registration_id_; |
409 std::string last_message_id_; | 417 std::string last_message_id_; |
410 GCMClient::Result last_result_; | 418 GCMClient::Result last_result_; |
411 IncomingMessage last_message_; | 419 IncomingMessage last_message_; |
412 GCMClient::SendErrorDetails last_error_details_; | 420 GCMClient::SendErrorDetails last_error_details_; |
413 base::Time last_token_fetch_time_; | 421 base::Time last_token_fetch_time_; |
414 std::vector<AccountMapping> last_account_mappings_; | 422 std::vector<AccountMapping> last_account_mappings_; |
423 base::test::ScopedFeatureList scoped_feature_list_; | |
424 std::unique_ptr<base::FieldTrialList> field_trial_list_; | |
415 | 425 |
416 std::unique_ptr<GCMClientImpl> gcm_client_; | 426 std::unique_ptr<GCMClientImpl> gcm_client_; |
417 | 427 |
418 net::TestURLFetcherFactory url_fetcher_factory_; | 428 net::TestURLFetcherFactory url_fetcher_factory_; |
419 | 429 |
420 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 430 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
421 base::ThreadTaskRunnerHandle task_runner_handle_; | 431 base::ThreadTaskRunnerHandle task_runner_handle_; |
422 | 432 |
423 // Injected to GCM client. | 433 // Injected to GCM client. |
424 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 434 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
(...skipping 10 matching lines...) Expand all Loading... | |
435 | 445 |
436 GCMClientImplTest::~GCMClientImplTest() {} | 446 GCMClientImplTest::~GCMClientImplTest() {} |
437 | 447 |
438 void GCMClientImplTest::SetUp() { | 448 void GCMClientImplTest::SetUp() { |
439 testing::Test::SetUp(); | 449 testing::Test::SetUp(); |
440 ASSERT_TRUE(CreateUniqueTempDir()); | 450 ASSERT_TRUE(CreateUniqueTempDir()); |
441 BuildGCMClient(base::TimeDelta()); | 451 BuildGCMClient(base::TimeDelta()); |
442 InitializeGCMClient(); | 452 InitializeGCMClient(); |
443 StartGCMClient(); | 453 StartGCMClient(); |
444 SetUpUrlFetcherFactory(); | 454 SetUpUrlFetcherFactory(); |
455 InitializeFieldTrials(); | |
445 ASSERT_NO_FATAL_FAILURE( | 456 ASSERT_NO_FATAL_FAILURE( |
446 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(), | 457 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(), |
447 std::map<std::string, std::string>())); | 458 std::map<std::string, std::string>())); |
448 } | 459 } |
449 | 460 |
450 void GCMClientImplTest::SetUpUrlFetcherFactory() { | 461 void GCMClientImplTest::SetUpUrlFetcherFactory() { |
451 url_fetcher_factory_.set_remove_fetcher_on_delete(true); | 462 url_fetcher_factory_.set_remove_fetcher_on_delete(true); |
452 } | 463 } |
453 | 464 |
465 void GCMClientImplTest::InitializeFieldTrials() { | |
466 const std::string kTrialName = "TestGCMMessageReceipts"; | |
467 const std::string kGroupName = "UnusedName"; | |
468 // kFeatureName and kParam name are also defined in gcm_client_impl.cc. | |
469 const std::string kFeatureName = "GCMMessageReceiptsFeature"; | |
470 const std::string kParamName = "message_receipts_to_send"; | |
471 | |
472 // Create the global list of field trials. | |
473 field_trial_list_ = base::MakeUnique<base::FieldTrialList>( | |
474 base::MakeUnique<base::MockEntropyProvider>()); | |
475 | |
476 // Create a field trial with all message receipts enabled. | |
477 scoped_refptr<base::FieldTrial> trial = | |
478 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); | |
479 std::map<std::string, std::string> trial_params; | |
480 trial_params[kParamName] = "2"; // SEND_ALL. | |
481 base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( | |
482 kTrialName, kGroupName, trial_params); | |
483 | |
484 // Create a FeatureList and add the new trial to the list. | |
485 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
486 feature_list->RegisterFieldTrialOverride( | |
487 kFeatureName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); | |
488 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); | |
Peter Beverloo
2017/02/15 15:17:15
Can we have a test for making sure that *disabling
harkness
2017/02/22 17:19:32
Done.
| |
489 } | |
490 | |
454 void GCMClientImplTest::PumpLoopUntilIdle() { | 491 void GCMClientImplTest::PumpLoopUntilIdle() { |
455 task_runner_->RunUntilIdle(); | 492 task_runner_->RunUntilIdle(); |
456 } | 493 } |
457 | 494 |
458 bool GCMClientImplTest::CreateUniqueTempDir() { | 495 bool GCMClientImplTest::CreateUniqueTempDir() { |
459 return temp_directory_.CreateUniqueTempDir(); | 496 return temp_directory_.CreateUniqueTempDir(); |
460 } | 497 } |
461 | 498 |
462 void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) { | 499 void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) { |
463 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>( | 500 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>( |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 } | 646 } |
610 | 647 |
611 void GCMClientImplTest::OnGCMReady( | 648 void GCMClientImplTest::OnGCMReady( |
612 const std::vector<AccountMapping>& account_mappings, | 649 const std::vector<AccountMapping>& account_mappings, |
613 const base::Time& last_token_fetch_time) { | 650 const base::Time& last_token_fetch_time) { |
614 last_event_ = LOADING_COMPLETED; | 651 last_event_ = LOADING_COMPLETED; |
615 last_account_mappings_ = account_mappings; | 652 last_account_mappings_ = account_mappings; |
616 last_token_fetch_time_ = last_token_fetch_time; | 653 last_token_fetch_time_ = last_token_fetch_time; |
617 } | 654 } |
618 | 655 |
619 void GCMClientImplTest::OnMessageReceived(const std::string& registration_id, | 656 void GCMClientImplTest::OnMessageReceived( |
620 const IncomingMessage& message) { | 657 const std::string& registration_id, |
658 const IncomingMessage& message, | |
659 const MessageReceiptCallback& optional_receipt_callback) { | |
621 last_event_ = MESSAGE_RECEIVED; | 660 last_event_ = MESSAGE_RECEIVED; |
622 last_app_id_ = registration_id; | 661 last_app_id_ = registration_id; |
623 last_message_ = message; | 662 last_message_ = message; |
624 } | 663 } |
625 | 664 |
626 void GCMClientImplTest::OnRegisterFinished( | 665 void GCMClientImplTest::OnRegisterFinished( |
627 const linked_ptr<RegistrationInfo>& registration_info, | 666 const linked_ptr<RegistrationInfo>& registration_info, |
628 const std::string& registration_id, | 667 const std::string& registration_id, |
629 GCMClient::Result result) { | 668 GCMClient::Result result) { |
630 last_event_ = REGISTRATION_COMPLETED; | 669 last_event_ = REGISTRATION_COMPLETED; |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
929 | 968 |
930 reset_last_event(); | 969 reset_last_event(); |
931 | 970 |
932 // Message from kSender3 will be dropped. | 971 // Message from kSender3 will be dropped. |
933 MCSMessage message3(BuildDownstreamMessage( | 972 MCSMessage message3(BuildDownstreamMessage( |
934 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data, | 973 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data, |
935 std::string() /* raw_data */)); | 974 std::string() /* raw_data */)); |
936 EXPECT_TRUE(message3.IsValid()); | 975 EXPECT_TRUE(message3.IsValid()); |
937 ReceiveMessageFromMCS(message3); | 976 ReceiveMessageFromMCS(message3); |
938 | 977 |
978 // TODO(harkness): Add a check for invalid app handler once the | |
979 // DefaultAppHandler is removed. | |
980 | |
939 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | 981 EXPECT_NE(MESSAGE_RECEIVED, last_event()); |
940 EXPECT_NE(kExtensionAppId, last_app_id()); | 982 EXPECT_NE(kExtensionAppId, last_app_id()); |
941 } | 983 } |
942 | 984 |
943 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { | 985 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { |
944 std::vector<std::string> senders(1, kSender); | 986 std::vector<std::string> senders(1, kSender); |
945 AddRegistration(kExtensionAppId, senders, "reg_id"); | 987 AddRegistration(kExtensionAppId, senders, "reg_id"); |
946 | 988 |
947 std::map<std::string, std::string> expected_data; | 989 std::map<std::string, std::string> expected_data; |
948 | 990 |
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1782 | 1824 |
1783 // Message for kSender with a subtype will be dropped. | 1825 // Message for kSender with a subtype will be dropped. |
1784 MCSMessage message0(BuildDownstreamMessage( | 1826 MCSMessage message0(BuildDownstreamMessage( |
1785 kSender, kProductCategoryForSubtypes, kExtensionAppId /* subtype */, | 1827 kSender, kProductCategoryForSubtypes, kExtensionAppId /* subtype */, |
1786 expected_data, std::string() /* raw_data */)); | 1828 expected_data, std::string() /* raw_data */)); |
1787 EXPECT_TRUE(message0.IsValid()); | 1829 EXPECT_TRUE(message0.IsValid()); |
1788 ReceiveMessageFromMCS(message0); | 1830 ReceiveMessageFromMCS(message0); |
1789 | 1831 |
1790 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | 1832 EXPECT_NE(MESSAGE_RECEIVED, last_event()); |
1791 | 1833 |
1834 // Check that the last message sent was a message receipt for a failed message | |
1835 // and that it had the values expected. | |
1836 mcs_proto::DataMessageStanza stanza = | |
1837 mcs_client()->last_data_message_stanza(); | |
1838 EXPECT_EQ(kExtensionAppId, stanza.category()); | |
1839 const auto& app_data = stanza.app_data(); | |
1840 ASSERT_EQ(3, app_data.size()); | |
1841 for (const auto& pair : app_data) { | |
1842 if (pair.key() == "message_id") | |
1843 EXPECT_EQ("", pair.value()); | |
1844 else if (pair.key() == "status") | |
1845 EXPECT_EQ("2", pair.value()); | |
1846 else if (pair.key() == "message_type") | |
1847 EXPECT_EQ("message_receipt", pair.value()); | |
1848 else | |
1849 FAIL() << "Unexpected key: " << pair.key(); | |
1850 } | |
1851 | |
1792 reset_last_event(); | 1852 reset_last_event(); |
1793 | 1853 |
1794 // Message for kSender will be received. | 1854 // Message for kSender will be received. |
1795 MCSMessage message1(BuildDownstreamMessage( | 1855 MCSMessage message1(BuildDownstreamMessage( |
1796 kSender, kExtensionAppId, std::string() /* subtype */, expected_data, | 1856 kSender, kExtensionAppId, std::string() /* subtype */, expected_data, |
1797 std::string() /* raw_data */)); | 1857 std::string() /* raw_data */)); |
1798 EXPECT_TRUE(message1.IsValid()); | 1858 EXPECT_TRUE(message1.IsValid()); |
1799 ReceiveMessageFromMCS(message1); | 1859 ReceiveMessageFromMCS(message1); |
1800 | 1860 |
1801 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 1861 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1843 | 1903 |
1844 // Message for kSender without a subtype will be dropped. | 1904 // Message for kSender without a subtype will be dropped. |
1845 MCSMessage message0(BuildDownstreamMessage( | 1905 MCSMessage message0(BuildDownstreamMessage( |
1846 kSender, kSubtypeAppId, std::string() /* subtype */, expected_data, | 1906 kSender, kSubtypeAppId, std::string() /* subtype */, expected_data, |
1847 std::string() /* raw_data */)); | 1907 std::string() /* raw_data */)); |
1848 EXPECT_TRUE(message0.IsValid()); | 1908 EXPECT_TRUE(message0.IsValid()); |
1849 ReceiveMessageFromMCS(message0); | 1909 ReceiveMessageFromMCS(message0); |
1850 | 1910 |
1851 EXPECT_NE(MESSAGE_RECEIVED, last_event()); | 1911 EXPECT_NE(MESSAGE_RECEIVED, last_event()); |
1852 | 1912 |
1913 // Check that the last message sent was a message receipt for a failed message | |
1914 // and that it had the values expected. | |
1915 mcs_proto::DataMessageStanza stanza = | |
1916 mcs_client()->last_data_message_stanza(); | |
1917 EXPECT_EQ(kSubtypeAppId, stanza.category()); | |
1918 const auto& app_data = stanza.app_data(); | |
1919 ASSERT_EQ(3, app_data.size()); | |
1920 for (const auto& pair : app_data) { | |
1921 if (pair.key() == "message_id") | |
1922 EXPECT_EQ("", pair.value()); | |
1923 else if (pair.key() == "status") | |
1924 EXPECT_EQ("2", pair.value()); | |
1925 else if (pair.key() == "message_type") | |
1926 EXPECT_EQ("message_receipt", pair.value()); | |
1927 else | |
1928 FAIL() << "Unexpected key: " << pair.key(); | |
1929 } | |
1930 | |
1853 reset_last_event(); | 1931 reset_last_event(); |
1854 | 1932 |
1855 // Message for kSender will be received. | 1933 // Message for kSender will be received. |
1856 MCSMessage message1(BuildDownstreamMessage( | 1934 MCSMessage message1(BuildDownstreamMessage( |
1857 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */, | 1935 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */, |
1858 expected_data, std::string() /* raw_data */)); | 1936 expected_data, std::string() /* raw_data */)); |
1859 EXPECT_TRUE(message1.IsValid()); | 1937 EXPECT_TRUE(message1.IsValid()); |
1860 ReceiveMessageFromMCS(message1); | 1938 ReceiveMessageFromMCS(message1); |
1861 | 1939 |
1862 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 1940 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1916 ReceiveMessageFromMCS(message); | 1994 ReceiveMessageFromMCS(message); |
1917 | 1995 |
1918 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); | 1996 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
1919 EXPECT_EQ(kExtensionAppId, last_app_id()); | 1997 EXPECT_EQ(kExtensionAppId, last_app_id()); |
1920 EXPECT_EQ(expected_data.size(), last_message().data.size()); | 1998 EXPECT_EQ(expected_data.size(), last_message().data.size()); |
1921 EXPECT_EQ(expected_data, last_message().data); | 1999 EXPECT_EQ(expected_data, last_message().data); |
1922 EXPECT_EQ(kSender, last_message().sender_id); | 2000 EXPECT_EQ(kSender, last_message().sender_id); |
1923 } | 2001 } |
1924 | 2002 |
1925 } // namespace gcm | 2003 } // namespace gcm |
OLD | NEW |