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/proximity_auth/bluetooth_throttler.h" | |
18 #include "components/proximity_auth/connection_finder.h" | |
19 #include "components/proximity_auth/connection_observer.h" | |
20 #include "components/proximity_auth/proximity_auth_test_util.h" | |
21 #include "components/proximity_auth/remote_device.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 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 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( | |
355 &generator_factory_); | |
356 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
357 &receiver_factory_); | |
358 } | |
359 | |
360 void SetUp() override { | |
361 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( | |
362 adapter_.get(), 0, kTestRemoteDeviceName, | |
363 kTestRemoteDeviceBluetoothAddress, false, false)); | |
364 | |
365 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( | |
366 device_.get(), kServiceID, service_uuid_, true, false)); | |
367 tx_characteristic_ = | |
368 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | |
369 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false, | |
370 kCharacteristicProperties, | |
371 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | |
372 | |
373 rx_characteristic_ = | |
374 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( | |
375 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false, | |
376 kCharacteristicProperties, | |
377 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | |
378 | |
379 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
380 | |
381 std::vector<const device::BluetoothDevice*> devices; | |
382 devices.push_back(device_.get()); | |
383 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
384 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | |
385 .WillByDefault(Return(device_.get())); | |
386 ON_CALL(*device_, GetGattService(kServiceID)) | |
387 .WillByDefault(Return(service_.get())); | |
388 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID)) | |
389 .WillByDefault(Return(rx_characteristic_.get())); | |
390 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID)) | |
391 .WillByDefault(Return(tx_characteristic_.get())); | |
392 } | |
393 | |
394 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in | |
395 // DISCONNECTED state. | |
396 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> | |
397 CreateConnection() { | |
398 EXPECT_CALL(*adapter_, AddObserver(_)); | |
399 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
400 | |
401 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
402 new TestBluetoothLowEnergyWeaveClientConnection( | |
403 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
404 kMaxNumberOfTries)); | |
405 | |
406 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
407 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
408 | |
409 // Add the mock observer to observe on OnDidMessageSend. | |
410 connection->AddObserver(&connection_observer_); | |
411 | |
412 connection->SetTaskRunnerForTesting(task_runner_); | |
413 | |
414 return connection; | |
415 } | |
416 | |
417 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS | |
418 // state, without an existing GATT connection. | |
419 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
420 // Preparing |connection| for a CreateGattConnection call. | |
421 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
422 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
423 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
424 | |
425 // No throttling by default | |
426 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
427 .WillOnce(Return(base::TimeDelta())); | |
428 | |
429 connection->Connect(); | |
430 | |
431 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
432 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
433 | |
434 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
435 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
436 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
437 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
438 .WillOnce(DoAll( | |
439 SaveArg<0>(&characteristics_finder_success_callback_), | |
440 SaveArg<1>(&characteristics_finder_error_callback_), | |
441 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
442 | |
443 create_gatt_connection_success_callback_.Run( | |
444 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | |
445 adapter_, kTestRemoteDeviceBluetoothAddress))); | |
446 | |
447 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS); | |
448 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
449 } | |
450 | |
451 // Transitions |connection| from WAITING_CHARACTERISTICS to | |
452 // WAITING_NOTIFY_SESSION state. | |
453 void CharacteristicsFound( | |
454 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
455 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)) | |
456 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), | |
457 SaveArg<1>(¬ify_session_error_callback_))); | |
458 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); | |
459 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); | |
460 | |
461 characteristics_finder_success_callback_.Run( | |
462 {service_uuid_, kServiceID}, | |
463 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
464 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
465 | |
466 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION); | |
467 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
468 } | |
469 | |
470 // Transitions |connection| from WAITING_NOTIFY_SESSION to | |
471 // WAITING_CONNECTION_RESPONSE state. | |
472 void NotifySessionStarted( | |
473 TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
474 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
475 .WillOnce( | |
476 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
477 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
478 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
479 EXPECT_FALSE(notify_session_error_callback_.is_null()); | |
480 ASSERT_FALSE(notify_session_success_callback_.is_null()); | |
481 | |
482 // Store an alias for the notify session passed |connection|. | |
483 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( | |
484 new NiceMock<device::MockBluetoothGattNotifySession>( | |
485 kTXCharacteristicID)); | |
486 notify_session_alias_ = notify_session.get(); | |
487 | |
488 notify_session_success_callback_.Run(std::move(notify_session)); | |
489 task_runner_->RunUntilIdle(); | |
490 | |
491 // Written value contains only the mock Connection Request. | |
492 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
493 | |
494 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE); | |
495 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
496 } | |
497 | |
498 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED. | |
499 void ConnectionResponseReceived( | |
500 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
501 uint16_t selected_packet_size) { | |
502 // Written value contains only the mock Connection Request. | |
503 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
504 | |
505 // OnDidSendMessage is not called. | |
506 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
507 | |
508 RunWriteCharacteristicSuccessCallback(); | |
509 | |
510 // Received Connection Response. | |
511 if (selected_packet_size == kDefaultMaxPacketSize) { | |
512 connection->GattCharacteristicValueChanged( | |
513 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse); | |
514 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
515 kDefaultMaxPacketSize); | |
516 EXPECT_EQ(generator_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
517 kDefaultMaxPacketSize); | |
518 } else if (selected_packet_size == kLargeMaxPacketSize) { | |
519 connection->GattCharacteristicValueChanged( | |
520 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse); | |
521 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
522 kLargeMaxPacketSize); | |
523 EXPECT_EQ(generator_factory_.GetMostRecentInstance()->GetMaxPacketSize(), | |
524 kLargeMaxPacketSize); | |
525 } else { | |
526 NOTREACHED(); | |
527 } | |
528 | |
529 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
530 EXPECT_EQ(connection->status(), Connection::CONNECTED); | |
531 } | |
532 | |
533 // Transitions |connection| to a DISCONNECTED state regardless of its initial | |
534 // state. | |
535 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) { | |
536 // A notify session was previously set. | |
537 if (notify_session_alias_) | |
538 EXPECT_CALL(*notify_session_alias_, Stop(_)); | |
539 | |
540 if (connection->sub_status() == SubStatus::CONNECTED) { | |
541 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
542 .WillOnce( | |
543 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
544 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
545 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
546 } | |
547 | |
548 connection->Disconnect(); | |
549 | |
550 if (connection->sub_status() == SubStatus::CONNECTED) { | |
551 connection->DestroyConnection(); | |
552 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
553 kConnectionCloseSuccess); | |
554 RunWriteCharacteristicSuccessCallback(); | |
555 } | |
556 | |
557 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
558 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
559 } | |
560 | |
561 void InitializeConnection( | |
562 TestBluetoothLowEnergyWeaveClientConnection* connection, | |
563 uint32_t selected_packet_size) { | |
564 ConnectGatt(connection); | |
565 CharacteristicsFound(connection); | |
566 NotifySessionStarted(connection); | |
567 ConnectionResponseReceived(connection, selected_packet_size); | |
568 } | |
569 | |
570 void RunWriteCharacteristicSuccessCallback() { | |
571 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
572 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
573 write_remote_characteristic_success_callback_.Run(); | |
574 } | |
575 | |
576 protected: | |
577 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
578 RemoteDevice remote_device_; | |
579 device::BluetoothUUID service_uuid_; | |
580 device::BluetoothUUID tx_characteristic_uuid_; | |
581 device::BluetoothUUID rx_characteristic_uuid_; | |
582 std::unique_ptr<device::MockBluetoothDevice> device_; | |
583 std::unique_ptr<device::MockBluetoothGattService> service_; | |
584 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_; | |
585 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_; | |
586 std::vector<uint8_t> last_value_written_on_tx_characteristic_; | |
587 device::MockBluetoothGattNotifySession* notify_session_alias_; | |
588 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | |
589 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
590 base::MessageLoop message_loop_; | |
591 WireMessage last_completed_wire_message_; | |
592 bool last_wire_message_success_; | |
593 MockBluetoothLowEnergyWeavePacketGeneratorFactory generator_factory_; | |
594 MockBluetoothLowEnergyWeavePacketReceiverFactory receiver_factory_; | |
595 MockConnectionObserver connection_observer_; | |
596 | |
597 // Callbacks | |
598 device::BluetoothDevice::GattConnectionCallback | |
599 create_gatt_connection_success_callback_; | |
600 device::BluetoothDevice::ConnectErrorCallback | |
601 create_gatt_connection_error_callback_; | |
602 | |
603 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | |
604 characteristics_finder_success_callback_; | |
605 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | |
606 characteristics_finder_error_callback_; | |
607 | |
608 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | |
609 notify_session_success_callback_; | |
610 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
611 notify_session_error_callback_; | |
612 | |
613 base::Closure write_remote_characteristic_success_callback_; | |
614 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
615 write_remote_characteristic_error_callback_; | |
616 }; | |
617 | |
618 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
619 CreateAndDestroyWithoutConnectCallDoesntCrash) { | |
620 BluetoothLowEnergyWeaveClientConnection connection( | |
621 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
622 kMaxNumberOfTries); | |
623 } | |
624 | |
625 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
626 DisconectWithoutConnectDoesntCrash) { | |
627 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
628 CreateConnection()); | |
629 Disconnect(connection.get()); | |
630 } | |
631 | |
632 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
633 ConnectSuccess) { | |
634 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
635 CreateConnection()); | |
636 ConnectGatt(connection.get()); | |
637 CharacteristicsFound(connection.get()); | |
638 NotifySessionStarted(connection.get()); | |
639 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
640 } | |
641 | |
642 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
643 ConnectSuccessDisconnect) { | |
644 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
645 CreateConnection()); | |
646 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
647 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
648 Disconnect(connection.get()); | |
649 } | |
650 | |
651 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
652 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) { | |
653 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
654 CreateConnection()); | |
655 ConnectGatt(connection.get()); | |
656 Disconnect(connection.get()); | |
657 } | |
658 | |
659 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
660 ConnectIncompleteDisconnectFromWaitingNotifySessionState) { | |
661 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
662 CreateConnection()); | |
663 ConnectGatt(connection.get()); | |
664 CharacteristicsFound(connection.get()); | |
665 Disconnect(connection.get()); | |
666 } | |
667 | |
668 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
669 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) { | |
670 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
671 CreateConnection()); | |
672 ConnectGatt(connection.get()); | |
673 CharacteristicsFound(connection.get()); | |
674 NotifySessionStarted(connection.get()); | |
675 Disconnect(connection.get()); | |
676 } | |
677 | |
678 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
679 ConnectFailsCharacteristicsNotFound) { | |
680 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
681 CreateConnection()); | |
682 ConnectGatt(connection.get()); | |
683 | |
684 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0); | |
685 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | |
686 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | |
687 | |
688 characteristics_finder_error_callback_.Run( | |
689 {tx_characteristic_uuid_, kTXCharacteristicID}, | |
690 {rx_characteristic_uuid_, kRXCharacteristicID}); | |
691 | |
692 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
693 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
694 } | |
695 | |
696 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
697 ConnectFailsNotifySessionError) { | |
698 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
699 CreateConnection()); | |
700 ConnectGatt(connection.get()); | |
701 CharacteristicsFound(connection.get()); | |
702 | |
703 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0); | |
704 EXPECT_FALSE(notify_session_success_callback_.is_null()); | |
705 ASSERT_FALSE(notify_session_error_callback_.is_null()); | |
706 | |
707 notify_session_error_callback_.Run( | |
708 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
709 | |
710 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
711 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
712 } | |
713 | |
714 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
715 ConnectFailsErrorSendingConnectionRequest) { | |
716 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
717 CreateConnection()); | |
718 ConnectGatt(connection.get()); | |
719 CharacteristicsFound(connection.get()); | |
720 NotifySessionStarted(connection.get()); | |
721 | |
722 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | |
723 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | |
724 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | |
725 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | |
726 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
727 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
728 .Times(kMaxNumberOfTries - 1) | |
729 .WillRepeatedly( | |
730 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
731 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
732 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
733 | |
734 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
735 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); | |
736 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
737 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
738 write_remote_characteristic_error_callback_.Run( | |
739 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
740 } | |
741 | |
742 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
743 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
744 } | |
745 | |
746 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
747 ReceiveMessageSmallerThanCharacteristicSize) { | |
748 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
749 CreateConnection()); | |
750 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
751 | |
752 std::string received_bytes; | |
753 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
754 .WillOnce(SaveArg<0>(&received_bytes)); | |
755 | |
756 connection->GattCharacteristicValueChanged( | |
757 adapter_.get(), rx_characteristic_.get(), kSmallPackets0); | |
758 | |
759 EXPECT_EQ(received_bytes, kSmallMessage); | |
760 } | |
761 | |
762 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
763 ReceiveMessageLargerThanCharacteristicSize) { | |
764 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
765 CreateConnection()); | |
766 | |
767 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
768 | |
769 std::string received_bytes; | |
770 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
771 .WillOnce(SaveArg<0>(&received_bytes)); | |
772 | |
773 std::vector<Packet> packets = kLargePackets; | |
774 | |
775 for (auto packet : packets) { | |
776 connection->GattCharacteristicValueChanged( | |
777 adapter_.get(), rx_characteristic_.get(), packet); | |
778 } | |
779 EXPECT_EQ(received_bytes, kLargeMessage); | |
780 } | |
781 | |
782 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
783 SendMessageSmallerThanCharacteristicSize) { | |
784 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
785 CreateConnection()); | |
786 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
787 | |
788 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
789 // called. | |
790 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
791 .WillOnce( | |
792 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
793 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
794 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
795 | |
796 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); | |
797 | |
798 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); | |
799 | |
800 RunWriteCharacteristicSuccessCallback(); | |
801 | |
802 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
803 EXPECT_EQ(kSmallMessage, connection_observer_.GetLastDeserializedMessage()); | |
804 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); | |
805 } | |
806 | |
807 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
808 SendMessageLargerThanCharacteristicSize) { | |
809 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
810 CreateConnection()); | |
811 | |
812 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
813 | |
814 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
815 // called. | |
816 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
817 .WillOnce( | |
818 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
819 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
820 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
821 | |
822 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); | |
823 | |
824 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); | |
825 std::vector<uint8_t> bytes_received( | |
826 last_value_written_on_tx_characteristic_.begin() + 1, | |
827 last_value_written_on_tx_characteristic_.end()); | |
828 | |
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 RunWriteCharacteristicSuccessCallback(); | |
836 bytes_received.insert(bytes_received.end(), | |
837 last_value_written_on_tx_characteristic_.begin() + 1, | |
838 last_value_written_on_tx_characteristic_.end()); | |
839 | |
840 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end()); | |
841 EXPECT_EQ(expected, bytes_received); | |
842 | |
843 RunWriteCharacteristicSuccessCallback(); | |
844 | |
845 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
846 EXPECT_EQ(kLargeMessage, connection_observer_.GetLastDeserializedMessage()); | |
847 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); | |
848 } | |
849 | |
850 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
851 SendMessageKeepsFailing) { | |
852 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
853 CreateConnection()); | |
854 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
855 | |
856 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
857 .Times(kMaxNumberOfTries) | |
858 .WillRepeatedly( | |
859 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
860 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
861 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
862 | |
863 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); | |
864 | |
865 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
866 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); | |
867 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
868 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
869 write_remote_characteristic_error_callback_.Run( | |
870 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
871 if (i == kMaxNumberOfTries - 1) { | |
872 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); | |
873 EXPECT_EQ(kSmallMessage, | |
874 connection_observer_.GetLastDeserializedMessage()); | |
875 EXPECT_FALSE(connection_observer_.GetLastSendSuccess()); | |
876 } else { | |
877 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); | |
878 } | |
879 } | |
880 | |
881 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
882 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
883 } | |
884 | |
885 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
886 ReceiveCloseConnectionTest) { | |
887 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
888 CreateConnection()); | |
889 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
890 | |
891 connection->GattCharacteristicValueChanged( | |
892 adapter_.get(), rx_characteristic_.get(), kConnectionCloseUnknownError); | |
893 | |
894 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetReasonForClose(), | |
895 ReasonForClose::UNKNOWN_ERROR); | |
896 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
897 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
898 } | |
899 | |
900 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
901 ReceiverErrorTest) { | |
902 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
903 CreateConnection()); | |
904 | |
905 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
906 | |
907 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
908 .WillOnce( | |
909 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
910 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
911 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
912 | |
913 connection->GattCharacteristicValueChanged( | |
914 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); | |
915 | |
916 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
917 kConnectionCloseApplicationError); | |
918 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetReasonToClose(), | |
919 ReasonForClose::APPLICATION_ERROR); | |
920 | |
921 RunWriteCharacteristicSuccessCallback(); | |
922 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
923 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
924 } | |
925 | |
926 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
927 ReceiverErrorWithPendingWritesTest) { | |
928 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
929 CreateConnection()); | |
930 | |
931 InitializeConnection(connection.get(), kLargeMaxPacketSize); | |
932 | |
933 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
934 .WillOnce( | |
935 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
936 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
937 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
938 | |
939 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); | |
940 | |
941 connection->GattCharacteristicValueChanged( | |
942 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); | |
943 | |
944 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); | |
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 RunWriteCharacteristicSuccessCallback(); | |
953 | |
954 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
955 kConnectionCloseApplicationError); | |
956 EXPECT_EQ(receiver_factory_.GetMostRecentInstance()->GetReasonToClose(), | |
957 ReasonForClose::APPLICATION_ERROR); | |
958 | |
959 RunWriteCharacteristicSuccessCallback(); | |
960 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
961 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
962 } | |
963 | |
964 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
965 WriteConnectionCloseMaxNumberOfTimes) { | |
966 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
967 CreateConnection()); | |
968 | |
969 InitializeConnection(connection.get(), kDefaultMaxPacketSize); | |
970 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
971 | |
972 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
973 .WillOnce( | |
974 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
975 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
976 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
977 connection->Disconnect(); | |
978 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); | |
979 | |
980 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
981 EXPECT_EQ(last_value_written_on_tx_characteristic_, | |
982 kConnectionCloseSuccess); | |
983 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
984 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
985 | |
986 if (i != kMaxNumberOfTries - 1) { | |
987 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) | |
988 .WillOnce( | |
989 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), | |
990 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
991 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
992 } | |
993 | |
994 write_remote_characteristic_error_callback_.Run( | |
995 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
996 } | |
997 | |
998 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); | |
999 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | |
1000 } | |
1001 | |
1002 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, | |
1003 ConnectAfterADelayWhenThrottled) { | |
1004 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( | |
1005 CreateConnection()); | |
1006 | |
1007 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
1008 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | |
1009 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
1010 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
1011 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
1012 | |
1013 // No GATT connection should be created before the delay. | |
1014 connection->Connect(); | |
1015 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); | |
1016 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | |
1017 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | |
1018 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | |
1019 | |
1020 // A GATT connection should be created after the delay. | |
1021 task_runner_->RunUntilIdle(); | |
1022 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
1023 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
1024 | |
1025 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
1026 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
1027 .WillOnce(DoAll( | |
1028 SaveArg<0>(&characteristics_finder_success_callback_), | |
1029 SaveArg<1>(&characteristics_finder_error_callback_), | |
1030 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
1031 | |
1032 create_gatt_connection_success_callback_.Run( | |
1033 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( | |
1034 adapter_, kTestRemoteDeviceBluetoothAddress))); | |
1035 | |
1036 CharacteristicsFound(connection.get()); | |
1037 NotifySessionStarted(connection.get()); | |
1038 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); | |
1039 } | |
1040 | |
1041 } // namespace weave | |
1042 | |
1043 } // namespace proximity_auth | |
OLD | NEW |