OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chromeos/components/tether/ble_connection_manager.h" | 5 #include "chromeos/components/tether/ble_connection_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/timer/mock_timer.h" | 8 #include "base/timer/mock_timer.h" |
9 #include "chromeos/components/tether/ble_constants.h" | 9 #include "chromeos/components/tether/ble_constants.h" |
10 #include "chromeos/components/tether/proto/tether.pb.h" | 10 #include "chromeos/components/tether/proto/tether.pb.h" |
11 #include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection.
h" | 11 #include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection.
h" |
12 #include "components/cryptauth/bluetooth_throttler.h" | 12 #include "components/cryptauth/bluetooth_throttler.h" |
13 #include "components/cryptauth/connection.h" | 13 #include "components/cryptauth/connection.h" |
14 #include "components/cryptauth/fake_connection.h" | 14 #include "components/cryptauth/fake_connection.h" |
| 15 #include "components/cryptauth/fake_cryptauth_service.h" |
15 #include "components/cryptauth/fake_secure_channel.h" | 16 #include "components/cryptauth/fake_secure_channel.h" |
16 #include "components/cryptauth/fake_secure_message_delegate.h" | 17 #include "components/cryptauth/fake_secure_message_delegate.h" |
17 #include "components/cryptauth/remote_device_test_util.h" | 18 #include "components/cryptauth/remote_device_test_util.h" |
18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 19 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using testing::_; | 23 using testing::_; |
23 using testing::NiceMock; | 24 using testing::NiceMock; |
24 using testing::Return; | 25 using testing::Return; |
25 | 26 |
26 namespace chromeos { | 27 namespace chromeos { |
27 | 28 |
28 namespace tether { | 29 namespace tether { |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 const char kTetherFeature[] = "magic_tether"; | 33 const char kTetherFeature[] = "magic_tether"; |
33 | 34 |
34 const char kUserId[] = "userId"; | 35 const char kUserId[] = "userId"; |
35 | 36 |
36 const char kBluetoothAddress1[] = "11:22:33:44:55:66"; | 37 const char kBluetoothAddress1[] = "11:22:33:44:55:66"; |
37 const char kBluetoothAddress2[] = "22:33:44:55:66:77"; | 38 const char kBluetoothAddress2[] = "22:33:44:55:66:77"; |
38 const char kBluetoothAddress3[] = "33:44:55:66:77:88"; | 39 const char kBluetoothAddress3[] = "33:44:55:66:77:88"; |
39 | 40 |
40 class FakeSecureChannelDelegate : public cryptauth::SecureChannel::Delegate { | |
41 public: | |
42 FakeSecureChannelDelegate() {} | |
43 ~FakeSecureChannelDelegate() override {} | |
44 | |
45 std::unique_ptr<cryptauth::SecureMessageDelegate> | |
46 CreateSecureMessageDelegate() override { | |
47 return base::MakeUnique<cryptauth::FakeSecureMessageDelegate>(); | |
48 } | |
49 }; | |
50 | |
51 class TestDelegate : public BleConnectionManager::Delegate { | |
52 public: | |
53 TestDelegate() {} | |
54 ~TestDelegate() {} | |
55 | |
56 std::unique_ptr<cryptauth::SecureChannel::Delegate> | |
57 CreateSecureChannelDelegate() override { | |
58 return base::WrapUnique(new FakeSecureChannelDelegate()); | |
59 } | |
60 }; | |
61 | |
62 struct SecureChannelStatusChange { | 41 struct SecureChannelStatusChange { |
63 SecureChannelStatusChange(const cryptauth::RemoteDevice& remote_device, | 42 SecureChannelStatusChange(const cryptauth::RemoteDevice& remote_device, |
64 const cryptauth::SecureChannel::Status& old_status, | 43 const cryptauth::SecureChannel::Status& old_status, |
65 const cryptauth::SecureChannel::Status& new_status) | 44 const cryptauth::SecureChannel::Status& new_status) |
66 : remote_device(remote_device), | 45 : remote_device(remote_device), |
67 old_status(old_status), | 46 old_status(old_status), |
68 new_status(new_status) {} | 47 new_status(new_status) {} |
69 | 48 |
70 cryptauth::RemoteDevice remote_device; | 49 cryptauth::RemoteDevice remote_device; |
71 cryptauth::SecureChannel::Status old_status; | 50 cryptauth::SecureChannel::Status old_status; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 181 } |
203 return test_devices; | 182 return test_devices; |
204 } | 183 } |
205 | 184 |
206 } // namespace | 185 } // namespace |
207 | 186 |
208 class BleConnectionManagerTest : public testing::Test { | 187 class BleConnectionManagerTest : public testing::Test { |
209 protected: | 188 protected: |
210 class FakeSecureChannel : public cryptauth::FakeSecureChannel { | 189 class FakeSecureChannel : public cryptauth::FakeSecureChannel { |
211 public: | 190 public: |
212 FakeSecureChannel( | 191 FakeSecureChannel(std::unique_ptr<cryptauth::Connection> connection, |
213 std::unique_ptr<cryptauth::Connection> connection, | 192 cryptauth::CryptAuthService* cryptauth_service) |
214 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate) | |
215 : cryptauth::FakeSecureChannel(std::move(connection), | 193 : cryptauth::FakeSecureChannel(std::move(connection), |
216 std::move(delegate)) {} | 194 cryptauth_service) {} |
217 ~FakeSecureChannel() override {} | 195 ~FakeSecureChannel() override {} |
218 | 196 |
219 void AddObserver(Observer* observer) override { | 197 void AddObserver(Observer* observer) override { |
220 cryptauth::FakeSecureChannel::AddObserver(observer); | 198 cryptauth::FakeSecureChannel::AddObserver(observer); |
221 | 199 |
222 EXPECT_EQ(static_cast<size_t>(1), observers().size()); | 200 EXPECT_EQ(static_cast<size_t>(1), observers().size()); |
223 } | 201 } |
224 | 202 |
225 void RemoveObserver(Observer* observer) override { | 203 void RemoveObserver(Observer* observer) override { |
226 cryptauth::FakeSecureChannel::RemoveObserver(observer); | 204 cryptauth::FakeSecureChannel::RemoveObserver(observer); |
227 EXPECT_EQ(static_cast<size_t>(0), observers().size()); | 205 EXPECT_EQ(static_cast<size_t>(0), observers().size()); |
228 } | 206 } |
229 }; | 207 }; |
230 | 208 |
231 class FakeSecureChannelFactory : public cryptauth::SecureChannel::Factory { | 209 class FakeSecureChannelFactory : public cryptauth::SecureChannel::Factory { |
232 public: | 210 public: |
233 FakeSecureChannelFactory() {} | 211 FakeSecureChannelFactory() {} |
234 | 212 |
235 void SetExpectedDeviceAddress(const std::string& expected_device_address) { | 213 void SetExpectedDeviceAddress(const std::string& expected_device_address) { |
236 expected_device_address_ = expected_device_address; | 214 expected_device_address_ = expected_device_address; |
237 } | 215 } |
238 | 216 |
239 std::unique_ptr<cryptauth::SecureChannel> BuildInstance( | 217 std::unique_ptr<cryptauth::SecureChannel> BuildInstance( |
240 std::unique_ptr<cryptauth::Connection> connection, | 218 std::unique_ptr<cryptauth::Connection> connection, |
241 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate) override { | 219 cryptauth::CryptAuthService* cryptauth_service) override { |
242 FakeConnectionWithAddress* fake_connection = | 220 FakeConnectionWithAddress* fake_connection = |
243 static_cast<FakeConnectionWithAddress*>(connection.get()); | 221 static_cast<FakeConnectionWithAddress*>(connection.get()); |
244 EXPECT_EQ(expected_device_address_, fake_connection->GetDeviceAddress()); | 222 EXPECT_EQ(expected_device_address_, fake_connection->GetDeviceAddress()); |
245 return base::WrapUnique( | 223 return base::WrapUnique( |
246 new FakeSecureChannel(std::move(connection), std::move(delegate))); | 224 new FakeSecureChannel(std::move(connection), cryptauth_service)); |
247 } | 225 } |
248 | 226 |
249 private: | 227 private: |
250 std::string expected_device_address_; | 228 std::string expected_device_address_; |
251 }; | 229 }; |
252 | 230 |
253 class MockTimerFactory : public BleConnectionManager::TimerFactory { | 231 class MockTimerFactory : public BleConnectionManager::TimerFactory { |
254 public: | 232 public: |
255 std::unique_ptr<base::Timer> CreateTimer() override { | 233 std::unique_ptr<base::Timer> CreateTimer() override { |
256 return base::MakeUnique<base::MockTimer>(false /* retains_user_task */, | 234 return base::MakeUnique<base::MockTimer>(false /* retains_user_task */, |
257 false /* is_repeating */); | 235 false /* is_repeating */); |
258 } | 236 } |
259 }; | 237 }; |
260 | 238 |
261 BleConnectionManagerTest() : test_devices_(CreateTestDevices(4)) { | 239 BleConnectionManagerTest() : test_devices_(CreateTestDevices(4)) { |
262 // These tests assume a maximum of two concurrent advertisers. Some of the | 240 // These tests assume a maximum of two concurrent advertisers. Some of the |
263 // multi-device tests would need to be re-written if this constant changes. | 241 // multi-device tests would need to be re-written if this constant changes. |
264 EXPECT_EQ(2, kMaxConcurrentAdvertisements); | 242 EXPECT_EQ(2, kMaxConcurrentAdvertisements); |
265 } | 243 } |
266 | 244 |
267 void SetUp() override { | 245 void SetUp() override { |
268 verified_status_changes_.clear(); | 246 verified_status_changes_.clear(); |
269 verified_received_messages_.clear(); | 247 verified_received_messages_.clear(); |
270 | 248 |
271 delegate_ = new TestDelegate(); | 249 fake_cryptauth_service_ = |
| 250 base::MakeUnique<cryptauth::FakeCryptAuthService>(); |
272 mock_adapter_ = | 251 mock_adapter_ = |
273 make_scoped_refptr(new NiceMock<device::MockBluetoothAdapter>()); | 252 make_scoped_refptr(new NiceMock<device::MockBluetoothAdapter>()); |
274 | 253 |
275 mock_ble_scanner_ = new MockBleScanner(); | 254 mock_ble_scanner_ = new MockBleScanner(); |
276 ON_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(_)) | 255 ON_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(_)) |
277 .WillByDefault(Return(true)); | 256 .WillByDefault(Return(true)); |
278 ON_CALL(*mock_ble_scanner_, UnregisterScanFilterForDevice(_)) | 257 ON_CALL(*mock_ble_scanner_, UnregisterScanFilterForDevice(_)) |
279 .WillByDefault(Return(true)); | 258 .WillByDefault(Return(true)); |
280 | 259 |
281 mock_ble_advertiser_ = new MockBleAdvertiser(); | 260 mock_ble_advertiser_ = new MockBleAdvertiser(); |
(...skipping 11 matching lines...) Expand all Loading... |
293 mock_bluetooth_throttler_.get())); | 272 mock_bluetooth_throttler_.get())); |
294 cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory:: | 273 cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory:: |
295 SetInstanceForTesting(std::move(fake_connection_factory_)); | 274 SetInstanceForTesting(std::move(fake_connection_factory_)); |
296 | 275 |
297 fake_secure_channel_factory_ = | 276 fake_secure_channel_factory_ = |
298 base::WrapUnique(new FakeSecureChannelFactory()); | 277 base::WrapUnique(new FakeSecureChannelFactory()); |
299 cryptauth::SecureChannel::Factory::SetInstanceForTesting( | 278 cryptauth::SecureChannel::Factory::SetInstanceForTesting( |
300 fake_secure_channel_factory_.get()); | 279 fake_secure_channel_factory_.get()); |
301 | 280 |
302 manager_ = base::WrapUnique(new BleConnectionManager( | 281 manager_ = base::WrapUnique(new BleConnectionManager( |
303 base::WrapUnique(delegate_), mock_adapter_, | 282 fake_cryptauth_service_.get(), mock_adapter_, |
304 base::WrapUnique(mock_ble_scanner_), | 283 base::WrapUnique(mock_ble_scanner_), |
305 base::WrapUnique(mock_ble_advertiser_), base::WrapUnique(device_queue_), | 284 base::WrapUnique(mock_ble_advertiser_), base::WrapUnique(device_queue_), |
306 base::WrapUnique(mock_timer_factory_), | 285 base::WrapUnique(mock_timer_factory_), |
307 mock_bluetooth_throttler_.get())); | 286 mock_bluetooth_throttler_.get())); |
308 test_observer_ = base::WrapUnique(new TestObserver()); | 287 test_observer_ = base::WrapUnique(new TestObserver()); |
309 manager_->AddObserver(test_observer_.get()); | 288 manager_->AddObserver(test_observer_.get()); |
310 } | 289 } |
311 | 290 |
312 void TearDown() override { | 291 void TearDown() override { |
313 // All state changes should have already been verified. This ensures that | 292 // All state changes should have already been verified. This ensures that |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 size_t expected_size) { | 455 size_t expected_size) { |
477 ASSERT_EQ(expected_size, channel->sent_messages().size()); | 456 ASSERT_EQ(expected_size, channel->sent_messages().size()); |
478 cryptauth::FakeSecureChannel::SentMessage sent_message = | 457 cryptauth::FakeSecureChannel::SentMessage sent_message = |
479 channel->sent_messages()[expected_size - 1]; | 458 channel->sent_messages()[expected_size - 1]; |
480 EXPECT_EQ(std::string(kTetherFeature), sent_message.feature); | 459 EXPECT_EQ(std::string(kTetherFeature), sent_message.feature); |
481 EXPECT_EQ(payload, sent_message.payload); | 460 EXPECT_EQ(payload, sent_message.payload); |
482 } | 461 } |
483 | 462 |
484 const std::vector<cryptauth::RemoteDevice> test_devices_; | 463 const std::vector<cryptauth::RemoteDevice> test_devices_; |
485 | 464 |
486 BleConnectionManager::Delegate* delegate_; | 465 std::unique_ptr<cryptauth::FakeCryptAuthService> fake_cryptauth_service_; |
487 scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_adapter_; | 466 scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_adapter_; |
488 MockBleScanner* mock_ble_scanner_; | 467 MockBleScanner* mock_ble_scanner_; |
489 MockBleAdvertiser* mock_ble_advertiser_; | 468 MockBleAdvertiser* mock_ble_advertiser_; |
490 BleAdvertisementDeviceQueue* device_queue_; | 469 BleAdvertisementDeviceQueue* device_queue_; |
491 MockTimerFactory* mock_timer_factory_; | 470 MockTimerFactory* mock_timer_factory_; |
492 std::unique_ptr<MockBluetoothThrottler> mock_bluetooth_throttler_; | 471 std::unique_ptr<MockBluetoothThrottler> mock_bluetooth_throttler_; |
493 std::unique_ptr<FakeConnectionFactory> fake_connection_factory_; | 472 std::unique_ptr<FakeConnectionFactory> fake_connection_factory_; |
494 std::unique_ptr<FakeSecureChannelFactory> fake_secure_channel_factory_; | 473 std::unique_ptr<FakeSecureChannelFactory> fake_secure_channel_factory_; |
495 std::unique_ptr<TestObserver> test_observer_; | 474 std::unique_ptr<TestObserver> test_observer_; |
496 | 475 |
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1194 cryptauth::SecureChannel::Status::DISCONNECTED}, | 1173 cryptauth::SecureChannel::Status::DISCONNECTED}, |
1195 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, | 1174 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
1196 cryptauth::SecureChannel::Status::DISCONNECTED}, | 1175 cryptauth::SecureChannel::Status::DISCONNECTED}, |
1197 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING, | 1176 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING, |
1198 cryptauth::SecureChannel::Status::DISCONNECTED}}); | 1177 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
1199 } | 1178 } |
1200 | 1179 |
1201 } // namespace tether | 1180 } // namespace tether |
1202 | 1181 |
1203 } // namespace cryptauth | 1182 } // namespace cryptauth |
OLD | NEW |