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_device_manager.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_device_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/base64url.h" | 11 #include "base/base64url.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
14 #include "base/test/simple_test_clock.h" | 16 #include "base/test/simple_test_clock.h" |
15 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
16 #include "components/prefs/testing_pref_service.h" | 18 #include "components/prefs/testing_pref_service.h" |
17 #include "components/proximity_auth/cryptauth/fake_cryptauth_gcm_manager.h" | 19 #include "components/proximity_auth/cryptauth/fake_cryptauth_gcm_manager.h" |
18 #include "components/proximity_auth/cryptauth/mock_cryptauth_client.h" | 20 #include "components/proximity_auth/cryptauth/mock_cryptauth_client.h" |
19 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h" | 21 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h" |
20 #include "components/proximity_auth/cryptauth/pref_names.h" | 22 #include "components/proximity_auth/cryptauth/pref_names.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 EXPECT_EQ(expected_unlock_key.friendly_device_name(), device_name); | 108 EXPECT_EQ(expected_unlock_key.friendly_device_name(), device_name); |
107 EXPECT_EQ(expected_unlock_key.bluetooth_address(), bluetooth_address); | 109 EXPECT_EQ(expected_unlock_key.bluetooth_address(), bluetooth_address); |
108 EXPECT_TRUE(expected_unlock_key.unlock_key()); | 110 EXPECT_TRUE(expected_unlock_key.unlock_key()); |
109 EXPECT_FALSE(expected_unlock_key.unlockable()); | 111 EXPECT_FALSE(expected_unlock_key.unlockable()); |
110 } | 112 } |
111 } | 113 } |
112 | 114 |
113 // Harness for testing CryptAuthDeviceManager. | 115 // Harness for testing CryptAuthDeviceManager. |
114 class TestCryptAuthDeviceManager : public CryptAuthDeviceManager { | 116 class TestCryptAuthDeviceManager : public CryptAuthDeviceManager { |
115 public: | 117 public: |
116 TestCryptAuthDeviceManager(scoped_ptr<base::Clock> clock, | 118 TestCryptAuthDeviceManager( |
117 scoped_ptr<CryptAuthClientFactory> client_factory, | 119 std::unique_ptr<base::Clock> clock, |
118 CryptAuthGCMManager* gcm_manager, | 120 std::unique_ptr<CryptAuthClientFactory> client_factory, |
119 PrefService* pref_service) | 121 CryptAuthGCMManager* gcm_manager, |
| 122 PrefService* pref_service) |
120 : CryptAuthDeviceManager(std::move(clock), | 123 : CryptAuthDeviceManager(std::move(clock), |
121 std::move(client_factory), | 124 std::move(client_factory), |
122 gcm_manager, | 125 gcm_manager, |
123 pref_service), | 126 pref_service), |
124 scoped_sync_scheduler_(new NiceMock<MockSyncScheduler>()), | 127 scoped_sync_scheduler_(new NiceMock<MockSyncScheduler>()), |
125 weak_sync_scheduler_factory_(scoped_sync_scheduler_.get()) {} | 128 weak_sync_scheduler_factory_(scoped_sync_scheduler_.get()) {} |
126 | 129 |
127 ~TestCryptAuthDeviceManager() override {} | 130 ~TestCryptAuthDeviceManager() override {} |
128 | 131 |
129 scoped_ptr<SyncScheduler> CreateSyncScheduler() override { | 132 std::unique_ptr<SyncScheduler> CreateSyncScheduler() override { |
130 EXPECT_TRUE(scoped_sync_scheduler_); | 133 EXPECT_TRUE(scoped_sync_scheduler_); |
131 return std::move(scoped_sync_scheduler_); | 134 return std::move(scoped_sync_scheduler_); |
132 } | 135 } |
133 | 136 |
134 base::WeakPtr<MockSyncScheduler> GetSyncScheduler() { | 137 base::WeakPtr<MockSyncScheduler> GetSyncScheduler() { |
135 return weak_sync_scheduler_factory_.GetWeakPtr(); | 138 return weak_sync_scheduler_factory_.GetWeakPtr(); |
136 } | 139 } |
137 | 140 |
138 private: | 141 private: |
139 // Ownership is passed to |CryptAuthDeviceManager| super class when | 142 // Ownership is passed to |CryptAuthDeviceManager| super class when |
140 // |CreateSyncScheduler()| is called. | 143 // |CreateSyncScheduler()| is called. |
141 scoped_ptr<MockSyncScheduler> scoped_sync_scheduler_; | 144 std::unique_ptr<MockSyncScheduler> scoped_sync_scheduler_; |
142 | 145 |
143 // Stores the pointer of |scoped_sync_scheduler_| after ownership is passed to | 146 // Stores the pointer of |scoped_sync_scheduler_| after ownership is passed to |
144 // the super class. | 147 // the super class. |
145 // This should be safe because the life-time this SyncScheduler will always be | 148 // This should be safe because the life-time this SyncScheduler will always be |
146 // within the life of the TestCryptAuthDeviceManager object. | 149 // within the life of the TestCryptAuthDeviceManager object. |
147 base::WeakPtrFactory<MockSyncScheduler> weak_sync_scheduler_factory_; | 150 base::WeakPtrFactory<MockSyncScheduler> weak_sync_scheduler_factory_; |
148 | 151 |
149 DISALLOW_COPY_AND_ASSIGN(TestCryptAuthDeviceManager); | 152 DISALLOW_COPY_AND_ASSIGN(TestCryptAuthDeviceManager); |
150 }; | 153 }; |
151 | 154 |
(...skipping 23 matching lines...) Expand all Loading... |
175 CryptAuthDeviceManager::RegisterPrefs(pref_service_.registry()); | 178 CryptAuthDeviceManager::RegisterPrefs(pref_service_.registry()); |
176 pref_service_.SetUserPref( | 179 pref_service_.SetUserPref( |
177 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, | 180 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure, |
178 new base::FundamentalValue(false)); | 181 new base::FundamentalValue(false)); |
179 pref_service_.SetUserPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, | 182 pref_service_.SetUserPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds, |
180 new base::FundamentalValue(kLastSyncTimeSeconds)); | 183 new base::FundamentalValue(kLastSyncTimeSeconds)); |
181 pref_service_.SetUserPref( | 184 pref_service_.SetUserPref( |
182 prefs::kCryptAuthDeviceSyncReason, | 185 prefs::kCryptAuthDeviceSyncReason, |
183 new base::FundamentalValue(cryptauth::INVOCATION_REASON_UNKNOWN)); | 186 new base::FundamentalValue(cryptauth::INVOCATION_REASON_UNKNOWN)); |
184 | 187 |
185 scoped_ptr<base::DictionaryValue> unlock_key_dictionary( | 188 std::unique_ptr<base::DictionaryValue> unlock_key_dictionary( |
186 new base::DictionaryValue()); | 189 new base::DictionaryValue()); |
187 | 190 |
188 std::string public_key_b64, device_name_b64, bluetooth_address_b64; | 191 std::string public_key_b64, device_name_b64, bluetooth_address_b64; |
189 base::Base64UrlEncode(kStoredPublicKey, | 192 base::Base64UrlEncode(kStoredPublicKey, |
190 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 193 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
191 &public_key_b64); | 194 &public_key_b64); |
192 base::Base64UrlEncode(kStoredDeviceName, | 195 base::Base64UrlEncode(kStoredDeviceName, |
193 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 196 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
194 &device_name_b64); | 197 &device_name_b64); |
195 base::Base64UrlEncode(kStoredBluetoothAddress, | 198 base::Base64UrlEncode(kStoredBluetoothAddress, |
196 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 199 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
197 &bluetooth_address_b64); | 200 &bluetooth_address_b64); |
198 | 201 |
199 unlock_key_dictionary->SetString("public_key", public_key_b64); | 202 unlock_key_dictionary->SetString("public_key", public_key_b64); |
200 unlock_key_dictionary->SetString("device_name", device_name_b64); | 203 unlock_key_dictionary->SetString("device_name", device_name_b64); |
201 unlock_key_dictionary->SetString("bluetooth_address", | 204 unlock_key_dictionary->SetString("bluetooth_address", |
202 bluetooth_address_b64); | 205 bluetooth_address_b64); |
203 { | 206 { |
204 ListPrefUpdate update(&pref_service_, | 207 ListPrefUpdate update(&pref_service_, |
205 prefs::kCryptAuthDeviceSyncUnlockKeys); | 208 prefs::kCryptAuthDeviceSyncUnlockKeys); |
206 update.Get()->Append(std::move(unlock_key_dictionary)); | 209 update.Get()->Append(std::move(unlock_key_dictionary)); |
207 } | 210 } |
208 | 211 |
209 device_manager_.reset(new TestCryptAuthDeviceManager( | 212 device_manager_.reset(new TestCryptAuthDeviceManager( |
210 make_scoped_ptr(clock_), make_scoped_ptr(client_factory_), | 213 base::WrapUnique(clock_), base::WrapUnique(client_factory_), |
211 &gcm_manager_, &pref_service_)); | 214 &gcm_manager_, &pref_service_)); |
212 device_manager_->AddObserver(this); | 215 device_manager_->AddObserver(this); |
213 | 216 |
214 cryptauth::ExternalDeviceInfo unlock_key; | 217 cryptauth::ExternalDeviceInfo unlock_key; |
215 unlock_key.set_public_key(kPublicKey1); | 218 unlock_key.set_public_key(kPublicKey1); |
216 unlock_key.set_friendly_device_name(kDeviceName1); | 219 unlock_key.set_friendly_device_name(kDeviceName1); |
217 unlock_key.set_bluetooth_address(kBluetoothAddress1); | 220 unlock_key.set_bluetooth_address(kBluetoothAddress1); |
218 unlock_key.set_unlock_key(true); | 221 unlock_key.set_unlock_key(true); |
219 unlock_key.set_unlockable(false); | 222 unlock_key.set_unlockable(false); |
220 | 223 |
(...skipping 25 matching lines...) Expand all Loading... |
246 MOCK_METHOD2(OnSyncFinishedProxy, | 249 MOCK_METHOD2(OnSyncFinishedProxy, |
247 void(CryptAuthDeviceManager::SyncResult, | 250 void(CryptAuthDeviceManager::SyncResult, |
248 CryptAuthDeviceManager::DeviceChangeResult)); | 251 CryptAuthDeviceManager::DeviceChangeResult)); |
249 | 252 |
250 // Simulates firing the SyncScheduler to trigger a device sync attempt. | 253 // Simulates firing the SyncScheduler to trigger a device sync attempt. |
251 void FireSchedulerForSync( | 254 void FireSchedulerForSync( |
252 cryptauth::InvocationReason expected_invocation_reason) { | 255 cryptauth::InvocationReason expected_invocation_reason) { |
253 SyncScheduler::Delegate* delegate = | 256 SyncScheduler::Delegate* delegate = |
254 static_cast<SyncScheduler::Delegate*>(device_manager_.get()); | 257 static_cast<SyncScheduler::Delegate*>(device_manager_.get()); |
255 | 258 |
256 scoped_ptr<SyncScheduler::SyncRequest> sync_request = make_scoped_ptr( | 259 std::unique_ptr<SyncScheduler::SyncRequest> sync_request = base::WrapUnique( |
257 new SyncScheduler::SyncRequest(device_manager_->GetSyncScheduler())); | 260 new SyncScheduler::SyncRequest(device_manager_->GetSyncScheduler())); |
258 EXPECT_CALL(*this, OnSyncStartedProxy()); | 261 EXPECT_CALL(*this, OnSyncStartedProxy()); |
259 delegate->OnSyncRequested(std::move(sync_request)); | 262 delegate->OnSyncRequested(std::move(sync_request)); |
260 | 263 |
261 EXPECT_EQ(expected_invocation_reason, | 264 EXPECT_EQ(expected_invocation_reason, |
262 get_my_devices_request_.invocation_reason()); | 265 get_my_devices_request_.invocation_reason()); |
263 | 266 |
264 // The allow_stale_read flag is set if the sync was not forced. | 267 // The allow_stale_read flag is set if the sync was not forced. |
265 bool allow_stale_read = | 268 bool allow_stale_read = |
266 pref_service_.GetInteger(prefs::kCryptAuthDeviceSyncReason) != | 269 pref_service_.GetInteger(prefs::kCryptAuthDeviceSyncReason) != |
(...skipping 16 matching lines...) Expand all Loading... |
283 // Owned by |device_manager_|. | 286 // Owned by |device_manager_|. |
284 base::SimpleTestClock* clock_; | 287 base::SimpleTestClock* clock_; |
285 | 288 |
286 // Owned by |device_manager_|. | 289 // Owned by |device_manager_|. |
287 MockCryptAuthClientFactory* client_factory_; | 290 MockCryptAuthClientFactory* client_factory_; |
288 | 291 |
289 TestingPrefServiceSimple pref_service_; | 292 TestingPrefServiceSimple pref_service_; |
290 | 293 |
291 FakeCryptAuthGCMManager gcm_manager_; | 294 FakeCryptAuthGCMManager gcm_manager_; |
292 | 295 |
293 scoped_ptr<TestCryptAuthDeviceManager> device_manager_; | 296 std::unique_ptr<TestCryptAuthDeviceManager> device_manager_; |
294 | 297 |
295 cryptauth::GetMyDevicesResponse get_my_devices_response_; | 298 cryptauth::GetMyDevicesResponse get_my_devices_response_; |
296 | 299 |
297 cryptauth::GetMyDevicesRequest get_my_devices_request_; | 300 cryptauth::GetMyDevicesRequest get_my_devices_request_; |
298 | 301 |
299 CryptAuthClient::GetMyDevicesCallback success_callback_; | 302 CryptAuthClient::GetMyDevicesCallback success_callback_; |
300 | 303 |
301 CryptAuthClient::ErrorCallback error_callback_; | 304 CryptAuthClient::ErrorCallback error_callback_; |
302 | 305 |
303 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthDeviceManagerTest); | 306 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthDeviceManagerTest); |
(...skipping 30 matching lines...) Expand all Loading... |
334 ON_CALL(*sync_scheduler(), GetSyncState()) | 337 ON_CALL(*sync_scheduler(), GetSyncState()) |
335 .WillByDefault(Return(SyncScheduler::SyncState::SYNC_IN_PROGRESS)); | 338 .WillByDefault(Return(SyncScheduler::SyncState::SYNC_IN_PROGRESS)); |
336 EXPECT_TRUE(device_manager_->IsSyncInProgress()); | 339 EXPECT_TRUE(device_manager_->IsSyncInProgress()); |
337 | 340 |
338 ON_CALL(*sync_scheduler(), GetSyncState()) | 341 ON_CALL(*sync_scheduler(), GetSyncState()) |
339 .WillByDefault(Return(SyncScheduler::SyncState::WAITING_FOR_REFRESH)); | 342 .WillByDefault(Return(SyncScheduler::SyncState::WAITING_FOR_REFRESH)); |
340 EXPECT_FALSE(device_manager_->IsSyncInProgress()); | 343 EXPECT_FALSE(device_manager_->IsSyncInProgress()); |
341 } | 344 } |
342 | 345 |
343 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, InitWithDefaultPrefs) { | 346 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, InitWithDefaultPrefs) { |
344 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 347 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
345 clock->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); | 348 clock->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds)); |
346 base::TimeDelta elapsed_time = clock->Now() - base::Time::FromDoubleT(0); | 349 base::TimeDelta elapsed_time = clock->Now() - base::Time::FromDoubleT(0); |
347 | 350 |
348 TestingPrefServiceSimple pref_service; | 351 TestingPrefServiceSimple pref_service; |
349 CryptAuthDeviceManager::RegisterPrefs(pref_service.registry()); | 352 CryptAuthDeviceManager::RegisterPrefs(pref_service.registry()); |
350 | 353 |
351 TestCryptAuthDeviceManager device_manager( | 354 TestCryptAuthDeviceManager device_manager( |
352 std::move(clock), | 355 std::move(clock), |
353 make_scoped_ptr(new MockCryptAuthClientFactory( | 356 base::WrapUnique(new MockCryptAuthClientFactory( |
354 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS)), | 357 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS)), |
355 &gcm_manager_, &pref_service); | 358 &gcm_manager_, &pref_service); |
356 | 359 |
357 EXPECT_CALL( | 360 EXPECT_CALL( |
358 *(device_manager.GetSyncScheduler()), | 361 *(device_manager.GetSyncScheduler()), |
359 Start(elapsed_time, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY)); | 362 Start(elapsed_time, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY)); |
360 device_manager.Start(); | 363 device_manager.Start(); |
361 EXPECT_TRUE(device_manager.GetLastSyncTime().is_null()); | 364 EXPECT_TRUE(device_manager.GetLastSyncTime().is_null()); |
362 EXPECT_EQ(0u, device_manager.unlock_keys().size()); | 365 EXPECT_EQ(0u, device_manager.unlock_keys().size()); |
363 } | 366 } |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 CryptAuthDeviceManager::DeviceChangeResult::CHANGED)); | 599 CryptAuthDeviceManager::DeviceChangeResult::CHANGED)); |
597 success_callback_.Run(get_my_devices_response_); | 600 success_callback_.Run(get_my_devices_response_); |
598 | 601 |
599 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>( | 602 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>( |
600 1, get_my_devices_response_.devices(0)), | 603 1, get_my_devices_response_.devices(0)), |
601 device_manager_->unlock_keys(), | 604 device_manager_->unlock_keys(), |
602 pref_service_); | 605 pref_service_); |
603 } | 606 } |
604 | 607 |
605 } // namespace proximity_auth | 608 } // namespace proximity_auth |
OLD | NEW |