| 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> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const char kDeviceNamePrefix[] = "device"; | 22 const char kDeviceNamePrefix[] = "device"; |
| 23 const char kPublicKeyPrefix[] = "pk"; | 23 const char kPublicKeyPrefix[] = "pk"; |
| 24 const char kBluetoothAddressPrefix[] = "11:22:33:44:55:0"; | 24 const char kBluetoothAddressPrefix[] = "11:22:33:44:55:0"; |
| 25 | 25 |
| 26 // The id of the user who the remote devices belong to. | 26 // The id of the user who the remote devices belong to. |
| 27 const char kUserId[] = "example@gmail.com"; | 27 const char kUserId[] = "example@gmail.com"; |
| 28 | 28 |
| 29 // The public key of the user's local device. | 29 // The public key of the user's local device. |
| 30 const char kUserPublicKey[] = "User public key"; | 30 const char kUserPublicKey[] = "User public key"; |
| 31 | 31 |
| 32 // BeaconSeed values. |
| 33 const int64_t kBeaconSeedStartTimeMs = 1000; |
| 34 const int64_t kBeaconSeedEndTimeMs = 2000; |
| 35 const char kBeaconSeedData[] = "Beacon Seed Data"; |
| 36 |
| 32 // Creates and returns an ExternalDeviceInfo proto with the fields appended with | 37 // Creates and returns an ExternalDeviceInfo proto with the fields appended with |
| 33 // |suffix|. | 38 // |suffix|. |
| 34 cryptauth::ExternalDeviceInfo CreateDeviceInfo(const std::string& suffix) { | 39 cryptauth::ExternalDeviceInfo CreateDeviceInfo(const std::string& suffix) { |
| 35 cryptauth::ExternalDeviceInfo device_info; | 40 cryptauth::ExternalDeviceInfo device_info; |
| 36 device_info.set_friendly_device_name(std::string(kDeviceNamePrefix) + suffix); | 41 device_info.set_friendly_device_name(std::string(kDeviceNamePrefix) + suffix); |
| 37 device_info.set_public_key(std::string(kPublicKeyPrefix) + suffix); | 42 device_info.set_public_key(std::string(kPublicKeyPrefix) + suffix); |
| 38 device_info.set_bluetooth_address(std::string(kBluetoothAddressPrefix) + | 43 device_info.set_bluetooth_address(std::string(kBluetoothAddressPrefix) + |
| 39 suffix); | 44 suffix); |
| 45 device_info.add_beacon_seeds(); |
| 46 BeaconSeed* beacon_seed = device_info.mutable_beacon_seeds(0); |
| 47 beacon_seed->set_start_time_millis(kBeaconSeedStartTimeMs); |
| 48 beacon_seed->set_end_time_millis(kBeaconSeedEndTimeMs); |
| 49 beacon_seed->set_data(kBeaconSeedData); |
| 40 return device_info; | 50 return device_info; |
| 41 } | 51 } |
| 42 | 52 |
| 43 } // namespace | 53 } // namespace |
| 44 | 54 |
| 45 class CryptAuthRemoteDeviceLoaderTest : public testing::Test { | 55 class CryptAuthRemoteDeviceLoaderTest : public testing::Test { |
| 46 public: | 56 public: |
| 47 CryptAuthRemoteDeviceLoaderTest() | 57 CryptAuthRemoteDeviceLoaderTest() |
| 48 : secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate()), | 58 : secure_message_delegate_(new cryptauth::FakeSecureMessageDelegate()), |
| 49 user_private_key_(secure_message_delegate_->GetPrivateKeyForPublicKey( | 59 user_private_key_(secure_message_delegate_->GetPrivateKeyForPublicKey( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 91 |
| 82 std::vector<cryptauth::RemoteDevice> result; | 92 std::vector<cryptauth::RemoteDevice> result; |
| 83 EXPECT_CALL(*this, LoadCompleted()); | 93 EXPECT_CALL(*this, LoadCompleted()); |
| 84 loader.Load( | 94 loader.Load( |
| 85 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 95 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 86 base::Unretained(this))); | 96 base::Unretained(this))); |
| 87 | 97 |
| 88 EXPECT_EQ(0u, remote_devices_.size()); | 98 EXPECT_EQ(0u, remote_devices_.size()); |
| 89 } | 99 } |
| 90 | 100 |
| 101 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithBeaconSeeds) { |
| 102 std::vector<cryptauth::ExternalDeviceInfo> device_infos( |
| 103 1, CreateDeviceInfo("0")); |
| 104 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
| 105 std::move(secure_message_delegate_)); |
| 106 loader.set_should_load_beacon_seeds(true); |
| 107 |
| 108 std::vector<cryptauth::RemoteDevice> result; |
| 109 EXPECT_CALL(*this, LoadCompleted()); |
| 110 loader.Load( |
| 111 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 ASSERT_EQ(1u, remote_devices_[0].beacon_seeds.size()); |
| 119 |
| 120 const BeaconSeed& beacon_seed = remote_devices_[0].beacon_seeds[0]; |
| 121 EXPECT_EQ(kBeaconSeedData, beacon_seed.data()); |
| 122 EXPECT_EQ(kBeaconSeedStartTimeMs, beacon_seed.start_time_millis()); |
| 123 EXPECT_EQ(kBeaconSeedEndTimeMs, beacon_seed.end_time_millis()); |
| 124 } |
| 125 |
| 91 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { | 126 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithAddress) { |
| 92 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 127 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
| 93 CreateDeviceInfo("0")); | 128 CreateDeviceInfo("0")); |
| 94 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 129 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
| 95 std::move(secure_message_delegate_)); | 130 std::move(secure_message_delegate_)); |
| 96 | 131 |
| 97 std::vector<cryptauth::RemoteDevice> result; | 132 std::vector<cryptauth::RemoteDevice> result; |
| 98 EXPECT_CALL(*this, LoadCompleted()); | 133 EXPECT_CALL(*this, LoadCompleted()); |
| 99 loader.Load( | 134 loader.Load( |
| 100 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, | 135 base::Bind(&CryptAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 101 base::Unretained(this))); | 136 base::Unretained(this))); |
| 102 | 137 |
| 103 EXPECT_EQ(1u, remote_devices_.size()); | 138 EXPECT_EQ(1u, remote_devices_.size()); |
| 104 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); | 139 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
| 105 EXPECT_EQ(device_infos[0].friendly_device_name(), remote_devices_[0].name); | 140 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); | 141 EXPECT_EQ(device_infos[0].public_key(), remote_devices_[0].public_key); |
| 107 EXPECT_EQ(device_infos[0].bluetooth_address(), | 142 EXPECT_EQ(device_infos[0].bluetooth_address(), |
| 108 remote_devices_[0].bluetooth_address); | 143 remote_devices_[0].bluetooth_address); |
| 144 EXPECT_EQ(0u, remote_devices_[0].beacon_seeds.size()); |
| 109 } | 145 } |
| 110 | 146 |
| 111 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { | 147 TEST_F(CryptAuthRemoteDeviceLoaderTest, LoadOneDeviceWithoutAddress) { |
| 112 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, | 148 std::vector<cryptauth::ExternalDeviceInfo> device_infos(1, |
| 113 CreateDeviceInfo("0")); | 149 CreateDeviceInfo("0")); |
| 114 device_infos[0].set_bluetooth_address(std::string()); | 150 device_infos[0].set_bluetooth_address(std::string()); |
| 115 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, | 151 RemoteDeviceLoader loader(device_infos, user_private_key_, kUserId, |
| 116 std::move(secure_message_delegate_)); | 152 std::move(secure_message_delegate_)); |
| 117 | 153 |
| 118 std::vector<cryptauth::RemoteDevice> result; | 154 std::vector<cryptauth::RemoteDevice> result; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 for (size_t i = 0; i < 3; ++i) { | 186 for (size_t i = 0; i < 3; ++i) { |
| 151 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); | 187 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); |
| 152 EXPECT_EQ(device_infos[i].friendly_device_name(), remote_devices_[i].name); | 188 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); | 189 EXPECT_EQ(device_infos[i].public_key(), remote_devices_[i].public_key); |
| 154 EXPECT_EQ(device_infos[i].bluetooth_address(), | 190 EXPECT_EQ(device_infos[i].bluetooth_address(), |
| 155 remote_devices_[i].bluetooth_address); | 191 remote_devices_[i].bluetooth_address); |
| 156 } | 192 } |
| 157 } | 193 } |
| 158 | 194 |
| 159 } // namespace cryptauth | 195 } // namespace cryptauth |
| OLD | NEW |