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

Side by Side Diff: chromeos/components/tether/ble_connection_manager_unittest.cc

Issue 2697763002: [CrOS Tether]: Create BleConnectionManager, which manages secure connections between the current de… (Closed)
Patch Set: Created 3 years, 10 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
(Empty)
1 // Copyright 2016 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 "chromeos/components/tether/ble_connection_manager.h"
6
7 #include "base/logging.h"
8 #include "base/timer/mock_timer.h"
9 #include "chromeos/components/tether/ble_constants.h"
10 #include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection. h"
11 #include "components/cryptauth/bluetooth_throttler.h"
12 #include "components/cryptauth/connection.h"
13 #include "components/cryptauth/fake_connection.h"
14 #include "components/cryptauth/fake_secure_channel.h"
15 #include "components/cryptauth/fake_secure_message_delegate.h"
16 #include "components/cryptauth/remote_device_test_util.h"
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using testing::_;
22 using testing::NiceMock;
23 using testing::Return;
24
25 namespace chromeos {
26
27 namespace tether {
28
29 namespace {
30
31 const char kTetherFeature[] = "magic_tether";
32
33 const int64_t kAdvertisingTimeoutInSeconds = 12;
34 const int64_t kShortErrorTimeoutInSeconds = 1;
35
36 const char kUserId[] = "userId";
37
38 const char kBluetoothAddress1[] = "11:22:33:44:55:66";
39 const char kBluetoothAddress2[] = "22:33:44:55:66:77";
40 const char kBluetoothAddress3[] = "33:44:55:66:77:88";
41 // const char kBluetoothAddress4[] = "44:55:66:77:88:99";
42
43 class FakeSecureChannelDelegate : public cryptauth::SecureChannel::Delegate {
44 public:
45 FakeSecureChannelDelegate() {}
46 ~FakeSecureChannelDelegate() override {}
47
48 std::unique_ptr<cryptauth::SecureMessageDelegate>
49 CreateSecureMessageDelegate() override {
50 return base::MakeUnique<cryptauth::FakeSecureMessageDelegate>();
51 }
52 };
53
54 class TestDelegate : public BleConnectionManager::Delegate {
55 public:
56 TestDelegate() {}
57 ~TestDelegate() {}
58
59 std::unique_ptr<cryptauth::SecureChannel::Delegate>
60 CreateSecureChannelDelegate() override {
61 return base::WrapUnique(new FakeSecureChannelDelegate());
62 }
63 };
64
65 struct SecureChannelStatusChange {
66 SecureChannelStatusChange(const cryptauth::RemoteDevice& remote_device,
67 const cryptauth::SecureChannel::Status& old_status,
68 const cryptauth::SecureChannel::Status& new_status)
69 : remote_device(remote_device),
70 old_status(old_status),
71 new_status(new_status) {}
72
73 cryptauth::RemoteDevice remote_device;
74 cryptauth::SecureChannel::Status old_status;
75 cryptauth::SecureChannel::Status new_status;
76 };
77
78 struct ReceivedMessage {
79 ReceivedMessage(const cryptauth::RemoteDevice& remote_device,
80 const std::string& payload)
81 : remote_device(remote_device), payload(payload) {}
82
83 cryptauth::RemoteDevice remote_device;
84 std::string payload;
85 };
86
87 class TestObserver : public BleConnectionManager::Observer {
88 public:
89 TestObserver() {}
90
91 // BleConnectionManager::Observer:
92 void OnSecureChannelStatusChanged(
93 const cryptauth::RemoteDevice& remote_device,
94 const cryptauth::SecureChannel::Status& old_status,
95 const cryptauth::SecureChannel::Status& new_status) override {
96 connection_status_changes_.push_back(
97 SecureChannelStatusChange(remote_device, old_status, new_status));
98 }
99
100 void OnMessageReceived(const cryptauth::RemoteDevice& remote_device,
101 const std::string& payload) override {
102 received_messages_.push_back(ReceivedMessage(remote_device, payload));
103 }
104
105 std::vector<SecureChannelStatusChange>& connection_status_changes() {
106 return connection_status_changes_;
107 }
108
109 std::vector<ReceivedMessage>& received_messages() {
110 return received_messages_;
111 }
112
113 private:
114 std::vector<SecureChannelStatusChange> connection_status_changes_;
115 std::vector<ReceivedMessage> received_messages_;
116 };
117
118 class MockBleScanner : public BleScanner {
119 public:
120 MockBleScanner() : BleScanner(nullptr) {}
121 ~MockBleScanner() override {}
122
123 MOCK_METHOD1(RegisterScanFilterForDevice,
124 bool(const cryptauth::RemoteDevice&));
125 MOCK_METHOD1(UnregisterScanFilterForDevice,
126 bool(const cryptauth::RemoteDevice&));
127
128 void SimulateScanResults(const std::string& device_address,
129 const cryptauth::RemoteDevice& remote_device) {
130 for (auto& observer : observer_list_) {
131 observer.OnReceivedAdvertisementFromDevice(device_address, remote_device);
132 }
133 }
134 };
135
136 class MockBleAdvertiser : public BleAdvertiser {
137 public:
138 MockBleAdvertiser() : BleAdvertiser(nullptr, nullptr, nullptr) {}
139 ~MockBleAdvertiser() override {}
140
141 MOCK_METHOD1(StartAdvertisingToDevice, bool(const cryptauth::RemoteDevice&));
142 MOCK_METHOD1(StopAdvertisingToDevice, bool(const cryptauth::RemoteDevice&));
143 };
144
145 class MockBluetoothThrottler : public cryptauth::BluetoothThrottler {
146 public:
147 MockBluetoothThrottler() {}
148 virtual ~MockBluetoothThrottler() {}
149
150 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
151 MOCK_METHOD1(OnConnection, void(cryptauth::Connection*));
152 };
153
154 class FakeConnectionWithAddress : public cryptauth::FakeConnection {
155 public:
156 FakeConnectionWithAddress(const cryptauth::RemoteDevice& remote_device,
157 const std::string& device_address)
158 : FakeConnection(remote_device, /* should_auto_connect */ false),
159 device_address_(device_address) {}
160
161 std::string device_address() { return device_address_; }
162
163 private:
164 const std::string device_address_;
165 };
166
167 class FakeConnectionFactory
168 : public cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::
169 Factory {
170 public:
171 FakeConnectionFactory(
172 scoped_refptr<device::BluetoothAdapter> expected_adapter,
173 const device::BluetoothUUID expected_remote_service_uuid,
174 cryptauth::BluetoothThrottler* expected_bluetooth_throttler)
175 : expected_adapter_(expected_adapter),
176 expected_remote_service_uuid_(expected_remote_service_uuid),
177 expected_bluetooth_throttler_(expected_bluetooth_throttler) {}
178
179 std::unique_ptr<cryptauth::Connection> BuildInstance(
180 const cryptauth::RemoteDevice& remote_device,
181 const std::string& device_address,
182 scoped_refptr<device::BluetoothAdapter> adapter,
183 const device::BluetoothUUID remote_service_uuid,
184 cryptauth::BluetoothThrottler* bluetooth_throttler) override {
185 EXPECT_EQ(expected_adapter_, adapter);
186 EXPECT_EQ(expected_remote_service_uuid_, remote_service_uuid);
187 EXPECT_EQ(expected_bluetooth_throttler_, bluetooth_throttler);
188
189 return base::WrapUnique<FakeConnectionWithAddress>(
190 new FakeConnectionWithAddress(remote_device, device_address));
191 }
192
193 private:
194 scoped_refptr<device::BluetoothAdapter> expected_adapter_;
195 const device::BluetoothUUID expected_remote_service_uuid_;
196 cryptauth::BluetoothThrottler* expected_bluetooth_throttler_;
197 };
198
199 std::vector<cryptauth::RemoteDevice> CreateTestDevices(size_t num_to_create) {
200 std::vector<cryptauth::RemoteDevice> test_devices =
201 cryptauth::GenerateTestRemoteDevices(num_to_create);
202 for (auto& device : test_devices) {
203 device.user_id = std::string(kUserId);
204 }
205 return test_devices;
206 }
207
208 } // namespace
209
210 class BleConnectionManagerTest : public testing::Test {
211 protected:
212 class FakeSecureChannel : public cryptauth::FakeSecureChannel {
213 public:
214 FakeSecureChannel(
215 std::unique_ptr<cryptauth::Connection> connection,
216 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate)
217 : cryptauth::FakeSecureChannel(std::move(connection),
218 std::move(delegate)) {}
219
220 void AddObserver(Observer* observer) override {
221 cryptauth::FakeSecureChannel::AddObserver(observer);
222
223 EXPECT_EQ(static_cast<size_t>(1), observers().size());
224 }
225
226 void RemoveObserver(Observer* observer) override {
227 cryptauth::FakeSecureChannel::RemoveObserver(observer);
228 EXPECT_EQ(static_cast<size_t>(0), observers().size());
229 }
230 };
231
232 class FakeSecureChannelFactory : public cryptauth::SecureChannel::Factory {
233 public:
234 FakeSecureChannelFactory() {}
235
236 void SetExpectedDeviceAddress(const std::string& expected_device_address) {
237 expected_device_address_ = expected_device_address;
238 }
239
240 std::unique_ptr<cryptauth::SecureChannel> BuildInstance(
241 std::unique_ptr<cryptauth::Connection> connection,
242 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate) override {
243 FakeConnectionWithAddress* fake_connection =
244 static_cast<FakeConnectionWithAddress*>(connection.get());
245 EXPECT_EQ(expected_device_address_, fake_connection->device_address());
246 return base::WrapUnique(
247 new FakeSecureChannel(std::move(connection), std::move(delegate)));
248 }
249
250 private:
251 std::string expected_device_address_;
252 };
253
254 class MockTimerFactory : public BleConnectionManager::TimerFactory {
255 public:
256 std::unique_ptr<base::Timer> CreateTimer() override {
257 return base::MakeUnique<base::MockTimer>(
258 /* retains_user_task */ false, /* is_repeating */ false);
259 }
260 };
261
262 BleConnectionManagerTest() : test_devices_(CreateTestDevices(4)) {
263 // These tests assume a maximum of two concurrent advertisers. Some of the
264 // multi-device tests would need to be re-written if this constant changes.
265 EXPECT_EQ(2, kMaxConcurrentAdvertisements);
266 }
267
268 void SetUp() override {
269 verified_status_changes_.clear();
270 verified_received_messages_.clear();
271
272 delegate_ = new TestDelegate();
273 mock_adapter_ =
274 make_scoped_refptr(new NiceMock<device::MockBluetoothAdapter>());
275
276 mock_ble_scanner_ = new MockBleScanner();
277 ON_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(_))
278 .WillByDefault(Return(true));
279 ON_CALL(*mock_ble_scanner_, UnregisterScanFilterForDevice(_))
280 .WillByDefault(Return(true));
281
282 mock_ble_advertiser_ = new MockBleAdvertiser();
283 ON_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(_))
284 .WillByDefault(Return(true));
285 ON_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(_))
286 .WillByDefault(Return(true));
287
288 device_queue_ = new BleAdvertisementDeviceQueue();
289 mock_timer_factory_ = new MockTimerFactory();
290 mock_bluetooth_throttler_ = base::WrapUnique(new MockBluetoothThrottler());
291
292 fake_connection_factory_ = base::WrapUnique(new FakeConnectionFactory(
293 mock_adapter_, device::BluetoothUUID(std::string(kGattServerUuid)),
294 mock_bluetooth_throttler_.get()));
295 cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory::
296 SetInstanceForTesting(std::move(fake_connection_factory_));
297
298 fake_secure_channel_factory_ =
299 base::WrapUnique(new FakeSecureChannelFactory());
300 cryptauth::SecureChannel::Factory::SetInstanceForTesting(
301 fake_secure_channel_factory_.get());
302
303 manager_ = base::WrapUnique(new BleConnectionManager(
304 base::WrapUnique(delegate_), mock_adapter_,
305 base::WrapUnique(mock_ble_scanner_),
306 base::WrapUnique(mock_ble_advertiser_), base::WrapUnique(device_queue_),
307 base::WrapUnique(mock_timer_factory_),
308 mock_bluetooth_throttler_.get()));
309 test_observer_ = base::WrapUnique(new TestObserver());
310 manager_->AddObserver(test_observer_.get());
311 }
312
313 void TearDown() override {
314 // All state changes should have already been verified. This ensures that
315 // no test has missed one.
316 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>());
317
318 // Same with received messages.
319 VerifyReceivedMessages(std::vector<ReceivedMessage>());
320 }
321
322 void VerifyConnectionStateChanges(
323 const std::vector<SecureChannelStatusChange>& expected_changes) {
324 verified_status_changes_.insert(verified_status_changes_.end(),
325 expected_changes.begin(),
326 expected_changes.end());
327
328 ASSERT_EQ(verified_status_changes_.size(),
329 test_observer_->connection_status_changes().size());
330
331 for (size_t i = 0; i < verified_status_changes_.size(); i++) {
332 EXPECT_EQ(verified_status_changes_[i].remote_device,
333 test_observer_->connection_status_changes()[i].remote_device);
334 EXPECT_EQ(verified_status_changes_[i].old_status,
335 test_observer_->connection_status_changes()[i].old_status);
336 EXPECT_EQ(verified_status_changes_[i].new_status,
337 test_observer_->connection_status_changes()[i].new_status);
338 }
339 }
340
341 void VerifyReceivedMessages(
342 const std::vector<ReceivedMessage>& expected_messages) {
343 verified_received_messages_.insert(verified_received_messages_.end(),
344 expected_messages.begin(),
345 expected_messages.end());
346
347 ASSERT_EQ(verified_received_messages_.size(),
348 test_observer_->received_messages().size());
349
350 for (size_t i = 0; i < verified_received_messages_.size(); i++) {
351 EXPECT_EQ(verified_received_messages_[i].remote_device,
352 test_observer_->received_messages()[i].remote_device);
353 EXPECT_EQ(verified_received_messages_[i].payload,
354 test_observer_->received_messages()[i].payload);
355 }
356 }
357
358 void VerifyNoTimeoutSet(const cryptauth::RemoteDevice& remote_device) {
359 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
360 manager_->GetConnectionMetadata(remote_device);
361 EXPECT_TRUE(data);
362 EXPECT_FALSE(data->timer_->IsRunning());
363 }
364
365 void VerifyShortErrorTimeoutSet(
366 const cryptauth::RemoteDevice& remote_device) {
367 VerifyTimeoutSet(remote_device, kShortErrorTimeoutInSeconds);
368 }
369
370 void VerifyAdvertisingTimeoutSet(
371 const cryptauth::RemoteDevice& remote_device) {
372 VerifyTimeoutSet(remote_device, kAdvertisingTimeoutInSeconds);
373 }
374
375 void VerifyTimeoutSet(const cryptauth::RemoteDevice& remote_device,
376 int64_t expected_num_seconds) {
377 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
378 manager_->GetConnectionMetadata(remote_device);
379 EXPECT_TRUE(data);
380 EXPECT_TRUE(data->timer_->IsRunning());
381 EXPECT_EQ(base::TimeDelta::FromSeconds(expected_num_seconds),
382 data->timer_->GetCurrentDelay());
383 }
384
385 void FireTimerForDevice(const cryptauth::RemoteDevice& remote_device) {
386 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
387 manager_->GetConnectionMetadata(remote_device);
388 EXPECT_TRUE(data);
389 EXPECT_TRUE(data->timer_->IsRunning());
390 static_cast<base::MockTimer*>(data->timer_.get())->Fire();
391 }
392
393 FakeSecureChannel* GetChannelForDevice(
394 const cryptauth::RemoteDevice& remote_device) {
395 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
396 manager_->GetConnectionMetadata(remote_device);
397 EXPECT_TRUE(data);
398 EXPECT_TRUE(data->secure_channel_);
399 return static_cast<FakeSecureChannel*>(data->secure_channel_.get());
400 }
401
402 void VerifyDeviceRegistered(const cryptauth::RemoteDevice& remote_device) {
403 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
404 manager_->GetConnectionMetadata(remote_device);
405 EXPECT_TRUE(data);
406 }
407
408 void VerifyDeviceNotRegistered(const cryptauth::RemoteDevice& remote_device) {
409 std::shared_ptr<BleConnectionManager::ConnectionMetadata> data =
410 manager_->GetConnectionMetadata(remote_device);
411 EXPECT_FALSE(data);
412 }
413
414 FakeSecureChannel* ConnectSuccessfully(
415 const cryptauth::RemoteDevice& remote_device,
416 const std::string& bluetooth_address,
417 const BleConnectionManager::ConnectionReason connection_reason) {
418 manager_->RegisterRemoteDevice(remote_device, connection_reason);
419 VerifyAdvertisingTimeoutSet(remote_device);
420 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
421 {remote_device, cryptauth::SecureChannel::Status::DISCONNECTED,
422 cryptauth::SecureChannel::Status::CONNECTING}});
423
424 FakeSecureChannel* channel =
425 ConnectChannel(remote_device, bluetooth_address);
426 AuthenticateChannel(remote_device);
427 return channel;
428 }
429
430 FakeSecureChannel* ConnectChannel(
431 const cryptauth::RemoteDevice& remote_device,
432 const std::string& bluetooth_address) {
433 VerifyDeviceRegistered(remote_device);
434
435 fake_secure_channel_factory_->SetExpectedDeviceAddress(bluetooth_address);
436 mock_ble_scanner_->SimulateScanResults(bluetooth_address, remote_device);
437 return GetChannelForDevice(remote_device);
438 }
439
440 void AuthenticateChannel(const cryptauth::RemoteDevice& remote_device) {
441 VerifyDeviceRegistered(remote_device);
442
443 FakeSecureChannel* channel = GetChannelForDevice(remote_device);
444 DCHECK(channel);
445
446 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTING);
447 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTED);
448 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATING);
449 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATED);
450 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
451 {remote_device, cryptauth::SecureChannel::Status::CONNECTING,
452 cryptauth::SecureChannel::Status::CONNECTED},
453 {remote_device, cryptauth::SecureChannel::Status::CONNECTED,
454 cryptauth::SecureChannel::Status::AUTHENTICATING},
455 {remote_device, cryptauth::SecureChannel::Status::AUTHENTICATING,
456 cryptauth::SecureChannel::Status::AUTHENTICATED}});
457 }
458
459 void VerifyLastMessageSent(FakeSecureChannel* channel,
460 const std::string& payload,
461 size_t expected_size) {
462 ASSERT_EQ(expected_size, channel->sent_messages().size());
463 cryptauth::FakeSecureChannel::SentMessage sent_message =
464 channel->sent_messages()[expected_size - 1];
465 EXPECT_EQ(std::string(kTetherFeature), sent_message.feature);
466 EXPECT_EQ(payload, sent_message.payload);
467 }
468
469 const std::vector<cryptauth::RemoteDevice> test_devices_;
470
471 BleConnectionManager::Delegate* delegate_;
472 scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_adapter_;
473 MockBleScanner* mock_ble_scanner_;
474 MockBleAdvertiser* mock_ble_advertiser_;
475 BleAdvertisementDeviceQueue* device_queue_;
476 MockTimerFactory* mock_timer_factory_;
477 std::unique_ptr<MockBluetoothThrottler> mock_bluetooth_throttler_;
478 std::unique_ptr<FakeConnectionFactory> fake_connection_factory_;
479 std::unique_ptr<FakeSecureChannelFactory> fake_secure_channel_factory_;
480 std::unique_ptr<TestObserver> test_observer_;
481
482 std::vector<SecureChannelStatusChange> verified_status_changes_;
483 std::vector<ReceivedMessage> verified_received_messages_;
484
485 std::unique_ptr<BleConnectionManager> manager_;
486
487 private:
488 DISALLOW_COPY_AND_ASSIGN(BleConnectionManagerTest);
489 };
490
491 TEST_F(BleConnectionManagerTest, TestCannotAdvertise) {
492 EXPECT_CALL(*mock_ble_scanner_,
493 RegisterScanFilterForDevice(test_devices_[0]));
494 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
495 .WillOnce(Return(false));
496
497 manager_->RegisterRemoteDevice(
498 test_devices_[0],
499 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
500 VerifyShortErrorTimeoutSet(test_devices_[0]);
501 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
502 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
503 cryptauth::SecureChannel::Status::CONNECTING}});
504 }
505
506 TEST_F(BleConnectionManagerTest, TestCannotScan) {
507 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
508 .WillOnce(Return(false));
509 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
510 .Times(0);
511
512 manager_->RegisterRemoteDevice(
513 test_devices_[0],
514 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
515 VerifyShortErrorTimeoutSet(test_devices_[0]);
516 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
517 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
518 cryptauth::SecureChannel::Status::CONNECTING}});
519 }
520
521 TEST_F(BleConnectionManagerTest, TestRegistersButNoResult) {
522 // Expected to start a connection attempt, then stop once the timer fires,
523 // then start again; 2 starts and 1 stop.
524 EXPECT_CALL(*mock_ble_scanner_,
525 RegisterScanFilterForDevice(test_devices_[0]));
526 EXPECT_CALL(*mock_ble_advertiser_,
527 StartAdvertisingToDevice(test_devices_[0]));
528
529 manager_->RegisterRemoteDevice(
530 test_devices_[0],
531 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
532 VerifyAdvertisingTimeoutSet(test_devices_[0]);
533 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
534 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
535 cryptauth::SecureChannel::Status::CONNECTING}});
536 }
537
538 TEST_F(BleConnectionManagerTest, TestRegistersAndUnregister_NoConnection) {
539 // Expected to start a connection attempt after the device is registered and
540 // to stop the attempt once the device is unregistered.
541 EXPECT_CALL(*mock_ble_scanner_,
542 RegisterScanFilterForDevice(test_devices_[0]));
543 EXPECT_CALL(*mock_ble_advertiser_,
544 StartAdvertisingToDevice(test_devices_[0]));
545 EXPECT_CALL(*mock_ble_scanner_,
546 UnregisterScanFilterForDevice(test_devices_[0]));
547 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
548
549 manager_->RegisterRemoteDevice(
550 test_devices_[0],
551 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
552 VerifyAdvertisingTimeoutSet(test_devices_[0]);
553 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
554 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
555 cryptauth::SecureChannel::Status::CONNECTING}});
556
557 manager_->UnregisterRemoteDevice(
558 test_devices_[0],
559 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
560 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
561 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
562 cryptauth::SecureChannel::Status::DISCONNECTED}});
563 }
564
565 TEST_F(BleConnectionManagerTest, TestRegisterWithNoConnection_TimerFires) {
566 // Expected to start a connection attempt, then stop once the timer fires,
567 // then start again, then stop when the device is unregistered; in total, 2
568 // starts and 2 stops.
569 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
570 .Times(2);
571 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
572 .Times(2);
573 EXPECT_CALL(*mock_ble_scanner_,
574 UnregisterScanFilterForDevice(test_devices_[0]))
575 .Times(2);
576 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]))
577 .Times(2);
578
579 manager_->RegisterRemoteDevice(
580 test_devices_[0],
581 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
582 VerifyAdvertisingTimeoutSet(test_devices_[0]);
583 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
584 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
585 cryptauth::SecureChannel::Status::CONNECTING}});
586
587 FireTimerForDevice(test_devices_[0]);
588 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
589 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
590 cryptauth::SecureChannel::Status::DISCONNECTED},
591 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
592 cryptauth::SecureChannel::Status::CONNECTING}});
593
594 manager_->UnregisterRemoteDevice(
595 test_devices_[0],
596 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
597 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
598 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
599 cryptauth::SecureChannel::Status::DISCONNECTED}});
600 }
601
602 TEST_F(BleConnectionManagerTest, TestSuccessfulConnection_FailsAuthentication) {
603 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
604 .Times(2);
605 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
606 .Times(2);
607 EXPECT_CALL(*mock_ble_scanner_,
608 UnregisterScanFilterForDevice(test_devices_[0]));
609 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
610
611 manager_->RegisterRemoteDevice(
612 test_devices_[0],
613 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
614 VerifyAdvertisingTimeoutSet(test_devices_[0]);
615 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
616 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
617 cryptauth::SecureChannel::Status::CONNECTING}});
618
619 fake_secure_channel_factory_->SetExpectedDeviceAddress(
620 std::string(kBluetoothAddress1));
621 mock_ble_scanner_->SimulateScanResults(std::string(kBluetoothAddress1),
622 test_devices_[0]);
623 FakeSecureChannel* channel = GetChannelForDevice(test_devices_[0]);
624
625 // Should not result in an additional "disconnected => connecting" broadcast.
626 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTING);
627 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>());
628
629 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTED);
630 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
631 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
632 cryptauth::SecureChannel::Status::CONNECTED}});
633
634 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATING);
635 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
636 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED,
637 cryptauth::SecureChannel::Status::AUTHENTICATING}});
638
639 // Fail authentication, which should automatically start a retry.
640 channel->ChangeStatus(cryptauth::SecureChannel::Status::DISCONNECTED);
641 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
642 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING,
643 cryptauth::SecureChannel::Status::DISCONNECTED},
644 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
645 cryptauth::SecureChannel::Status::CONNECTING}});
646 }
647
648 TEST_F(BleConnectionManagerTest, TestSuccessfulConnection_SendAndReceive) {
649 EXPECT_CALL(*mock_ble_scanner_,
650 RegisterScanFilterForDevice(test_devices_[0]));
651 EXPECT_CALL(*mock_ble_advertiser_,
652 StartAdvertisingToDevice(test_devices_[0]));
653 EXPECT_CALL(*mock_ble_scanner_,
654 UnregisterScanFilterForDevice(test_devices_[0]));
655 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
656
657 FakeSecureChannel* channel = ConnectSuccessfully(
658 test_devices_[0], std::string(kBluetoothAddress1),
659 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
660
661 manager_->SendMessage(test_devices_[0], "request1");
662 VerifyLastMessageSent(channel, "request1", 1);
663
664 channel->ReceiveMessage(std::string(kTetherFeature), "response1");
665 VerifyReceivedMessages(
666 std::vector<ReceivedMessage>{{test_devices_[0], "response1"}});
667
668 manager_->SendMessage(test_devices_[0], "request2");
669 VerifyLastMessageSent(channel, "request2", 2);
670
671 channel->ReceiveMessage(std::string(kTetherFeature), "response2");
672 VerifyReceivedMessages(
673 std::vector<ReceivedMessage>{{test_devices_[0], "response2"}});
674
675 manager_->UnregisterRemoteDevice(
676 test_devices_[0],
677 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
678 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
679 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
680 cryptauth::SecureChannel::Status::DISCONNECTED}});
681 VerifyDeviceNotRegistered(test_devices_[0]);
682 }
683
684 TEST_F(BleConnectionManagerTest,
685 TestSuccessfulConnection_MultipleConnectionReasons) {
686 EXPECT_CALL(*mock_ble_scanner_,
687 RegisterScanFilterForDevice(test_devices_[0]));
688 EXPECT_CALL(*mock_ble_advertiser_,
689 StartAdvertisingToDevice(test_devices_[0]));
690 EXPECT_CALL(*mock_ble_scanner_,
691 UnregisterScanFilterForDevice(test_devices_[0]));
692 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
693
694 ConnectSuccessfully(
695 test_devices_[0], std::string(kBluetoothAddress1),
696 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
697
698 // Now, register a different connection reason.
699 manager_->RegisterRemoteDevice(
700 test_devices_[0],
701 BleConnectionManager::ConnectionReason::CONNECT_TETHERING_REQUEST);
702
703 // Unregister the |TETHER_AVAILABILITY_REQUEST| reason, but leave the
704 // |CONNECT_TETHERING_REQUEST| registered.
705 manager_->UnregisterRemoteDevice(
706 test_devices_[0],
707 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
708 VerifyDeviceRegistered(test_devices_[0]);
709
710 // Now, unregister the other reason; this should cause the device to be
711 // fully unregistered.
712 manager_->UnregisterRemoteDevice(
713 test_devices_[0],
714 BleConnectionManager::ConnectionReason::CONNECT_TETHERING_REQUEST);
715 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
716 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
717 cryptauth::SecureChannel::Status::DISCONNECTED}});
718 VerifyDeviceNotRegistered(test_devices_[0]);
719 }
720
721 TEST_F(BleConnectionManagerTest,
722 TestSuccessfulConnection_DisconnectsAfterConnection) {
723 // A reconnection attempt is expected once the disconnection occurs, meaning
724 // two separate connection attempts.
725 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
726 .Times(2);
727 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
728 .Times(2);
729 EXPECT_CALL(*mock_ble_scanner_,
730 UnregisterScanFilterForDevice(test_devices_[0]));
731 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
732
733 FakeSecureChannel* channel = ConnectSuccessfully(
734 test_devices_[0], std::string(kBluetoothAddress1),
735 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
736
737 channel->ChangeStatus(cryptauth::SecureChannel::Status::DISCONNECTED);
738 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
739 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
740 cryptauth::SecureChannel::Status::DISCONNECTED},
741 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
742 cryptauth::SecureChannel::Status::CONNECTING}});
743 }
744
745 TEST_F(BleConnectionManagerTest, TwoDevices_NeitherCanScan) {
746 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
747 .WillOnce(Return(false));
748 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
749 .Times(0);
750 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1]))
751 .WillOnce(Return(false));
752 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1]))
753 .Times(0);
754
755 manager_->RegisterRemoteDevice(
756 test_devices_[0],
757 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
758 VerifyShortErrorTimeoutSet(test_devices_[0]);
759 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
760 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
761 cryptauth::SecureChannel::Status::CONNECTING}});
762
763 manager_->RegisterRemoteDevice(
764 test_devices_[1],
765 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
766 VerifyShortErrorTimeoutSet(test_devices_[1]);
767 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
768 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
769 cryptauth::SecureChannel::Status::CONNECTING}});
770 }
771
772 TEST_F(BleConnectionManagerTest, TwoDevices_NeitherCanAdvertise) {
773 EXPECT_CALL(*mock_ble_scanner_,
774 RegisterScanFilterForDevice(test_devices_[0]));
775 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
776 .WillOnce(Return(false));
777 EXPECT_CALL(*mock_ble_scanner_,
778 RegisterScanFilterForDevice(test_devices_[1]));
779 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1]))
780 .WillOnce(Return(false));
781
782 manager_->RegisterRemoteDevice(
783 test_devices_[0],
784 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
785 VerifyShortErrorTimeoutSet(test_devices_[0]);
786 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
787 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
788 cryptauth::SecureChannel::Status::CONNECTING}});
789
790 manager_->RegisterRemoteDevice(
791 test_devices_[1],
792 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
793 VerifyShortErrorTimeoutSet(test_devices_[1]);
794 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
795 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
796 cryptauth::SecureChannel::Status::CONNECTING}});
797 }
798
799 TEST_F(BleConnectionManagerTest,
800 TwoDevices_RegisterWithNoConnection_TimerFires) {
801 // Expected to start a connection attempt, then stop once the timer fires,
802 // then start again, then stop when the device is unregistered; in total, 2
803 // starts and 2 stops.
804 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0]))
805 .Times(2);
806 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0]))
807 .Times(2);
808 EXPECT_CALL(*mock_ble_scanner_,
809 UnregisterScanFilterForDevice(test_devices_[0]))
810 .Times(2);
811 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]))
812 .Times(2);
813 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1]))
814 .Times(2);
815 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1]))
816 .Times(2);
817 EXPECT_CALL(*mock_ble_scanner_,
818 UnregisterScanFilterForDevice(test_devices_[1]))
819 .Times(2);
820 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1]))
821 .Times(2);
822
823 // Register device 0.
824 manager_->RegisterRemoteDevice(
825 test_devices_[0],
826 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
827 VerifyAdvertisingTimeoutSet(test_devices_[0]);
828 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
829 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
830 cryptauth::SecureChannel::Status::CONNECTING}});
831
832 // Register device 1.
833 manager_->RegisterRemoteDevice(
834 test_devices_[1],
835 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
836 VerifyAdvertisingTimeoutSet(test_devices_[1]);
837 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
838 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
839 cryptauth::SecureChannel::Status::CONNECTING}});
840
841 // Simulate timeout for device 0 by firing timeout.
842 FireTimerForDevice(test_devices_[0]);
843 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
844 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
845 cryptauth::SecureChannel::Status::DISCONNECTED},
846 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
847 cryptauth::SecureChannel::Status::CONNECTING}});
848
849 // Simulate timeout for device 1 by firing timeout.
850 FireTimerForDevice(test_devices_[1]);
851 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
852 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
853 cryptauth::SecureChannel::Status::DISCONNECTED},
854 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
855 cryptauth::SecureChannel::Status::CONNECTING}});
856
857 // Unregister device 0.
858 manager_->UnregisterRemoteDevice(
859 test_devices_[0],
860 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
861 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
862 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING,
863 cryptauth::SecureChannel::Status::DISCONNECTED}});
864
865 // Unregister device 1.
866 manager_->UnregisterRemoteDevice(
867 test_devices_[1],
868 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
869 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
870 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
871 cryptauth::SecureChannel::Status::DISCONNECTED}});
872 }
873
874 TEST_F(BleConnectionManagerTest, TwoDevices_OneConnects) {
875 // Device 0 is expected to start the attempt and stop once a connection has
876 // been achieved. Device 1 is expected to start, then stop once the tiemr
877 // fires, then start again.
878 EXPECT_CALL(*mock_ble_scanner_,
879 RegisterScanFilterForDevice(test_devices_[0]));
880 EXPECT_CALL(*mock_ble_advertiser_,
881 StartAdvertisingToDevice(test_devices_[0]));
882 EXPECT_CALL(*mock_ble_scanner_,
883 UnregisterScanFilterForDevice(test_devices_[0]));
884 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
885 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1]))
886 .Times(2);
887 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1]))
888 .Times(2);
889 EXPECT_CALL(*mock_ble_scanner_,
890 UnregisterScanFilterForDevice(test_devices_[1]))
891 .Times(2);
892 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1]))
893 .Times(2);
894
895 // Successfully connect to device 0.
896 ConnectSuccessfully(
897 test_devices_[0], std::string(kBluetoothAddress1),
898 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
899
900 // Register device 1.
901 manager_->RegisterRemoteDevice(
902 test_devices_[1],
903 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
904 VerifyAdvertisingTimeoutSet(test_devices_[1]);
905 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
906 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
907 cryptauth::SecureChannel::Status::CONNECTING}});
908
909 // Simulate timeout for device 1 by firing timeout.
910 FireTimerForDevice(test_devices_[1]);
911 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
912 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
913 cryptauth::SecureChannel::Status::DISCONNECTED},
914 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
915 cryptauth::SecureChannel::Status::CONNECTING}});
916
917 // Unregister device 0.
918 manager_->UnregisterRemoteDevice(
919 test_devices_[0],
920 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
921 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
922 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
923 cryptauth::SecureChannel::Status::DISCONNECTED}});
924
925 // Unregister device 1.
926 manager_->UnregisterRemoteDevice(
927 test_devices_[1],
928 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
929 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
930 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
931 cryptauth::SecureChannel::Status::DISCONNECTED}});
932 }
933
934 TEST_F(BleConnectionManagerTest, TwoDevices_BothConnectSendAndReceive) {
935 // Device 0 is expected to start the attempt and stop once a connection has
936 // been achieved. Device 1 is expected to start, then stop once the tiemr
937 // fires, then start again.
938 EXPECT_CALL(*mock_ble_scanner_,
939 RegisterScanFilterForDevice(test_devices_[0]));
940 EXPECT_CALL(*mock_ble_advertiser_,
941 StartAdvertisingToDevice(test_devices_[0]));
942 EXPECT_CALL(*mock_ble_scanner_,
943 UnregisterScanFilterForDevice(test_devices_[0]));
944 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
945 EXPECT_CALL(*mock_ble_scanner_,
946 RegisterScanFilterForDevice(test_devices_[1]));
947 EXPECT_CALL(*mock_ble_advertiser_,
948 StartAdvertisingToDevice(test_devices_[1]));
949 EXPECT_CALL(*mock_ble_scanner_,
950 UnregisterScanFilterForDevice(test_devices_[1]));
951 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1]));
952
953 FakeSecureChannel* channel0 = ConnectSuccessfully(
954 test_devices_[0], std::string(kBluetoothAddress1),
955 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
956
957 FakeSecureChannel* channel1 = ConnectSuccessfully(
958 test_devices_[1], std::string(kBluetoothAddress2),
959 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
960
961 manager_->SendMessage(test_devices_[0], "request1_device0");
962 VerifyLastMessageSent(channel0, "request1_device0", 1);
963
964 manager_->SendMessage(test_devices_[1], "request1_device1");
965 VerifyLastMessageSent(channel1, "request1_device1", 1);
966
967 channel0->ReceiveMessage(std::string(kTetherFeature), "response1_device0");
968 VerifyReceivedMessages(
969 std::vector<ReceivedMessage>{{test_devices_[0], "response1_device0"}});
970
971 channel1->ReceiveMessage(std::string(kTetherFeature), "response1_device1");
972 VerifyReceivedMessages(
973 std::vector<ReceivedMessage>{{test_devices_[1], "response1_device1"}});
974
975 manager_->SendMessage(test_devices_[0], "request2_device0");
976 VerifyLastMessageSent(channel0, "request2_device0", 2);
977
978 manager_->SendMessage(test_devices_[1], "request2_device1");
979 VerifyLastMessageSent(channel1, "request2_device1", 2);
980
981 channel0->ReceiveMessage(std::string(kTetherFeature), "response2_device0");
982 VerifyReceivedMessages(
983 std::vector<ReceivedMessage>{{test_devices_[0], "response2_device0"}});
984
985 channel1->ReceiveMessage(std::string(kTetherFeature), "response2_device1");
986 VerifyReceivedMessages(
987 std::vector<ReceivedMessage>{{test_devices_[1], "response2_device1"}});
988
989 manager_->UnregisterRemoteDevice(
990 test_devices_[0],
991 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
992 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
993 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
994 cryptauth::SecureChannel::Status::DISCONNECTED}});
995 VerifyDeviceNotRegistered(test_devices_[0]);
996
997 manager_->UnregisterRemoteDevice(
998 test_devices_[1],
999 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1000 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1001 {test_devices_[1], cryptauth::SecureChannel::Status::AUTHENTICATED,
1002 cryptauth::SecureChannel::Status::DISCONNECTED}});
1003 VerifyDeviceNotRegistered(test_devices_[1]);
1004 }
1005
1006 TEST_F(BleConnectionManagerTest, FourDevices_ComprehensiveTest) {
1007 EXPECT_CALL(*mock_ble_scanner_,
1008 RegisterScanFilterForDevice(test_devices_[0]));
1009 EXPECT_CALL(*mock_ble_advertiser_,
1010 StartAdvertisingToDevice(test_devices_[0]));
1011 EXPECT_CALL(*mock_ble_scanner_,
1012 UnregisterScanFilterForDevice(test_devices_[0]));
1013 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0]));
1014 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1]))
1015 .Times(2);
1016 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1]))
1017 .Times(2);
1018 EXPECT_CALL(*mock_ble_scanner_,
1019 UnregisterScanFilterForDevice(test_devices_[1]))
1020 .Times(2);
1021 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1]))
1022 .Times(2);
1023 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[2]))
1024 .Times(2);
1025 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[2]))
1026 .Times(2);
1027 EXPECT_CALL(*mock_ble_scanner_,
1028 UnregisterScanFilterForDevice(test_devices_[2]))
1029 .Times(2);
1030 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[2]))
1031 .Times(2);
1032 EXPECT_CALL(*mock_ble_scanner_,
1033 RegisterScanFilterForDevice(test_devices_[3]));
1034 EXPECT_CALL(*mock_ble_advertiser_,
1035 StartAdvertisingToDevice(test_devices_[3]));
1036 EXPECT_CALL(*mock_ble_scanner_,
1037 UnregisterScanFilterForDevice(test_devices_[3]));
1038 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[3]));
1039
1040 // Register all devices. Since the maximum number of simultaneous connection
1041 // attempts is 2, only devices 0 and 1 should actually start connecting.
1042 manager_->RegisterRemoteDevice(
1043 test_devices_[0],
1044 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1045 manager_->RegisterRemoteDevice(
1046 test_devices_[1],
1047 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1048 manager_->RegisterRemoteDevice(
1049 test_devices_[2],
1050 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1051 manager_->RegisterRemoteDevice(
1052 test_devices_[3],
1053 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1054
1055 // Devices 0 and 1 should be advertising; devices 2 and 3 should not be.
1056 VerifyAdvertisingTimeoutSet(test_devices_[0]);
1057 VerifyAdvertisingTimeoutSet(test_devices_[1]);
1058 VerifyNoTimeoutSet(test_devices_[2]);
1059 VerifyNoTimeoutSet(test_devices_[3]);
1060 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1061 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED,
1062 cryptauth::SecureChannel::Status::CONNECTING},
1063 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
1064 cryptauth::SecureChannel::Status::CONNECTING}});
1065
1066 // Device 0 connects successfully.
1067 FakeSecureChannel* channel0 =
1068 ConnectChannel(test_devices_[0], std::string(kBluetoothAddress1));
1069
1070 // Since device 0 has connected, advertising to that device is no longer
1071 // necessary. Device 2 should have filled up that advertising slot.
1072 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1073 {test_devices_[2], cryptauth::SecureChannel::Status::DISCONNECTED,
1074 cryptauth::SecureChannel::Status::CONNECTING}});
1075
1076 // Meanwhile, device 1 fails to connect, so the timeout fires. The advertising
1077 // slot left by device 1 creates space for device 3 to start connecting.
1078 FireTimerForDevice(test_devices_[1]);
1079 VerifyAdvertisingTimeoutSet(test_devices_[3]);
1080 VerifyNoTimeoutSet(test_devices_[1]);
1081 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1082 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
1083 cryptauth::SecureChannel::Status::DISCONNECTED},
1084 {test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED,
1085 cryptauth::SecureChannel::Status::CONNECTING}});
1086
1087 // Now, device 0 authenticates and sends and receives a message.
1088 AuthenticateChannel(test_devices_[0]);
1089 manager_->SendMessage(test_devices_[0], "request1");
1090 VerifyLastMessageSent(channel0, "request1", 1);
1091
1092 channel0->ReceiveMessage(std::string(kTetherFeature), "response1");
1093 VerifyReceivedMessages(
1094 std::vector<ReceivedMessage>{{test_devices_[0], "response1"}});
1095
1096 // Now, device 0 is unregistered.
1097 manager_->UnregisterRemoteDevice(
1098 test_devices_[0],
1099 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1100 VerifyDeviceNotRegistered(test_devices_[0]);
1101 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1102 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED,
1103 cryptauth::SecureChannel::Status::DISCONNECTED}});
1104
1105 // Device 2 fails to connect, so the timeout fires. Device 1 takes its spot.
1106 FireTimerForDevice(test_devices_[2]);
1107 VerifyAdvertisingTimeoutSet(test_devices_[1]);
1108 VerifyNoTimeoutSet(test_devices_[2]);
1109 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1110 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING,
1111 cryptauth::SecureChannel::Status::DISCONNECTED},
1112 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED,
1113 cryptauth::SecureChannel::Status::CONNECTING}});
1114
1115 // Device 3 connects successfully.
1116 FakeSecureChannel* channel3 =
1117 ConnectChannel(test_devices_[3], std::string(kBluetoothAddress3));
1118
1119 // Since device 3 has connected, device 2 starts connecting again.
1120 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1121 {test_devices_[2], cryptauth::SecureChannel::Status::DISCONNECTED,
1122 cryptauth::SecureChannel::Status::CONNECTING}});
1123
1124 // Now, device 3 authenticates and sends and receives a message.
1125 AuthenticateChannel(test_devices_[3]);
1126 manager_->SendMessage(test_devices_[3], "request3");
1127 VerifyLastMessageSent(channel3, "request3", 1);
1128
1129 channel3->ReceiveMessage(std::string(kTetherFeature), "response3");
1130 VerifyReceivedMessages(
1131 std::vector<ReceivedMessage>{{test_devices_[3], "response3"}});
1132
1133 // Assume that none of the other devices can connect, and unregister the
1134 // remaining 3 devices.
1135 manager_->UnregisterRemoteDevice(
1136 test_devices_[3],
1137 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1138 VerifyDeviceNotRegistered(test_devices_[3]);
1139 manager_->UnregisterRemoteDevice(
1140 test_devices_[1],
1141 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1142 VerifyDeviceNotRegistered(test_devices_[1]);
1143 manager_->UnregisterRemoteDevice(
1144 test_devices_[2],
1145 BleConnectionManager::ConnectionReason::TETHER_AVAILABILITY_REQUEST);
1146 VerifyDeviceNotRegistered(test_devices_[2]);
1147
1148 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{
1149 {test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATED,
1150 cryptauth::SecureChannel::Status::DISCONNECTED},
1151 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING,
1152 cryptauth::SecureChannel::Status::DISCONNECTED},
1153 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING,
1154 cryptauth::SecureChannel::Status::DISCONNECTED}});
1155 }
1156
1157 } // namespace tether
1158
1159 } // namespace cryptauth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698