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

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

Powered by Google App Engine
This is Rietveld 408576698