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/fake_cryptauth_gcm_manager.h"
14 #include "components/proximity_auth/cryptauth/mock_cryptauth_client.h"
15 #include "components/proximity_auth/cryptauth/mock_sync_scheduler.h"
16 #include "components/proximity_auth/cryptauth/pref_names.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 using ::testing::DoAll
;
22 using ::testing::NiceMock
;
23 using ::testing::Return
;
24 using ::testing::SaveArg
;
26 namespace proximity_auth
{
29 // The initial "Now" time for testing.
30 const double kInitialTimeNowSeconds
= 20000000;
32 // A later "Now" time for testing.
33 const double kLaterTimeNowSeconds
= kInitialTimeNowSeconds
+ 30;
35 // The timestamp of a last successful sync in seconds.
36 const double kLastSyncTimeSeconds
= kInitialTimeNowSeconds
- (60 * 60 * 5);
38 // Unlock key fields originally stored in the user prefs.
39 const char kStoredPublicKey
[] = "AAPL";
40 const char kStoredDeviceName
[] = "iPhone 6";
41 const char kStoredBluetoothAddress
[] = "12:34:56:78:90:AB";
43 // ExternalDeviceInfo fields for the synced unlock key.
44 const char kPublicKey1
[] = "GOOG";
45 const char kDeviceName1
[] = "Nexus 5";
46 const char kBluetoothAddress1
[] = "aa:bb:cc:ee:dd:ff";
48 // ExternalDeviceInfo fields for a non-synced unlockable device.
49 const char kPublicKey2
[] = "MSFT";
50 const char kDeviceName2
[] = "Surface Pro 3";
52 // Validates that |unlock_keys| and the corresponding preferences stored by
53 // |pref_service| are equal to |expected_unlock_keys|.
54 void ExpectUnlockKeysAndPrefAreEqual(
55 const std::vector
<cryptauth::ExternalDeviceInfo
>& expected_unlock_keys
,
56 const std::vector
<cryptauth::ExternalDeviceInfo
>& unlock_keys
,
57 const PrefService
& pref_service
) {
58 ASSERT_EQ(expected_unlock_keys
.size(), unlock_keys
.size());
59 for (size_t i
= 0; i
< unlock_keys
.size(); ++i
) {
61 base::StringPrintf("Compare protos at index=%d", static_cast<int>(i
)));
62 const auto& expected_unlock_key
= expected_unlock_keys
[i
];
63 const auto& unlock_key
= unlock_keys
.at(i
);
64 EXPECT_EQ(expected_unlock_key
.public_key(), unlock_key
.public_key());
65 EXPECT_EQ(expected_unlock_key
.friendly_device_name(),
66 unlock_key
.friendly_device_name());
67 EXPECT_EQ(expected_unlock_key
.bluetooth_address(),
68 unlock_key
.bluetooth_address());
69 EXPECT_TRUE(expected_unlock_key
.unlock_key());
70 EXPECT_FALSE(expected_unlock_key
.unlockable());
73 const base::ListValue
* unlock_keys_pref
=
74 pref_service
.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys
);
75 ASSERT_EQ(expected_unlock_keys
.size(), unlock_keys_pref
->GetSize());
76 for (size_t i
= 0; i
< unlock_keys_pref
->GetSize(); ++i
) {
77 SCOPED_TRACE(base::StringPrintf("Compare pref dictionary at index=%d",
78 static_cast<int>(i
)));
79 const base::DictionaryValue
* unlock_key_dictionary
;
80 EXPECT_TRUE(unlock_keys_pref
->GetDictionary(i
, &unlock_key_dictionary
));
81 std::string public_key_b64
, device_name_b64
, bluetooth_address_b64
;
83 unlock_key_dictionary
->GetString("public_key", &public_key_b64
));
85 unlock_key_dictionary
->GetString("device_name", &device_name_b64
));
86 ASSERT_TRUE(unlock_key_dictionary
->GetString("bluetooth_address",
87 &bluetooth_address_b64
));
89 std::string public_key
, device_name
, bluetooth_address
;
90 ASSERT_TRUE(Base64UrlDecode(public_key_b64
, &public_key
));
91 ASSERT_TRUE(Base64UrlDecode(device_name_b64
, &device_name
));
92 ASSERT_TRUE(Base64UrlDecode(bluetooth_address_b64
, &bluetooth_address
));
94 const auto& expected_unlock_key
= expected_unlock_keys
[i
];
95 EXPECT_EQ(expected_unlock_key
.public_key(), public_key
);
96 EXPECT_EQ(expected_unlock_key
.friendly_device_name(), device_name
);
97 EXPECT_EQ(expected_unlock_key
.bluetooth_address(), bluetooth_address
);
98 EXPECT_TRUE(expected_unlock_key
.unlock_key());
99 EXPECT_FALSE(expected_unlock_key
.unlockable());
103 // Harness for testing CryptAuthDeviceManager.
104 class TestCryptAuthDeviceManager
: public CryptAuthDeviceManager
{
106 TestCryptAuthDeviceManager(scoped_ptr
<base::Clock
> clock
,
107 scoped_ptr
<CryptAuthClientFactory
> client_factory
,
108 CryptAuthGCMManager
* gcm_manager
,
109 PrefService
* pref_service
)
110 : CryptAuthDeviceManager(clock
.Pass(),
111 client_factory
.Pass(),
114 scoped_sync_scheduler_(new NiceMock
<MockSyncScheduler
>()),
115 weak_sync_scheduler_factory_(scoped_sync_scheduler_
.get()) {}
117 ~TestCryptAuthDeviceManager() override
{}
119 scoped_ptr
<SyncScheduler
> CreateSyncScheduler() override
{
120 EXPECT_TRUE(scoped_sync_scheduler_
);
121 return scoped_sync_scheduler_
.Pass();
124 base::WeakPtr
<MockSyncScheduler
> GetSyncScheduler() {
125 return weak_sync_scheduler_factory_
.GetWeakPtr();
129 // Ownership is passed to |CryptAuthDeviceManager| super class when
130 // |CreateSyncScheduler()| is called.
131 scoped_ptr
<MockSyncScheduler
> scoped_sync_scheduler_
;
133 // Stores the pointer of |scoped_sync_scheduler_| after ownership is passed to
135 // This should be safe because the life-time this SyncScheduler will always be
136 // within the life of the TestCryptAuthDeviceManager object.
137 base::WeakPtrFactory
<MockSyncScheduler
> weak_sync_scheduler_factory_
;
139 DISALLOW_COPY_AND_ASSIGN(TestCryptAuthDeviceManager
);
144 class ProximityAuthCryptAuthDeviceManagerTest
145 : public testing::Test
,
146 public CryptAuthDeviceManager::Observer
,
147 public MockCryptAuthClientFactory::Observer
{
149 ProximityAuthCryptAuthDeviceManagerTest()
150 : clock_(new base::SimpleTestClock()),
151 client_factory_(new MockCryptAuthClientFactory(
152 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS
)),
153 gcm_manager_("existing gcm registration id"),
154 device_manager_(make_scoped_ptr(clock_
),
155 make_scoped_ptr(client_factory_
),
158 client_factory_
->AddObserver(this);
161 ~ProximityAuthCryptAuthDeviceManagerTest() {
162 client_factory_
->RemoveObserver(this);
166 void SetUp() override
{
167 clock_
->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds
));
168 device_manager_
.AddObserver(this);
170 CryptAuthDeviceManager::RegisterPrefs(pref_service_
.registry());
171 pref_service_
.SetUserPref(
172 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
,
173 new base::FundamentalValue(false));
174 pref_service_
.SetUserPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds
,
175 new base::FundamentalValue(kLastSyncTimeSeconds
));
176 pref_service_
.SetUserPref(
177 prefs::kCryptAuthDeviceSyncReason
,
178 new base::FundamentalValue(cryptauth::INVOCATION_REASON_UNKNOWN
));
180 scoped_ptr
<base::DictionaryValue
> unlock_key_dictionary(
181 new base::DictionaryValue());
183 std::string public_key_b64
, device_name_b64
, bluetooth_address_b64
;
184 Base64UrlEncode(kStoredPublicKey
, &public_key_b64
);
185 Base64UrlEncode(kStoredDeviceName
, &device_name_b64
);
186 Base64UrlEncode(kStoredBluetoothAddress
, &bluetooth_address_b64
);
188 unlock_key_dictionary
->SetString("public_key", public_key_b64
);
189 unlock_key_dictionary
->SetString("device_name", device_name_b64
);
190 unlock_key_dictionary
->SetString("bluetooth_address",
191 bluetooth_address_b64
);
193 ListPrefUpdate
update(&pref_service_
,
194 prefs::kCryptAuthDeviceSyncUnlockKeys
);
195 update
.Get()->Append(unlock_key_dictionary
.Pass());
198 cryptauth::ExternalDeviceInfo unlock_key
;
199 unlock_key
.set_public_key(kPublicKey1
);
200 unlock_key
.set_friendly_device_name(kDeviceName1
);
201 unlock_key
.set_bluetooth_address(kBluetoothAddress1
);
202 unlock_key
.set_unlock_key(true);
203 unlock_key
.set_unlockable(false);
205 cryptauth::ExternalDeviceInfo unlockable_device
;
206 unlockable_device
.set_public_key(kPublicKey2
);
207 unlockable_device
.set_friendly_device_name(kDeviceName2
);
208 unlockable_device
.set_unlock_key(false);
209 unlockable_device
.set_unlockable(true);
211 get_my_devices_response_
.add_devices()->CopyFrom(unlock_key
);
212 get_my_devices_response_
.add_devices()->CopyFrom(unlockable_device
);
214 ON_CALL(*sync_scheduler(), GetStrategy())
215 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH
));
218 void TearDown() override
{ device_manager_
.RemoveObserver(this); }
220 // CryptAuthDeviceManager::Observer:
221 void OnSyncStarted() override
{ OnSyncStartedProxy(); }
223 void OnSyncFinished(CryptAuthDeviceManager::SyncResult sync_result
,
224 CryptAuthDeviceManager::DeviceChangeResult
225 device_change_result
) override
{
226 OnSyncFinishedProxy(sync_result
, device_change_result
);
229 MOCK_METHOD0(OnSyncStartedProxy
, void());
230 MOCK_METHOD2(OnSyncFinishedProxy
,
231 void(CryptAuthDeviceManager::SyncResult
,
232 CryptAuthDeviceManager::DeviceChangeResult
));
234 // Simulates firing the SyncScheduler to trigger a device sync attempt.
235 void FireSchedulerForSync(
236 cryptauth::InvocationReason expected_invocation_reason
) {
237 SyncScheduler::Delegate
* delegate
=
238 static_cast<SyncScheduler::Delegate
*>(&device_manager_
);
240 scoped_ptr
<SyncScheduler::SyncRequest
> sync_request
= make_scoped_ptr(
241 new SyncScheduler::SyncRequest(device_manager_
.GetSyncScheduler()));
242 EXPECT_CALL(*this, OnSyncStartedProxy());
243 delegate
->OnSyncRequested(sync_request
.Pass());
245 EXPECT_EQ(expected_invocation_reason
,
246 get_my_devices_request_
.invocation_reason());
248 // The allow_stale_read flag is set if the sync was not forced.
249 bool allow_stale_read
=
250 pref_service_
.GetInteger(prefs::kCryptAuthDeviceSyncReason
) !=
251 cryptauth::INVOCATION_REASON_UNKNOWN
;
252 EXPECT_EQ(allow_stale_read
, get_my_devices_request_
.allow_stale_read());
255 // MockCryptAuthClientFactory::Observer:
256 void OnCryptAuthClientCreated(MockCryptAuthClient
* client
) override
{
257 EXPECT_CALL(*client
, GetMyDevices(_
, _
, _
))
258 .WillOnce(DoAll(SaveArg
<0>(&get_my_devices_request_
),
259 SaveArg
<1>(&success_callback_
),
260 SaveArg
<2>(&error_callback_
)));
263 MockSyncScheduler
* sync_scheduler() {
264 return device_manager_
.GetSyncScheduler().get();
267 // Owned by |device_manager_|.
268 base::SimpleTestClock
* clock_
;
270 // Owned by |device_manager_|.
271 MockCryptAuthClientFactory
* client_factory_
;
273 TestingPrefServiceSimple pref_service_
;
275 FakeCryptAuthGCMManager gcm_manager_
;
277 TestCryptAuthDeviceManager device_manager_
;
279 cryptauth::GetMyDevicesResponse get_my_devices_response_
;
281 cryptauth::GetMyDevicesRequest get_my_devices_request_
;
283 CryptAuthClient::GetMyDevicesCallback success_callback_
;
285 CryptAuthClient::ErrorCallback error_callback_
;
287 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthDeviceManagerTest
);
290 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, RegisterPrefs
) {
291 TestingPrefServiceSimple pref_service
;
292 CryptAuthDeviceManager::RegisterPrefs(pref_service
.registry());
293 EXPECT_TRUE(pref_service
.FindPreference(
294 prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds
));
295 EXPECT_TRUE(pref_service
.FindPreference(
296 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
297 EXPECT_TRUE(pref_service
.FindPreference(prefs::kCryptAuthDeviceSyncReason
));
299 pref_service
.FindPreference(prefs::kCryptAuthDeviceSyncUnlockKeys
));
302 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, GetSyncState
) {
303 device_manager_
.Start();
305 ON_CALL(*sync_scheduler(), GetStrategy())
306 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH
));
307 EXPECT_FALSE(device_manager_
.IsRecoveringFromFailure());
309 ON_CALL(*sync_scheduler(), GetStrategy())
310 .WillByDefault(Return(SyncScheduler::Strategy::AGGRESSIVE_RECOVERY
));
311 EXPECT_TRUE(device_manager_
.IsRecoveringFromFailure());
313 base::TimeDelta time_to_next_sync
= base::TimeDelta::FromMinutes(60);
314 ON_CALL(*sync_scheduler(), GetTimeToNextSync())
315 .WillByDefault(Return(time_to_next_sync
));
316 EXPECT_EQ(time_to_next_sync
, device_manager_
.GetTimeToNextAttempt());
318 ON_CALL(*sync_scheduler(), GetSyncState())
319 .WillByDefault(Return(SyncScheduler::SyncState::SYNC_IN_PROGRESS
));
320 EXPECT_TRUE(device_manager_
.IsSyncInProgress());
322 ON_CALL(*sync_scheduler(), GetSyncState())
323 .WillByDefault(Return(SyncScheduler::SyncState::WAITING_FOR_REFRESH
));
324 EXPECT_FALSE(device_manager_
.IsSyncInProgress());
327 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, InitWithDefaultPrefs
) {
328 scoped_ptr
<base::SimpleTestClock
> clock(new base::SimpleTestClock
);
329 clock
->SetNow(base::Time::FromDoubleT(kInitialTimeNowSeconds
));
330 base::TimeDelta elapsed_time
= clock
->Now() - base::Time::FromDoubleT(0);
332 TestingPrefServiceSimple pref_service
;
333 CryptAuthDeviceManager::RegisterPrefs(pref_service
.registry());
335 TestCryptAuthDeviceManager
device_manager(
337 make_scoped_ptr(new MockCryptAuthClientFactory(
338 MockCryptAuthClientFactory::MockType::MAKE_STRICT_MOCKS
)),
339 &gcm_manager_
, &pref_service
);
342 *(device_manager
.GetSyncScheduler()),
343 Start(elapsed_time
, SyncScheduler::Strategy::AGGRESSIVE_RECOVERY
));
344 device_manager
.Start();
345 EXPECT_TRUE(device_manager
.GetLastSyncTime().is_null());
346 EXPECT_EQ(0u, device_manager
.unlock_keys().size());
349 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, InitWithExistingPrefs
) {
352 Start(clock_
->Now() - base::Time::FromDoubleT(kLastSyncTimeSeconds
),
353 SyncScheduler::Strategy::PERIODIC_REFRESH
));
355 device_manager_
.Start();
356 EXPECT_EQ(base::Time::FromDoubleT(kLastSyncTimeSeconds
),
357 device_manager_
.GetLastSyncTime());
359 auto unlock_keys
= device_manager_
.unlock_keys();
360 ASSERT_EQ(1u, unlock_keys
.size());
361 EXPECT_EQ(kStoredPublicKey
, unlock_keys
[0].public_key());
362 EXPECT_EQ(kStoredDeviceName
, unlock_keys
[0].friendly_device_name());
363 EXPECT_EQ(kStoredBluetoothAddress
, unlock_keys
[0].bluetooth_address());
364 EXPECT_TRUE(unlock_keys
[0].unlock_key());
367 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, SyncSucceedsForFirstTime
) {
368 pref_service_
.ClearPref(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds
);
369 device_manager_
.Start();
371 FireSchedulerForSync(cryptauth::INVOCATION_REASON_INITIALIZATION
);
372 ASSERT_FALSE(success_callback_
.is_null());
374 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
));
375 EXPECT_CALL(*this, OnSyncFinishedProxy(
376 CryptAuthDeviceManager::SyncResult::SUCCESS
,
377 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
379 success_callback_
.Run(get_my_devices_response_
);
380 EXPECT_EQ(clock_
->Now(), device_manager_
.GetLastSyncTime());
382 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(
383 1, get_my_devices_response_
.devices(0)),
384 device_manager_
.unlock_keys(), pref_service_
);
387 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, ForceSync
) {
388 device_manager_
.Start();
390 EXPECT_CALL(*sync_scheduler(), ForceSync());
391 device_manager_
.ForceSyncNow(cryptauth::INVOCATION_REASON_MANUAL
);
393 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL
);
395 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
));
396 EXPECT_CALL(*this, OnSyncFinishedProxy(
397 CryptAuthDeviceManager::SyncResult::SUCCESS
,
398 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
399 success_callback_
.Run(get_my_devices_response_
);
400 EXPECT_EQ(clock_
->Now(), device_manager_
.GetLastSyncTime());
402 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(
403 1, get_my_devices_response_
.devices(0)),
404 device_manager_
.unlock_keys(), pref_service_
);
407 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, ForceSyncFailsThenSucceeds
) {
408 device_manager_
.Start();
409 EXPECT_FALSE(pref_service_
.GetBoolean(
410 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
411 base::Time old_sync_time
= device_manager_
.GetLastSyncTime();
413 // The first force sync fails.
414 EXPECT_CALL(*sync_scheduler(), ForceSync());
415 device_manager_
.ForceSyncNow(cryptauth::INVOCATION_REASON_MANUAL
);
416 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL
);
417 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
));
420 CryptAuthDeviceManager::SyncResult::FAILURE
,
421 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED
));
422 error_callback_
.Run("404");
423 EXPECT_EQ(old_sync_time
, device_manager_
.GetLastSyncTime());
424 EXPECT_TRUE(pref_service_
.GetBoolean(
425 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
426 EXPECT_EQ(static_cast<int>(cryptauth::INVOCATION_REASON_MANUAL
),
427 pref_service_
.GetInteger(prefs::kCryptAuthDeviceSyncReason
));
429 // The second recovery sync succeeds.
430 ON_CALL(*sync_scheduler(), GetStrategy())
431 .WillByDefault(Return(SyncScheduler::Strategy::AGGRESSIVE_RECOVERY
));
432 FireSchedulerForSync(cryptauth::INVOCATION_REASON_MANUAL
);
433 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
+ 30));
434 EXPECT_CALL(*this, OnSyncFinishedProxy(
435 CryptAuthDeviceManager::SyncResult::SUCCESS
,
436 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
437 success_callback_
.Run(get_my_devices_response_
);
438 EXPECT_EQ(clock_
->Now(), device_manager_
.GetLastSyncTime());
440 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(
441 1, get_my_devices_response_
.devices(0)),
442 device_manager_
.unlock_keys(), pref_service_
);
445 clock_
->Now().ToDoubleT(),
446 pref_service_
.GetDouble(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds
));
447 EXPECT_EQ(static_cast<int>(cryptauth::INVOCATION_REASON_UNKNOWN
),
448 pref_service_
.GetInteger(prefs::kCryptAuthDeviceSyncReason
));
449 EXPECT_FALSE(pref_service_
.GetBoolean(
450 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
453 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, PeriodicSyncFailsThenSucceeds
) {
454 device_manager_
.Start();
455 base::Time old_sync_time
= device_manager_
.GetLastSyncTime();
457 // The first periodic sync fails.
458 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC
);
459 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
));
462 CryptAuthDeviceManager::SyncResult::FAILURE
,
463 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED
));
464 error_callback_
.Run("401");
465 EXPECT_EQ(old_sync_time
, device_manager_
.GetLastSyncTime());
466 EXPECT_TRUE(pref_service_
.GetBoolean(
467 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
469 // The second recovery sync succeeds.
470 ON_CALL(*sync_scheduler(), GetStrategy())
471 .WillByDefault(Return(SyncScheduler::Strategy::AGGRESSIVE_RECOVERY
));
472 FireSchedulerForSync(cryptauth::INVOCATION_REASON_FAILURE_RECOVERY
);
473 clock_
->SetNow(base::Time::FromDoubleT(kLaterTimeNowSeconds
+ 30));
474 EXPECT_CALL(*this, OnSyncFinishedProxy(
475 CryptAuthDeviceManager::SyncResult::SUCCESS
,
476 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
477 success_callback_
.Run(get_my_devices_response_
);
478 EXPECT_EQ(clock_
->Now(), device_manager_
.GetLastSyncTime());
480 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(
481 1, get_my_devices_response_
.devices(0)),
482 device_manager_
.unlock_keys(), pref_service_
);
485 clock_
->Now().ToDoubleT(),
486 pref_service_
.GetDouble(prefs::kCryptAuthDeviceSyncLastSyncTimeSeconds
));
487 EXPECT_FALSE(pref_service_
.GetBoolean(
488 prefs::kCryptAuthDeviceSyncIsRecoveringFromFailure
));
491 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, SyncSameDevice
) {
492 // Set the same unlock key in the user prefs as the one that would be synced.
494 scoped_ptr
<base::DictionaryValue
> unlock_key_dictionary(
495 new base::DictionaryValue());
496 std::string public_key_b64
, device_name_b64
, bluetooth_address_b64
;
497 Base64UrlEncode(kPublicKey1
, &public_key_b64
);
498 Base64UrlEncode(kDeviceName1
, &device_name_b64
);
499 Base64UrlEncode(kBluetoothAddress1
, &bluetooth_address_b64
);
500 unlock_key_dictionary
->SetString("public_key", public_key_b64
);
501 unlock_key_dictionary
->SetString("device_name", device_name_b64
);
502 unlock_key_dictionary
->SetString("bluetooth_address",
503 bluetooth_address_b64
);
505 ListPrefUpdate
update(&pref_service_
,
506 prefs::kCryptAuthDeviceSyncUnlockKeys
);
507 update
.Get()->Clear();
508 update
.Get()->Append(unlock_key_dictionary
.Pass());
511 // Check unlock keys before sync.
512 device_manager_
.Start();
513 auto original_unlock_keys
= device_manager_
.unlock_keys();
514 ASSERT_EQ(1u, original_unlock_keys
.size());
515 EXPECT_EQ(kPublicKey1
, original_unlock_keys
[0].public_key());
516 EXPECT_EQ(kDeviceName1
, original_unlock_keys
[0].friendly_device_name());
517 EXPECT_EQ(kBluetoothAddress1
, original_unlock_keys
[0].bluetooth_address());
520 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC
);
521 ASSERT_FALSE(success_callback_
.is_null());
524 CryptAuthDeviceManager::SyncResult::SUCCESS
,
525 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED
));
526 success_callback_
.Run(get_my_devices_response_
);
528 // Check that unlock keys are still the same after sync.
529 ExpectUnlockKeysAndPrefAreEqual(original_unlock_keys
,
530 device_manager_
.unlock_keys(), pref_service_
);
533 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, SyncEmptyDeviceList
) {
534 cryptauth::GetMyDevicesResponse empty_response
;
536 device_manager_
.Start();
537 EXPECT_EQ(1u, device_manager_
.unlock_keys().size());
539 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC
);
540 ASSERT_FALSE(success_callback_
.is_null());
541 EXPECT_CALL(*this, OnSyncFinishedProxy(
542 CryptAuthDeviceManager::SyncResult::SUCCESS
,
543 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
544 success_callback_
.Run(empty_response
);
546 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(),
547 device_manager_
.unlock_keys(), pref_service_
);
550 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, SyncTwoUnlockKeys
) {
551 cryptauth::GetMyDevicesResponse
response(get_my_devices_response_
);
552 cryptauth::ExternalDeviceInfo unlock_key2
;
553 unlock_key2
.set_public_key("new public key");
554 unlock_key2
.set_friendly_device_name("new device name");
555 unlock_key2
.set_bluetooth_address("aa:bb:cc:dd:ee:ff");
556 unlock_key2
.set_unlock_key(true);
557 response
.add_devices()->CopyFrom(unlock_key2
);
559 std::vector
<cryptauth::ExternalDeviceInfo
> expected_unlock_keys
;
560 expected_unlock_keys
.push_back(get_my_devices_response_
.devices(0));
561 expected_unlock_keys
.push_back(unlock_key2
);
563 device_manager_
.Start();
564 EXPECT_EQ(1u, device_manager_
.unlock_keys().size());
565 EXPECT_EQ(1u, pref_service_
.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys
)
568 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC
);
569 ASSERT_FALSE(success_callback_
.is_null());
570 EXPECT_CALL(*this, OnSyncFinishedProxy(
571 CryptAuthDeviceManager::SyncResult::SUCCESS
,
572 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
573 success_callback_
.Run(response
);
575 ExpectUnlockKeysAndPrefAreEqual(expected_unlock_keys
,
576 device_manager_
.unlock_keys(), pref_service_
);
579 TEST_F(ProximityAuthCryptAuthDeviceManagerTest
, SyncOnGCMPushMessage
) {
580 device_manager_
.Start();
582 EXPECT_CALL(*sync_scheduler(), ForceSync());
583 gcm_manager_
.PushResyncMessage();
585 FireSchedulerForSync(cryptauth::INVOCATION_REASON_SERVER_INITIATED
);
587 EXPECT_CALL(*this, OnSyncFinishedProxy(
588 CryptAuthDeviceManager::SyncResult::SUCCESS
,
589 CryptAuthDeviceManager::DeviceChangeResult::CHANGED
));
590 success_callback_
.Run(get_my_devices_response_
);
592 ExpectUnlockKeysAndPrefAreEqual(std::vector
<cryptauth::ExternalDeviceInfo
>(
593 1, get_my_devices_response_
.devices(0)),
594 device_manager_
.unlock_keys(), pref_service_
);
597 } // namespace proximity_auth