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