OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/proximity_auth/remote_device_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace proximity_auth { |
| 14 namespace { |
| 15 |
| 16 // Prefixes for RemoteDevice fields. |
| 17 const char kDeviceNamePrefix[] = "device"; |
| 18 const char kPublicKeyPrefix[] = "pk"; |
| 19 const char kBluetoothAddressPrefix[] = "11:22:33:44:55:0"; |
| 20 |
| 21 // The id of the user who the remote devices belong to. |
| 22 const char kUserId[] = "example@gmail.com"; |
| 23 |
| 24 // The public key of the user's local device. |
| 25 const char kUserPublicKey[] = "User public key"; |
| 26 |
| 27 // Creates and returns an ExternalDeviceInfo proto with the fields appended with |
| 28 // |suffix|. |
| 29 cryptauth::ExternalDeviceInfo CreateUnlockKey(const std::string& suffix) { |
| 30 cryptauth::ExternalDeviceInfo unlock_key; |
| 31 unlock_key.set_friendly_device_name(std::string(kDeviceNamePrefix) + suffix); |
| 32 unlock_key.set_public_key(std::string(kPublicKeyPrefix) + suffix); |
| 33 unlock_key.set_bluetooth_address(std::string(kBluetoothAddressPrefix) + |
| 34 suffix); |
| 35 return unlock_key; |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 class ProximityAuthRemoteDeviceLoaderTest : public testing::Test { |
| 41 public: |
| 42 ProximityAuthRemoteDeviceLoaderTest() |
| 43 : secure_message_delegate_(new FakeSecureMessageDelegate()), |
| 44 user_private_key_(secure_message_delegate_->GetPrivateKeyForPublicKey( |
| 45 kUserPublicKey)) {} |
| 46 |
| 47 ~ProximityAuthRemoteDeviceLoaderTest() {} |
| 48 |
| 49 void OnRemoteDevicesLoaded(const std::vector<RemoteDevice>& remote_devices) { |
| 50 remote_devices_ = remote_devices; |
| 51 LoadCompleted(); |
| 52 } |
| 53 |
| 54 MOCK_METHOD0(LoadCompleted, void()); |
| 55 |
| 56 protected: |
| 57 // Handles deriving the PSK. Ownership will be passed to the |
| 58 // RemoteDeviceLoader under test. |
| 59 scoped_ptr<FakeSecureMessageDelegate> secure_message_delegate_; |
| 60 |
| 61 // The private key of the user local device. |
| 62 std::string user_private_key_; |
| 63 |
| 64 // Stores the result of the RemoteDeviceLoader. |
| 65 std::vector<RemoteDevice> remote_devices_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(ProximityAuthRemoteDeviceLoaderTest); |
| 68 }; |
| 69 |
| 70 TEST_F(ProximityAuthRemoteDeviceLoaderTest, LoadZeroDevices) { |
| 71 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys; |
| 72 RemoteDeviceLoader loader(unlock_keys, user_private_key_, kUserId, |
| 73 secure_message_delegate_.Pass()); |
| 74 |
| 75 std::vector<RemoteDevice> result; |
| 76 EXPECT_CALL(*this, LoadCompleted()); |
| 77 loader.Load( |
| 78 base::Bind(&ProximityAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 79 base::Unretained(this))); |
| 80 |
| 81 EXPECT_EQ(0u, remote_devices_.size()); |
| 82 } |
| 83 |
| 84 TEST_F(ProximityAuthRemoteDeviceLoaderTest, LoadOneClassicRemoteDevice) { |
| 85 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys(1, |
| 86 CreateUnlockKey("1")); |
| 87 RemoteDeviceLoader loader(unlock_keys, user_private_key_, kUserId, |
| 88 secure_message_delegate_.Pass()); |
| 89 |
| 90 std::vector<RemoteDevice> result; |
| 91 EXPECT_CALL(*this, LoadCompleted()); |
| 92 loader.Load( |
| 93 base::Bind(&ProximityAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 94 base::Unretained(this))); |
| 95 |
| 96 EXPECT_EQ(1u, remote_devices_.size()); |
| 97 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
| 98 EXPECT_EQ(unlock_keys[0].friendly_device_name(), remote_devices_[0].name); |
| 99 EXPECT_EQ(unlock_keys[0].public_key(), remote_devices_[0].public_key); |
| 100 EXPECT_EQ(unlock_keys[0].bluetooth_address(), |
| 101 remote_devices_[0].bluetooth_address); |
| 102 EXPECT_EQ(RemoteDevice::BLUETOOTH_CLASSIC, remote_devices_[0].bluetooth_type); |
| 103 } |
| 104 |
| 105 TEST_F(ProximityAuthRemoteDeviceLoaderTest, LoadOneBLERemoteDevice) { |
| 106 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys(1, |
| 107 CreateUnlockKey("1")); |
| 108 unlock_keys[0].set_bluetooth_address(std::string()); |
| 109 RemoteDeviceLoader loader(unlock_keys, user_private_key_, kUserId, |
| 110 secure_message_delegate_.Pass()); |
| 111 |
| 112 std::vector<RemoteDevice> result; |
| 113 EXPECT_CALL(*this, LoadCompleted()); |
| 114 loader.Load( |
| 115 base::Bind(&ProximityAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 116 base::Unretained(this))); |
| 117 |
| 118 EXPECT_EQ(1u, remote_devices_.size()); |
| 119 EXPECT_FALSE(remote_devices_[0].persistent_symmetric_key.empty()); |
| 120 EXPECT_EQ(unlock_keys[0].friendly_device_name(), remote_devices_[0].name); |
| 121 EXPECT_EQ(unlock_keys[0].public_key(), remote_devices_[0].public_key); |
| 122 EXPECT_EQ(unlock_keys[0].bluetooth_address(), |
| 123 remote_devices_[0].bluetooth_address); |
| 124 EXPECT_EQ(RemoteDevice::BLUETOOTH_LE, remote_devices_[0].bluetooth_type); |
| 125 } |
| 126 |
| 127 TEST_F(ProximityAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevice) { |
| 128 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys; |
| 129 unlock_keys.push_back(CreateUnlockKey("1")); |
| 130 unlock_keys.push_back(CreateUnlockKey("2")); |
| 131 unlock_keys.push_back(CreateUnlockKey("3")); |
| 132 RemoteDeviceLoader loader(unlock_keys, user_private_key_, kUserId, |
| 133 secure_message_delegate_.Pass()); |
| 134 |
| 135 std::vector<RemoteDevice> result; |
| 136 EXPECT_CALL(*this, LoadCompleted()); |
| 137 loader.Load( |
| 138 base::Bind(&ProximityAuthRemoteDeviceLoaderTest::OnRemoteDevicesLoaded, |
| 139 base::Unretained(this))); |
| 140 |
| 141 EXPECT_EQ(3u, remote_devices_.size()); |
| 142 for (size_t i = 0; i < 3; ++i) { |
| 143 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); |
| 144 EXPECT_EQ(unlock_keys[i].friendly_device_name(), remote_devices_[i].name); |
| 145 EXPECT_EQ(unlock_keys[i].public_key(), remote_devices_[i].public_key); |
| 146 EXPECT_EQ(unlock_keys[i].bluetooth_address(), |
| 147 remote_devices_[i].bluetooth_address); |
| 148 } |
| 149 } |
| 150 |
| 151 } // namespace proximity_auth |
OLD | NEW |