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/cryptauth/remote_device_loader.h" | 5 #include "components/cryptauth/remote_device_loader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | |
14 #include "components/cryptauth/fake_secure_message_delegate.h" | 15 #include "components/cryptauth/fake_secure_message_delegate.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace cryptauth { | 19 namespace cryptauth { |
19 namespace { | 20 namespace { |
20 | 21 |
21 // Prefixes for RemoteDevice fields. | 22 // Prefixes for RemoteDevice fields. |
22 const char kDeviceNamePrefix[] = "device"; | 23 const char kDeviceNamePrefix[] = "device"; |
23 const char kPublicKeyPrefix[] = "pk"; | 24 const char kPublicKeyPrefix[] = "pk"; |
24 const char kBluetoothAddressPrefix[] = "11:22:33:44:55:0"; | 25 const char kBluetoothAddressPrefix[] = "11:22:33:44:55:0"; |
25 | 26 |
26 // The id of the user who the remote devices belong to. | 27 // The id of the user who the remote devices belong to. |
27 const char kUserId[] = "example@gmail.com"; | 28 const char kUserId[] = "example@gmail.com"; |
28 | 29 |
29 // The public key of the user's local device. | 30 // The public key of the user's local device. |
30 const char kUserPublicKey[] = "User public key"; | 31 const char kUserPublicKey[] = "User public key"; |
31 | 32 |
33 // BeaconSeed values. | |
34 const int64_t kBeaconSeedStartTimeMs = 1000; | |
35 const int64_t kBeaconSeedEndTimeMs = 2000; | |
36 const char kBeaconSeedData[] = "Beacon Seed Data"; | |
37 | |
32 // Creates and returns an ExternalDeviceInfo proto with the fields appended with | 38 // Creates and returns an ExternalDeviceInfo proto with the fields appended with |
33 // |suffix|. | 39 // |suffix|. |
34 cryptauth::ExternalDeviceInfo CreateDeviceInfo(const std::string& suffix) { | 40 cryptauth::ExternalDeviceInfo CreateDeviceInfo(const std::string& suffix) { |
35 cryptauth::ExternalDeviceInfo device_info; | 41 cryptauth::ExternalDeviceInfo device_info; |
36 device_info.set_friendly_device_name(std::string(kDeviceNamePrefix) + suffix); | 42 device_info.set_friendly_device_name(std::string(kDeviceNamePrefix) + suffix); |
37 device_info.set_public_key(std::string(kPublicKeyPrefix) + suffix); | 43 device_info.set_public_key(std::string(kPublicKeyPrefix) + suffix); |
38 device_info.set_bluetooth_address(std::string(kBluetoothAddressPrefix) + | 44 device_info.set_bluetooth_address(std::string(kBluetoothAddressPrefix) + |
39 suffix); | 45 suffix); |
46 device_info.add_beacon_seeds(); | |
47 BeaconSeed* beacon_seed = device_info.mutable_beacon_seeds(0); | |
48 beacon_seed->set_start_time_millis(kBeaconSeedStartTimeMs); | |
49 beacon_seed->set_end_time_millis(kBeaconSeedEndTimeMs); | |
50 beacon_seed->set_data(kBeaconSeedData); | |
40 return device_info; | 51 return device_info; |
41 } | 52 } |
42 | 53 |
43 } // namespace | 54 } // namespace |
44 | 55 |
45 class CryptAuthRemoteDeviceLoaderTest : public testing::Test { | 56 class CryptAuthRemoteDeviceLoaderTest : public testing::Test { |
46 public: | 57 public: |
47 CryptAuthRemoteDeviceLoaderTest() | 58 CryptAuthRemoteDeviceLoaderTest() |
48 : secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate()), | 59 : secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate()), |
49 user_private_key_(secure_message_delegate_->GetPrivateKeyForPublicKey( | 60 user_private_key_(secure_message_delegate_->GetPrivateKeyForPublicKey( |
(...skipping 25 matching lines...) Expand all Loading... | |
75 }; | 86 }; |
76 | 87 |
77 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadZeroDevices) { | 88 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadZeroDevices) { |
78 std::vector<cryptauth::ExternalDeviceInfo> device_infos; | 89 std::vector<cryptauth::ExternalDeviceInfo> device_infos; |
79 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 90 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
80 std::move(secure_message_delegate_)); | 91 std::move(secure_message_delegate_)); |
81 | 92 |
82 std::vector<cryptauth::RemoteDevice> result; | 93 std::vector<cryptauth::RemoteDevice> result; |
83 EXPECT_CALL(*this, LoadCompleted()); | 94 EXPECT_CALL(*this, LoadCompleted()); |
84 loader.Load( | 95 loader.Load( |
85 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 96 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
86 base::Unretained(this))); | 97 base::Unretained(this))); |
87 | 98 |
88 EXPECT_EQ(0u, remote_devices_.size()); | 99 EXPECT_EQ(0u, remote_devices_.size()); |
89 } | 100 } |
90 | 101 |
102 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithBeaconSeeds) { | |
103 std::vector<cryptauth::ExternalDeviceInfo> device_infos( | |
104 1, CreateDeviceInfo("0")); | |
105 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | |
106 std::move(secure_message_delegate_)); | |
107 | |
108 std::vector<cryptauth::RemoteDevice> result; | |
109 EXPECT_CALL(*this, LoadCompleted()); | |
110 loader.Load( | |
111 true, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | |
112 base::Unretained(this))); | |
113 | |
114 EXPECT_EQ(1u, remote_devices_.size()); | |
115 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | |
116 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | |
117 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); | |
118 EXPECT_TRUE(remote_devices_[0].are_beacon_seeds_loaded); | |
119 ASSERT_EQ(1u, remote_devices_[0].beacon_seeds.size()); | |
120 | |
121 const BeaconSeed& beacon_seed = remote_devices_[0].beacon_seeds[0]; | |
122 EXPECT_EQ(kBeaconSeedData, beacon_seed.data()); | |
123 EXPECT_EQ(kBeaconSeedStartTimeMs, beacon_seed.start_time_millis()); | |
124 EXPECT_EQ(kBeaconSeedEndTimeMs, beacon_seed.end_time_millis()); | |
125 } | |
126 | |
127 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadDevicesWithAndWithoutBeaconSeed) { | |
Ryan Hansberry
2017/05/19 18:28:43
super nit: LoadDevicesWithAndWithoutBeaconSeed*s*
Tim Song
2017/05/19 21:19:34
Done.
| |
128 std::vector<cryptauth::ExternalDeviceInfo> device_infos( | |
129 1, CreateDeviceInfo("0")); | |
130 | |
131 { | |
Ryan Hansberry
2017/05/19 18:28:43
I'd prefer not to use sub-blocks. Can these be bro
Tim Song
2017/05/19 21:19:34
Done.
| |
132 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | |
133 base::MakeUnique<FakeSecureMessageDelegate>()); | |
134 EXPECT_CALL(*this, LoadCompleted()); | |
135 loader.Load( | |
136 false /* should_load_beacon_seeds */, | |
137 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | |
138 base::Unretained(this))); | |
139 } | |
140 RemoteDevice remote_device_without_beacon_seed = remote_devices_[0]; | |
141 | |
142 { | |
143 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | |
144 base::MakeUnique<FakeSecureMessageDelegate>()); | |
145 EXPECT_CALL(*this, LoadCompleted()); | |
146 loader.Load( | |
147 true /* should_load_beacon_seeds */, | |
148 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | |
149 base::Unretained(this))); | |
150 } | |
151 RemoteDevice remote_device_with_beacon_seed = remote_devices_[0]; | |
152 | |
153 EXPECT_EQ(remote_device_without_beacon_seed, | |
154 remote_device_without_beacon_seed); | |
155 EXPECT_EQ(remote_device_with_beacon_seed, remote_device_with_beacon_seed); | |
156 EXPECT_FALSE(remote_device_with_beacon_seed == | |
157 remote_device_without_beacon_seed); | |
158 } | |
159 | |
91 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { | 160 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { |
92 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 161 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
93 CreateDeviceInfo("0")); | 162 CreateDeviceInfo("0")); |
94 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 163 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
95 std::move(secure_message_delegate_)); | 164 std::move(secure_message_delegate_)); |
96 | 165 |
97 std::vector<cryptauth::RemoteDevice> result; | 166 std::vector<cryptauth::RemoteDevice> result; |
98 EXPECT_CALL(*this, LoadCompleted()); | 167 EXPECT_CALL(*this, LoadCompleted()); |
99 loader.Load( | 168 loader.Load( |
100 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 169 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
101 base::Unretained(this))); | 170 base::Unretained(this))); |
102 | 171 |
103 EXPECT_EQ(1u, remote_devices_.size()); | 172 EXPECT_EQ(1u, remote_devices_.size()); |
104 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | 173 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
105 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | 174 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); |
106 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); | 175 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); |
107 EXPECT_EQ(device_infos[0].bluetooth_address(), | 176 EXPECT_EQ(device_infos[0].bluetooth_address(), |
108 remote_devices_[0].bluetooth_address); | 177 remote_devices_[0].bluetooth_address); |
178 EXPECT_EQ(0u, remote_devices_[0].beacon_seeds.size()); | |
109 } | 179 } |
110 | 180 |
111 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { | 181 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { |
112 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 182 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
113 CreateDeviceInfo("0")); | 183 CreateDeviceInfo("0")); |
114 device_infos[0].set_bluetooth_address(std::string()); | 184 device_infos[0].set_bluetooth_address(std::string()); |
115 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 185 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
116 std::move(secure_message_delegate_)); | 186 std::move(secure_message_delegate_)); |
117 | 187 |
118 std::vector<cryptauth::RemoteDevice> result; | 188 std::vector<cryptauth::RemoteDevice> result; |
119 EXPECT_CALL(*this, LoadCompleted()); | 189 EXPECT_CALL(*this, LoadCompleted()); |
120 loader.Load( | 190 loader.Load( |
121 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 191 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
122 base::Unretained(this))); | 192 base::Unretained(this))); |
123 | 193 |
124 EXPECT_EQ(1u, remote_devices_.size()); | 194 EXPECT_EQ(1u, remote_devices_.size()); |
125 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | 195 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
126 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | 196 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); |
127 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); | 197 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); |
128 EXPECT_EQ("", remote_devices_[0].bluetooth_address); | 198 EXPECT_EQ("", remote_devices_[0].bluetooth_address); |
129 } | 199 } |
130 | 200 |
131 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevices) { | 201 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevices) { |
132 std::vector<cryptauth::ExternalDeviceInfo> device_infos; | 202 std::vector<cryptauth::ExternalDeviceInfo> device_infos; |
133 device_infos.push_back(CreateDeviceInfo("0")); | 203 device_infos.push_back(CreateDeviceInfo("0")); |
134 device_infos.push_back(CreateDeviceInfo("1")); | 204 device_infos.push_back(CreateDeviceInfo("1")); |
135 device_infos.push_back(CreateDeviceInfo("2")); | 205 device_infos.push_back(CreateDeviceInfo("2")); |
136 | 206 |
137 // Devices 0 and 1 do not have a Bluetooth address, but device 2 does. | 207 // Devices 0 and 1 do not have a Bluetooth address, but device 2 does. |
138 device_infos[0].set_bluetooth_address(std::string()); | 208 device_infos[0].set_bluetooth_address(std::string()); |
139 device_infos[1].set_bluetooth_address(std::string()); | 209 device_infos[1].set_bluetooth_address(std::string()); |
140 | 210 |
141 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 211 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
142 std::move(secure_message_delegate_)); | 212 std::move(secure_message_delegate_)); |
143 | 213 |
144 EXPECT_CALL(*this, LoadCompleted()); | 214 EXPECT_CALL(*this, LoadCompleted()); |
145 loader.Load( | 215 loader.Load( |
146 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 216 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
147 base::Unretained(this))); | 217 base::Unretained(this))); |
148 | 218 |
149 EXPECT_EQ(3u, remote_devices_.size()); | 219 EXPECT_EQ(3u, remote_devices_.size()); |
150 for (size_t i = 0; i < 3; ++i) { | 220 for (size_t i = 0; i < 3; ++i) { |
151 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); | 221 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); |
152 EXPECT_EQ(device_infos[i].friendly_device_name(), remote_devices_[i].name); | 222 EXPECT_EQ(device_infos[i].friendly_device_name(), remote_devices_[i].name); |
153 EXPECT_EQ(device_infos[i].public_key(), remote_devices_[i].public_key); | 223 EXPECT_EQ(device_infos[i].public_key(), remote_devices_[i].public_key); |
154 EXPECT_EQ(device_infos[i].bluetooth_address(), | 224 EXPECT_EQ(device_infos[i].bluetooth_address(), |
155 remote_devices_[i].bluetooth_address); | 225 remote_devices_[i].bluetooth_address); |
156 } | 226 } |
157 } | 227 } |
158 | 228 |
159 } // namespace cryptauth | 229 } // namespace cryptauth |
OLD | NEW |