Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: components/proximity_auth/bluetooth_low_energy_connection_finder_unittest.cc

Issue 2841743003: [EasyUnlock] Update BluetoothLowEnergyConnectionFinder to look for EIDs. (Closed)
Patch Set: [EasyUnlock] Update BluetoothLowEnergyConnectionFinder to look for EIDs. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/proximity_auth/ble/bluetooth_low_energy_connection_finder.h " 5 #include "components/proximity_auth/bluetooth_low_energy_connection_finder.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "components/cryptauth/connection.h" 19 #include "components/cryptauth/connection.h"
20 #include "components/cryptauth/cryptauth_test_util.h" 20 #include "components/cryptauth/cryptauth_test_util.h"
21 #include "components/cryptauth/fake_connection.h" 21 #include "components/cryptauth/fake_connection.h"
22 #include "components/cryptauth/remote_device.h" 22 #include "components/cryptauth/remote_device.h"
23 #include "components/cryptauth/wire_message.h" 23 #include "components/cryptauth/wire_message.h"
24 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" 24 #include "components/proximity_auth/logging/logging.h"
25 #include "device/bluetooth/bluetooth_adapter_factory.h" 25 #include "device/bluetooth/bluetooth_adapter_factory.h"
26 #include "device/bluetooth/bluetooth_uuid.h" 26 #include "device/bluetooth/bluetooth_uuid.h"
27 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 27 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
28 #include "device/bluetooth/test/mock_bluetooth_device.h" 28 #include "device/bluetooth/test/mock_bluetooth_device.h"
29 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" 29 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
30 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 30 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
31 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 using testing::_; 34 using testing::_;
35 using testing::AtLeast; 35 using testing::AtLeast;
36 using testing::NiceMock; 36 using testing::NiceMock;
37 using testing::Return; 37 using testing::Return;
38 using testing::StrictMock; 38 using testing::StrictMock;
39 using testing::SaveArg; 39 using testing::SaveArg;
40 40
41 using device::BluetoothDevice; 41 using device::BluetoothDevice;
42 using device::MockBluetoothDevice;
42 43
43 namespace proximity_auth { 44 namespace proximity_auth {
44 namespace { 45 namespace {
45 46
46 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; 47 const char kAdvertisementUUID[] = "0000fe50-0000-1000-8000-00805f9b34fb";
47 const char kOtherUUID[] = "AAAAAAAA-AAAA-AAAA-AAAA-D15EA5EBEEEF"; 48 const int8_t kRssi = -30;
49 const char kEidForPreviousTimeQuantum[] = "\x12\x34";
50 const char kEidForCurrentTimeQuantum[] = "\xab\xcd";
51 const char kEidForNextTimeQuantum[] = "\x56\x78";
52 const char kWrongEid[] = "\xff\xff";
48 53
49 const int kMaxNumberOfAttempts = 2; 54 class MockBluetoothLowEnergyConnectionFinder;
55 class FakeEidGenerator : public cryptauth::BackgroundEidGenerator {
56 public:
57 FakeEidGenerator(MockBluetoothLowEnergyConnectionFinder* connection_finder)
58 : connection_finder_(connection_finder) {}
59 ~FakeEidGenerator() override {}
50 60
51 class MockBluetoothLowEnergyDeviceWhitelist 61 std::vector<std::string> GenerateNearestEids(
52 : public BluetoothLowEnergyDeviceWhitelist { 62 const std::vector<cryptauth::BeaconSeed>& beacon_seed) const override;
63
64 private:
65 MockBluetoothLowEnergyConnectionFinder* connection_finder_;
66
67 DISALLOW_COPY_AND_ASSIGN(FakeEidGenerator);
68 };
69
70 class FakeBeaconSeedFetcher : public cryptauth::RemoteBeaconSeedFetcher {
53 public: 71 public:
54 MockBluetoothLowEnergyDeviceWhitelist() 72 FakeBeaconSeedFetcher() : cryptauth::RemoteBeaconSeedFetcher(nullptr) {}
55 : BluetoothLowEnergyDeviceWhitelist(nullptr) {} 73 ~FakeBeaconSeedFetcher() override {}
56 ~MockBluetoothLowEnergyDeviceWhitelist() override {}
57 74
58 MOCK_CONST_METHOD1(HasDeviceWithAddress, bool(const std::string&)); 75 bool FetchSeedsForDevice(
76 const cryptauth::RemoteDevice& remote_device,
77 std::vector<cryptauth::BeaconSeed>* beacon_seeds_out) const override {
78 EXPECT_EQ(cryptauth::CreateLERemoteDeviceForTest(), remote_device);
79 return true;
80 }
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(FakeBeaconSeedFetcher);
59 }; 84 };
60 85
61 class MockBluetoothLowEnergyConnectionFinder 86 class MockBluetoothLowEnergyConnectionFinder
62 : public BluetoothLowEnergyConnectionFinder { 87 : public BluetoothLowEnergyConnectionFinder {
63 public: 88 public:
64 MockBluetoothLowEnergyConnectionFinder( 89 MockBluetoothLowEnergyConnectionFinder()
65 const BluetoothLowEnergyDeviceWhitelist* device_whitelist,
66 FinderStrategy finder_strategy)
67 : BluetoothLowEnergyConnectionFinder( 90 : BluetoothLowEnergyConnectionFinder(
68 cryptauth::CreateLERemoteDeviceForTest(), 91 cryptauth::CreateLERemoteDeviceForTest(),
69 kServiceUUID, 92 base::MakeUnique<FakeBeaconSeedFetcher>(),
70 finder_strategy, 93 base::MakeUnique<FakeEidGenerator>(this),
71 device_whitelist, 94 nullptr) {}
72 nullptr,
73 kMaxNumberOfAttempts) {}
74 95
75 ~MockBluetoothLowEnergyConnectionFinder() override {} 96 ~MockBluetoothLowEnergyConnectionFinder() override {}
76 97
77 // Mock methods don't support return type std::unique_ptr<>. This is a 98 // Mock methods don't support return type std::unique_ptr<>. This is a
78 // possible workaround: mock a proxy method to be called by the target 99 // possible workaround: mock a proxy method to be called by the target
79 // overridden method (CreateConnection). 100 // overridden method (CreateConnection).
80 MOCK_METHOD0(CreateConnectionProxy, cryptauth::Connection*()); 101 MOCK_METHOD0(CreateConnectionProxy, cryptauth::Connection*());
81 102
82 // Creates a mock connection and sets an expectation that the mock connection 103 // Creates a mock connection and sets an expectation that the mock connection
83 // finder's CreateConnection() method will be called and will return the 104 // finder's CreateConnection() method will be called and will return the
84 // created connection. Returns a reference to the created connection. 105 // created connection. Returns a reference to the created connection.
85 // NOTE: The returned connection's lifetime is managed by the connection 106 // NOTE: The returned connection's lifetime is managed by the connection
86 // finder. 107 // finder.
87 cryptauth::FakeConnection* ExpectCreateConnection() { 108 cryptauth::FakeConnection* ExpectCreateConnection() {
88 std::unique_ptr<cryptauth::FakeConnection> connection( 109 std::unique_ptr<cryptauth::FakeConnection> connection(
89 new cryptauth::FakeConnection( 110 new cryptauth::FakeConnection(
90 cryptauth::CreateLERemoteDeviceForTest())); 111 cryptauth::CreateLERemoteDeviceForTest()));
91 cryptauth::FakeConnection* connection_alias = connection.get(); 112 cryptauth::FakeConnection* connection_alias = connection.get();
92 EXPECT_CALL(*this, CreateConnectionProxy()) 113 EXPECT_CALL(*this, CreateConnectionProxy())
93 .WillOnce(Return(connection.release())); 114 .WillOnce(Return(connection.release()));
94 return connection_alias; 115 return connection_alias;
95 } 116 }
96 117
97 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); 118 void SetNearestEids(const std::vector<std::string>& eids) {
119 nearest_eids_ = eids;
120 }
121
122 const std::vector<std::string>& nearest_eids() { return nearest_eids_; }
98 123
99 protected: 124 protected:
100 std::unique_ptr<cryptauth::Connection> CreateConnection( 125 std::unique_ptr<cryptauth::Connection> CreateConnection(
101 const std::string& device_address) override { 126 const std::string& device_address) override {
102 return base::WrapUnique(CreateConnectionProxy()); 127 return base::WrapUnique(CreateConnectionProxy());
103 } 128 }
104 129
105 private: 130 private:
131 std::vector<std::string> nearest_eids_;
132
106 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); 133 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder);
107 }; 134 };
108 135
136 // Not declared in-line due to dependency on
137 // MockBluetoothLowEnergyConnectionFinder.
138 std::vector<std::string> FakeEidGenerator::GenerateNearestEids(
139 const std::vector<cryptauth::BeaconSeed>& beacon_seed) const {
140 return connection_finder_->nearest_eids();
141 }
142
109 } // namespace 143 } // namespace
110 144
111 class ProximityAuthBluetoothLowEnergyConnectionFinderTest 145 class ProximityAuthBluetoothLowEnergyConnectionFinderTest
112 : public testing::Test { 146 : public testing::Test {
113 protected: 147 protected:
114 ProximityAuthBluetoothLowEnergyConnectionFinderTest() 148 ProximityAuthBluetoothLowEnergyConnectionFinderTest()
115 : adapter_(new NiceMock<device::MockBluetoothAdapter>), 149 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
116 connection_callback_( 150 connection_callback_(
117 base::Bind(&ProximityAuthBluetoothLowEnergyConnectionFinderTest:: 151 base::Bind(&ProximityAuthBluetoothLowEnergyConnectionFinderTest::
118 OnConnectionFound, 152 OnConnectionFound,
119 base::Unretained(this))), 153 base::Unretained(this))),
120 device_(new NiceMock<device::MockBluetoothDevice>( 154 device_(new NiceMock<device::MockBluetoothDevice>(
121 adapter_.get(), 155 adapter_.get(),
122 0, 156 0,
123 cryptauth::kTestRemoteDeviceName, 157 cryptauth::kTestRemoteDeviceName,
124 cryptauth::kTestRemoteDeviceBluetoothAddress, 158 cryptauth::kTestRemoteDeviceBluetoothAddress,
125 false, 159 false,
126 false)), 160 false)),
127 device_whitelist_(new MockBluetoothLowEnergyDeviceWhitelist()),
128 last_discovery_session_alias_(nullptr) { 161 last_discovery_session_alias_(nullptr) {
129 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 162 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
130 163
131 std::vector<const device::BluetoothDevice*> devices; 164 std::vector<const device::BluetoothDevice*> devices;
132 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 165 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
133 166
134 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); 167 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
135 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); 168 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
136 169
137 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) 170 std::vector<std::string> nearest_eids;
138 .WillByDefault(Return(false)); 171 nearest_eids.push_back(kEidForPreviousTimeQuantum);
172 nearest_eids.push_back(kEidForCurrentTimeQuantum);
173 nearest_eids.push_back(kEidForNextTimeQuantum);
174 connection_finder_.SetNearestEids(nearest_eids);
139 } 175 }
140 176
141 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection) { 177 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection) {
142 last_found_connection_ = std::move(connection); 178 last_found_connection_ = std::move(connection);
143 } 179 }
144 180
145 void FindAndExpectStartDiscovery( 181 void FindAndExpectStartDiscovery() {
146 BluetoothLowEnergyConnectionFinder& connection_finder) {
147 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 182 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
148 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 183 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
149 new NiceMock<device::MockBluetoothDiscoverySession>()); 184 new NiceMock<device::MockBluetoothDiscoverySession>());
150 last_discovery_session_alias_ = discovery_session.get(); 185 last_discovery_session_alias_ = discovery_session.get();
151 186
152 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for 187 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for
153 // StartDiscoveryWithFilter. 188 // StartDiscoveryWithFilter.
154 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) 189 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _))
155 .WillOnce(SaveArg<1>(&discovery_callback)); 190 .WillOnce(SaveArg<1>(&discovery_callback));
156 EXPECT_CALL(*adapter_, AddObserver(_)); 191 EXPECT_CALL(*adapter_, AddObserver(_));
157 ON_CALL(*last_discovery_session_alias_, IsActive()) 192 ON_CALL(*last_discovery_session_alias_, IsActive())
158 .WillByDefault(Return(true)); 193 .WillByDefault(Return(true));
159 connection_finder.Find(connection_callback_); 194 connection_finder_.Find(connection_callback_);
160 ASSERT_FALSE(discovery_callback.is_null()); 195 ASSERT_FALSE(discovery_callback.is_null());
161 discovery_callback.Run(std::move(discovery_session)); 196 discovery_callback.Run(std::move(discovery_session));
162 }
163 197
164 void ExpectRemoveObserver() {
165 EXPECT_CALL(*adapter_, RemoveObserver(_)).Times(AtLeast(1)); 198 EXPECT_CALL(*adapter_, RemoveObserver(_)).Times(AtLeast(1));
166 } 199 }
167 200
168 // Prepare |device_| with |uuid|. 201 // Prepare |device_| with the given EID.
169 void PrepareDevice(const std::string& uuid, 202 void PrepareDevice(const std::string& eid) {
170 const std::string& address, 203 PrepareDevice(device_.get(), eid);
171 bool paired) { 204 }
172 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(uuid)}; 205
173 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); 206 void PrepareDevice(MockBluetoothDevice* device, const std::string& eid) {
174 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); 207 device::BluetoothUUID advertisement_uuid(kAdvertisementUUID);
175 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); 208 std::vector<uint8_t> eid_vector(eid.c_str(), eid.c_str() + eid.length());
209 device::BluetoothDevice::UUIDList uuid_list;
210 uuid_list.push_back(advertisement_uuid);
211 device::BluetoothDevice::ServiceDataMap service_data_map;
212 service_data_map[advertisement_uuid] = eid_vector;
213
214 device_->UpdateAdvertisementData(kRssi, uuid_list, service_data_map,
215 nullptr);
176 } 216 }
177 217
178 scoped_refptr<device::MockBluetoothAdapter> adapter_; 218 scoped_refptr<device::MockBluetoothAdapter> adapter_;
179 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; 219 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_;
180 std::unique_ptr<device::MockBluetoothDevice> device_; 220 std::unique_ptr<device::MockBluetoothDevice> device_;
181 std::unique_ptr<cryptauth::Connection> last_found_connection_; 221 std::unique_ptr<cryptauth::Connection> last_found_connection_;
182 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_;
183 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; 222 device::MockBluetoothDiscoverySession* last_discovery_session_alias_;
223 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder_;
184 224
185 private: 225 private:
186 base::MessageLoop message_loop_; 226 base::MessageLoop message_loop_;
187 }; 227 };
188 228
189 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 229 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
190 ConstructAndDestroyDoesntCrash) {
191 // Destroying a BluetoothConnectionFinder for which Find() has not been called
192 // should not crash.
193 BluetoothLowEnergyConnectionFinder connection_finder(
194 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID,
195 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE,
196 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts);
197 }
198
199 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
200 Find_StartsDiscoverySession) { 230 Find_StartsDiscoverySession) {
201 BluetoothLowEnergyConnectionFinder connection_finder(
202 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID,
203 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE,
204 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts);
205
206 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)); 231 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _));
207 EXPECT_CALL(*adapter_, AddObserver(_)); 232 EXPECT_CALL(*adapter_, AddObserver(_));
208 connection_finder.Find(connection_callback_); 233 connection_finder_.Find(connection_callback_);
209 } 234 }
210 235
211 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 236 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
212 Find_StopsDiscoverySessionBeforeDestroying) { 237 Find_StopsDiscoverySessionBeforeDestroying) {
213 BluetoothLowEnergyConnectionFinder connection_finder(
214 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID,
215 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE,
216 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts);
217
218 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 238 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
219 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 239 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
220 new NiceMock<device::MockBluetoothDiscoverySession>()); 240 new NiceMock<device::MockBluetoothDiscoverySession>());
221 device::MockBluetoothDiscoverySession* discovery_session_alias = 241 device::MockBluetoothDiscoverySession* discovery_session_alias =
222 discovery_session.get(); 242 discovery_session.get();
223 243
224 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) 244 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _))
225 .WillOnce(SaveArg<1>(&discovery_callback)); 245 .WillOnce(SaveArg<1>(&discovery_callback));
226 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); 246 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true));
227 EXPECT_CALL(*adapter_, AddObserver(_)); 247 EXPECT_CALL(*adapter_, AddObserver(_));
228 connection_finder.Find(connection_callback_); 248 connection_finder_.Find(connection_callback_);
229 249
230 ASSERT_FALSE(discovery_callback.is_null()); 250 ASSERT_FALSE(discovery_callback.is_null());
231 discovery_callback.Run(std::move(discovery_session)); 251 discovery_callback.Run(std::move(discovery_session));
232 252
233 EXPECT_CALL(*adapter_, RemoveObserver(_)); 253 EXPECT_CALL(*adapter_, RemoveObserver(_));
234 } 254 }
235 255
236 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed anymore.
237 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 256 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
238 Find_CreatesConnectionWhenWhitelistedDeviceIsAdded) { 257 Find_DeviceAdded_EidMatches) {
239 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 258 FindAndExpectStartDiscovery();
240 device_whitelist_.get(),
241 BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
242 FindAndExpectStartDiscovery(connection_finder);
243 ExpectRemoveObserver();
244 259
245 BluetoothDevice::UUIDSet uuids; 260 connection_finder_.ExpectCreateConnection();
246 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); 261 PrepareDevice(kEidForCurrentTimeQuantum);
247 ON_CALL(*device_, IsPaired()).WillByDefault(Return(true)); 262 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
248 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_))
249 .WillByDefault(Return(true));
250
251 connection_finder.ExpectCreateConnection();
252 connection_finder.DeviceAdded(adapter_.get(), device_.get());
253 } 263 }
254 264
255 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 265 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
256 Find_CreatesConnectionWhenRightDeviceIsAdded_NoPublicAddress) { 266 Find_DeviceChanged_EidMatches) {
257 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 267 FindAndExpectStartDiscovery();
258 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
259 268
260 FindAndExpectStartDiscovery(connection_finder); 269 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
261 ExpectRemoveObserver();
262 270
263 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 271 connection_finder_.ExpectCreateConnection();
264 false); 272 PrepareDevice(kEidForPreviousTimeQuantum);
265 ON_CALL(*device_, GetName()) 273 connection_finder_.DeviceChanged(adapter_.get(), device_.get());
266 .WillByDefault(Return(std::string(cryptauth::kTestRemoteDeviceName)));
267
268 connection_finder.ExpectCreateConnection();
269 connection_finder.DeviceAdded(adapter_.get(), device_.get());
270 } 274 }
271 275
272 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 276 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
273 Find_DoesntCreatesConnectionWhenWrongDeviceIsAdded_NoPublicAddress) { 277 Find_DeviceAdded_EidDoesNotMatch) {
274 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 278 FindAndExpectStartDiscovery();
275 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
276 279
277 FindAndExpectStartDiscovery(connection_finder); 280 PrepareDevice(kWrongEid);
278 ExpectRemoveObserver();
279 281
280 PrepareDevice(kOtherUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 282 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
281 false); 283 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
282 ON_CALL(*device_, GetName()).WillByDefault(Return(std::string("Other name")));
283
284 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
285 connection_finder.DeviceAdded(adapter_.get(), device_.get());
286 } 284 }
287 285
288 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 286 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
289 Find_CreatesConnectionWhenRightDeviceIsAdded_HasPublicAddress) { 287 Find_DeviceChanged_EidDoesNotMatch) {
290 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 288 FindAndExpectStartDiscovery();
291 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
292 289
293 FindAndExpectStartDiscovery(connection_finder); 290 PrepareDevice(kWrongEid);
294 ExpectRemoveObserver(); 291 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
295 292
296 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 293 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
297 true); 294 connection_finder_.DeviceChanged(adapter_.get(), device_.get());
298 connection_finder.ExpectCreateConnection();
299 connection_finder.DeviceAdded(adapter_.get(), device_.get());
300 }
301
302 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
303 Find_DoesntCreateConnectionWhenWrongDeviceIsAdded_HasPublicAddress) {
304 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
305 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
306 FindAndExpectStartDiscovery(connection_finder);
307 ExpectRemoveObserver();
308
309 PrepareDevice(kOtherUUID, "", true);
310 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
311 connection_finder.DeviceAdded(adapter_.get(), device_.get());
312 }
313
314 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
315 Find_CreatesConnectionWhenRightDeviceIsChanged_HasPublicAddress) {
316 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
317 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
318
319 FindAndExpectStartDiscovery(connection_finder);
320 ExpectRemoveObserver();
321
322 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress,
323 true);
324 connection_finder.ExpectCreateConnection();
325 connection_finder.DeviceChanged(adapter_.get(), device_.get());
326 }
327
328 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
329 Find_DoesntCreateConnectionWhenWrongDeviceIsChanged_HasPublicAddress) {
330 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
331 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
332
333 FindAndExpectStartDiscovery(connection_finder);
334 ExpectRemoveObserver();
335
336 PrepareDevice(kOtherUUID, "", true);
337 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0);
338 connection_finder.DeviceChanged(adapter_.get(), device_.get());
339 } 295 }
340 296
341 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 297 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
342 Find_CreatesOnlyOneConnection) { 298 Find_CreatesOnlyOneConnection) {
343 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 299 FindAndExpectStartDiscovery();
344 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE);
345 FindAndExpectStartDiscovery(connection_finder);
346 ExpectRemoveObserver();
347 300
348 // Prepare to add |device_|. 301 // Prepare first device with valid EID.
349 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 302 PrepareDevice(kEidForCurrentTimeQuantum);
350 true);
351 303
352 // Prepare to add |other_device|. 304 // Prepare second device with valid EID.
353 NiceMock<device::MockBluetoothDevice> other_device( 305 NiceMock<device::MockBluetoothDevice> other_device(
354 adapter_.get(), 0, cryptauth::kTestRemoteDeviceName, 306 adapter_.get(), 0, cryptauth::kTestRemoteDeviceName,
355 cryptauth::kTestRemoteDeviceBluetoothAddress, false, false); 307 cryptauth::kTestRemoteDeviceBluetoothAddress, false, false);
356 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(kServiceUUID)}; 308 PrepareDevice(&other_device, kEidForPreviousTimeQuantum);
357 ON_CALL(other_device, GetAddress())
358 .WillByDefault(Return(cryptauth::kTestRemoteDeviceBluetoothAddress));
359 ON_CALL(other_device, IsPaired()).WillByDefault(Return(true));
360 ON_CALL(other_device, GetUUIDs()).WillByDefault((Return(uuids)));
361 309
362 // Only one connection should be created. 310 // Add the devices. Only one connection is expected.
363 connection_finder.ExpectCreateConnection(); 311 connection_finder_.ExpectCreateConnection();
364 312 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
365 // Add the devices. 313 connection_finder_.DeviceAdded(adapter_.get(), &other_device);
366 connection_finder.DeviceAdded(adapter_.get(), device_.get());
367 connection_finder.DeviceAdded(adapter_.get(), &other_device);
368 } 314 }
369 315
370 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 316 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
371 Find_ConnectionSucceeds_WithRemoteDevice) { 317 Find_EidMatches_ConnectionSucceeds) {
372 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
373 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
374 // Starting discovery. 318 // Starting discovery.
375 FindAndExpectStartDiscovery(connection_finder); 319 FindAndExpectStartDiscovery();
376 ExpectRemoveObserver();
377 320
378 // Finding and creating a connection to the right device. 321 // Finding and creating a connection to the right device.
379 cryptauth::FakeConnection* connection = 322 cryptauth::FakeConnection* connection =
380 connection_finder.ExpectCreateConnection(); 323 connection_finder_.ExpectCreateConnection();
381 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 324 PrepareDevice(kEidForCurrentTimeQuantum);
382 true); 325 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
383 connection_finder.DeviceAdded(adapter_.get(), device_.get());
384 326
385 // Creating a connection. 327 // Creating a connection.
386 base::RunLoop run_loop; 328 base::RunLoop run_loop;
387 EXPECT_FALSE(last_found_connection_); 329 EXPECT_FALSE(last_found_connection_);
388 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); 330 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
389 connection->SetStatus(cryptauth::Connection::CONNECTED); 331 connection->SetStatus(cryptauth::Connection::CONNECTED);
390 run_loop.RunUntilIdle(); 332 run_loop.RunUntilIdle();
391 EXPECT_TRUE(last_found_connection_); 333 EXPECT_TRUE(last_found_connection_);
392 } 334 }
393 335
394 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 336 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
395 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) { 337 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) {
396 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
397 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
398
399 // Starting discovery. 338 // Starting discovery.
400 FindAndExpectStartDiscovery(connection_finder); 339 FindAndExpectStartDiscovery();
401 340
402 // Preparing to create a GATT connection to the right device. 341 // Preparing to create a GATT connection to the right device.
403 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 342 PrepareDevice(kEidForNextTimeQuantum);
404 true);
405 cryptauth::FakeConnection* connection = 343 cryptauth::FakeConnection* connection =
406 connection_finder.ExpectCreateConnection(); 344 connection_finder_.ExpectCreateConnection();
407 345
408 // Trying to create a connection. 346 // Trying to create a connection.
409 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 347 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
410 ASSERT_FALSE(last_found_connection_); 348 ASSERT_FALSE(last_found_connection_);
411 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); 349 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
412 350
413 // Preparing to restart the discovery session. 351 // Preparing to restart the discovery session.
414 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 352 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
415 std::vector<const device::BluetoothDevice*> devices; 353 std::vector<const device::BluetoothDevice*> devices;
416 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 354 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
417 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) 355 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _))
418 .WillOnce(SaveArg<1>(&discovery_callback)); 356 .WillOnce(SaveArg<1>(&discovery_callback));
419 357
420 // Connection fails. 358 // Connection fails.
421 { 359 {
422 base::RunLoop run_loop; 360 base::RunLoop run_loop;
423 connection->SetStatus(cryptauth::Connection::DISCONNECTED); 361 connection->SetStatus(cryptauth::Connection::DISCONNECTED);
424 run_loop.RunUntilIdle(); 362 run_loop.RunUntilIdle();
425 } 363 }
426 364
427 // Restarting the discovery session. 365 // Restarting the discovery session.
428 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 366 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
429 new NiceMock<device::MockBluetoothDiscoverySession>()); 367 new NiceMock<device::MockBluetoothDiscoverySession>());
430 last_discovery_session_alias_ = discovery_session.get(); 368 last_discovery_session_alias_ = discovery_session.get();
431 ON_CALL(*last_discovery_session_alias_, IsActive()) 369 ON_CALL(*last_discovery_session_alias_, IsActive())
432 .WillByDefault(Return(true)); 370 .WillByDefault(Return(true));
433 ASSERT_FALSE(discovery_callback.is_null()); 371 ASSERT_FALSE(discovery_callback.is_null());
434 discovery_callback.Run(std::move(discovery_session)); 372 discovery_callback.Run(std::move(discovery_session));
435 373
436 // Preparing to create a GATT connection to the right device. 374 // Connect again.
437 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 375 PrepareDevice(kEidForNextTimeQuantum);
438 true); 376 connection = connection_finder_.ExpectCreateConnection();
439 connection = connection_finder.ExpectCreateConnection(); 377 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
440
441 // Trying to create a connection.
442 connection_finder.DeviceAdded(adapter_.get(), device_.get());
443 378
444 // Completing the connection. 379 // Completing the connection.
445 { 380 {
446 base::RunLoop run_loop; 381 base::RunLoop run_loop;
447 EXPECT_FALSE(last_found_connection_); 382 EXPECT_FALSE(last_found_connection_);
448 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); 383 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
449 connection->SetStatus(cryptauth::Connection::CONNECTED); 384 connection->SetStatus(cryptauth::Connection::CONNECTED);
450 run_loop.RunUntilIdle(); 385 run_loop.RunUntilIdle();
451 } 386 }
452 EXPECT_TRUE(last_found_connection_); 387 EXPECT_TRUE(last_found_connection_);
453 } 388 }
454 389
455 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 390 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
456 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds) { 391 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds) {
457 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
458 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
459
460 // Starting discovery. 392 // Starting discovery.
461 FindAndExpectStartDiscovery(connection_finder); 393 FindAndExpectStartDiscovery();
462 394
463 // Removing the adapter. 395 // Removing the adapter.
464 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); 396 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
465 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); 397 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false));
466 ON_CALL(*last_discovery_session_alias_, IsActive()) 398 ON_CALL(*last_discovery_session_alias_, IsActive())
467 .WillByDefault(Return(false)); 399 .WillByDefault(Return(false));
468 connection_finder.AdapterPoweredChanged(adapter_.get(), false); 400 connection_finder_.AdapterPoweredChanged(adapter_.get(), false);
469 connection_finder.AdapterPresentChanged(adapter_.get(), false); 401 connection_finder_.AdapterPresentChanged(adapter_.get(), false);
470 402
471 // Adding the adapter. 403 // Adding the adapter.
472 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); 404 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
473 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); 405 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
474 406
475 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 407 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
476 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 408 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
477 new NiceMock<device::MockBluetoothDiscoverySession>()); 409 new NiceMock<device::MockBluetoothDiscoverySession>());
478 last_discovery_session_alias_ = discovery_session.get(); 410 last_discovery_session_alias_ = discovery_session.get();
479 411
480 // Restarting the discovery session. 412 // Restarting the discovery session.
481 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) 413 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _))
482 .WillOnce(SaveArg<1>(&discovery_callback)); 414 .WillOnce(SaveArg<1>(&discovery_callback));
483 connection_finder.AdapterPresentChanged(adapter_.get(), true); 415 connection_finder_.AdapterPresentChanged(adapter_.get(), true);
484 connection_finder.AdapterPoweredChanged(adapter_.get(), true); 416 connection_finder_.AdapterPoweredChanged(adapter_.get(), true);
485 ON_CALL(*last_discovery_session_alias_, IsActive()) 417 ON_CALL(*last_discovery_session_alias_, IsActive())
486 .WillByDefault(Return(true)); 418 .WillByDefault(Return(true));
487 419
488 ASSERT_FALSE(discovery_callback.is_null()); 420 ASSERT_FALSE(discovery_callback.is_null());
489 discovery_callback.Run(std::move(discovery_session)); 421 discovery_callback.Run(std::move(discovery_session));
490 422
491 // Preparing to create a GATT connection to the right device. 423 // Preparing to create a GATT connection to the right device.
492 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, 424 PrepareDevice(kEidForPreviousTimeQuantum);
493 true);
494 cryptauth::FakeConnection* connection = 425 cryptauth::FakeConnection* connection =
495 connection_finder.ExpectCreateConnection(); 426 connection_finder_.ExpectCreateConnection();
496 427
497 // Trying to create a connection. 428 // Trying to create a connection.
498 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 429 connection_finder_.DeviceAdded(adapter_.get(), device_.get());
499 430
500 // Completing the connection. 431 // Completing the connection.
501 base::RunLoop run_loop; 432 base::RunLoop run_loop;
502 ASSERT_FALSE(last_found_connection_); 433 ASSERT_FALSE(last_found_connection_);
503 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); 434 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
504 connection->SetStatus(cryptauth::Connection::CONNECTED); 435 connection->SetStatus(cryptauth::Connection::CONNECTED);
505 run_loop.RunUntilIdle(); 436 run_loop.RunUntilIdle();
506 EXPECT_TRUE(last_found_connection_); 437 EXPECT_TRUE(last_found_connection_);
507 } 438 }
508 439
509 } // namespace proximity_auth 440 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698