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