OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h" | 5 #include "components/cryptauth/cryptauth_gcm_manager_impl.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "components/cryptauth/pref_names.h" |
8 #include "components/gcm_driver/fake_gcm_driver.h" | 9 #include "components/gcm_driver/fake_gcm_driver.h" |
9 #include "components/gcm_driver/gcm_client.h" | 10 #include "components/gcm_driver/gcm_client.h" |
10 #include "components/prefs/testing_pref_service.h" | 11 #include "components/prefs/testing_pref_service.h" |
11 #include "components/proximity_auth/cryptauth/pref_names.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 using ::testing::_; | 15 using ::testing::_; |
16 using ::testing::SaveArg; | 16 using ::testing::SaveArg; |
17 | 17 |
18 namespace proximity_auth { | 18 namespace cryptauth { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth"; | 22 const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth"; |
23 const char kCryptAuthGCMSenderId[] = "381449029288"; | 23 const char kCryptAuthGCMSenderId[] = "381449029288"; |
24 const char kExistingGCMRegistrationId[] = "cirrus"; | 24 const char kExistingGCMRegistrationId[] = "cirrus"; |
25 const char kNewGCMRegistrationId[] = "stratus"; | 25 const char kNewGCMRegistrationId[] = "stratus"; |
26 const char kCryptAuthMessageCollapseKey[] = | 26 const char kCryptAuthMessageCollapseKey[] = |
27 "collapse_cryptauth_sync_DEVICES_SYNC"; | 27 "collapse_cryptauth_sync_DEVICES_SYNC"; |
28 | 28 |
(...skipping 11 matching lines...) Expand all Loading... |
40 const std::vector<std::string>& sender_ids)); | 40 const std::vector<std::string>& sender_ids)); |
41 | 41 |
42 using gcm::GCMDriver::RegisterFinished; | 42 using gcm::GCMDriver::RegisterFinished; |
43 | 43 |
44 private: | 44 private: |
45 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); | 45 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver); |
46 }; | 46 }; |
47 | 47 |
48 } // namespace | 48 } // namespace |
49 | 49 |
50 class ProximityAuthCryptAuthGCMManagerImplTest | 50 class CryptAuthGCMManagerImplTest |
51 : public testing::Test, | 51 : public testing::Test, |
52 public CryptAuthGCMManager::Observer { | 52 public CryptAuthGCMManager::Observer { |
53 protected: | 53 protected: |
54 ProximityAuthCryptAuthGCMManagerImplTest() | 54 CryptAuthGCMManagerImplTest() |
55 : gcm_manager_(&gcm_driver_, &pref_service_) {} | 55 : gcm_manager_(&gcm_driver_, &pref_service_) {} |
56 | 56 |
57 // testing::Test: | 57 // testing::Test: |
58 void SetUp() override { | 58 void SetUp() override { |
59 CryptAuthGCMManager::RegisterPrefs(pref_service_.registry()); | 59 CryptAuthGCMManager::RegisterPrefs(pref_service_.registry()); |
60 gcm_manager_.AddObserver(this); | 60 gcm_manager_.AddObserver(this); |
61 EXPECT_CALL(gcm_driver_, AddAppHandler(kCryptAuthGCMAppId, &gcm_manager_)); | 61 EXPECT_CALL(gcm_driver_, AddAppHandler(kCryptAuthGCMAppId, &gcm_manager_)); |
62 gcm_manager_.StartListening(); | 62 gcm_manager_.StartListening(); |
63 } | 63 } |
64 | 64 |
(...skipping 26 matching lines...) Expand all Loading... |
91 MOCK_METHOD1(OnGCMRegistrationResultProxy, void(bool)); | 91 MOCK_METHOD1(OnGCMRegistrationResultProxy, void(bool)); |
92 MOCK_METHOD0(OnReenrollMessageProxy, void()); | 92 MOCK_METHOD0(OnReenrollMessageProxy, void()); |
93 MOCK_METHOD0(OnResyncMessageProxy, void()); | 93 MOCK_METHOD0(OnResyncMessageProxy, void()); |
94 | 94 |
95 testing::StrictMock<MockGCMDriver> gcm_driver_; | 95 testing::StrictMock<MockGCMDriver> gcm_driver_; |
96 | 96 |
97 TestingPrefServiceSimple pref_service_; | 97 TestingPrefServiceSimple pref_service_; |
98 | 98 |
99 CryptAuthGCMManagerImpl gcm_manager_; | 99 CryptAuthGCMManagerImpl gcm_manager_; |
100 | 100 |
101 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthGCMManagerImplTest); | 101 DISALLOW_COPY_AND_ASSIGN(CryptAuthGCMManagerImplTest); |
102 }; | 102 }; |
103 | 103 |
104 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterPrefs) { | 104 TEST_F(CryptAuthGCMManagerImplTest, RegisterPrefs) { |
105 TestingPrefServiceSimple pref_service; | 105 TestingPrefServiceSimple pref_service; |
106 CryptAuthGCMManager::RegisterPrefs(pref_service.registry()); | 106 CryptAuthGCMManager::RegisterPrefs(pref_service.registry()); |
107 EXPECT_TRUE(pref_service.FindPreference(prefs::kCryptAuthGCMRegistrationId)); | 107 EXPECT_TRUE(pref_service.FindPreference(prefs::kCryptAuthGCMRegistrationId)); |
108 } | 108 } |
109 | 109 |
110 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegistrationSucceeds) { | 110 TEST_F(CryptAuthGCMManagerImplTest, RegistrationSucceeds) { |
111 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | 111 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); |
112 RegisterWithGCM(gcm::GCMClient::SUCCESS); | 112 RegisterWithGCM(gcm::GCMClient::SUCCESS); |
113 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 113 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
114 } | 114 } |
115 | 115 |
116 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, | 116 TEST_F(CryptAuthGCMManagerImplTest, |
117 RegistrationSucceedsWithExistingRegistration) { | 117 RegistrationSucceedsWithExistingRegistration) { |
118 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId, | 118 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId, |
119 kExistingGCMRegistrationId); | 119 kExistingGCMRegistrationId); |
120 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 120 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
121 RegisterWithGCM(gcm::GCMClient::SUCCESS); | 121 RegisterWithGCM(gcm::GCMClient::SUCCESS); |
122 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 122 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
123 EXPECT_EQ(kNewGCMRegistrationId, | 123 EXPECT_EQ(kNewGCMRegistrationId, |
124 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); | 124 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); |
125 } | 125 } |
126 | 126 |
127 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterWithGCMFails) { | 127 TEST_F(CryptAuthGCMManagerImplTest, RegisterWithGCMFails) { |
128 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | 128 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); |
129 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR); | 129 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR); |
130 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | 130 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); |
131 EXPECT_EQ(std::string(), | 131 EXPECT_EQ(std::string(), |
132 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); | 132 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); |
133 } | 133 } |
134 | 134 |
135 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, | 135 TEST_F(CryptAuthGCMManagerImplTest, |
136 RegisterWithGCMFailsWithExistingRegistration) { | 136 RegisterWithGCMFailsWithExistingRegistration) { |
137 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId, | 137 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId, |
138 kExistingGCMRegistrationId); | 138 kExistingGCMRegistrationId); |
139 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 139 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
140 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR); | 140 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR); |
141 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 141 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
142 EXPECT_EQ(kExistingGCMRegistrationId, | 142 EXPECT_EQ(kExistingGCMRegistrationId, |
143 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); | 143 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId)); |
144 } | 144 } |
145 | 145 |
146 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, | 146 TEST_F(CryptAuthGCMManagerImplTest, |
147 RegistrationFailsThenSucceeds) { | 147 RegistrationFailsThenSucceeds) { |
148 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | 148 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); |
149 RegisterWithGCM(gcm::GCMClient::NETWORK_ERROR); | 149 RegisterWithGCM(gcm::GCMClient::NETWORK_ERROR); |
150 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | 150 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); |
151 RegisterWithGCM(gcm::GCMClient::SUCCESS); | 151 RegisterWithGCM(gcm::GCMClient::SUCCESS); |
152 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 152 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
153 } | 153 } |
154 | 154 |
155 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ConcurrentRegistrations) { | 155 TEST_F(CryptAuthGCMManagerImplTest, ConcurrentRegistrations) { |
156 // If multiple RegisterWithGCM() calls are made concurrently, only one | 156 // If multiple RegisterWithGCM() calls are made concurrently, only one |
157 // registration attempt should actually be made. | 157 // registration attempt should actually be made. |
158 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _)); | 158 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _)); |
159 gcm_manager_.RegisterWithGCM(); | 159 gcm_manager_.RegisterWithGCM(); |
160 gcm_manager_.RegisterWithGCM(); | 160 gcm_manager_.RegisterWithGCM(); |
161 gcm_manager_.RegisterWithGCM(); | 161 gcm_manager_.RegisterWithGCM(); |
162 | 162 |
163 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(true)); | 163 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(true)); |
164 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId, | 164 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId, |
165 gcm::GCMClient::SUCCESS); | 165 gcm::GCMClient::SUCCESS); |
166 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | 166 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); |
167 } | 167 } |
168 | 168 |
169 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ReenrollmentMessagesReceived) { | 169 TEST_F(CryptAuthGCMManagerImplTest, ReenrollmentMessagesReceived) { |
170 EXPECT_CALL(*this, OnReenrollMessageProxy()).Times(2); | 170 EXPECT_CALL(*this, OnReenrollMessageProxy()).Times(2); |
171 | 171 |
172 gcm::IncomingMessage message; | 172 gcm::IncomingMessage message; |
173 message.data["registrationTickleType"] = "1"; // FORCE_ENROLLMENT | 173 message.data["registrationTickleType"] = "1"; // FORCE_ENROLLMENT |
174 message.collapse_key = kCryptAuthMessageCollapseKey; | 174 message.collapse_key = kCryptAuthMessageCollapseKey; |
175 message.sender_id = kCryptAuthGCMSenderId; | 175 message.sender_id = kCryptAuthGCMSenderId; |
176 | 176 |
177 gcm::GCMAppHandler* gcm_app_handler = | 177 gcm::GCMAppHandler* gcm_app_handler = |
178 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); | 178 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); |
179 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | 179 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); |
180 message.data["registrationTickleType"] = "2"; // UPDATE_ENROLLMENT | 180 message.data["registrationTickleType"] = "2"; // UPDATE_ENROLLMENT |
181 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | 181 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); |
182 } | 182 } |
183 | 183 |
184 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ResyncMessagesReceived) { | 184 TEST_F(CryptAuthGCMManagerImplTest, ResyncMessagesReceived) { |
185 EXPECT_CALL(*this, OnResyncMessageProxy()).Times(2); | 185 EXPECT_CALL(*this, OnResyncMessageProxy()).Times(2); |
186 | 186 |
187 gcm::IncomingMessage message; | 187 gcm::IncomingMessage message; |
188 message.data["registrationTickleType"] = "3"; // DEVICES_SYNC | 188 message.data["registrationTickleType"] = "3"; // DEVICES_SYNC |
189 message.collapse_key = kCryptAuthMessageCollapseKey; | 189 message.collapse_key = kCryptAuthMessageCollapseKey; |
190 message.sender_id = kCryptAuthGCMSenderId; | 190 message.sender_id = kCryptAuthGCMSenderId; |
191 | 191 |
192 gcm::GCMAppHandler* gcm_app_handler = | 192 gcm::GCMAppHandler* gcm_app_handler = |
193 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); | 193 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); |
194 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | 194 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); |
195 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | 195 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); |
196 } | 196 } |
197 | 197 |
198 } // namespace proximity_auth | 198 } // namespace cryptauth |
OLD | NEW |