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

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_client_connection_unittest.cc

Issue 2075313002: Substituting legacy protocol with uWeave protocol (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: readible reason for close and removed dlog in test Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_client_connec tion.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 15 #include "base/run_loop.h"
18 #include "base/test/test_simple_task_runner.h" 16 #include "base/test/test_simple_task_runner.h"
19 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h"
20 #include "components/proximity_auth/bluetooth_throttler.h" 17 #include "components/proximity_auth/bluetooth_throttler.h"
21 #include "components/proximity_auth/connection_finder.h" 18 #include "components/proximity_auth/connection_finder.h"
22 #include "components/proximity_auth/proximity_auth_test_util.h" 19 #include "components/proximity_auth/proximity_auth_test_util.h"
23 #include "components/proximity_auth/remote_device.h" 20 #include "components/proximity_auth/remote_device.h"
24 #include "components/proximity_auth/wire_message.h" 21 #include "components/proximity_auth/wire_message.h"
25 #include "device/bluetooth/bluetooth_adapter_factory.h" 22 #include "device/bluetooth/bluetooth_adapter_factory.h"
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
27 #include "device/bluetooth/bluetooth_uuid.h"
28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 23 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
29 #include "device/bluetooth/test/mock_bluetooth_device.h" 24 #include "device/bluetooth/test/mock_bluetooth_device.h"
30 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" 25 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
31 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" 26 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
32 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 27 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
33 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" 28 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
34 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
36 31
37 using testing::_; 32 using testing::_;
38 using testing::AtLeast; 33 using testing::AtLeast;
39 using testing::NiceMock; 34 using testing::NiceMock;
40 using testing::Return; 35 using testing::Return;
41 using testing::StrictMock; 36 using testing::StrictMock;
42 using testing::SaveArg; 37 using testing::SaveArg;
43 38
44 namespace proximity_auth { 39 namespace proximity_auth {
45 namespace { 40 namespace {
46 41
42 class MockBluetoothThrottler : public BluetoothThrottler {
43 public:
44 MockBluetoothThrottler() {}
45 ~MockBluetoothThrottler() override {}
46
47 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
48 MOCK_METHOD1(OnConnection, void(Connection* connection));
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler);
52 };
53
54 class MockBluetoothLowEnergyCharacteristicsFinder
55 : public BluetoothLowEnergyCharacteristicsFinder {
56 public:
57 MockBluetoothLowEnergyCharacteristicsFinder() {}
58 ~MockBluetoothLowEnergyCharacteristicsFinder() override {}
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder);
62 };
63
64 } // namespace
65
66 namespace weave {
67 namespace {
68
69 typedef BluetoothLowEnergyWeaveClientConnection::SubStatus SubStatus;
70 typedef BluetoothLowEnergyWeavePacketReceiver::State ReceiverState;
71 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverError ReceiverError;
72 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverType ReceiverType;
73
47 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; 74 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF";
48 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; 75 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
49 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; 76 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
50 77
51 const char kServiceID[] = "service id"; 78 const char kServiceID[] = "service id";
52 const char kToPeripheralCharID[] = "to peripheral char id"; 79 const char kTXCharacteristicID[] = "TX characteristic id";
53 const char kFromPeripheralCharID[] = "from peripheral char id"; 80 const char kRXCharacteristicID[] = "RX characteristic id";
54 81
55 const device::BluetoothRemoteGattCharacteristic::Properties 82 const device::BluetoothRemoteGattCharacteristic::Properties
56 kCharacteristicProperties = 83 kCharacteristicProperties =
57 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | 84 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST |
58 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | 85 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ |
59 device::BluetoothRemoteGattCharacteristic:: 86 device::BluetoothRemoteGattCharacteristic::
60 PROPERTY_WRITE_WITHOUT_RESPONSE | 87 PROPERTY_WRITE_WITHOUT_RESPONSE |
61 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; 88 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE;
62 89
63 const int kMaxNumberOfTries = 3; 90 const int kMaxNumberOfTries = 3;
91 const uint16_t kLargeMaxPacketSize = 30;
64 92
65 class MockBluetoothThrottler : public BluetoothThrottler { 93 const uint8_t kDataHeader = 0;
94 const uint8_t kConnectionRequestHeader = 1;
95 const uint8_t kSmallConnectionResponseHeader = 2;
96 const uint8_t kLargeConnectionResponseHeader = 3;
97
98 const std::string kSmallMessage = "bb";
99 const std::string kLargeMessage = "aaabbb";
100
101 const Packet kConnectionRequest{kConnectionRequestHeader};
102 const Packet kSmallConnectionResponse{kSmallConnectionResponseHeader};
103 const Packet kLargeConnectionResponse{kLargeConnectionResponseHeader};
104
105 const std::vector<Packet> kSmallPackets{Packet{kDataHeader, 'b', 'b'}};
106 const std::vector<Packet> kLargePackets{Packet{kDataHeader, 'a', 'a', 'a'},
107 Packet{kDataHeader, 'b', 'b', 'b'}};
108
109 class MockBluetoothLowEnergyWeavePacketGenerator
110 : public BluetoothLowEnergyWeavePacketGenerator {
66 public: 111 public:
67 MockBluetoothThrottler() {} 112 MockBluetoothLowEnergyWeavePacketGenerator()
68 ~MockBluetoothThrottler() override {} 113 : max_packet_size_(kDefaultMaxPacketSize) {}
69 114
70 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); 115 Packet CreateConnectionRequest() override { return kConnectionRequest; }
71 MOCK_METHOD1(OnConnection, void(Connection* connection)); 116
117 Packet CreateConnectionResponse() override {
118 if (max_packet_size_ == kDefaultMaxPacketSize) {
119 return kSmallConnectionResponse;
120 } else {
121 return kLargeConnectionResponse;
122 }
123 }
124
125 Packet CreateConnectionClose(ReasonForClose reason_for_close) override {
126 return Packet{static_cast<uint8_t>(reason_for_close)};
127 }
128
129 void SetMaxPacketSize(uint16_t size) override { max_packet_size_ = size; }
130
131 std::vector<Packet> EncodeDataMessage(std::string message) override {
132 if (max_packet_size_ == kDefaultMaxPacketSize) {
133 return kSmallPackets;
134 } else {
135 return kLargePackets;
136 }
137 }
72 138
73 private: 139 private:
74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); 140 uint16_t max_packet_size_;
75 }; 141 };
76 142
77 class MockBluetoothLowEnergyCharacteristicsFinder 143 class MockBluetoothLowEnergyWeavePacketReceiver
78 : public BluetoothLowEnergyCharacteristicsFinder { 144 : public BluetoothLowEnergyWeavePacketReceiver {
79 public: 145 public:
80 MockBluetoothLowEnergyCharacteristicsFinder() {} 146 MockBluetoothLowEnergyWeavePacketReceiver()
81 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} 147 : BluetoothLowEnergyWeavePacketReceiver(ReceiverType::CLIENT),
148 state_(State::CONNECTING),
149 max_packet_size_(kDefaultMaxPacketSize) {}
150
151 ReceiverState GetState() override { return state_; }
152
153 uint16_t GetMaxPacketSize() override { return max_packet_size_; }
154
155 ReasonForClose GetReasonForClose() override {
156 return ReasonForClose::CLOSE_WITHOUT_ERROR;
157 }
158
159 ReasonForClose GetReasonToClose() override {
160 return ReasonForClose::UNKNOWN_ERROR;
161 }
162
163 std::string GetDataMessage() override {
164 if (max_packet_size_ == kDefaultMaxPacketSize) {
165 return kSmallMessage;
166 } else {
167 return kLargeMessage;
168 }
169 }
170
171 ReceiverError GetReceiverError() override {
172 return ReceiverError::NO_ERROR_DETECTED;
173 }
174
175 ReceiverState ReceivePacket(const Packet& packet) override {
176 switch (packet[0]) {
177 case kSmallConnectionResponseHeader:
178 max_packet_size_ = kDefaultMaxPacketSize;
179 state_ = ReceiverState::WAITING;
180 break;
181 case kLargeConnectionResponseHeader:
182 max_packet_size_ = kLargeMaxPacketSize;
183 state_ = ReceiverState::WAITING;
184 break;
185 case kDataHeader:
186 if (packet[1] == 'b') {
187 state_ = ReceiverState::DATA_READY;
188 } else {
189 state_ = ReceiverState::RECEIVING_DATA;
190 }
191 break;
192 default:
193 NOTREACHED();
194 }
195 return state_;
196 }
82 197
83 private: 198 private:
84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); 199 ReceiverState state_;
200 uint16_t max_packet_size_;
85 }; 201 };
86 202
87 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { 203 class MockBluetoothLowEnergyWeavePacketGeneratorFactory
204 : public BluetoothLowEnergyWeavePacketGenerator::Factory {
205 private:
206 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> BuildInstance()
207 override {
208 return std::unique_ptr<MockBluetoothLowEnergyWeavePacketGenerator>(
209 new MockBluetoothLowEnergyWeavePacketGenerator());
210 }
211 };
212
213 class MockBluetoothLowEnergyWeavePacketReceiverFactory
214 : public BluetoothLowEnergyWeavePacketReceiver::Factory {
215 private:
216 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> BuildInstance(
217 ReceiverType receiver_type) override {
218 return std::unique_ptr<MockBluetoothLowEnergyWeavePacketReceiver>(
219 new MockBluetoothLowEnergyWeavePacketReceiver());
220 }
221 };
222
223 class TestBluetoothLowEnergyWeaveClientConnection
224 : public BluetoothLowEnergyWeaveClientConnection {
88 public: 225 public:
89 MockBluetoothLowEnergyConnection( 226 TestBluetoothLowEnergyWeaveClientConnection(
90 const RemoteDevice& remote_device, 227 const RemoteDevice& remote_device,
91 scoped_refptr<device::BluetoothAdapter> adapter, 228 scoped_refptr<device::BluetoothAdapter> adapter,
92 const device::BluetoothUUID remote_service_uuid, 229 const device::BluetoothUUID remote_service_uuid,
93 BluetoothThrottler* bluetooth_throttler, 230 BluetoothThrottler* bluetooth_throttler,
94 int max_number_of_write_attempts) 231 int max_number_of_write_attempts)
95 : BluetoothLowEnergyConnection(remote_device, 232 : BluetoothLowEnergyWeaveClientConnection(remote_device,
96 adapter, 233 adapter,
97 remote_service_uuid, 234 remote_service_uuid,
98 bluetooth_throttler, 235 bluetooth_throttler,
99 max_number_of_write_attempts) {} 236 max_number_of_write_attempts) {}
100 237
101 ~MockBluetoothLowEnergyConnection() override {} 238 ~TestBluetoothLowEnergyWeaveClientConnection() override {}
102 239
103 MOCK_METHOD2( 240 MOCK_METHOD2(
104 CreateCharacteristicsFinder, 241 CreateCharacteristicsFinder,
105 BluetoothLowEnergyCharacteristicsFinder*( 242 BluetoothLowEnergyCharacteristicsFinder*(
106 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& 243 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback&
107 success, 244 success,
108 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); 245 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error));
109 246
110 MOCK_METHOD2(OnDidSendMessage, 247 MOCK_METHOD2(OnDidSendMessage,
111 void(const WireMessage& message, bool success)); 248 void(const WireMessage& message, bool success));
112 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); 249 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes));
113 250
114 // Exposing inherited protected methods for testing. 251 // Exposing inherited protected methods for testing.
115 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; 252 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged;
116 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; 253 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting;
117 254
118 // Exposing inherited protected fields for testing. 255 // Exposing inherited protected fields for testing.
119 using BluetoothLowEnergyConnection::status; 256 using BluetoothLowEnergyWeaveClientConnection::status;
120 using BluetoothLowEnergyConnection::sub_status; 257 using BluetoothLowEnergyWeaveClientConnection::sub_status;
121 258
122 private: 259 private:
123 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); 260 DISALLOW_COPY_AND_ASSIGN(TestBluetoothLowEnergyWeaveClientConnection);
124 }; 261 };
125 262
126 } // namespace 263 } // namespace
127 264
128 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { 265 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest
266 : public testing::Test {
129 public: 267 public:
130 ProximityAuthBluetoothLowEnergyConnectionTest() 268 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest()
131 : adapter_(new NiceMock<device::MockBluetoothAdapter>), 269 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
132 remote_device_(CreateLERemoteDeviceForTest()), 270 remote_device_(CreateLERemoteDeviceForTest()),
133 service_uuid_(device::BluetoothUUID(kServiceUUID)), 271 service_uuid_(device::BluetoothUUID(kServiceUUID)),
134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), 272 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)),
135 from_peripheral_char_uuid_( 273 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)),
136 device::BluetoothUUID(kFromPeripheralCharUUID)),
137 notify_session_alias_(NULL), 274 notify_session_alias_(NULL),
138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), 275 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>),
139 task_runner_(new base::TestSimpleTaskRunner) {} 276 task_runner_(new base::TestSimpleTaskRunner) {}
140 277
141 void SetUp() override { 278 void SetUp() override {
142 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( 279 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>(
143 adapter_.get(), 0, kTestRemoteDeviceName, 280 adapter_.get(), 0, kTestRemoteDeviceName,
144 kTestRemoteDeviceBluetoothAddress, false, false)); 281 kTestRemoteDeviceBluetoothAddress, false, false));
145 282
146 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( 283 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>(
147 device_.get(), kServiceID, service_uuid_, true, false)); 284 device_.get(), kServiceID, service_uuid_, true, false));
148 to_peripheral_char_ = 285 tx_characteristic_ =
149 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( 286 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>(
150 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, 287 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false,
151 false, kCharacteristicProperties, 288 kCharacteristicProperties,
152 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); 289 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
153 290
154 from_peripheral_char_ = 291 rx_characteristic_ =
155 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( 292 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>(
156 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, 293 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false,
157 false, kCharacteristicProperties, 294 kCharacteristicProperties,
158 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); 295 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
159 296
160 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 297 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
161 298
162 std::vector<const device::BluetoothDevice*> devices; 299 std::vector<const device::BluetoothDevice*> devices;
163 devices.push_back(device_.get()); 300 devices.push_back(device_.get());
164 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 301 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
165 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) 302 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress))
166 .WillByDefault(Return(device_.get())); 303 .WillByDefault(Return(device_.get()));
167 ON_CALL(*device_, GetGattService(kServiceID)) 304 ON_CALL(*device_, GetGattService(kServiceID))
168 .WillByDefault(Return(service_.get())); 305 .WillByDefault(Return(service_.get()));
169 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) 306 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID))
170 .WillByDefault(Return(from_peripheral_char_.get())); 307 .WillByDefault(Return(rx_characteristic_.get()));
171 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) 308 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID))
172 .WillByDefault(Return(to_peripheral_char_.get())); 309 .WillByDefault(Return(tx_characteristic_.get()));
173 } 310 }
174 311
175 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED 312 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in
176 // state. 313 // DISCONNECTED state.
177 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { 314 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection>
315 CreateConnection() {
178 EXPECT_CALL(*adapter_, AddObserver(_)); 316 EXPECT_CALL(*adapter_, AddObserver(_));
179 EXPECT_CALL(*adapter_, RemoveObserver(_)); 317 EXPECT_CALL(*adapter_, RemoveObserver(_));
180 318
181 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 319 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
182 new MockBluetoothLowEnergyConnection( 320 &generator_factory_);
321 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting(
322 &receiver_factory_);
323
324 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
325 new TestBluetoothLowEnergyWeaveClientConnection(
183 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 326 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
184 kMaxNumberOfTries)); 327 kMaxNumberOfTries));
185 328
186 EXPECT_EQ(connection->sub_status(), 329 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
187 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
188 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 330 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
189 331
190 connection->SetTaskRunnerForTesting(task_runner_); 332 connection->SetTaskRunnerForTesting(task_runner_);
191 333
192 return connection; 334 return connection;
193 } 335 }
194 336
195 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS 337 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS
196 // state, without an existing GATT connection. 338 // state, without an existing GATT connection.
197 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) { 339 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) {
198 // Preparing |connection| for a CreateGattConnection call. 340 // Preparing |connection| for a CreateGattConnection call.
199 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 341 EXPECT_CALL(*device_, CreateGattConnection(_, _))
200 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 342 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
201 SaveArg<1>(&create_gatt_connection_error_callback_))); 343 SaveArg<1>(&create_gatt_connection_error_callback_)));
202 344
203 // No throttling by default 345 // No throttling by default
204 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 346 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
205 .WillOnce(Return(base::TimeDelta())); 347 .WillOnce(Return(base::TimeDelta()));
206 348
207 connection->Connect(); 349 connection->Connect();
208 350
209 EXPECT_EQ(connection->sub_status(), 351 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION);
210 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
211 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 352 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
212 353
213 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 354 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
214 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 355 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
215 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 356 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
216 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 357 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
217 .WillOnce(DoAll( 358 .WillOnce(DoAll(
218 SaveArg<0>(&characteristics_finder_success_callback_), 359 SaveArg<0>(&characteristics_finder_success_callback_),
219 SaveArg<1>(&characteristics_finder_error_callback_), 360 SaveArg<1>(&characteristics_finder_error_callback_),
220 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 361 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
221 362
222 create_gatt_connection_success_callback_.Run( 363 create_gatt_connection_success_callback_.Run(
223 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( 364 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>(
224 adapter_, kTestRemoteDeviceBluetoothAddress))); 365 adapter_, kTestRemoteDeviceBluetoothAddress)));
225 366
226 EXPECT_EQ(connection->sub_status(), 367 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS);
227 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS);
228 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 368 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
229 } 369 }
230 370
231 // Transitions |connection| from WAITING_CHARACTERISTICS to 371 // Transitions |connection| from WAITING_CHARACTERISTICS to
232 // WAITING_NOTIFY_SESSION state. 372 // WAITING_NOTIFY_SESSION state.
233 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { 373 void CharacteristicsFound(
234 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) 374 TestBluetoothLowEnergyWeaveClientConnection* connection) {
375 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _))
235 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_), 376 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_),
236 SaveArg<1>(&notify_session_error_callback_))); 377 SaveArg<1>(&notify_session_error_callback_)));
237 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); 378 EXPECT_FALSE(characteristics_finder_error_callback_.is_null());
238 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); 379 ASSERT_FALSE(characteristics_finder_success_callback_.is_null());
239 380
240 characteristics_finder_success_callback_.Run( 381 characteristics_finder_success_callback_.Run(
241 {service_uuid_, kServiceID}, 382 {service_uuid_, kServiceID},
242 {to_peripheral_char_uuid_, kToPeripheralCharID}, 383 {tx_characteristic_uuid_, kTXCharacteristicID},
243 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 384 {rx_characteristic_uuid_, kRXCharacteristicID});
244 385
245 EXPECT_EQ(connection->sub_status(), 386 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION);
246 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION);
247 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 387 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
248 } 388 }
249 389
250 // Transitions |connection| from WAITING_NOTIFY_SESSION to 390 // Transitions |connection| from WAITING_NOTIFY_SESSION to
251 // WAITING_RESPONSE_SIGNAL state. 391 // WAITING_CONNECTION_RESPONSE state.
252 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { 392 void NotifySessionStarted(
253 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 393 TestBluetoothLowEnergyWeaveClientConnection* connection) {
394 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
254 .WillOnce( 395 .WillOnce(
255 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 396 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
256 SaveArg<1>(&write_remote_characteristic_success_callback_), 397 SaveArg<1>(&write_remote_characteristic_success_callback_),
257 SaveArg<2>(&write_remote_characteristic_error_callback_))); 398 SaveArg<2>(&write_remote_characteristic_error_callback_)));
258 EXPECT_FALSE(notify_session_error_callback_.is_null()); 399 EXPECT_FALSE(notify_session_error_callback_.is_null());
259 ASSERT_FALSE(notify_session_success_callback_.is_null()); 400 ASSERT_FALSE(notify_session_success_callback_.is_null());
260 401
261 // Store an alias for the notify session passed |connection|. 402 // Store an alias for the notify session passed |connection|.
262 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( 403 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session(
263 new NiceMock<device::MockBluetoothGattNotifySession>( 404 new NiceMock<device::MockBluetoothGattNotifySession>(
264 kToPeripheralCharID)); 405 kTXCharacteristicID));
265 notify_session_alias_ = notify_session.get(); 406 notify_session_alias_ = notify_session.get();
266 407
267 notify_session_success_callback_.Run(std::move(notify_session)); 408 notify_session_success_callback_.Run(std::move(notify_session));
268 task_runner_->RunUntilIdle(); 409 task_runner_->RunUntilIdle();
269 410
270 EXPECT_EQ(connection->sub_status(), 411 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE);
271 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL);
272 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 412 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
273 } 413 }
274 414
275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. 415 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED.
276 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { 416 void ConnectionResponseReceived(
277 // Written value contains only the 417 TestBluetoothLowEnergyWeaveClientConnection* connection,
278 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. 418 uint16_t selected_packet_size) {
279 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 419 // Written value contains only the mock Connection Request.
280 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 420 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest);
281 kInviteToConnectSignal));
282 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
283 kInviteToConnectSignal);
284 421
285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); 422 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0);
286 RunWriteCharacteristicSuccessCallback(); 423 RunWriteCharacteristicSuccessCallback();
287 424
288 // Received the 425 // Received Connection Response.
289 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. 426 if (selected_packet_size == kDefaultMaxPacketSize) {
290 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( 427 connection->GattCharacteristicValueChanged(
291 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 428 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse);
292 kInvitationResponseSignal)); 429 } else {
293 connection->GattCharacteristicValueChanged( 430 connection->GattCharacteristicValueChanged(
294 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); 431 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse);
432 }
295 433
296 EXPECT_EQ(connection->sub_status(), 434 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED);
297 BluetoothLowEnergyConnection::SubStatus::CONNECTED);
298 EXPECT_EQ(connection->status(), Connection::CONNECTED); 435 EXPECT_EQ(connection->status(), Connection::CONNECTED);
299 } 436 }
300 437
301 // Transitions |connection| to a DISCONNECTED state regardless of its initial 438 // Transitions |connection| to a DISCONNECTED state regardless of its initial
302 // state. 439 // state.
303 void Disconnect(MockBluetoothLowEnergyConnection* connection) { 440 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) {
304 // A notify session was previously set. 441 // A notify session was previously set.
305 if (notify_session_alias_) 442 if (notify_session_alias_)
306 EXPECT_CALL(*notify_session_alias_, Stop(_)); 443 EXPECT_CALL(*notify_session_alias_, Stop(_));
307 444
308 connection->Disconnect(); 445 connection->Disconnect();
309 446
310 EXPECT_EQ(connection->sub_status(), 447 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
311 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
312 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 448 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
313 } 449 }
314 450
315 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) { 451 void InitializeConnection(
452 TestBluetoothLowEnergyWeaveClientConnection* connection,
453 uint32_t selected_packet_size) {
316 ConnectGatt(connection); 454 ConnectGatt(connection);
317 CharacteristicsFound(connection); 455 CharacteristicsFound(connection);
318 NotifySessionStarted(connection); 456 NotifySessionStarted(connection);
319 ResponseSignalReceived(connection); 457 ConnectionResponseReceived(connection, selected_packet_size);
320 } 458 }
321 459
322 void RunWriteCharacteristicSuccessCallback() { 460 void RunWriteCharacteristicSuccessCallback() {
323 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); 461 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null());
324 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); 462 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null());
325 write_remote_characteristic_success_callback_.Run(); 463 write_remote_characteristic_success_callback_.Run();
326 } 464 }
327 465
328 std::vector<uint8_t> CreateSendSignalWithSize(int message_size) {
329 std::vector<uint8_t> value = ToByteVector(static_cast<uint32_t>(
330 BluetoothLowEnergyConnection::ControlSignal::kSendSignal));
331 std::vector<uint8_t> size =
332 ToByteVector(static_cast<uint32_t>(message_size));
333 value.insert(value.end(), size.begin(), size.end());
334 return value;
335 }
336
337 std::vector<uint8_t> CreateFirstCharacteristicValue(
338 const std::string& message,
339 int size) {
340 std::vector<uint8_t> value(CreateSendSignalWithSize(size));
341 std::vector<uint8_t> bytes(message.begin(), message.end());
342 value.insert(value.end(), bytes.begin(), bytes.end());
343 return value;
344 }
345
346 std::vector<uint8_t> ToByteVector(uint32_t value) {
347 std::vector<uint8_t> bytes(4, 0);
348 bytes[0] = static_cast<uint8_t>(value);
349 bytes[1] = static_cast<uint8_t>(value >> 8);
350 bytes[2] = static_cast<uint8_t>(value >> 16);
351 bytes[3] = static_cast<uint8_t>(value >> 24);
352 return bytes;
353 }
354
355 protected: 466 protected:
356 scoped_refptr<device::MockBluetoothAdapter> adapter_; 467 scoped_refptr<device::MockBluetoothAdapter> adapter_;
357 RemoteDevice remote_device_; 468 RemoteDevice remote_device_;
358 device::BluetoothUUID service_uuid_; 469 device::BluetoothUUID service_uuid_;
359 device::BluetoothUUID to_peripheral_char_uuid_; 470 device::BluetoothUUID tx_characteristic_uuid_;
360 device::BluetoothUUID from_peripheral_char_uuid_; 471 device::BluetoothUUID rx_characteristic_uuid_;
361 std::unique_ptr<device::MockBluetoothDevice> device_; 472 std::unique_ptr<device::MockBluetoothDevice> device_;
362 std::unique_ptr<device::MockBluetoothGattService> service_; 473 std::unique_ptr<device::MockBluetoothGattService> service_;
363 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; 474 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_;
364 std::unique_ptr<device::MockBluetoothGattCharacteristic> 475 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_;
365 from_peripheral_char_; 476 std::vector<uint8_t> last_value_written_on_tx_characteristic_;
366 std::vector<uint8_t> last_value_written_on_to_peripheral_char_;
367 device::MockBluetoothGattNotifySession* notify_session_alias_; 477 device::MockBluetoothGattNotifySession* notify_session_alias_;
368 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; 478 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_;
369 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 479 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
370 base::MessageLoop message_loop_; 480 base::MessageLoop message_loop_;
371 481
372 // Callbacks 482 // Callbacks
373 device::BluetoothDevice::GattConnectionCallback 483 device::BluetoothDevice::GattConnectionCallback
374 create_gatt_connection_success_callback_; 484 create_gatt_connection_success_callback_;
375 device::BluetoothDevice::ConnectErrorCallback 485 device::BluetoothDevice::ConnectErrorCallback
376 create_gatt_connection_error_callback_; 486 create_gatt_connection_error_callback_;
377 487
378 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback 488 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback
379 characteristics_finder_success_callback_; 489 characteristics_finder_success_callback_;
380 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback 490 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback
381 characteristics_finder_error_callback_; 491 characteristics_finder_error_callback_;
382 492
383 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback 493 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback
384 notify_session_success_callback_; 494 notify_session_success_callback_;
385 device::BluetoothRemoteGattCharacteristic::ErrorCallback 495 device::BluetoothRemoteGattCharacteristic::ErrorCallback
386 notify_session_error_callback_; 496 notify_session_error_callback_;
387 497
388 base::Closure write_remote_characteristic_success_callback_; 498 base::Closure write_remote_characteristic_success_callback_;
389 device::BluetoothRemoteGattCharacteristic::ErrorCallback 499 device::BluetoothRemoteGattCharacteristic::ErrorCallback
390 write_remote_characteristic_error_callback_; 500 write_remote_characteristic_error_callback_;
501
502 MockBluetoothLowEnergyWeavePacketGeneratorFactory generator_factory_;
503 MockBluetoothLowEnergyWeavePacketReceiverFactory receiver_factory_;
391 }; 504 };
392 505
393 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 506 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
394 CreateAndDestroyWithouthConnectCallDoesntCrash) { 507 CreateAndDestroyWithoutConnectCallDoesntCrash) {
395 BluetoothLowEnergyConnection connection( 508 BluetoothLowEnergyWeaveClientConnection connection(
396 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 509 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
397 kMaxNumberOfTries); 510 kMaxNumberOfTries);
398 } 511 }
399 512
400 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 513 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
401 Disconect_WithoutConnectDoesntCrash) { 514 DisconectWithoutConnectDoesntCrash) {
402 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 515 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
403 CreateConnection()); 516 CreateConnection());
404 Disconnect(connection.get()); 517 Disconnect(connection.get());
405 } 518 }
406 519
407 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { 520 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
408 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 521 ConnectSuccess) {
522 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
409 CreateConnection()); 523 CreateConnection());
410 ConnectGatt(connection.get()); 524 ConnectGatt(connection.get());
411 CharacteristicsFound(connection.get()); 525 CharacteristicsFound(connection.get());
412 NotifySessionStarted(connection.get()); 526 NotifySessionStarted(connection.get());
413 ResponseSignalReceived(connection.get()); 527 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize);
414 } 528 }
415 529
416 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 530 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
417 Connect_Success_Disconnect) { 531 ConnectSuccessDisconnect) {
418 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 532 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
419 CreateConnection()); 533 CreateConnection());
420 InitializeConnection(connection.get()); 534 InitializeConnection(connection.get(), kDefaultMaxPacketSize);
421 Disconnect(connection.get()); 535 Disconnect(connection.get());
422 } 536 }
423 537
424 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 538 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
425 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { 539 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) {
426 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 540 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
427 CreateConnection()); 541 CreateConnection());
428 ConnectGatt(connection.get()); 542 ConnectGatt(connection.get());
429 Disconnect(connection.get()); 543 Disconnect(connection.get());
430 } 544 }
431 545
432 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 546 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
433 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { 547 ConnectIncompleteDisconnectFromWaitingNotifySessionState) {
434 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 548 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
435 CreateConnection()); 549 CreateConnection());
436 ConnectGatt(connection.get()); 550 ConnectGatt(connection.get());
437 CharacteristicsFound(connection.get()); 551 CharacteristicsFound(connection.get());
438 Disconnect(connection.get()); 552 Disconnect(connection.get());
439 } 553 }
440 554
441 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 555 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
442 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { 556 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) {
443 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 557 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
444 CreateConnection()); 558 CreateConnection());
445 ConnectGatt(connection.get()); 559 ConnectGatt(connection.get());
446 CharacteristicsFound(connection.get()); 560 CharacteristicsFound(connection.get());
447 NotifySessionStarted(connection.get()); 561 NotifySessionStarted(connection.get());
448 Disconnect(connection.get()); 562 Disconnect(connection.get());
449 } 563 }
450 564
451 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 565 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
452 Connect_Fails_CharacteristicsNotFound) { 566 ConnectFailsCharacteristicsNotFound) {
453 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 567 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
454 CreateConnection()); 568 CreateConnection());
455 ConnectGatt(connection.get()); 569 ConnectGatt(connection.get());
456 570
457 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); 571 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0);
458 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); 572 EXPECT_FALSE(characteristics_finder_success_callback_.is_null());
459 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); 573 ASSERT_FALSE(characteristics_finder_error_callback_.is_null());
460 574
461 characteristics_finder_error_callback_.Run( 575 characteristics_finder_error_callback_.Run(
462 {to_peripheral_char_uuid_, kToPeripheralCharID}, 576 {tx_characteristic_uuid_, kTXCharacteristicID},
463 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 577 {rx_characteristic_uuid_, kRXCharacteristicID});
464 578
465 EXPECT_EQ(connection->sub_status(), 579 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
466 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
467 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 580 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
468 } 581 }
469 582
470 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 583 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
471 Connect_Fails_NotifySessionError) { 584 ConnectFailsNotifySessionError) {
472 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 585 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
473 CreateConnection()); 586 CreateConnection());
474 ConnectGatt(connection.get()); 587 ConnectGatt(connection.get());
475 CharacteristicsFound(connection.get()); 588 CharacteristicsFound(connection.get());
476 589
477 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 590 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0);
478 .Times(0);
479 EXPECT_FALSE(notify_session_success_callback_.is_null()); 591 EXPECT_FALSE(notify_session_success_callback_.is_null());
480 ASSERT_FALSE(notify_session_error_callback_.is_null()); 592 ASSERT_FALSE(notify_session_error_callback_.is_null());
481 593
482 notify_session_error_callback_.Run( 594 notify_session_error_callback_.Run(
483 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 595 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
484 596
485 EXPECT_EQ(connection->sub_status(), 597 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
486 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
487 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 598 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
488 } 599 }
489 600
490 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 601 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
491 Connect_Fails_ErrorSendingInviteToConnectSignal) { 602 ConnectFailsErrorSendingConnectionRequest) {
492 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 603 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
493 CreateConnection()); 604 CreateConnection());
494 ConnectGatt(connection.get()); 605 ConnectGatt(connection.get());
495 CharacteristicsFound(connection.get()); 606 CharacteristicsFound(connection.get());
496 NotifySessionStarted(connection.get()); 607 NotifySessionStarted(connection.get());
497 608
498 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the 609 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the
499 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for 610 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for
500 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use 611 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use
501 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. 612 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement.
502 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); 613 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0);
503 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 614 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
504 .Times(kMaxNumberOfTries - 1) 615 .Times(kMaxNumberOfTries - 1)
505 .WillRepeatedly( 616 .WillRepeatedly(
506 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 617 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
507 SaveArg<1>(&write_remote_characteristic_success_callback_), 618 SaveArg<1>(&write_remote_characteristic_success_callback_),
508 SaveArg<2>(&write_remote_characteristic_error_callback_))); 619 SaveArg<2>(&write_remote_characteristic_error_callback_)));
509 620
510 for (int i = 0; i < kMaxNumberOfTries; i++) { 621 for (int i = 0; i < kMaxNumberOfTries; i++) {
511 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 622 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest);
512 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal::
513 kInviteToConnectSignal));
514 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
515 kInviteToConnectSignal);
516 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); 623 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null());
517 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); 624 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null());
518 write_remote_characteristic_error_callback_.Run( 625 write_remote_characteristic_error_callback_.Run(
519 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 626 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
520 } 627 }
521 628
522 EXPECT_EQ(connection->sub_status(), 629 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
523 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
524 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 630 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
525 } 631 }
526 632
527 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 633 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
528 Receive_MessageSmallerThanCharacteristicSize) { 634 ReceiveMessageSmallerThanCharacteristicSize) {
529 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 635 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
530 CreateConnection()); 636 CreateConnection());
531 InitializeConnection(connection.get()); 637 InitializeConnection(connection.get(), kDefaultMaxPacketSize);
532 638
533 std::string received_bytes; 639 std::string received_bytes;
534 EXPECT_CALL(*connection, OnBytesReceived(_)) 640 EXPECT_CALL(*connection, OnBytesReceived(_))
535 .WillOnce(SaveArg<0>(&received_bytes)); 641 .WillOnce(SaveArg<0>(&received_bytes));
536 642
537 // Message (bytes) that is going to be received. 643 connection->GattCharacteristicValueChanged(
538 std::string message(100, 'A'); 644 adapter_.get(), rx_characteristic_.get(), kSmallPackets[0]);
539 645
540 // Sending the |kSendSignal| + |message_size| + |message|. 646 EXPECT_EQ(received_bytes, kSmallMessage);
541 connection->GattCharacteristicValueChanged(
542 adapter_.get(), from_peripheral_char_.get(),
543 CreateFirstCharacteristicValue(message, message.size()));
544
545 EXPECT_EQ(received_bytes, message);
546 } 647 }
547 648
548 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 649 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
549 Receive_MessageLargerThanCharacteristicSize) { 650 ReceiveMessageLargerThanCharacteristicSize) {
550 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 651 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
551 CreateConnection()); 652 CreateConnection());
552 InitializeConnection(connection.get()); 653
654 const uint32_t selected_packet_size = 500;
655 InitializeConnection(connection.get(), selected_packet_size);
553 656
554 std::string received_bytes; 657 std::string received_bytes;
555 int chunk_size = 500;
556 EXPECT_CALL(*connection, OnBytesReceived(_)) 658 EXPECT_CALL(*connection, OnBytesReceived(_))
557 .WillOnce(SaveArg<0>(&received_bytes)); 659 .WillOnce(SaveArg<0>(&received_bytes));
558 660
559 // Message (bytes) that is going to be received. 661 std::vector<Packet> packets = kLargePackets;
560 int message_size = 600;
561 std::string message(message_size, 'A');
562 662
563 // Sending the |kSendSignal| + |message_size| + |message| (truncated at 663 for (auto packet : packets) {
564 // |chunk_size|). 664 connection->GattCharacteristicValueChanged(
565 int first_write_payload_size = 665 adapter_.get(), rx_characteristic_.get(), packet);
566 chunk_size - CreateSendSignalWithSize(message_size).size(); 666 }
567 connection->GattCharacteristicValueChanged( 667 EXPECT_EQ(received_bytes, kLargeMessage);
568 adapter_.get(), from_peripheral_char_.get(),
569 CreateFirstCharacteristicValue(
570 message.substr(0, first_write_payload_size), message.size()));
571
572 // Sending the remaining bytes.
573 std::vector<uint8_t> value;
574 value.push_back(0);
575 value.insert(value.end(), message.begin() + first_write_payload_size,
576 message.end());
577 connection->GattCharacteristicValueChanged(
578 adapter_.get(), from_peripheral_char_.get(), value);
579
580 EXPECT_EQ(received_bytes, message);
581 } 668 }
582 669
583 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 670 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
584 SendMessage_SmallerThanCharacteristicSize) { 671 SendMessageSmallerThanCharacteristicSize) {
585 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 672 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
586 CreateConnection()); 673 CreateConnection());
587 InitializeConnection(connection.get()); 674 InitializeConnection(connection.get(), kDefaultMaxPacketSize);
588 675
589 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is 676 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is
590 // called. 677 // called.
591 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 678 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
592 .WillOnce( 679 .WillOnce(
593 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 680 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
594 SaveArg<1>(&write_remote_characteristic_success_callback_), 681 SaveArg<1>(&write_remote_characteristic_success_callback_),
595 SaveArg<2>(&write_remote_characteristic_error_callback_))); 682 SaveArg<2>(&write_remote_characteristic_error_callback_)));
596 683
597 // Message (bytes) that is going to be sent. 684 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage)));
598 int message_size = 100;
599 std::string message(message_size, 'A');
600 message[0] = 'B';
601 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message)));
602 685
603 // Expecting that |kSendSignal| + |message_size| + |message| was written. 686 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets[0]);
604 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
605 CreateFirstCharacteristicValue(message, message.size()));
606 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); 687 EXPECT_CALL(*connection, OnDidSendMessage(_, _));
607 688
608 RunWriteCharacteristicSuccessCallback(); 689 RunWriteCharacteristicSuccessCallback();
609 } 690 }
610 691
611 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 692 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
612 SendMessage_LagerThanCharacteristicSize) { 693 SendMessageLargerThanCharacteristicSize) {
613 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 694 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
614 CreateConnection()); 695 CreateConnection());
615 InitializeConnection(connection.get()); 696
697 const uint32_t selected_packet_size = 500;
698 InitializeConnection(connection.get(), selected_packet_size);
616 699
617 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is 700 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is
618 // called. 701 // called.
619 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 702 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
620 .WillOnce( 703 .WillOnce(
621 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 704 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
622 SaveArg<1>(&write_remote_characteristic_success_callback_), 705 SaveArg<1>(&write_remote_characteristic_success_callback_),
623 SaveArg<2>(&write_remote_characteristic_error_callback_))); 706 SaveArg<2>(&write_remote_characteristic_error_callback_)));
624 707
625 // Message (bytes) that is going to be sent. 708 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage)));
626 int message_size = 600;
627 std::string message(message_size, 'A');
628 message[0] = 'B';
629 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message)));
630 709
631 // Expecting that |kSendSignal| + |message_size| was written in the first 8 710 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets[0]);
632 // bytes.
633 std::vector<uint8_t> prefix(
634 last_value_written_on_to_peripheral_char_.begin(),
635 last_value_written_on_to_peripheral_char_.begin() + 8);
636 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size));
637 std::vector<uint8_t> bytes_received( 711 std::vector<uint8_t> bytes_received(
638 last_value_written_on_to_peripheral_char_.begin() + 8, 712 last_value_written_on_tx_characteristic_.begin() + 1,
639 last_value_written_on_to_peripheral_char_.end()); 713 last_value_written_on_tx_characteristic_.end());
640 714
641 // Expecting a second call of WriteRemoteCharacteristic, after success 715 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
642 // callback is called.
643 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _))
644 .WillOnce( 716 .WillOnce(
645 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 717 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
646 SaveArg<1>(&write_remote_characteristic_success_callback_), 718 SaveArg<1>(&write_remote_characteristic_success_callback_),
647 SaveArg<2>(&write_remote_characteristic_error_callback_))); 719 SaveArg<2>(&write_remote_characteristic_error_callback_)));
648 720
649 RunWriteCharacteristicSuccessCallback(); 721 RunWriteCharacteristicSuccessCallback();
650 bytes_received.insert(bytes_received.end(), 722 bytes_received.insert(bytes_received.end(),
651 last_value_written_on_to_peripheral_char_.begin() + 1, 723 last_value_written_on_tx_characteristic_.begin() + 1,
652 last_value_written_on_to_peripheral_char_.end()); 724 last_value_written_on_tx_characteristic_.end());
653 725
654 // Expecting that the message was written. 726 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end());
655 std::vector<uint8_t> expected_value(message.begin(), message.end()); 727 EXPECT_EQ(expected, bytes_received);
656 EXPECT_EQ(expected_value.size(), bytes_received.size());
657 EXPECT_EQ(expected_value, bytes_received);
658 728
659 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); 729 EXPECT_CALL(*connection, OnDidSendMessage(_, _));
660 RunWriteCharacteristicSuccessCallback(); 730 RunWriteCharacteristicSuccessCallback();
661 } 731 }
662 732
663 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 733 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
664 Connect_AfterADelayWhenThrottled) { 734 ConnectAfterDelayWhenThrottled) {
665 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 735 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection(
666 CreateConnection()); 736 CreateConnection());
667 737
668 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 738 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
669 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); 739 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1))));
670 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 740 EXPECT_CALL(*device_, CreateGattConnection(_, _))
671 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 741 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
672 SaveArg<1>(&create_gatt_connection_error_callback_))); 742 SaveArg<1>(&create_gatt_connection_error_callback_)));
673 743
674 // No GATT connection should be created before the delay. 744 // No GATT connection should be created before the delay.
675 connection->Connect(); 745 connection->Connect();
676 EXPECT_EQ(connection->sub_status(), 746 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION);
677 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
678 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 747 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
679 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); 748 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null());
680 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); 749 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null());
681 750
682 // A GATT connection should be created after the delay. 751 // A GATT connection should be created after the delay.
683 task_runner_->RunUntilIdle(); 752 task_runner_->RunUntilIdle();
684 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 753 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
685 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 754 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
686 755
687 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 756 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
688 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 757 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
689 .WillOnce(DoAll( 758 .WillOnce(DoAll(
690 SaveArg<0>(&characteristics_finder_success_callback_), 759 SaveArg<0>(&characteristics_finder_success_callback_),
691 SaveArg<1>(&characteristics_finder_error_callback_), 760 SaveArg<1>(&characteristics_finder_error_callback_),
692 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 761 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
693 762
694 create_gatt_connection_success_callback_.Run( 763 create_gatt_connection_success_callback_.Run(
695 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( 764 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>(
696 adapter_, kTestRemoteDeviceBluetoothAddress))); 765 adapter_, kTestRemoteDeviceBluetoothAddress)));
697 766
698 CharacteristicsFound(connection.get()); 767 CharacteristicsFound(connection.get());
699 NotifySessionStarted(connection.get()); 768 NotifySessionStarted(connection.get());
700 ResponseSignalReceived(connection.get()); 769 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize);
701 } 770 }
702 771
772 } // namespace weave
773
703 } // namespace proximity_auth 774 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698