Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_gcm_manager_impl_unittest.cc
blob9b93bca91a01f10bbd9dadca30eff611f8f83588
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h"
7 #include "base/prefs/testing_pref_service.h"
8 #include "components/gcm_driver/fake_gcm_driver.h"
9 #include "components/gcm_driver/gcm_client.h"
10 #include "components/proximity_auth/cryptauth/pref_names.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::_;
15 using ::testing::SaveArg;
17 namespace proximity_auth {
19 namespace {
21 const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth";
22 const char kCryptAuthGCMSenderId[] = "381449029288";
23 const char kExistingGCMRegistrationId[] = "cirrus";
24 const char kNewGCMRegistrationId[] = "stratus";
25 const char kCryptAuthMessageCollapseKey[] =
26 "collapse_cryptauth_sync_DEVICES_SYNC";
28 // Mock GCMDriver implementation for testing.
29 class MockGCMDriver : public gcm::FakeGCMDriver {
30 public:
31 MockGCMDriver() {}
32 ~MockGCMDriver() override {}
34 MOCK_METHOD2(AddAppHandler,
35 void(const std::string& app_id, gcm::GCMAppHandler* handler));
37 MOCK_METHOD2(RegisterImpl,
38 void(const std::string& app_id,
39 const std::vector<std::string>& sender_ids));
41 using gcm::GCMDriver::RegisterFinished;
43 private:
44 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver);
47 } // namespace
49 class ProximityAuthCryptAuthGCMManagerImplTest
50 : public testing::Test,
51 public CryptAuthGCMManager::Observer {
52 protected:
53 ProximityAuthCryptAuthGCMManagerImplTest()
54 : gcm_manager_(&gcm_driver_, &pref_service_) {}
56 // testing::Test:
57 void SetUp() override {
58 CryptAuthGCMManager::RegisterPrefs(pref_service_.registry());
59 gcm_manager_.AddObserver(this);
60 EXPECT_CALL(gcm_driver_, AddAppHandler(kCryptAuthGCMAppId, &gcm_manager_));
61 gcm_manager_.StartListening();
64 void TearDown() override { gcm_manager_.RemoveObserver(this); }
66 void RegisterWithGCM(gcm::GCMClient::Result registration_result) {
67 std::vector<std::string> sender_ids;
68 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _))
69 .WillOnce(SaveArg<1>(&sender_ids));
70 gcm_manager_.RegisterWithGCM();
72 ASSERT_EQ(1u, sender_ids.size());
73 EXPECT_EQ(kCryptAuthGCMSenderId, sender_ids[0]);
75 bool success = (registration_result == gcm::GCMClient::SUCCESS);
76 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(success));
77 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId,
78 registration_result);
81 // CryptAuthGCMManager::Observer:
82 void OnGCMRegistrationResult(bool success) override {
83 OnGCMRegistrationResultProxy(success);
86 void OnReenrollMessage() override { OnReenrollMessageProxy(); }
88 void OnResyncMessage() override { OnResyncMessageProxy(); }
90 MOCK_METHOD1(OnGCMRegistrationResultProxy, void(bool));
91 MOCK_METHOD0(OnReenrollMessageProxy, void());
92 MOCK_METHOD0(OnResyncMessageProxy, void());
94 testing::StrictMock<MockGCMDriver> gcm_driver_;
96 TestingPrefServiceSimple pref_service_;
98 CryptAuthGCMManagerImpl gcm_manager_;
100 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthGCMManagerImplTest);
103 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterPrefs) {
104 TestingPrefServiceSimple pref_service;
105 CryptAuthGCMManager::RegisterPrefs(pref_service.registry());
106 EXPECT_TRUE(pref_service.FindPreference(prefs::kCryptAuthGCMRegistrationId));
109 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegistrationSucceeds) {
110 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
111 RegisterWithGCM(gcm::GCMClient::SUCCESS);
112 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
115 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
116 RegistrationSucceedsWithExistingRegistration) {
117 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId,
118 kExistingGCMRegistrationId);
119 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
120 RegisterWithGCM(gcm::GCMClient::SUCCESS);
121 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
122 EXPECT_EQ(kNewGCMRegistrationId,
123 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
126 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterWithGCMFails) {
127 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
128 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR);
129 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
130 EXPECT_EQ(std::string(),
131 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
134 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
135 RegisterWithGCMFailsWithExistingRegistration) {
136 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId,
137 kExistingGCMRegistrationId);
138 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
139 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR);
140 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
141 EXPECT_EQ(kExistingGCMRegistrationId,
142 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
145 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
146 RegistrationFailsThenSucceeds) {
147 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
148 RegisterWithGCM(gcm::GCMClient::NETWORK_ERROR);
149 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
150 RegisterWithGCM(gcm::GCMClient::SUCCESS);
151 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
154 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ConcurrentRegistrations) {
155 // If multiple RegisterWithGCM() calls are made concurrently, only one
156 // registration attempt should actually be made.
157 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _));
158 gcm_manager_.RegisterWithGCM();
159 gcm_manager_.RegisterWithGCM();
160 gcm_manager_.RegisterWithGCM();
162 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(true));
163 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId,
164 gcm::GCMClient::SUCCESS);
165 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
168 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ReenrollmentMessagesReceived) {
169 EXPECT_CALL(*this, OnReenrollMessageProxy()).Times(2);
171 gcm::IncomingMessage message;
172 message.data["registrationTickleType"] = "1"; // FORCE_ENROLLMENT
173 message.collapse_key = kCryptAuthMessageCollapseKey;
174 message.sender_id = kCryptAuthGCMSenderId;
176 gcm::GCMAppHandler* gcm_app_handler =
177 static_cast<gcm::GCMAppHandler*>(&gcm_manager_);
178 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
179 message.data["registrationTickleType"] = "2"; // UPDATE_ENROLLMENT
180 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
183 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ResyncMessagesReceived) {
184 EXPECT_CALL(*this, OnResyncMessageProxy()).Times(2);
186 gcm::IncomingMessage message;
187 message.data["registrationTickleType"] = "3"; // DEVICES_SYNC
188 message.collapse_key = kCryptAuthMessageCollapseKey;
189 message.sender_id = kCryptAuthGCMSenderId;
191 gcm::GCMAppHandler* gcm_app_handler =
192 static_cast<gcm::GCMAppHandler*>(&gcm_manager_);
193 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
194 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
197 } // namespace proximity_auth