OLD | NEW |
| (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 "components/proximity_auth/ble/bluetooth_low_energy_weave_client_connec
tion.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/test/test_simple_task_runner.h" | |
17 #include "components/cryptauth/remote_device.h" | |
18 #include "components/proximity_auth/bluetooth_throttler.h" | |
19 #include "components/proximity_auth/connection_finder.h" | |
20 #include "components/proximity_auth/connection_observer.h" | |
21 #include "components/proximity_auth/proximity_auth_test_util.h" | |
22 #include "components/proximity_auth/wire_message.h" | |
23 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
24 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
25 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
26 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | |
27 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | |
28 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
29 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | |
30 #include "testing/gmock/include/gmock/gmock.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | |
32 | |
33 using testing::_; | |
34 using testing::AtLeast; | |
35 using testing::NiceMock; | |
36 using testing::Return; | |
37 using testing::StrictMock; | |
38 using testing::SaveArg; | |
39 | |
40 namespace proximity_auth { | |
41 namespace { | |
42 | |
43 class MockBluetoothThrottler : public BluetoothThrottler { | |
44 public: | |
45 MockBluetoothThrottler() {} | |
46 ~MockBluetoothThrottler() override {} | |
47 | |
48 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | |
49 MOCK_METHOD1(OnConnection, void(Connection* connection)); | |
50 | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | |
53 }; | |
54 | |
55 class MockBluetoothLowEnergyCharacteristicsFinder | |
56 : public BluetoothLowEnergyCharacteristicsFinder { | |
57 public: | |
58 MockBluetoothLowEnergyCharacteristicsFinder() {} | |
59 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} | |
60 | |
61 private: | |
62 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); | |
63 }; | |
64 | |
65 class MockConnectionObserver : public ConnectionObserver { | |
66 public: | |
67 MockConnectionObserver() : num_send_completed_(0) {} | |
68 | |
69 void OnConnectionStatusChanged(Connection* connection, | |
70 Connection::Status old_status, | |
71 Connection::Status new_status) override {} | |
72 | |
73 void OnMessageReceived(const Connection& connection, | |
74 const WireMessage& message) override {} | |
75 | |
76 void OnSendCompleted(const Connection& conenction, | |
77 const WireMessage& message, | |
78 bool success) override { | |
79 last_deserialized_message_ = message.payload(); | |
80 last_send_success_ = success; | |
81 num_send_completed_++; | |
82 } | |
83 | |
84 std::string GetLastDeserializedMessage() { | |
85 return last_deserialized_message_; | |
86 } | |
87 | |
88 bool GetLastSendSuccess() { return last_send_success_; } | |
89 | |
90 int GetNumSendCompleted() { return num_send_completed_; } | |
91 | |
92 private: | |
93 std::string last_deserialized_message_; | |
94 bool last_send_success_; | |
95 int num_send_completed_; | |
96 }; | |
97 | |
98 } // namespace | |
99 | |
100 namespace weave { | |
101 namespace { | |
102 | |
103 typedef BluetoothLowEnergyWeaveClientConnection::SubStatus SubStatus; | |
104 typedef BluetoothLowEnergyWeavePacketReceiver::State ReceiverState; | |
105 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverError ReceiverError; | |
106 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverType ReceiverType; | |
107 | |
108 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | |
109 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | |
110 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | |
111 | |
112 const char kServiceID[] = "service id"; | |
113 const char kTXCharacteristicID[] = "TX characteristic id"; | |
114 const char kRXCharacteristicID[] = "RX characteristic id"; | |
115 | |
116 const device::BluetoothRemoteGattCharacteristic::Properties | |
117 kCharacteristicProperties = | |
118 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | | |
119 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | | |
120 device::BluetoothRemoteGattCharacteristic:: | |
121 PROPERTY_WRITE_WITHOUT_RESPONSE | | |
122 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; | |
123 | |
124 const int kMaxNumberOfTries = 3; | |
125 const uint16_t kLargeMaxPacketSize = 30; | |
126 | |
127 const uint8_t kDataHeader = 0; | |
128 const uint8_t kConnectionRequestHeader = 1; | |
129 const uint8_t kSmallConnectionResponseHeader = 2; | |
130 const uint8_t kLargeConnectionResponseHeader = 3; | |
131 const uint8_t kConnectionCloseHeader = 4; | |
132 const uint8_t kErroneousHeader = 5; | |
133 | |
134 const std::string kSmallMessage = "bb"; | |
135 const std::string kLargeMessage = "aaabbb"; | |
136 const std::string kLargeMessage0 = "aaa"; | |
137 const std::string kLargeMessage1 = "bbb"; | |
138 | |
139 const Packet kConnectionRequest{kConnectionRequestHeader}; | |
140 const Packet kSmallConnectionResponse{kSmallConnectionResponseHeader}; | |
141 const Packet kLargeConnectionResponse{kLargeConnectionResponseHeader}; | |
142 const Packet kConnectionCloseSuccess{kConnectionCloseHeader, | |
143 ReasonForClose::CLOSE_WITHOUT_ERROR}; | |
144 const Packet kConnectionCloseUnknownError{kConnectionCloseHeader, | |
145 ReasonForClose::UNKNOWN_ERROR}; | |
146 const Packet kConnectionCloseApplicationError{ | |
147 kConnectionCloseHeader, ReasonForClose::APPLICATION_ERROR}; | |
148 | |
149 const Packet kSmallPackets0 = Packet{kDataHeader, 'b', 'b'}; | |
150 const Packet kLargePackets0 = Packet{kDataHeader, 'a', 'a', 'a'}; | |
151 const Packet kLargePackets1 = Packet{kDataHeader, 'b', 'b', 'b'}; | |
152 const Packet kErroneousPacket = Packet{kErroneousHeader}; | |
153 | |
154 const std::vector<Packet> kSmallPackets{kSmallPackets0}; | |
155 const std::vector<Packet> kLargePackets{kLargePackets0, kLargePackets1}; | |
156 | |
157 class MockBluetoothLowEnergyWeavePacketGenerator | |
158 : public BluetoothLowEnergyWeavePacketGenerator { | |
159 public: | |
160 MockBluetoothLowEnergyWeavePacketGenerator() | |
161 : max_packet_size_(kDefaultMaxPacketSize) {} | |
162 | |
163 Packet CreateConnectionRequest() override { return kConnectionRequest; } | |
164 | |
165 Packet CreateConnectionResponse() override { | |
166 NOTIMPLEMENTED(); | |
167 return Packet(); | |
168 } | |
169 | |
170 Packet CreateConnectionClose(ReasonForClose reason_for_close) override { | |
171 return Packet{kConnectionCloseHeader, | |
172 static_cast<uint8_t>(reason_for_close)}; | |
173 } | |
174 | |
175 void SetMaxPacketSize(uint16_t size) override { max_packet_size_ = size; } | |
176 | |
177 std::vector<Packet> EncodeDataMessage(std::string message) override { | |
178 if (message == kSmallMessage && max_packet_size_ == kDefaultMaxPacketSize) { | |
179 return kSmallPackets; | |
180 } else if (message == kLargeMessage && | |
181 max_packet_size_ == kLargeMaxPacketSize) { | |
182 return kLargePackets; | |
183 } else { | |
184 NOTREACHED(); | |
185 return std::vector<Packet>(); | |
186 } | |
187 } | |
188 | |
189 uint16_t GetMaxPacketSize() { return max_packet_size_; } | |
190 | |
191 private: | |
192 uint16_t max_packet_size_; | |
193 }; | |
194 | |
195 class MockBluetoothLowEnergyWeavePacketReceiver | |
196 : public BluetoothLowEnergyWeavePacketReceiver { | |
197 public: | |
198 MockBluetoothLowEnergyWeavePacketReceiver() | |
199 : BluetoothLowEnergyWeavePacketReceiver(ReceiverType::CLIENT), | |
200 state_(State::CONNECTING), | |
201 max_packet_size_(kDefaultMaxPacketSize), | |
202 reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR), | |
203 reason_to_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {} | |
204 | |
205 ReceiverState GetState() override { return state_; } | |
206 | |
207 uint16_t GetMaxPacketSize() override { return max_packet_size_; } | |
208 | |
209 ReasonForClose GetReasonForClose() override { return reason_for_close_; } | |
210 | |
211 ReasonForClose GetReasonToClose() override { return reason_to_close_; } | |
212 | |
213 std::string GetDataMessage() override { | |
214 if (max_packet_size_ == kDefaultMaxPacketSize) { | |
215 return kSmallMessage; | |
216 } else { | |
217 return kLargeMessage; | |
218 } | |
219 } | |
220 | |
221 ReceiverError GetReceiverError() override { | |
222 return ReceiverError::NO_ERROR_DETECTED; | |
223 } | |
224 | |
225 ReceiverState ReceivePacket(const Packet& packet) override { | |
226 switch (packet[0]) { | |
227 case kSmallConnectionResponseHeader: | |
228 max_packet_size_ = kDefaultMaxPacketSize; | |
229 state_ = ReceiverState::WAITING; | |
230 break; | |
231 case kLargeConnectionResponseHeader: | |
232 max_packet_size_ = kLargeMaxPacketSize; | |
233 state_ = ReceiverState::WAITING; | |
234 break; | |
235 case kConnectionCloseHeader: | |
236 state_ = ReceiverState::CONNECTION_CLOSED; | |
237 reason_for_close_ = static_cast<ReasonForClose>(packet[1]); | |
238 break; | |
239 case kDataHeader: | |
240 if (packet == kSmallPackets0 || packet == kLargePackets1) { | |
241 state_ = ReceiverState::DATA_READY; | |
242 } else { | |
243 state_ = ReceiverState::RECEIVING_DATA; | |
244 } | |
245 break; | |
246 default: | |
247 reason_to_close_ = ReasonForClose::APPLICATION_ERROR; | |
248 state_ = ReceiverState::ERROR_DETECTED; | |
249 } | |
250 return state_; | |
251 } | |
252 | |
253 private: | |
254 ReceiverState state_; | |
255 uint16_t max_packet_size_; | |
256 ReasonForClose reason_for_close_; | |
257 ReasonForClose reason_to_close_; | |
258 }; | |
259 | |
260 class MockBluetoothLowEnergyWeavePacketGeneratorFactory | |
261 : public BluetoothLowEnergyWeavePacketGenerator::Factory { | |
262 public: | |
263 // most_recent_instance_ will be obsolete after the connection class | |
264 // destructs. Do not use if that's the case. | |
265 MockBluetoothLowEnergyWeavePacketGenerator* GetMostRecentInstance() { | |
266 return most_recent_instance_; | |
267 } | |
268 | |
269 private: | |
270 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> BuildInstance() | |
271 override { | |
272 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketGenerator(); | |
273 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>( | |
274 most_recent_instance_); | |
275 } | |
276 | |
277 MockBluetoothLowEnergyWeavePacketGenerator* most_recent_instance_; | |
278 }; | |
279 | |
280 class MockBluetoothLowEnergyWeavePacketReceiverFactory | |
281 : public BluetoothLowEnergyWeavePacketReceiver::Factory { | |
282 public: | |
283 // most_recent_instance_ will be obsolete after the connection class | |
284 // destructs. Do not use if that's the case. | |
285 MockBluetoothLowEnergyWeavePacketReceiver* GetMostRecentInstance() { | |
286 return most_recent_instance_; | |
287 } | |
288 | |
289 private: | |
290 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> BuildInstance( | |
291 ReceiverType receiver_type) override { | |
292 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketReceiver(); | |
293 return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( | |
294 most_recent_instance_); | |
295 } | |
296 | |
297 MockBluetoothLowEnergyWeavePacketReceiver* most_recent_instance_; | |
298 }; | |
299 | |
300 class TestBluetoothLowEnergyWeaveClientConnection | |
301 : public BluetoothLowEnergyWeaveClientConnection { | |
302 public: | |
303 TestBluetoothLowEnergyWeaveClientConnection( | |
304 const cryptauth::RemoteDevice& remote_device, | |
305 scoped_refptr<device::BluetoothAdapter> adapter, | |
306 const device::BluetoothUUID remote_service_uuid, | |
307 BluetoothThrottler* bluetooth_throttler, | |
308 int max_number_of_write_attempts) | |
309 : BluetoothLowEnergyWeaveClientConnection(remote_device, | |
310 adapter, | |
311 remote_service_uuid, | |
312 bluetooth_throttler, | |
313 max_number_of_write_attempts) {} | |
314 | |
315 ~TestBluetoothLowEnergyWeaveClientConnection() override {} | |
316 | |
317 MOCK_METHOD2( | |
318 CreateCharacteristicsFinder, | |
319 BluetoothLowEnergyCharacteristicsFinder*( | |
320 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | |
321 success, | |
322 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); | |
323 | |
324 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | |
325 | |
326 // Exposing inherited protected methods for testing. | |
327 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged; | |
328 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting; | |
329 using BluetoothLowEnergyWeaveClientConnection::DestroyConnection; | |
330 | |
331 // Exposing inherited protected fields for testing. | |
332 using BluetoothLowEnergyWeaveClientConnection::status; | |
333 using BluetoothLowEnergyWeaveClientConnection::sub_status; | |
334 | |
335 private: | |
336 DISALLOW_COPY_AND_ASSIGN(TestBluetoothLowEnergyWeaveClientConnection); | |
337 }; | |
338 | |
339 } // namespace | |
340 | |
341 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest | |
342 : public testing::Test { | |
343 public: | |
344 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest() | |
345 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | |
346 remote_device_(CreateLERemoteDeviceForTest()), | |
347 service_uuid_(device::BluetoothUUID(kServiceUUID)), | |
348 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)), | |
349 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)), | |
350 notify_session_alias_(NULL), | |
351 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), | |
352 task_runner_(new base::TestSimpleTaskRunner), | |
353 last_completed_wire_message_(""), | |
354 generator_factory_( | |
355 new MockBluetoothLowEnergyWeavePacketGeneratorFactory()), | |
356 receiver_factory_( | |
357 new MockBluetoothLowEnergyWeavePacketReceiverFactory()) { | |
358 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( | |
359 generator_factory_); | |
360 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
361 receiver_factory_); | |
362 } | |
363 | |
364 ~ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest() override { | |
365 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( | |
366 nullptr); | |
367 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
368 nullptr); | |
369 } | |
370 | |
371 void SetUp() override { | |
372 device_ = base::MakeUnique<NiceMock<device::MockBluetoothDevice>>( | |
373 adapter_.get(), 0, kTestRemoteDeviceName, | |
374 kTestRemoteDeviceBluetoothAddress, false, false); | |
375 | |
376 service_ = base::MakeUnique<NiceMock<device::MockBluetoothGattService>>( | |
377 device_.get(), kServiceID, service_uuid_, true, false); | |
378 tx_characteristic_ = | |
379 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( | |
380 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false, | |
381 kCharacteristicProperties, | |
382 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); | |
383 | |
384 rx_characteristic_ = | |
385 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( | |
386 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false, | |
387 kCharacteristicProperties, | |
388 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); | |
389 | |
390 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
391 | |
392 std::vector<const device::BluetoothDevice*> devices; | |
393 devices.push_back(device_.get()); | |
394 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
395 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | |
396 .WillByDefault(Return(device_.get())); | |
397 ON_CALL(*device_, GetGattService(kServiceID)) | |
398 .WillByDefault(Return(service_.get())); | |
399 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID)) | |
400 .WillByDefault(Return(rx_characteristic_.get())); | |
401 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID)) | |
402 .WillByDefault(Return(tx_characteristic_.get())); | |
403 } | |
404 | |
405 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in | |
406 // DISCONNECTED state. | |
407 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> | |
408 CreateConnection() { | |
409 EXPECT_CALL(*adapter_, AddObserver(_)); | |
410 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
411 | |
412 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
413 new TestBluetoothLowEnergyWeaveClientConnection( | |
414 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
415 kMaxNumberOfTries)); | |
416 | |
417 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
418 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
419 | |
420 // Add the mock observer to observe on OnDidMessageSend. | |
421 connection->AddObserver(&connection_observer_); | |
422 | |
423 connection->SetTaskRunnerForTesting(task_runner_); | |
424 | |
425 return connection; | |
426 } | |
427 | |
428 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS | |
429 // state, without an existing GATT connection. | |
430 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
431 // Preparing |connection| for a CreateGattConnection call. | |
432 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
433 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
434 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
435 | |
436 // No throttling by default | |
437 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
438 .WillOnce(Return(base::TimeDelta())); | |
439 | |
440 connection->Connect(); | |
441 | |
442 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
443 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
444 | |
445 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
446 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
447 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
448 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
449 .WillOnce(DoAll( | |
450 SaveArg<0>(&characteristics_finder_success_callback_), | |
451 SaveArg<1>(&characteristics_finder_error_callback_), | |
452 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
453 | |
454 create_gatt_connection_success_callback_.Run( | |
455 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( | |
456 adapter_, kTestRemoteDeviceBluetoothAddress)); | |
457 | |
458 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS); | |
459 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
460 } | |
461 | |
462 // Transitions |connection| from WAITING_CHARACTERISTICS to | |
463 // WAITING_NOTIFY_SESSION state. | |
464 void CharacteristicsFound( | |
465 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
466 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)) | |
467 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), | |
468 SaveArg<1>(¬ify_session_error_callback_))); | |
469 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); | |
470 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); | |
471 | |
472 characteristics_finder_success_callback_.Run( | |
473 {service_uuid_, kServiceID}, | |
474 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
475 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
476 | |
477 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION); | |
478 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
479 } | |
480 | |
481 // Transitions |connection| from WAITING_NOTIFY_SESSION to | |
482 // WAITING_CONNECTION_RESPONSE state. | |
483 void NotifySessionStarted( | |
484 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
485 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
486 .WillOnce( | |
487 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
488 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
489 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
490 EXPECT_FALSE(notify_session_error_callback_.is_null()); | |
491 ASSERT_FALSE(notify_session_success_callback_.is_null()); | |
492 | |
493 // Store an alias for the notify session passed |connection|. | |
494 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( | |
495 new NiceMock<device::MockBluetoothGattNotifySession>( | |
496 tx_characteristic_->GetWeakPtr())); | |
497 notify_session_alias_ = notify_session.get(); | |
498 | |
499 notify_session_success_callback_.Run(std::move(notify_session)); | |
500 task_runner_->RunUntilIdle(); | |
501 | |
502 // Written value contains only the mock Connection Request. | |
503 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
504 | |
505 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE); | |
506 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
507 } | |
508 | |
509 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED. | |
510 void ConnectionResponseReceived( | |
511 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
512 uint16_t selected_packet_size) { | |
513 // Written value contains only the mock Connection Request. | |
514 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
515 | |
516 // OnDidSendMessage is not called. | |
517 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
518 | |
519 RunWriteCharacteristicSuccessCallback(); | |
520 | |
521 // Received Connection Response. | |
522 if (selected_packet_size == kDefaultMaxPacketSize) { | |
523 connection->GattCharacteristicValueChanged( | |
524 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse); | |
525 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetMaxPacketSize(), | |
526 kDefaultMaxPacketSize); | |
527 EXPECT_EQ(generator_factory_->GetMostRecentInstance()->GetMaxPacketSize(), | |
528 kDefaultMaxPacketSize); | |
529 } else if (selected_packet_size == kLargeMaxPacketSize) { | |
530 connection->GattCharacteristicValueChanged( | |
531 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse); | |
532 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetMaxPacketSize(), | |
533 kLargeMaxPacketSize); | |
534 EXPECT_EQ(generator_factory_->GetMostRecentInstance()->GetMaxPacketSize(), | |
535 kLargeMaxPacketSize); | |
536 } else { | |
537 NOTREACHED(); | |
538 } | |
539 | |
540 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
541 EXPECT_EQ(connection->status(), Connection::CONNECTED); | |
542 } | |
543 | |
544 // Transitions |connection| to a DISCONNECTED state regardless of its initial | |
545 // state. | |
546 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
547 // A notify session was previously set. | |
548 if (notify_session_alias_) | |
549 EXPECT_CALL(*notify_session_alias_, Stop(_)); | |
550 | |
551 if (connection->sub_status() == SubStatus::CONNECTED) { | |
552 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
553 .WillOnce( | |
554 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
555 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
556 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
557 } | |
558 | |
559 connection->Disconnect(); | |
560 | |
561 if (connection->sub_status() == SubStatus::CONNECTED) { | |
562 connection->DestroyConnection(); | |
563 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
564 kConnectionCloseSuccess); | |
565 RunWriteCharacteristicSuccessCallback(); | |
566 } | |
567 | |
568 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
569 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
570 } | |
571 | |
572 void InitializeConnection( | |
573 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
574 uint32_t selected_packet_size) { | |
575 ConnectGatt(connection); | |
576 CharacteristicsFound(connection); | |
577 NotifySessionStarted(connection); | |
578 ConnectionResponseReceived(connection, selected_packet_size); | |
579 } | |
580 | |
581 void RunWriteCharacteristicSuccessCallback() { | |
582 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
583 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
584 write_remote_characteristic_success_callback_.Run(); | |
585 } | |
586 | |
587 protected: | |
588 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
589 cryptauth::RemoteDevice remote_device_; | |
590 device::BluetoothUUID service_uuid_; | |
591 device::BluetoothUUID tx_characteristic_uuid_; | |
592 device::BluetoothUUID rx_characteristic_uuid_; | |
593 std::unique_ptr<device::MockBluetoothDevice> device_; | |
594 std::unique_ptr<device::MockBluetoothGattService> service_; | |
595 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_; | |
596 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_; | |
597 std::vector<uint8_t> last_value_written_on_tx_characteristic_; | |
598 device::MockBluetoothGattNotifySession* notify_session_alias_; | |
599 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | |
600 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
601 base::MessageLoop message_loop_; | |
602 WireMessage last_completed_wire_message_; | |
603 bool last_wire_message_success_; | |
604 std::shared_ptr<MockBluetoothLowEnergyWeavePacketGeneratorFactory> | |
605 generator_factory_; | |
606 std::shared_ptr<MockBluetoothLowEnergyWeavePacketReceiverFactory> | |
607 receiver_factory_; | |
608 MockConnectionObserver connection_observer_; | |
609 | |
610 // Callbacks | |
611 device::BluetoothDevice::GattConnectionCallback | |
612 create_gatt_connection_success_callback_; | |
613 device::BluetoothDevice::ConnectErrorCallback | |
614 create_gatt_connection_error_callback_; | |
615 | |
616 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | |
617 characteristics_finder_success_callback_; | |
618 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | |
619 characteristics_finder_error_callback_; | |
620 | |
621 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | |
622 notify_session_success_callback_; | |
623 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
624 notify_session_error_callback_; | |
625 | |
626 base::Closure write_remote_characteristic_success_callback_; | |
627 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
628 write_remote_characteristic_error_callback_; | |
629 }; | |
630 | |
631 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
632 CreateAndDestroyWithoutConnectCallDoesntCrash) { | |
633 BluetoothLowEnergyWeaveClientConnection connection( | |
634 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
635 kMaxNumberOfTries); | |
636 } | |
637 | |
638 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
639 DisconnectWithoutConnectDoesntCrash) { | |
640 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
641 CreateConnection()); | |
642 Disconnect(connection.get()); | |
643 } | |
644 | |
645 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
646 ConnectSuccess) { | |
647 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
648 CreateConnection()); | |
649 ConnectGatt(connection.get()); | |
650 CharacteristicsFound(connection.get()); | |
651 NotifySessionStarted(connection.get()); | |
652 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
653 } | |
654 | |
655 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
656 ConnectSuccessDisconnect) { | |
657 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
658 CreateConnection()); | |
659 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
660 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
661 Disconnect(connection.get()); | |
662 } | |
663 | |
664 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
665 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) { | |
666 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
667 CreateConnection()); | |
668 ConnectGatt(connection.get()); | |
669 Disconnect(connection.get()); | |
670 } | |
671 | |
672 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
673 ConnectIncompleteDisconnectFromWaitingNotifySessionState) { | |
674 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
675 CreateConnection()); | |
676 ConnectGatt(connection.get()); | |
677 CharacteristicsFound(connection.get()); | |
678 Disconnect(connection.get()); | |
679 } | |
680 | |
681 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
682 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) { | |
683 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
684 CreateConnection()); | |
685 ConnectGatt(connection.get()); | |
686 CharacteristicsFound(connection.get()); | |
687 NotifySessionStarted(connection.get()); | |
688 Disconnect(connection.get()); | |
689 } | |
690 | |
691 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
692 ConnectFailsCharacteristicsNotFound) { | |
693 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
694 CreateConnection()); | |
695 ConnectGatt(connection.get()); | |
696 | |
697 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0); | |
698 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | |
699 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | |
700 | |
701 characteristics_finder_error_callback_.Run( | |
702 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
703 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
704 | |
705 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
706 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
707 } | |
708 | |
709 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
710 ConnectFailsNotifySessionError) { | |
711 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
712 CreateConnection()); | |
713 ConnectGatt(connection.get()); | |
714 CharacteristicsFound(connection.get()); | |
715 | |
716 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0); | |
717 EXPECT_FALSE(notify_session_success_callback_.is_null()); | |
718 ASSERT_FALSE(notify_session_error_callback_.is_null()); | |
719 | |
720 notify_session_error_callback_.Run( | |
721 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
722 | |
723 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
724 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
725 } | |
726 | |
727 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
728 ConnectFailsErrorSendingConnectionRequest) { | |
729 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
730 CreateConnection()); | |
731 ConnectGatt(connection.get()); | |
732 CharacteristicsFound(connection.get()); | |
733 NotifySessionStarted(connection.get()); | |
734 | |
735 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | |
736 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | |
737 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | |
738 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | |
739 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
740 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
741 .Times(kMaxNumberOfTries - 1) | |
742 .WillRepeatedly( | |
743 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
744 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
745 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
746 | |
747 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
748 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
749 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
750 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
751 write_remote_characteristic_error_callback_.Run( | |
752 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
753 } | |
754 | |
755 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
756 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
757 } | |
758 | |
759 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
760 ReceiveMessageSmallerThanCharacteristicSize) { | |
761 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
762 CreateConnection()); | |
763 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
764 | |
765 std::string received_bytes; | |
766 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
767 .WillOnce(SaveArg<0>(&received_bytes)); | |
768 | |
769 connection->GattCharacteristicValueChanged( | |
770 adapter_.get(), rx_characteristic_.get(), kSmallPackets0); | |
771 | |
772 EXPECT_EQ(received_bytes, kSmallMessage); | |
773 } | |
774 | |
775 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
776 ReceiveMessageLargerThanCharacteristicSize) { | |
777 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
778 CreateConnection()); | |
779 | |
780 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
781 | |
782 std::string received_bytes; | |
783 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
784 .WillOnce(SaveArg<0>(&received_bytes)); | |
785 | |
786 std::vector<Packet> packets = kLargePackets; | |
787 | |
788 for (auto packet : packets) { | |
789 connection->GattCharacteristicValueChanged( | |
790 adapter_.get(), rx_characteristic_.get(), packet); | |
791 } | |
792 EXPECT_EQ(received_bytes, kLargeMessage); | |
793 } | |
794 | |
795 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
796 SendMessageSmallerThanCharacteristicSize) { | |
797 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
798 CreateConnection()); | |
799 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
800 | |
801 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
802 // called. | |
803 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
804 .WillOnce( | |
805 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
806 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
807 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
808 | |
809 connection->SendMessage(base::MakeUnique<FakeWireMessage>(kSmallMessage)); | |
810 | |
811 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); | |
812 | |
813 RunWriteCharacteristicSuccessCallback(); | |
814 | |
815 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
816 EXPECT_EQ(kSmallMessage, connection_observer_.GetLastDeserializedMessage()); | |
817 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); | |
818 } | |
819 | |
820 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
821 SendMessageLargerThanCharacteristicSize) { | |
822 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
823 CreateConnection()); | |
824 | |
825 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
826 | |
827 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
828 // called. | |
829 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
830 .WillOnce( | |
831 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
832 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
833 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
834 | |
835 connection->SendMessage(base::MakeUnique<FakeWireMessage>(kLargeMessage)); | |
836 | |
837 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); | |
838 std::vector<uint8_t> bytes_received( | |
839 last_value_written_on_tx_characteristic_.begin() + 1, | |
840 last_value_written_on_tx_characteristic_.end()); | |
841 | |
842 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
843 .WillOnce( | |
844 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
845 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
846 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
847 | |
848 RunWriteCharacteristicSuccessCallback(); | |
849 bytes_received.insert(bytes_received.end(), | |
850 last_value_written_on_tx_characteristic_.begin() + 1, | |
851 last_value_written_on_tx_characteristic_.end()); | |
852 | |
853 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end()); | |
854 EXPECT_EQ(expected, bytes_received); | |
855 | |
856 RunWriteCharacteristicSuccessCallback(); | |
857 | |
858 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
859 EXPECT_EQ(kLargeMessage, connection_observer_.GetLastDeserializedMessage()); | |
860 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); | |
861 } | |
862 | |
863 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
864 SendMessageKeepsFailing) { | |
865 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
866 CreateConnection()); | |
867 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
868 | |
869 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
870 .Times(kMaxNumberOfTries) | |
871 .WillRepeatedly( | |
872 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
873 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
874 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
875 | |
876 connection->SendMessage(base::MakeUnique<FakeWireMessage>(kSmallMessage)); | |
877 | |
878 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
879 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); | |
880 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
881 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
882 write_remote_characteristic_error_callback_.Run( | |
883 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
884 if (i == kMaxNumberOfTries - 1) { | |
885 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
886 EXPECT_EQ(kSmallMessage, | |
887 connection_observer_.GetLastDeserializedMessage()); | |
888 EXPECT_FALSE(connection_observer_.GetLastSendSuccess()); | |
889 } else { | |
890 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
891 } | |
892 } | |
893 | |
894 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
895 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
896 } | |
897 | |
898 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
899 ReceiveCloseConnectionTest) { | |
900 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
901 CreateConnection()); | |
902 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
903 | |
904 connection->GattCharacteristicValueChanged( | |
905 adapter_.get(), rx_characteristic_.get(), kConnectionCloseUnknownError); | |
906 | |
907 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonForClose(), | |
908 ReasonForClose::UNKNOWN_ERROR); | |
909 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
910 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
911 } | |
912 | |
913 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
914 ReceiverErrorTest) { | |
915 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
916 CreateConnection()); | |
917 | |
918 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
919 | |
920 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
921 .WillOnce( | |
922 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
923 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
924 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
925 | |
926 connection->GattCharacteristicValueChanged( | |
927 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); | |
928 | |
929 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
930 kConnectionCloseApplicationError); | |
931 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonToClose(), | |
932 ReasonForClose::APPLICATION_ERROR); | |
933 | |
934 RunWriteCharacteristicSuccessCallback(); | |
935 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
936 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
937 } | |
938 | |
939 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
940 ReceiverErrorWithPendingWritesTest) { | |
941 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
942 CreateConnection()); | |
943 | |
944 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
945 | |
946 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
947 .WillOnce( | |
948 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
949 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
950 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
951 | |
952 connection->SendMessage(base::MakeUnique<FakeWireMessage>(kLargeMessage)); | |
953 | |
954 connection->GattCharacteristicValueChanged( | |
955 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); | |
956 | |
957 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); | |
958 | |
959 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
960 .WillOnce( | |
961 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
962 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
963 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
964 | |
965 RunWriteCharacteristicSuccessCallback(); | |
966 | |
967 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
968 kConnectionCloseApplicationError); | |
969 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonToClose(), | |
970 ReasonForClose::APPLICATION_ERROR); | |
971 | |
972 RunWriteCharacteristicSuccessCallback(); | |
973 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
974 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
975 } | |
976 | |
977 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
978 WriteConnectionCloseMaxNumberOfTimes) { | |
979 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
980 CreateConnection()); | |
981 | |
982 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
983 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
984 | |
985 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
986 .WillOnce( | |
987 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
988 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
989 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
990 connection->Disconnect(); | |
991 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
992 | |
993 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
994 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
995 kConnectionCloseSuccess); | |
996 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
997 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
998 | |
999 if (i != kMaxNumberOfTries - 1) { | |
1000 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
1001 .WillOnce( | |
1002 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
1003 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
1004 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
1005 } | |
1006 | |
1007 write_remote_characteristic_error_callback_.Run( | |
1008 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
1009 } | |
1010 | |
1011 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
1012 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
1013 } | |
1014 | |
1015 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
1016 ConnectAfterADelayWhenThrottled) { | |
1017 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
1018 CreateConnection()); | |
1019 | |
1020 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
1021 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | |
1022 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
1023 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
1024 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
1025 | |
1026 // No GATT connection should be created before the delay. | |
1027 connection->Connect(); | |
1028 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
1029 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
1030 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | |
1031 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | |
1032 | |
1033 // A GATT connection should be created after the delay. | |
1034 task_runner_->RunUntilIdle(); | |
1035 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
1036 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
1037 | |
1038 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
1039 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
1040 .WillOnce(DoAll( | |
1041 SaveArg<0>(&characteristics_finder_success_callback_), | |
1042 SaveArg<1>(&characteristics_finder_error_callback_), | |
1043 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
1044 | |
1045 create_gatt_connection_success_callback_.Run( | |
1046 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( | |
1047 adapter_, kTestRemoteDeviceBluetoothAddress)); | |
1048 | |
1049 CharacteristicsFound(connection.get()); | |
1050 NotifySessionStarted(connection.get()); | |
1051 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
1052 } | |
1053 | |
1054 } // namespace weave | |
1055 | |
1056 } // namespace proximity_auth | |
OLD | NEW |