Chromium Code Reviews| 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, LoadOneRemoteDevice) { | |
| 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()); | |
|
sacomoto
2015/09/24 12:41:59
nit: Can't you compare with an expected value?
Tim Song
2015/09/26 02:08:14
The FakeSecureMessageDelegate would essentially ge
| |
| 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 } | |
| 103 | |
| 104 TEST_F(ProximityAuthRemoteDeviceLoaderTest, LoadThreeRemoteDevice) { | |
| 105 std::vector<cryptauth::ExternalDeviceInfo> unlock_keys; | |
| 106 unlock_keys.push_back(CreateUnlockKey("1")); | |
| 107 unlock_keys.push_back(CreateUnlockKey("2")); | |
| 108 unlock_keys.push_back(CreateUnlockKey("3")); | |
| 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(3u, remote_devices_.size()); | |
| 119 for (size_t i = 0; i < 3; ++i) { | |
| 120 EXPECT_FALSE(remote_devices_[i].persistent_symmetric_key.empty()); | |
|
sacomoto
2015/09/24 12:41:59
nit: ditto
Tim Song
2015/09/26 02:08:14
Acknowledged.
| |
| 121 EXPECT_EQ(unlock_keys[i].friendly_device_name(), remote_devices_[i].name); | |
| 122 EXPECT_EQ(unlock_keys[i].public_key(), remote_devices_[i].public_key); | |
| 123 EXPECT_EQ(unlock_keys[i].bluetooth_address(), | |
| 124 remote_devices_[i].bluetooth_address); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 } // namespace proximity_auth | |
| OLD | NEW |