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, 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 |
91 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { | 156 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { |
92 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 157 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
93 CreateDeviceInfo("0")); | 158 CreateDeviceInfo("0")); |
94 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 159 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
95 std::move(secure_message_delegate_)); | 160 std::move(secure_message_delegate_)); |
96 | 161 |
97 std::vector<cryptauth::RemoteDevice> result; | 162 std::vector<cryptauth::RemoteDevice> result; |
98 EXPECT_CALL(*this, LoadCompleted()); | 163 EXPECT_CALL(*this, LoadCompleted()); |
99 loader.Load( | 164 loader.Load( |
100 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 165 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
101 base::Unretained(this))); | 166 base::Unretained(this))); |
102 | 167 |
103 EXPECT_EQ(1u, remote_devices_.size()); | 168 EXPECT_EQ(1u, remote_devices_.size()); |
104 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | 169 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
105 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | 170 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); | 171 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); |
107 EXPECT_EQ(device_infos[0].bluetooth_address(), | 172 EXPECT_EQ(device_infos[0].bluetooth_address(), |
108 remote_devices_[0].bluetooth_address); | 173 remote_devices_[0].bluetooth_address); |
| 174 EXPECT_EQ(0u, remote_devices_[0].beacon_seeds.size()); |
109 } | 175 } |
110 | 176 |
111 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { | 177 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { |
112 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 178 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
113 CreateDeviceInfo("0")); | 179 CreateDeviceInfo("0")); |
114 device_infos[0].set_bluetooth_address(std::string()); | 180 device_infos[0].set_bluetooth_address(std::string()); |
115 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 181 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
116 std::move(secure_message_delegate_)); | 182 std::move(secure_message_delegate_)); |
117 | 183 |
118 std::vector<cryptauth::RemoteDevice> result; | 184 std::vector<cryptauth::RemoteDevice> result; |
119 EXPECT_CALL(*this, LoadCompleted()); | 185 EXPECT_CALL(*this, LoadCompleted()); |
120 loader.Load( | 186 loader.Load( |
121 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 187 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
122 base::Unretained(this))); | 188 base::Unretained(this))); |
123 | 189 |
124 EXPECT_EQ(1u, remote_devices_.size()); | 190 EXPECT_EQ(1u, remote_devices_.size()); |
125 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | 191 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
126 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | 192 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); | 193 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); |
128 EXPECT_EQ("", remote_devices_[0].bluetooth_address); | 194 EXPECT_EQ("", remote_devices_[0].bluetooth_address); |
129 } | 195 } |
130 | 196 |
131 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevices) { | 197 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevices) { |
132 std::vector<cryptauth::ExternalDeviceInfo> device_infos; | 198 std::vector<cryptauth::ExternalDeviceInfo> device_infos; |
133 device_infos.push_back(CreateDeviceInfo("0")); | 199 device_infos.push_back(CreateDeviceInfo("0")); |
134 device_infos.push_back(CreateDeviceInfo("1")); | 200 device_infos.push_back(CreateDeviceInfo("1")); |
135 device_infos.push_back(CreateDeviceInfo("2")); | 201 device_infos.push_back(CreateDeviceInfo("2")); |
136 | 202 |
137 // Devices 0 and 1 do not have a Bluetooth address, but device 2 does. | 203 // Devices 0 and 1 do not have a Bluetooth address, but device 2 does. |
138 device_infos[0].set_bluetooth_address(std::string()); | 204 device_infos[0].set_bluetooth_address(std::string()); |
139 device_infos[1].set_bluetooth_address(std::string()); | 205 device_infos[1].set_bluetooth_address(std::string()); |
140 | 206 |
141 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 207 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
142 std::move(secure_message_delegate_)); | 208 std::move(secure_message_delegate_)); |
143 | 209 |
144 EXPECT_CALL(*this, LoadCompleted()); | 210 EXPECT_CALL(*this, LoadCompleted()); |
145 loader.Load( | 211 loader.Load( |
146 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 212 false, base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
147 base::Unretained(this))); | 213 base::Unretained(this))); |
148 | 214 |
149 EXPECT_EQ(3u, remote_devices_.size()); | 215 EXPECT_EQ(3u, remote_devices_.size()); |
150 for (size_t i = 0; i < 3; ++i) { | 216 for (size_t i = 0; i < 3; ++i) { |
151 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); | 217 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); |
152 EXPECT_EQ(device_infos[i].friendly_device_name(), remote_devices_[i].name); | 218 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); | 219 EXPECT_EQ(device_infos[i].public_key(), remote_devices_[i].public_key); |
154 EXPECT_EQ(device_infos[i].bluetooth_address(), | 220 EXPECT_EQ(device_infos[i].bluetooth_address(), |
155 remote_devices_[i].bluetooth_address); | 221 remote_devices_[i].bluetooth_address); |
156 } | 222 } |
157 } | 223 } |
158 | 224 |
159 } // namespace cryptauth | 225 } // namespace cryptauth |
OLD | NEW |