Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: components/cryptauth/cryptauth_device_manager_unittest.cc

Issue 2540073002: CryptAuthDeviceManager: Add support for syncing all of the ExternalDeviceInfo fields. (Closed)
Patch Set: tengs@ comment. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/cryptauth/cryptauth_device_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/cryptauth/cryptauth_device_manager.h" 5 #include "components/cryptauth/cryptauth_device_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/base64url.h" 11 #include "base/base64url.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
16 #include "base/test/simple_test_clock.h" 17 #include "base/test/simple_test_clock.h"
17 #include "components/cryptauth/fake_cryptauth_gcm_manager.h" 18 #include "components/cryptauth/fake_cryptauth_gcm_manager.h"
18 #include "components/cryptauth/mock_cryptauth_client.h" 19 #include "components/cryptauth/mock_cryptauth_client.h"
19 #include "components/cryptauth/mock_sync_scheduler.h" 20 #include "components/cryptauth/mock_sync_scheduler.h"
20 #include "components/cryptauth/pref_names.h" 21 #include "components/cryptauth/pref_names.h"
21 #include "components/prefs/scoped_user_pref_update.h" 22 #include "components/prefs/scoped_user_pref_update.h"
22 #include "components/prefs/testing_pref_service.h" 23 #include "components/prefs/testing_pref_service.h"
23 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 13 matching lines...) Expand all
38 // A later "Now" time for testing. 39 // A later "Now" time for testing.
39 const double kLaterTimeNowSeconds = kInitialTimeNowSeconds + 30; 40 const double kLaterTimeNowSeconds = kInitialTimeNowSeconds + 30;
40 41
41 // The timestamp of a last successful sync in seconds. 42 // The timestamp of a last successful sync in seconds.
42 const double kLastSyncTimeSeconds = kInitialTimeNowSeconds - (60 * 60 * 5); 43 const double kLastSyncTimeSeconds = kInitialTimeNowSeconds - (60 * 60 * 5);
43 44
44 // Unlock key fields originally stored in the user prefs. 45 // Unlock key fields originally stored in the user prefs.
45 const char kStoredPublicKey[] = "AAPL"; 46 const char kStoredPublicKey[] = "AAPL";
46 const char kStoredDeviceName[] = "iPhone 6"; 47 const char kStoredDeviceName[] = "iPhone 6";
47 const char kStoredBluetoothAddress[] = "12:34:56:78:90:AB"; 48 const char kStoredBluetoothAddress[] = "12:34:56:78:90:AB";
49 const bool kStoredUnlockKey = true;
50 const bool kStoredUnlockable = false;
48 51
49 // ExternalDeviceInfo fields for the synced unlock key. 52 // ExternalDeviceInfo fields for the synced unlock key.
50 const char kPublicKey1[] = "GOOG"; 53 const char kPublicKey1[] = "GOOG";
51 const char kDeviceName1[] = "Nexus 5"; 54 const char kDeviceName1[] = "Nexus 5";
52 const char kBluetoothAddress1[] = "aa:bb:cc:ee:dd:ff"; 55 const char kBluetoothAddress1[] = "aa:bb:cc:ee:dd:ff";
56 const char kBeaconSeed1Data[] = "beaconSeed1Data";
57 const int64_t kBeaconSeed1StartTime = 123456;
58 const int64_t kBeaconSeed1EndTime = 123457;
59 const char kBeaconSeed2Data[] = "beaconSeed2Data";
60 const int64_t kBeaconSeed2StartTime = 234567;
61 const int64_t kBeaconSeed2EndTime = 234568;
53 62
54 // ExternalDeviceInfo fields for a non-synced unlockable device. 63 // ExternalDeviceInfo fields for a non-synced unlockable device.
55 const char kPublicKey2[] = "MSFT"; 64 const char kPublicKey2[] = "MSFT";
56 const char kDeviceName2[] = "Surface Pro 3"; 65 const char kDeviceName2[] = "Surface Pro 3";
66 const char kBeaconSeed3Data[] = "beaconSeed3Data";
67 const int64_t kBeaconSeed3StartTime = 123456;
68 const int64_t kBeaconSeed3EndTime = 123457;
69 const char kBeaconSeed4Data[] = "beaconSeed4Data";
70 const int64_t kBeaconSeed4StartTime = 234567;
71 const int64_t kBeaconSeed4EndTime = 234568;
57 72
58 // Validates that |unlock_keys| and the corresponding preferences stored by 73 // Validates that |unlock_keys| and the corresponding preferences stored by
59 // |pref_service| are equal to |expected_unlock_keys|. 74 // |pref_service| are equal to |expected_unlock_keys|.
60 void ExpectUnlockKeysAndPrefAreEqual( 75 void ExpectUnlockKeysAndPrefAreEqual(
61 const std::vector<cryptauth::ExternalDeviceInfo>& expected_unlock_keys, 76 const std::vector<cryptauth::ExternalDeviceInfo>& expected_unlock_keys,
62 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys, 77 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys,
63 const PrefService& pref_service) { 78 const PrefService& pref_service) {
64 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys.size()); 79 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys.size());
65 for (size_t i = 0; i < unlock_keys.size(); ++i) { 80 for (size_t i = 0; i < unlock_keys.size(); ++i) {
66 SCOPED_TRACE( 81 SCOPED_TRACE(
67 base::StringPrintf("Compare protos at index=%d", static_cast<int>(i))); 82 base::StringPrintf("Compare protos at index=%d", static_cast<int>(i)));
68 const auto& expected_unlock_key = expected_unlock_keys[i]; 83 const auto& expected_unlock_key = expected_unlock_keys[i];
69 const auto& unlock_key = unlock_keys.at(i); 84 const auto& unlock_key = unlock_keys.at(i);
70 EXPECT_EQ(expected_unlock_key.public_key(), unlock_key.public_key()); 85 EXPECT_TRUE(expected_unlock_key.has_public_key());
86 EXPECT_TRUE(unlock_key.has_public_key());
87 EXPECT_EQ(expected_unlock_key.public_key(),
88 unlock_key.public_key());
89
90 EXPECT_EQ(expected_unlock_key.has_friendly_device_name(),
91 unlock_key.has_friendly_device_name());
71 EXPECT_EQ(expected_unlock_key.friendly_device_name(), 92 EXPECT_EQ(expected_unlock_key.friendly_device_name(),
72 unlock_key.friendly_device_name()); 93 unlock_key.friendly_device_name());
94
95 EXPECT_EQ(expected_unlock_key.has_bluetooth_address(),
96 unlock_key.has_bluetooth_address());
73 EXPECT_EQ(expected_unlock_key.bluetooth_address(), 97 EXPECT_EQ(expected_unlock_key.bluetooth_address(),
74 unlock_key.bluetooth_address()); 98 unlock_key.bluetooth_address());
75 EXPECT_TRUE(expected_unlock_key.unlock_key()); 99
76 EXPECT_FALSE(expected_unlock_key.unlockable()); 100 EXPECT_EQ(expected_unlock_key.has_unlock_key(),
101 unlock_key.has_unlock_key());
102 EXPECT_EQ(expected_unlock_key.unlock_key(),
103 unlock_key.unlock_key());
104
105 EXPECT_EQ(expected_unlock_key.has_unlockable(),
106 unlock_key.has_unlockable());
107 EXPECT_EQ(expected_unlock_key.unlockable(),
108 unlock_key.unlockable());
109
110 EXPECT_EQ(expected_unlock_key.has_last_update_time_millis(),
111 unlock_key.has_last_update_time_millis());
112 EXPECT_EQ(expected_unlock_key.last_update_time_millis(),
113 unlock_key.last_update_time_millis());
114
115 EXPECT_EQ(expected_unlock_key.has_mobile_hotspot_supported(),
116 unlock_key.has_mobile_hotspot_supported());
117 EXPECT_EQ(expected_unlock_key.mobile_hotspot_supported(),
118 unlock_key.mobile_hotspot_supported());
119
120 EXPECT_EQ(expected_unlock_key.has_device_type(),
121 unlock_key.has_device_type());
122 EXPECT_EQ(expected_unlock_key.device_type(),
123 unlock_key.device_type());
124
125 ASSERT_EQ(expected_unlock_key.beacon_seeds_size(),
126 unlock_key.beacon_seeds_size());
127 for (int i = 0; i < expected_unlock_key.beacon_seeds_size(); i++) {
128 const cryptauth::BeaconSeed expected_seed =
129 expected_unlock_key.beacon_seeds(i);
130 const cryptauth::BeaconSeed seed = unlock_key.beacon_seeds(i);
131 EXPECT_TRUE(expected_seed.has_data());
132 EXPECT_TRUE(seed.has_data());
133 EXPECT_EQ(expected_seed.data(), seed.data());
134
135 EXPECT_TRUE(expected_seed.has_start_time_millis());
136 EXPECT_TRUE(seed.has_start_time_millis());
137 EXPECT_EQ(expected_seed.start_time_millis(), seed.start_time_millis());
138
139 EXPECT_TRUE(expected_seed.has_end_time_millis());
140 EXPECT_TRUE(seed.has_end_time_millis());
141 EXPECT_EQ(expected_seed.end_time_millis(), seed.end_time_millis());
142 }
77 } 143 }
78 144
79 const base::ListValue* unlock_keys_pref = 145 const base::ListValue* unlock_keys_pref =
80 pref_service.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys); 146 pref_service.GetList(prefs::kCryptAuthDeviceSyncUnlockKeys);
81 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys_pref->GetSize()); 147 ASSERT_EQ(expected_unlock_keys.size(), unlock_keys_pref->GetSize());
82 for (size_t i = 0; i < unlock_keys_pref->GetSize(); ++i) { 148 for (size_t i = 0; i < unlock_keys_pref->GetSize(); ++i) {
83 SCOPED_TRACE(base::StringPrintf("Compare pref dictionary at index=%d", 149 SCOPED_TRACE(base::StringPrintf("Compare pref dictionary at index=%d",
84 static_cast<int>(i))); 150 static_cast<int>(i)));
85 const base::DictionaryValue* unlock_key_dictionary; 151 const base::DictionaryValue* unlock_key_dictionary;
86 EXPECT_TRUE(unlock_keys_pref->GetDictionary(i, &unlock_key_dictionary)); 152 EXPECT_TRUE(unlock_keys_pref->GetDictionary(i, &unlock_key_dictionary));
87 std::string public_key_b64, device_name_b64, bluetooth_address_b64; 153
88 ASSERT_TRUE( 154 const auto& expected_unlock_key = expected_unlock_keys[i];
155
156 std::string public_key_b64, public_key;
157 EXPECT_TRUE(
89 unlock_key_dictionary->GetString("public_key", &public_key_b64)); 158 unlock_key_dictionary->GetString("public_key", &public_key_b64));
90 ASSERT_TRUE( 159 EXPECT_TRUE(base::Base64UrlDecode(
91 unlock_key_dictionary->GetString("device_name", &device_name_b64));
92 ASSERT_TRUE(unlock_key_dictionary->GetString("bluetooth_address",
93 &bluetooth_address_b64));
94
95 std::string public_key, device_name, bluetooth_address;
96 ASSERT_TRUE(base::Base64UrlDecode(
97 public_key_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING, 160 public_key_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING,
98 &public_key)); 161 &public_key));
99 ASSERT_TRUE(base::Base64UrlDecode( 162 EXPECT_TRUE(expected_unlock_key.has_public_key());
100 device_name_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING, 163 EXPECT_EQ(expected_unlock_key.public_key(), public_key);
101 &device_name));
102 ASSERT_TRUE(base::Base64UrlDecode(
103 bluetooth_address_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING,
104 &bluetooth_address));
105 164
106 const auto& expected_unlock_key = expected_unlock_keys[i]; 165 std::string device_name_b64, device_name;
107 EXPECT_EQ(expected_unlock_key.public_key(), public_key); 166 if (unlock_key_dictionary->GetString("device_name", &device_name_b64)) {
108 EXPECT_EQ(expected_unlock_key.friendly_device_name(), device_name); 167 EXPECT_TRUE(base::Base64UrlDecode(
109 EXPECT_EQ(expected_unlock_key.bluetooth_address(), bluetooth_address); 168 device_name_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING,
110 EXPECT_TRUE(expected_unlock_key.unlock_key()); 169 &device_name));
111 EXPECT_FALSE(expected_unlock_key.unlockable()); 170 EXPECT_TRUE(expected_unlock_key.has_friendly_device_name());
171 EXPECT_EQ(expected_unlock_key.friendly_device_name(), device_name);
172 } else {
173 EXPECT_FALSE(expected_unlock_key.has_friendly_device_name());
174 }
175
176 std::string bluetooth_address_b64, bluetooth_address;
177 if (unlock_key_dictionary->GetString("bluetooth_address",
178 &bluetooth_address_b64)) {
179 EXPECT_TRUE(base::Base64UrlDecode(
180 bluetooth_address_b64, base::Base64UrlDecodePolicy::REQUIRE_PADDING,
181 &bluetooth_address));
182 EXPECT_TRUE(expected_unlock_key.has_bluetooth_address());
183 EXPECT_EQ(expected_unlock_key.bluetooth_address(), bluetooth_address);
184 } else {
185 EXPECT_FALSE(expected_unlock_key.has_bluetooth_address());
186 }
187
188 bool unlock_key;
189 if (unlock_key_dictionary->GetBoolean("unlock_key", &unlock_key)) {
190 EXPECT_TRUE(expected_unlock_key.has_unlock_key());
191 EXPECT_EQ(expected_unlock_key.unlock_key(), unlock_key);
192 } else {
193 EXPECT_FALSE(expected_unlock_key.has_unlock_key());
194 }
195
196 bool unlockable;
197 if (unlock_key_dictionary->GetBoolean("unlockable", &unlockable)) {
198 EXPECT_TRUE(expected_unlock_key.has_unlockable());
199 EXPECT_EQ(expected_unlock_key.unlockable(), unlockable);
200 } else {
201 EXPECT_FALSE(expected_unlock_key.has_unlockable());
202 }
203
204 std::string last_update_time_millis_str;
205 if (unlock_key_dictionary->GetString("last_update_time_millis",
206 &last_update_time_millis_str)) {
207 int64_t last_update_time_millis;
208 EXPECT_TRUE(base::StringToInt64(last_update_time_millis_str,
209 &last_update_time_millis));
210 EXPECT_TRUE(expected_unlock_key.has_last_update_time_millis());
211 EXPECT_EQ(expected_unlock_key.last_update_time_millis(),
212 last_update_time_millis);
213 } else {
214 EXPECT_FALSE(expected_unlock_key.has_last_update_time_millis());
215 }
216
217 bool mobile_hotspot_supported;
218 if (unlock_key_dictionary->GetBoolean("mobile_hotspot_supported",
219 &mobile_hotspot_supported)) {
220 EXPECT_TRUE(expected_unlock_key.has_mobile_hotspot_supported());
221 EXPECT_EQ(expected_unlock_key.mobile_hotspot_supported(),
222 mobile_hotspot_supported);
223 } else {
224 EXPECT_FALSE(expected_unlock_key.has_mobile_hotspot_supported());
225 }
226
227 int device_type;
228 if (unlock_key_dictionary->GetInteger("device_type",
229 &device_type)) {
230 EXPECT_TRUE(expected_unlock_key.has_device_type());
231 EXPECT_EQ(expected_unlock_key.device_type(),
232 device_type);
233 } else {
234 EXPECT_FALSE(expected_unlock_key.has_device_type());
235 }
236
237 const base::ListValue* beacon_seeds_from_prefs;
238 if (unlock_key_dictionary->GetList("beacon_seeds",
239 &beacon_seeds_from_prefs)) {
240 ASSERT_EQ(
241 (size_t) expected_unlock_key.beacon_seeds_size(),
242 beacon_seeds_from_prefs->GetSize());
243 for (size_t i = 0; i < beacon_seeds_from_prefs->GetSize(); i++) {
244 const base::DictionaryValue* seed;
245 ASSERT_TRUE(beacon_seeds_from_prefs->GetDictionary(i, &seed));
246
247 std::string data, start_ms, end_ms;
248 EXPECT_TRUE(seed->GetString("beacon_seed_data", &data));
249 EXPECT_TRUE(seed->GetString("beacon_seed_start_ms", &start_ms));
250 EXPECT_TRUE(seed->GetString("beacon_seed_end_ms", &end_ms));
251
252 const cryptauth::BeaconSeed& expected_seed =
253 expected_unlock_key.beacon_seeds((int) i);
254 EXPECT_TRUE(expected_seed.has_data());
255 EXPECT_EQ(expected_seed.data(), data);
256
257 EXPECT_TRUE(expected_seed.has_start_time_millis());
258 EXPECT_EQ(expected_seed.start_time_millis(), std::stol(start_ms));
259
260 EXPECT_TRUE(expected_seed.has_end_time_millis());
261 EXPECT_EQ(expected_seed.end_time_millis(), std::stol(end_ms));
262 }
263 } else {
264 EXPECT_FALSE(expected_unlock_key.beacon_seeds_size());
265 }
112 } 266 }
113 } 267 }
114 268
115 // Harness for testing CryptAuthDeviceManager. 269 // Harness for testing CryptAuthDeviceManager.
116 class TestCryptAuthDeviceManager : public CryptAuthDeviceManager { 270 class TestCryptAuthDeviceManager : public CryptAuthDeviceManager {
117 public: 271 public:
118 TestCryptAuthDeviceManager( 272 TestCryptAuthDeviceManager(
119 std::unique_ptr<base::Clock> clock, 273 std::unique_ptr<base::Clock> clock,
120 std::unique_ptr<CryptAuthClientFactory> client_factory, 274 std::unique_ptr<CryptAuthClientFactory> client_factory,
121 CryptAuthGCMManager* gcm_manager, 275 CryptAuthGCMManager* gcm_manager,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 base::Base64UrlEncodePolicy::INCLUDE_PADDING, 350 base::Base64UrlEncodePolicy::INCLUDE_PADDING,
197 &device_name_b64); 351 &device_name_b64);
198 base::Base64UrlEncode(kStoredBluetoothAddress, 352 base::Base64UrlEncode(kStoredBluetoothAddress,
199 base::Base64UrlEncodePolicy::INCLUDE_PADDING, 353 base::Base64UrlEncodePolicy::INCLUDE_PADDING,
200 &bluetooth_address_b64); 354 &bluetooth_address_b64);
201 355
202 unlock_key_dictionary->SetString("public_key", public_key_b64); 356 unlock_key_dictionary->SetString("public_key", public_key_b64);
203 unlock_key_dictionary->SetString("device_name", device_name_b64); 357 unlock_key_dictionary->SetString("device_name", device_name_b64);
204 unlock_key_dictionary->SetString("bluetooth_address", 358 unlock_key_dictionary->SetString("bluetooth_address",
205 bluetooth_address_b64); 359 bluetooth_address_b64);
360 unlock_key_dictionary->SetBoolean("unlock_key", kStoredUnlockKey);
361 unlock_key_dictionary->SetBoolean("unlockable", kStoredUnlockable);
362 unlock_key_dictionary->Set("beacon_seeds",
363 base::WrapUnique(new base::ListValue()));
206 { 364 {
207 ListPrefUpdate update(&pref_service_, 365 ListPrefUpdate update(&pref_service_,
208 prefs::kCryptAuthDeviceSyncUnlockKeys); 366 prefs::kCryptAuthDeviceSyncUnlockKeys);
209 update.Get()->Append(std::move(unlock_key_dictionary)); 367 update.Get()->Append(std::move(unlock_key_dictionary));
210 } 368 }
211 369
212 device_manager_.reset(new TestCryptAuthDeviceManager( 370 device_manager_.reset(new TestCryptAuthDeviceManager(
213 base::WrapUnique(clock_), base::WrapUnique(client_factory_), 371 base::WrapUnique(clock_), base::WrapUnique(client_factory_),
214 &gcm_manager_, &pref_service_)); 372 &gcm_manager_, &pref_service_));
215 device_manager_->AddObserver(this); 373 device_manager_->AddObserver(this);
216 374
217 cryptauth::ExternalDeviceInfo unlock_key; 375 cryptauth::ExternalDeviceInfo unlock_key;
218 unlock_key.set_public_key(kPublicKey1); 376 unlock_key.set_public_key(kPublicKey1);
219 unlock_key.set_friendly_device_name(kDeviceName1); 377 unlock_key.set_friendly_device_name(kDeviceName1);
220 unlock_key.set_bluetooth_address(kBluetoothAddress1); 378 unlock_key.set_bluetooth_address(kBluetoothAddress1);
221 unlock_key.set_unlock_key(true); 379 unlock_key.set_unlock_key(true);
222 unlock_key.set_unlockable(false); 380 unlock_key.set_unlockable(false);
381 cryptauth::BeaconSeed* seed1 = unlock_key.add_beacon_seeds();
382 seed1->set_data(kBeaconSeed1Data);
383 seed1->set_start_time_millis(kBeaconSeed1StartTime);
384 seed1->set_end_time_millis(kBeaconSeed1EndTime);
385 cryptauth::BeaconSeed* seed2 = unlock_key.add_beacon_seeds();
386 seed2->set_data(kBeaconSeed2Data);
387 seed2->set_start_time_millis(kBeaconSeed2StartTime);
388 seed2->set_end_time_millis(kBeaconSeed2EndTime);
223 389
224 cryptauth::ExternalDeviceInfo unlockable_device; 390 cryptauth::ExternalDeviceInfo unlockable_device;
225 unlockable_device.set_public_key(kPublicKey2); 391 unlockable_device.set_public_key(kPublicKey2);
226 unlockable_device.set_friendly_device_name(kDeviceName2); 392 unlockable_device.set_friendly_device_name(kDeviceName2);
227 unlockable_device.set_unlock_key(false); 393 unlockable_device.set_unlock_key(false);
228 unlockable_device.set_unlockable(true); 394 unlockable_device.set_unlockable(true);
395 cryptauth::BeaconSeed* seed3 = unlockable_device.add_beacon_seeds();
396 seed3->set_data(kBeaconSeed3Data);
397 seed3->set_start_time_millis(kBeaconSeed3StartTime);
398 seed3->set_end_time_millis(kBeaconSeed3EndTime);
399 cryptauth::BeaconSeed* seed4 = unlockable_device.add_beacon_seeds();
400 seed4->set_data(kBeaconSeed4Data);
401 seed4->set_start_time_millis(kBeaconSeed4StartTime);
402 seed4->set_end_time_millis(kBeaconSeed4EndTime);
229 403
230 get_my_devices_response_.add_devices()->CopyFrom(unlock_key); 404 get_my_devices_response_.add_devices()->CopyFrom(unlock_key);
231 get_my_devices_response_.add_devices()->CopyFrom(unlockable_device); 405 get_my_devices_response_.add_devices()->CopyFrom(unlockable_device);
232 406
233 ON_CALL(*sync_scheduler(), GetStrategy()) 407 ON_CALL(*sync_scheduler(), GetStrategy())
234 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH)); 408 .WillByDefault(Return(SyncScheduler::Strategy::PERIODIC_REFRESH));
235 } 409 }
236 410
237 void TearDown() override { device_manager_->RemoveObserver(this); } 411 void TearDown() override { device_manager_->RemoveObserver(this); }
238 412
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 EXPECT_CALL(*this, 696 EXPECT_CALL(*this,
523 OnSyncFinishedProxy( 697 OnSyncFinishedProxy(
524 CryptAuthDeviceManager::SyncResult::SUCCESS, 698 CryptAuthDeviceManager::SyncResult::SUCCESS,
525 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED)); 699 CryptAuthDeviceManager::DeviceChangeResult::UNCHANGED));
526 700
527 // Sync the same device. 701 // Sync the same device.
528 cryptauth::ExternalDeviceInfo synced_unlock_key; 702 cryptauth::ExternalDeviceInfo synced_unlock_key;
529 synced_unlock_key.set_public_key(kStoredPublicKey); 703 synced_unlock_key.set_public_key(kStoredPublicKey);
530 synced_unlock_key.set_friendly_device_name(kStoredDeviceName); 704 synced_unlock_key.set_friendly_device_name(kStoredDeviceName);
531 synced_unlock_key.set_bluetooth_address(kStoredBluetoothAddress); 705 synced_unlock_key.set_bluetooth_address(kStoredBluetoothAddress);
532 synced_unlock_key.set_unlock_key(true); 706 synced_unlock_key.set_unlock_key(kStoredUnlockKey);
533 synced_unlock_key.set_unlockable(false); 707 synced_unlock_key.set_unlockable(kStoredUnlockable);
534 cryptauth::GetMyDevicesResponse get_my_devices_response; 708 cryptauth::GetMyDevicesResponse get_my_devices_response;
535 get_my_devices_response.add_devices()->CopyFrom(synced_unlock_key); 709 get_my_devices_response.add_devices()->CopyFrom(synced_unlock_key);
536 success_callback_.Run(get_my_devices_response); 710 success_callback_.Run(get_my_devices_response);
537 711
538 // Check that unlock keys are still the same after sync. 712 // Check that unlock keys are still the same after sync.
539 ExpectUnlockKeysAndPrefAreEqual( 713 ExpectUnlockKeysAndPrefAreEqual(
540 original_unlock_keys, device_manager_->unlock_keys(), pref_service_); 714 original_unlock_keys, device_manager_->unlock_keys(), pref_service_);
541 } 715 }
542 716
543 TEST_F(CryptAuthDeviceManagerTest, SyncEmptyDeviceList) { 717 TEST_F(CryptAuthDeviceManagerTest, SyncEmptyDeviceList) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 CryptAuthDeviceManager::SyncResult::SUCCESS, 773 CryptAuthDeviceManager::SyncResult::SUCCESS,
600 CryptAuthDeviceManager::DeviceChangeResult::CHANGED)); 774 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
601 success_callback_.Run(get_my_devices_response_); 775 success_callback_.Run(get_my_devices_response_);
602 776
603 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>( 777 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(
604 1, get_my_devices_response_.devices(0)), 778 1, get_my_devices_response_.devices(0)),
605 device_manager_->unlock_keys(), 779 device_manager_->unlock_keys(),
606 pref_service_); 780 pref_service_);
607 } 781 }
608 782
783 TEST_F(CryptAuthDeviceManagerTest, SyncDeviceWithNoContents) {
784 device_manager_->Start();
785
786 EXPECT_CALL(*sync_scheduler(), ForceSync());
787 gcm_manager_.PushResyncMessage();
788
789 FireSchedulerForSync(cryptauth::INVOCATION_REASON_SERVER_INITIATED);
790
791 EXPECT_CALL(*this, OnSyncFinishedProxy(
792 CryptAuthDeviceManager::SyncResult::SUCCESS,
793 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
794 success_callback_.Run(get_my_devices_response_);
795
796 ExpectUnlockKeysAndPrefAreEqual(std::vector<cryptauth::ExternalDeviceInfo>(
797 1, get_my_devices_response_.devices(0)),
798 device_manager_->unlock_keys(),
799 pref_service_);
800 }
801
802 TEST_F(CryptAuthDeviceManagerTest, SyncFullyDetailedExternalDeviceInfos) {
803 cryptauth::GetMyDevicesResponse response;
804
805 // First, use a device with only a public key (a public key is the only
806 // required field). This ensures devices work properly when they do not have
807 // all fields filled out.
808 cryptauth::ExternalDeviceInfo device_with_only_public_key;
809 device_with_only_public_key.set_public_key("publicKey1");
810 // Currently, CryptAuthDeviceManager only stores devices which are unlock
811 // keys, so set_unlock_key(true) must be called here for storage to work.
812 // TODO(khorimoto): Remove this when support for storing all types of devices
813 // is added.
814 device_with_only_public_key.set_unlock_key(true);
815 response.add_devices()->CopyFrom(device_with_only_public_key);
816
817 // Second, use a device with all fields filled out. This ensures that all
818 // device details are properly saved.
819 cryptauth::ExternalDeviceInfo device_with_all_fields;
820 device_with_all_fields.set_public_key("publicKey2");
821 device_with_all_fields.set_friendly_device_name("deviceName");
822 device_with_all_fields.set_bluetooth_address("aa:bb:cc:dd:ee:ff");
823 device_with_all_fields.set_unlock_key(true);
824 device_with_all_fields.set_unlockable(true);
825 device_with_all_fields.set_last_update_time_millis(123456789L);
826 device_with_all_fields.set_mobile_hotspot_supported(true);
827 device_with_all_fields.set_device_type(DeviceType::ANDROIDOS);
828 cryptauth::BeaconSeed seed1;
829 seed1.set_data(kBeaconSeed1Data);
830 seed1.set_start_time_millis(kBeaconSeed1StartTime);
831 seed1.set_end_time_millis(kBeaconSeed1EndTime);
832 device_with_all_fields.add_beacon_seeds()->CopyFrom(seed1);
833 cryptauth::BeaconSeed seed2;
834 seed2.set_data(kBeaconSeed2Data);
835 seed2.set_start_time_millis(kBeaconSeed2StartTime);
836 seed2.set_end_time_millis(kBeaconSeed2EndTime);
837 device_with_all_fields.add_beacon_seeds()->CopyFrom(seed2);
838 response.add_devices()->CopyFrom(device_with_all_fields);
839
840 std::vector<cryptauth::ExternalDeviceInfo> expected_unlock_keys;
841 expected_unlock_keys.push_back(device_with_only_public_key);
842 expected_unlock_keys.push_back(device_with_all_fields);
843
844 device_manager_->Start();
845 FireSchedulerForSync(cryptauth::INVOCATION_REASON_PERIODIC);
846 ASSERT_FALSE(success_callback_.is_null());
847 EXPECT_CALL(*this, OnSyncFinishedProxy(
848 CryptAuthDeviceManager::SyncResult::SUCCESS,
849 CryptAuthDeviceManager::DeviceChangeResult::CHANGED));
850 success_callback_.Run(response);
851
852 ExpectUnlockKeysAndPrefAreEqual(
853 expected_unlock_keys, device_manager_->unlock_keys(), pref_service_);
854 }
855
609 } // namespace cryptauth 856 } // namespace cryptauth
OLDNEW
« no previous file with comments | « components/cryptauth/cryptauth_device_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698