Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_device_manager_unittest.cc
blobbd02b2fa40087c5686de2c1278fe5403a6b9a205
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_device_manager.h"
7 #include "base/memory/weak_ptr.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/test/simple_test_clock.h"
12 #include "components/proximity_auth/cryptauth/base64url.h"
13 #include "components/proximity_auth/cryptauth/mock_cryptauth_client.h"
14 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h"
15 #include "components/proximity_auth/cryptauth/pref_names.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::_;
20 using ::testing::DoAll;
21 using ::testing::NiceMock;
22 using ::testing::Return;
23 using ::testing::SaveArg;
25 namespace proximity_auth {
26 namespace {
28 // The initial "Now" time for testing.
29 const double kInitialTimeNowSeconds = 20000000;
31 // A later "Now" time for testing.
32 const double kLaterTimeNowSeconds = kInitialTimeNowSeconds + 30;
34 // The timestamp of a last successful sync in seconds.
35 const double kLastSyncTimeSeconds = kInitialTimeNowSeconds - (60 * 60 * 5);
37 // Unlock key fields originally stored in the user prefs.
38 const char kStoredPublicKey[] = "AAPL";
39 const char kStoredDeviceName[] = "iPhone 6";
40 const char kStoredBluetoothAddress[] = "12:34:56:78:90:AB";
42 // ExternalDeviceInfo fields for the synced unlock key.
43 const char kPublicKey1[] = "GOOG";
44 const char kDeviceName1[] = "Nexus 5";
45 const char kBluetoothAddress1[] = "aa:bb:cc:ee:dd:ff";
47 // ExternalDeviceInfo fields for a non-synced unlockable device.
48 const char kPublicKey2[] = "MSFT";
49 const char kDeviceName2[] = "Surface Pro 3";
51 // Validates that |unlock_keys| and the corresponding preferences stored by
52 // |pref_service| are equal to |expected_unlock_keys|.
53 void ExpectUnlockKeysAndPrefAreEqual(
54 const std::vector<cryptauth::ExternalDeviceInfo>& expected_unlock_keys,
55 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys,
56 const PrefService& pref_service) {
57 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys.size());
58 for (size_t i = 0; i < unlock_keys.size(); ++i) {
59 SCOPED_TRACE(
60 base::StringPrintf("Compare protos at index=%d", static_cast<int>(i)));
61 const auto& expected_unlock_key = expected_unlock_keys[i];
62 const auto& unlock_key = unlock_keys.at(i);
63 EXPECT_EQ(expected_unlock_key.public_key(), unlock_key.public_key());
64 EXPECT_EQ(expected_unlock_key.friendly_device_name(),
65 unlock_key.friendly_device_name());
66 EXPECT_EQ(expected_unlock_key.bluetooth_address(),
67 unlock_key.bluetooth_address());
68 EXPECT_TRUE(expected_unlock_key.unlock_key());
69 EXPECT_FALSE(expected_unlock_key.unlockable());
72 const base::ListValue* unlock_keys_pref =
73 pref_service.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys);
74 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys_pref->GetSize());
75 for (size_t i = 0; i < unlock_keys_pref->GetSize(); ++i) {
76 SCOPED_TRACE(base::StringPrintf("Compare pref dictionary at index=%d",
77 static_cast<int>(i)));
78 const base::DictionaryValue* unlock_key_dictionary;
79 EXPECT_TRUE(unlock_keys_pref->GetDictionary(i, &unlock_key_dictionary));
80 std::string public_key_b64, device_name_b64, bluetooth_address_b64;
81 ASSERT_TRUE(
82 unlock_key_dictionary->GetString("public_key", &public_key_b64));
83 ASSERT_TRUE(
84 unlock_key_dictionary->GetString("device_name", &device_name_b64));
85 ASSERT_TRUE(unlock_key_dictionary->GetString("bluetooth_address",
86 &bluetooth_address_b64));
88 std::string public_key, device_name, bluetooth_address;
89 ASSERT_TRUE(Base64UrlDecode(public_key_b64, &public_key));
90 ASSERT_TRUE(Base64UrlDecode(device_name_b64, &device_name));
91 ASSERT_TRUE(Base64UrlDecode(bluetooth_address_b64, &bluetooth_address));
93 const auto& expected_unlock_key = expected_unlock_keys[i];
94 EXPECT_EQ(expected_unlock_key.public_key(), public_key);
95 EXPECT_EQ(expected_unlock_key.friendly_device_name(), device_name);
96 EXPECT_EQ(expected_unlock_key.bluetooth_address(), bluetooth_address);
97 EXPECT_TRUE(expected_unlock_key.unlock_key());
98 EXPECT_FALSE(expected_unlock_key.unlockable());
102 // Harness for testing CryptAuthDeviceManager.
103 class TestCryptAuthDeviceManager : public CryptAuthDeviceManager {
104 public:
105 TestCryptAuthDeviceManager(scoped_ptr<base::Clock> clock,
106 scoped_ptr<CryptAuthClientFactory> client_factory,
107 PrefService* pref_service)
108 : CryptAuthDeviceManager(clock.Pass(),
109 client_factory.Pass(),
110 pref_service),
111 scoped_sync_scheduler_(new NiceMock<MockSyncScheduler>()),
112 weak_sync_scheduler_factory_(scoped_sync_scheduler_.get()) {}
114 ~TestCryptAuthDeviceManager() override {}
116 scoped_ptr<SyncScheduler> CreateSyncScheduler() override {
117 EXPECT_TRUE(scoped_sync_scheduler_);
118 return scoped_sync_scheduler_.Pass();
121 base::WeakPtr<MockSyncScheduler> GetSyncScheduler() {
122 return weak_sync_scheduler_factory_.GetWeakPtr();
125 private:
126 // Ownership is passed to |CryptAuthDeviceManager| super class when
127 // |CreateSyncScheduler()| is called.
128 scoped_ptr<MockSyncScheduler> scoped_sync_scheduler_;
130 // Stores the pointer of |scoped_sync_scheduler_| after ownership is passed to
131 // the super class.
132 // This should be safe because the life-time this SyncScheduler will always be
133 // within the life of the TestCryptAuthDeviceManager object.
134 base::WeakPtrFactory<MockSyncScheduler> weak_sync_scheduler_factory_;
136 DISALLOW_COPY_AND_ASSIGN(TestCryptAuthDeviceManager);
139 } // namespace
141 class ProximityAuthCryptAuthDeviceManagerTest
142 : public testing::Test,
143 public CryptAuthDeviceManager::Observer,
144 public MockCryptAuthClientFactory::Observer {
145 protected:
146 ProximityAuthCryptAuthDeviceManagerTest()
147 : clock_(new base::SimpleTestClock()),
148 client_factory_(new MockCryptAuthClientFactory(
149 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS)),
150 device_manager_(make_scoped_ptr(clock_),
151 make_scoped_ptr(client_factory_),
152 &pref_service_) {
153 client_factory_->AddObserver(this);
156 ~ProximityAuthCryptAuthDeviceManagerTest() {
157 client_factory_->RemoveObserver(this);
160 // testing::Test:
161 void SetUp() override {
162 clock_->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds));
163 device_manager_.AddObserver(this);
165 CryptAuthDeviceManager::RegisterPrefs(pref_service_.registry());
166 pref_service_.SetUserPref(
167 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure,
168 new base::FundamentalValue(false));
169 pref_service_.SetUserPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds,
170 new base::FundamentalValue(kLastSyncTimeSeconds));
171 pref_service_.SetUserPref(
172 prefs::kCryptAuthDeviceSyncReason,
173 new base::FundamentalValue(cryptauth::INVOCATION_REASON_UNKNOWN));
175 scoped_ptr<base::DictionaryValue> unlock_key_dictionary(
176 new base::DictionaryValue());
178 std::string public_key_b64, device_name_b64, bluetooth_address_b64;
179 Base64UrlEncode(kStoredPublicKey, &public_key_b64);
180 Base64UrlEncode(kStoredDeviceName, &device_name_b64);
181 Base64UrlEncode(kStoredBluetoothAddress, &bluetooth_address_b64);
183 unlock_key_dictionary->SetString("public_key", public_key_b64);
184 unlock_key_dictionary->SetString("device_name", device_name_b64);
185 unlock_key_dictionary->SetString("bluetooth_address",
186 bluetooth_address_b64);
188 ListPrefUpdate update(&pref_service_,
189 prefs::kCryptAuthDeviceSyncUnlockKeys);
190 update.Get()->Append(unlock_key_dictionary.Pass());
193 cryptauth::ExternalDeviceInfo unlock_key;
194 unlock_key.set_public_key(kPublicKey1);
195 unlock_key.set_friendly_device_name(kDeviceName1);
196 unlock_key.set_bluetooth_address(kBluetoothAddress1);
197 unlock_key.set_unlock_key(true);
198 unlock_key.set_unlockable(false);
200 cryptauth::ExternalDeviceInfo unlockable_device;
201 unlockable_device.set_public_key(kPublicKey2);
202 unlockable_device.set_friendly_device_name(kDeviceName2);
203 unlockable_device.set_unlock_key(false);
204 unlockable_device.set_unlockable(true);
206 get_my_devices_response_.add_devices()->CopyFrom(unlock_key);
207 get_my_devices_response_.add_devices()->CopyFrom(unlockable_device);
209 ON_CALL(*sync_scheduler(), GetStrategy())
210 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH));
213 void TearDown() override { device_manager_.RemoveObserver(this); }
215 // CryptAuthDeviceManager::Observer:
216 void OnSyncStarted() override { OnSyncStartedProxy(); }
218 void OnSyncFinished(CryptAuthDeviceManager::SyncResult sync_result,
219 CryptAuthDeviceManager::DeviceChangeResult
220 device_change_result) override {
221 OnSyncFinishedProxy(sync_result, device_change_result);
224 MOCK_METHOD0(OnSyncStartedProxy, void());
225 MOCK_METHOD2(OnSyncFinishedProxy,
226 void(CryptAuthDeviceManager::SyncResult,
227 CryptAuthDeviceManager::DeviceChangeResult));
229 // Simulates firing the SyncScheduler to trigger a device sync attempt.
230 void FireSchedulerForSync(
231 cryptauth::InvocationReason expected_invocation_reason) {
232 SyncScheduler::Delegate* delegate =
233 static_cast<SyncScheduler::Delegate*>(&device_manager_);
235 scoped_ptr<SyncScheduler::SyncRequest> sync_request = make_scoped_ptr(
236 new SyncScheduler::SyncRequest(device_manager_.GetSyncScheduler()));
237 EXPECT_CALL(*this, OnSyncStartedProxy());
238 delegate->OnSyncRequested(sync_request.Pass());
240 EXPECT_EQ(expected_invocation_reason,
241 get_my_devices_request_.invocation_reason());
244 // MockCryptAuthClientFactory::Observer:
245 void OnCryptAuthClientCreated(MockCryptAuthClient* client) override {
246 EXPECT_CALL(*client, GetMyDevices(_, _, _))
247 .WillOnce(DoAll(SaveArg<0>(&get_my_devices_request_),
248 SaveArg<1>(&success_callback_),
249 SaveArg<2>(&error_callback_)));
252 MockSyncScheduler* sync_scheduler() {
253 return device_manager_.GetSyncScheduler().get();
256 // Owned by |device_manager_|.
257 base::SimpleTestClock* clock_;
259 // Owned by |device_manager_|.
260 MockCryptAuthClientFactory* client_factory_;
262 TestingPrefServiceSimple pref_service_;
264 TestCryptAuthDeviceManager device_manager_;
266 cryptauth::GetMyDevicesResponse get_my_devices_response_;
268 cryptauth::GetMyDevicesRequest get_my_devices_request_;
270 CryptAuthClient::GetMyDevicesCallback success_callback_;
272 CryptAuthClient::ErrorCallback error_callback_;
274 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthDeviceManagerTest);
277 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, RegisterPrefs) {
278 TestingPrefServiceSimple pref_service;
279 CryptAuthDeviceManager::RegisterPrefs(pref_service.registry());
280 EXPECT_TRUE(pref_service.FindPreference(
281 prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds));
282 EXPECT_TRUE(pref_service.FindPreference(
283 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure));
284 EXPECT_TRUE(pref_service.FindPreference(prefs::kCryptAuthDeviceSyncReason));
285 EXPECT_TRUE(
286 pref_service.FindPreference(prefs::kCryptAuthDeviceSyncUnlockKeys));
289 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, GetSyncState) {
290 device_manager_.Start();
292 ON_CALL(*sync_scheduler(), GetStrategy())
293 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH));
294 EXPECT_FALSE(device_manager_.IsRecoveringFromFailure());
296 ON_CALL(*sync_scheduler(), GetStrategy())
297 .WillByDefault(Return(SyncScheduler::Strategy::AGGRESSIVE_RECOVERY));
298 EXPECT_TRUE(device_manager_.IsRecoveringFromFailure());
300 base::TimeDelta time_to_next_sync = base::TimeDelta::FromMinutes(60);
301 ON_CALL(*sync_scheduler(), GetTimeToNextSync())
302 .WillByDefault(Return(time_to_next_sync));
303 EXPECT_EQ(time_to_next_sync, device_manager_.GetTimeToNextAttempt());
305 ON_CALL(*sync_scheduler(), GetSyncState())
306 .WillByDefault(Return(SyncScheduler::SyncState::SYNC_IN_PROGRESS));
307 EXPECT_TRUE(device_manager_.IsSyncInProgress());
309 ON_CALL(*sync_scheduler(), GetSyncState())
310 .WillByDefault(Return(SyncScheduler::SyncState::WAITING_FOR_REFRESH));
311 EXPECT_FALSE(device_manager_.IsSyncInProgress());
314 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, InitWithDefaultPrefs) {
315 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock);
316 clock->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds));
317 base::TimeDelta elapsed_time = clock->Now() - base::Time::FromDoubleT(0);
319 TestingPrefServiceSimple pref_service;
320 CryptAuthDeviceManager::RegisterPrefs(pref_service.registry());
322 TestCryptAuthDeviceManager device_manager(
323 clock.Pass(),
324 make_scoped_ptr(new MockCryptAuthClientFactory(
325 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS)),
326 &pref_service);
328 EXPECT_CALL(
329 *(device_manager.GetSyncScheduler()),
330 Start(elapsed_time, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY));
331 device_manager.Start();
332 EXPECT_TRUE(device_manager.GetLastSyncTime().is_null());
333 EXPECT_EQ(0u, device_manager.unlock_keys().size());
336 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, InitWithExistingPrefs) {
337 EXPECT_CALL(
338 *sync_scheduler(),
339 Start(clock_->Now() - base::Time::FromDoubleT(kLastSyncTimeSeconds),
340 SyncScheduler::Strategy::PERIODIC_REFRESH));
342 device_manager_.Start();
343 EXPECT_EQ(base::Time::FromDoubleT(kLastSyncTimeSeconds),
344 device_manager_.GetLastSyncTime());
346 auto unlock_keys = device_manager_.unlock_keys();
347 ASSERT_EQ(1u, unlock_keys.size());
348 EXPECT_EQ(kStoredPublicKey, unlock_keys[0].public_key());
349 EXPECT_EQ(kStoredDeviceName, unlock_keys[0].friendly_device_name());
350 EXPECT_EQ(kStoredBluetoothAddress, unlock_keys[0].bluetooth_address());
351 EXPECT_TRUE(unlock_keys[0].unlock_key());
354 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, SyncSucceedsForFirstTime) {
355 pref_service_.ClearPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds);
356 device_manager_.Start();
358 FireSchedulerForSync(cryptauth::INVOCATION_REASON_INITIALIZATION);
359 ASSERT_FALSE(success_callback_.is_null());
361 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds));
362 EXPECT_CALL(*this, OnSyncFinishedProxy(
363 CryptAuthDeviceManager::SyncResult::SUCCESS,
364 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
366 success_callback_.Run(get_my_devices_response_);
367 EXPECT_EQ(clock_->Now(), device_manager_.GetLastSyncTime());
369 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(
370 1, get_my_devices_response_.devices(0)),
371 device_manager_.unlock_keys(), pref_service_);
374 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, ForceSync) {
375 device_manager_.Start();
377 EXPECT_CALL(*sync_scheduler(), ForceSync());
378 device_manager_.ForceSyncNow(cryptauth::INVOCATION_REASON_MANUAL);
380 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL);
382 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds));
383 EXPECT_CALL(*this, OnSyncFinishedProxy(
384 CryptAuthDeviceManager::SyncResult::SUCCESS,
385 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
386 success_callback_.Run(get_my_devices_response_);
387 EXPECT_EQ(clock_->Now(), device_manager_.GetLastSyncTime());
389 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(
390 1, get_my_devices_response_.devices(0)),
391 device_manager_.unlock_keys(), pref_service_);
394 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, ForceSyncFailsThenSucceeds) {
395 device_manager_.Start();
396 EXPECT_FALSE(pref_service_.GetBoolean(
397 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure));
398 base::Time old_sync_time = device_manager_.GetLastSyncTime();
400 // The first force sync fails.
401 EXPECT_CALL(*sync_scheduler(), ForceSync());
402 device_manager_.ForceSyncNow(cryptauth::INVOCATION_REASON_MANUAL);
403 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL);
404 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds));
405 EXPECT_CALL(*this,
406 OnSyncFinishedProxy(
407 CryptAuthDeviceManager::SyncResult::FAILURE,
408 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED));
409 error_callback_.Run("404");
410 EXPECT_EQ(old_sync_time, device_manager_.GetLastSyncTime());
411 EXPECT_TRUE(pref_service_.GetBoolean(
412 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure));
413 EXPECT_EQ(static_cast<int>(cryptauth::INVOCATION_REASON_MANUAL),
414 pref_service_.GetInteger(prefs::kCryptAuthDeviceSyncReason));
416 // The second recovery sync succeeds.
417 ON_CALL(*sync_scheduler(), GetStrategy())
418 .WillByDefault(Return(SyncScheduler::Strategy::AGGRESSIVE_RECOVERY));
419 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL);
420 clock_->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds + 30));
421 EXPECT_CALL(*this, OnSyncFinishedProxy(
422 CryptAuthDeviceManager::SyncResult::SUCCESS,
423 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
424 success_callback_.Run(get_my_devices_response_);
425 EXPECT_EQ(clock_->Now(), device_manager_.GetLastSyncTime());
427 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(
428 1, get_my_devices_response_.devices(0)),
429 device_manager_.unlock_keys(), pref_service_);
431 EXPECT_FLOAT_EQ(
432 clock_->Now().ToDoubleT(),
433 pref_service_.GetDouble(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds));
434 EXPECT_EQ(static_cast<int>(cryptauth::INVOCATION_REASON_UNKNOWN),
435 pref_service_.GetInteger(prefs::kCryptAuthDeviceSyncReason));
436 EXPECT_FALSE(pref_service_.GetBoolean(
437 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure));
440 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, SyncSameDevice) {
441 // Set the same unlock key in the user prefs as the one that would be synced.
443 scoped_ptr<base::DictionaryValue> unlock_key_dictionary(
444 new base::DictionaryValue());
445 std::string public_key_b64, device_name_b64, bluetooth_address_b64;
446 Base64UrlEncode(kPublicKey1, &public_key_b64);
447 Base64UrlEncode(kDeviceName1, &device_name_b64);
448 Base64UrlEncode(kBluetoothAddress1, &bluetooth_address_b64);
449 unlock_key_dictionary->SetString("public_key", public_key_b64);
450 unlock_key_dictionary->SetString("device_name", device_name_b64);
451 unlock_key_dictionary->SetString("bluetooth_address",
452 bluetooth_address_b64);
454 ListPrefUpdate update(&pref_service_,
455 prefs::kCryptAuthDeviceSyncUnlockKeys);
456 update.Get()->Clear();
457 update.Get()->Append(unlock_key_dictionary.Pass());
460 // Check unlock keys before sync.
461 device_manager_.Start();
462 auto original_unlock_keys = device_manager_.unlock_keys();
463 ASSERT_EQ(1u, original_unlock_keys.size());
464 EXPECT_EQ(kPublicKey1, original_unlock_keys[0].public_key());
465 EXPECT_EQ(kDeviceName1, original_unlock_keys[0].friendly_device_name());
466 EXPECT_EQ(kBluetoothAddress1, original_unlock_keys[0].bluetooth_address());
468 // Sync new devices.
469 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC);
470 ASSERT_FALSE(success_callback_.is_null());
471 EXPECT_CALL(*this,
472 OnSyncFinishedProxy(
473 CryptAuthDeviceManager::SyncResult::SUCCESS,
474 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED));
475 success_callback_.Run(get_my_devices_response_);
477 // Check that unlock keys are still the same after sync.
478 ExpectUnlockKeysAndPrefAreEqual(original_unlock_keys,
479 device_manager_.unlock_keys(), pref_service_);
482 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, SyncEmptyDeviceList) {
483 cryptauth::GetMyDevicesResponse empty_response;
485 device_manager_.Start();
486 EXPECT_EQ(1u, device_manager_.unlock_keys().size());
488 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC);
489 ASSERT_FALSE(success_callback_.is_null());
490 EXPECT_CALL(*this, OnSyncFinishedProxy(
491 CryptAuthDeviceManager::SyncResult::SUCCESS,
492 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
493 success_callback_.Run(empty_response);
495 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(),
496 device_manager_.unlock_keys(), pref_service_);
499 TEST_F(ProximityAuthCryptAuthDeviceManagerTest, SyncTwoUnlockKeys) {
500 cryptauth::GetMyDevicesResponse response(get_my_devices_response_);
501 cryptauth::ExternalDeviceInfo unlock_key2;
502 unlock_key2.set_public_key("new public key");
503 unlock_key2.set_friendly_device_name("new device name");
504 unlock_key2.set_bluetooth_address("aa:bb:cc:dd:ee:ff");
505 unlock_key2.set_unlock_key(true);
506 response.add_devices()->CopyFrom(unlock_key2);
508 std::vector<cryptauth::ExternalDeviceInfo> expected_unlock_keys;
509 expected_unlock_keys.push_back(get_my_devices_response_.devices(0));
510 expected_unlock_keys.push_back(unlock_key2);
512 device_manager_.Start();
513 EXPECT_EQ(1u, device_manager_.unlock_keys().size());
514 EXPECT_EQ(1u, pref_service_.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys)
515 ->GetSize());
517 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC);
518 ASSERT_FALSE(success_callback_.is_null());
519 EXPECT_CALL(*this, OnSyncFinishedProxy(
520 CryptAuthDeviceManager::SyncResult::SUCCESS,
521 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
522 success_callback_.Run(response);
524 ExpectUnlockKeysAndPrefAreEqual(expected_unlock_keys,
525 device_manager_.unlock_keys(), pref_service_);
528 } // namespace proximity_auth