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

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: renamed to weave client connection 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 typedef BluetoothLowEnergyWeaveClientConnection::SubStatus SubStatus;
43
47 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; 44 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF";
48 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; 45 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
49 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; 46 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
50 47
51 const char kServiceID[] = "service id"; 48 const char kServiceID[] = "service id";
52 const char kToPeripheralCharID[] = "to peripheral char id"; 49 const char kToPeripheralCharID[] = "to peripheral char id";
53 const char kFromPeripheralCharID[] = "from peripheral char id"; 50 const char kFromPeripheralCharID[] = "from peripheral char id";
54 51
55 const device::BluetoothRemoteGattCharacteristic::Properties 52 const device::BluetoothRemoteGattCharacteristic::Properties
56 kCharacteristicProperties = 53 kCharacteristicProperties =
57 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | 54 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST |
58 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | 55 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ |
59 device::BluetoothRemoteGattCharacteristic:: 56 device::BluetoothRemoteGattCharacteristic::
60 PROPERTY_WRITE_WITHOUT_RESPONSE | 57 PROPERTY_WRITE_WITHOUT_RESPONSE |
61 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; 58 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE;
62 59
63 const int kMaxNumberOfTries = 3; 60 const int kMaxNumberOfTries = 3;
61 const uint32_t kDefaultPacketSize = 20;
64 62
65 class MockBluetoothThrottler : public BluetoothThrottler { 63 class MockBluetoothThrottler : public BluetoothThrottler {
66 public: 64 public:
67 MockBluetoothThrottler() {} 65 MockBluetoothThrottler() {}
68 ~MockBluetoothThrottler() override {} 66 ~MockBluetoothThrottler() override {}
69 67
70 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); 68 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
71 MOCK_METHOD1(OnConnection, void(Connection* connection)); 69 MOCK_METHOD1(OnConnection, void(Connection* connection));
72 70
73 private: 71 private:
74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); 72 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler);
75 }; 73 };
76 74
77 class MockBluetoothLowEnergyCharacteristicsFinder 75 class MockBluetoothLowEnergyCharacteristicsFinder
78 : public BluetoothLowEnergyCharacteristicsFinder { 76 : public BluetoothLowEnergyCharacteristicsFinder {
79 public: 77 public:
80 MockBluetoothLowEnergyCharacteristicsFinder() {} 78 MockBluetoothLowEnergyCharacteristicsFinder() {}
81 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} 79 ~MockBluetoothLowEnergyCharacteristicsFinder() override {}
82 80
83 private: 81 private:
84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); 82 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder);
85 }; 83 };
86 84
87 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { 85 class MockBluetoothLowEnergyWeaveClientConnection
86 : public BluetoothLowEnergyWeaveClientConnection {
88 public: 87 public:
89 MockBluetoothLowEnergyConnection( 88 MockBluetoothLowEnergyWeaveClientConnection(
90 const RemoteDevice& remote_device, 89 const RemoteDevice& remote_device,
91 scoped_refptr<device::BluetoothAdapter> adapter, 90 scoped_refptr<device::BluetoothAdapter> adapter,
92 const device::BluetoothUUID remote_service_uuid, 91 const device::BluetoothUUID remote_service_uuid,
93 BluetoothThrottler* bluetooth_throttler, 92 BluetoothThrottler* bluetooth_throttler,
94 int max_number_of_write_attempts) 93 int max_number_of_write_attempts)
95 : BluetoothLowEnergyConnection(remote_device, 94 : BluetoothLowEnergyWeaveClientConnection(remote_device,
96 adapter, 95 adapter,
97 remote_service_uuid, 96 remote_service_uuid,
98 bluetooth_throttler, 97 bluetooth_throttler,
99 max_number_of_write_attempts) {} 98 max_number_of_write_attempts) {}
100 99
101 ~MockBluetoothLowEnergyConnection() override {} 100 ~MockBluetoothLowEnergyWeaveClientConnection() override {}
102 101
103 MOCK_METHOD2( 102 MOCK_METHOD2(
104 CreateCharacteristicsFinder, 103 CreateCharacteristicsFinder,
105 BluetoothLowEnergyCharacteristicsFinder*( 104 BluetoothLowEnergyCharacteristicsFinder*(
106 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& 105 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback&
107 success, 106 success,
108 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); 107 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error));
109 108
110 MOCK_METHOD2(OnDidSendMessage, 109 MOCK_METHOD2(OnDidSendMessage,
111 void(const WireMessage& message, bool success)); 110 void(const WireMessage& message, bool success));
112 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); 111 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes));
113 112
114 // Exposing inherited protected methods for testing. 113 // Exposing inherited protected methods for testing.
115 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; 114 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged;
116 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; 115 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting;
117 116
118 // Exposing inherited protected fields for testing. 117 // Exposing inherited protected fields for testing.
119 using BluetoothLowEnergyConnection::status; 118 using BluetoothLowEnergyWeaveClientConnection::status;
120 using BluetoothLowEnergyConnection::sub_status; 119 using BluetoothLowEnergyWeaveClientConnection::sub_status;
121 120
122 private: 121 private:
123 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); 122 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyWeaveClientConnection);
124 }; 123 };
125 124
126 } // namespace 125 } // namespace
127 126
128 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { 127 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest
128 : public testing::Test {
129 public: 129 public:
130 ProximityAuthBluetoothLowEnergyConnectionTest() 130 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest()
131 : adapter_(new NiceMock<device::MockBluetoothAdapter>), 131 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
132 remote_device_(CreateLERemoteDeviceForTest()), 132 remote_device_(CreateLERemoteDeviceForTest()),
133 service_uuid_(device::BluetoothUUID(kServiceUUID)), 133 service_uuid_(device::BluetoothUUID(kServiceUUID)),
134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), 134 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)),
135 from_peripheral_char_uuid_( 135 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)),
136 device::BluetoothUUID(kFromPeripheralCharUUID)),
137 notify_session_alias_(NULL), 136 notify_session_alias_(NULL),
138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), 137 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>),
139 task_runner_(new base::TestSimpleTaskRunner) {} 138 task_runner_(new base::TestSimpleTaskRunner) {}
140 139
141 void SetUp() override { 140 void SetUp() override {
142 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( 141 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>(
143 adapter_.get(), 0, kTestRemoteDeviceName, 142 adapter_.get(), 0, kTestRemoteDeviceName,
144 kTestRemoteDeviceBluetoothAddress, false, false)); 143 kTestRemoteDeviceBluetoothAddress, false, false));
145 144
146 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( 145 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>(
147 device_.get(), kServiceID, service_uuid_, true, false)); 146 device_.get(), kServiceID, service_uuid_, true, false));
148 to_peripheral_char_ = 147 tx_characteristic_ =
149 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( 148 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>(
150 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, 149 service_.get(), kToPeripheralCharID, tx_characteristic_uuid_, false,
150 kCharacteristicProperties,
151 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
152
153 rx_characteristic_ =
154 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>(
155 service_.get(), kFromPeripheralCharID, rx_characteristic_uuid_,
151 false, kCharacteristicProperties, 156 false, kCharacteristicProperties,
152 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); 157 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
153 158
154 from_peripheral_char_ = 159 packet_generator_ =
155 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( 160 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance();
156 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_,
157 false, kCharacteristicProperties,
158 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE));
159 161
160 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 162 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
161 163
162 std::vector<const device::BluetoothDevice*> devices; 164 std::vector<const device::BluetoothDevice*> devices;
163 devices.push_back(device_.get()); 165 devices.push_back(device_.get());
164 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 166 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
165 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) 167 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress))
166 .WillByDefault(Return(device_.get())); 168 .WillByDefault(Return(device_.get()));
167 ON_CALL(*device_, GetGattService(kServiceID)) 169 ON_CALL(*device_, GetGattService(kServiceID))
168 .WillByDefault(Return(service_.get())); 170 .WillByDefault(Return(service_.get()));
169 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) 171 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID))
170 .WillByDefault(Return(from_peripheral_char_.get())); 172 .WillByDefault(Return(rx_characteristic_.get()));
171 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) 173 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID))
172 .WillByDefault(Return(to_peripheral_char_.get())); 174 .WillByDefault(Return(tx_characteristic_.get()));
173 } 175 }
174 176
175 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED 177 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in
176 // state. 178 // DISCONNECTED state.
177 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { 179 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection>
180 CreateConnection() {
178 EXPECT_CALL(*adapter_, AddObserver(_)); 181 EXPECT_CALL(*adapter_, AddObserver(_));
179 EXPECT_CALL(*adapter_, RemoveObserver(_)); 182 EXPECT_CALL(*adapter_, RemoveObserver(_));
180 183
181 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 184 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
182 new MockBluetoothLowEnergyConnection( 185 new MockBluetoothLowEnergyWeaveClientConnection(
183 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 186 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
184 kMaxNumberOfTries)); 187 kMaxNumberOfTries));
185 188
186 EXPECT_EQ(connection->sub_status(), 189 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
187 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
188 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 190 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
189 191
190 connection->SetTaskRunnerForTesting(task_runner_); 192 connection->SetTaskRunnerForTesting(task_runner_);
191 193
192 return connection; 194 return connection;
193 } 195 }
194 196
195 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS 197 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS
196 // state, without an existing GATT connection. 198 // state, without an existing GATT connection.
197 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) { 199 void ConnectGatt(MockBluetoothLowEnergyWeaveClientConnection* connection) {
198 // Preparing |connection| for a CreateGattConnection call. 200 // Preparing |connection| for a CreateGattConnection call.
199 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 201 EXPECT_CALL(*device_, CreateGattConnection(_, _))
200 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 202 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
201 SaveArg<1>(&create_gatt_connection_error_callback_))); 203 SaveArg<1>(&create_gatt_connection_error_callback_)));
202 204
203 // No throttling by default 205 // No throttling by default
204 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 206 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
205 .WillOnce(Return(base::TimeDelta())); 207 .WillOnce(Return(base::TimeDelta()));
206 208
207 connection->Connect(); 209 connection->Connect();
208 210
209 EXPECT_EQ(connection->sub_status(), 211 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION);
210 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
211 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 212 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
212 213
213 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 214 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
214 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 215 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
215 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 216 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
216 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 217 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
217 .WillOnce(DoAll( 218 .WillOnce(DoAll(
218 SaveArg<0>(&characteristics_finder_success_callback_), 219 SaveArg<0>(&characteristics_finder_success_callback_),
219 SaveArg<1>(&characteristics_finder_error_callback_), 220 SaveArg<1>(&characteristics_finder_error_callback_),
220 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 221 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
221 222
222 create_gatt_connection_success_callback_.Run( 223 create_gatt_connection_success_callback_.Run(
223 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( 224 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>(
224 adapter_, kTestRemoteDeviceBluetoothAddress))); 225 adapter_, kTestRemoteDeviceBluetoothAddress)));
225 226
226 EXPECT_EQ(connection->sub_status(), 227 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS);
227 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS);
228 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 228 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
229 } 229 }
230 230
231 // Transitions |connection| from WAITING_CHARACTERISTICS to 231 // Transitions |connection| from WAITING_CHARACTERISTICS to
232 // WAITING_NOTIFY_SESSION state. 232 // WAITING_NOTIFY_SESSION state.
233 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { 233 void CharacteristicsFound(
234 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) 234 MockBluetoothLowEnergyWeaveClientConnection* connection) {
235 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _))
235 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_), 236 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_),
236 SaveArg<1>(&notify_session_error_callback_))); 237 SaveArg<1>(&notify_session_error_callback_)));
237 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); 238 EXPECT_FALSE(characteristics_finder_error_callback_.is_null());
238 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); 239 ASSERT_FALSE(characteristics_finder_success_callback_.is_null());
239 240
240 characteristics_finder_success_callback_.Run( 241 characteristics_finder_success_callback_.Run(
241 {service_uuid_, kServiceID}, 242 {service_uuid_, kServiceID},
242 {to_peripheral_char_uuid_, kToPeripheralCharID}, 243 {tx_characteristic_uuid_, kToPeripheralCharID},
243 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 244 {rx_characteristic_uuid_, kFromPeripheralCharID});
244 245
245 EXPECT_EQ(connection->sub_status(), 246 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION);
246 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION);
247 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 247 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
248 } 248 }
249 249
250 // Transitions |connection| from WAITING_NOTIFY_SESSION to 250 // Transitions |connection| from WAITING_NOTIFY_SESSION to
251 // WAITING_RESPONSE_SIGNAL state. 251 // WAITING_RESPONSE_SIGNAL state.
252 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { 252 void NotifySessionStarted(
253 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 253 MockBluetoothLowEnergyWeaveClientConnection* connection) {
254 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
254 .WillOnce( 255 .WillOnce(
255 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 256 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
256 SaveArg<1>(&write_remote_characteristic_success_callback_), 257 SaveArg<1>(&write_remote_characteristic_success_callback_),
257 SaveArg<2>(&write_remote_characteristic_error_callback_))); 258 SaveArg<2>(&write_remote_characteristic_error_callback_)));
258 EXPECT_FALSE(notify_session_error_callback_.is_null()); 259 EXPECT_FALSE(notify_session_error_callback_.is_null());
259 ASSERT_FALSE(notify_session_success_callback_.is_null()); 260 ASSERT_FALSE(notify_session_success_callback_.is_null());
260 261
261 // Store an alias for the notify session passed |connection|. 262 // Store an alias for the notify session passed |connection|.
262 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( 263 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session(
263 new NiceMock<device::MockBluetoothGattNotifySession>( 264 new NiceMock<device::MockBluetoothGattNotifySession>(
264 kToPeripheralCharID)); 265 kToPeripheralCharID));
265 notify_session_alias_ = notify_session.get(); 266 notify_session_alias_ = notify_session.get();
266 267
267 notify_session_success_callback_.Run(std::move(notify_session)); 268 notify_session_success_callback_.Run(std::move(notify_session));
268 task_runner_->RunUntilIdle(); 269 task_runner_->RunUntilIdle();
269 270
270 EXPECT_EQ(connection->sub_status(), 271 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_RESPONSE_SIGNAL);
271 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL);
272 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 272 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
273 } 273 }
274 274
275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. 275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state.
276 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { 276 void ResponseSignalReceived(
277 // Written value contains only the 277 MockBluetoothLowEnergyWeaveClientConnection* connection,
278 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. 278 uint32_t selected_packet_size) {
279 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 279 // Written value contains only the Connection Request.
280 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 280 packet_generator_->SetMaxPacketSize(selected_packet_size);
281 kInviteToConnectSignal)); 281 const std::vector<uint8_t> request =
282 EXPECT_EQ(last_value_written_on_to_peripheral_char_, 282 packet_generator_->CreateConnectionRequest();
283 kInviteToConnectSignal); 283 EXPECT_EQ(last_value_written_on_tx_characteristic_, request);
284 284
285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); 285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0);
286 RunWriteCharacteristicSuccessCallback(); 286 RunWriteCharacteristicSuccessCallback();
287 287
288 // Received the 288 // Received Connection Response.
289 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. 289 packet_generator_->SetMaxPacketSize(selected_packet_size);
290 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( 290 const std::vector<uint8_t> response =
291 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 291 packet_generator_->CreateConnectionResponse();
292 kInvitationResponseSignal));
293 connection->GattCharacteristicValueChanged( 292 connection->GattCharacteristicValueChanged(
294 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); 293 adapter_.get(), rx_characteristic_.get(), response);
295 294
296 EXPECT_EQ(connection->sub_status(), 295 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED);
297 BluetoothLowEnergyConnection::SubStatus::CONNECTED);
298 EXPECT_EQ(connection->status(), Connection::CONNECTED); 296 EXPECT_EQ(connection->status(), Connection::CONNECTED);
299 } 297 }
300 298
301 // Transitions |connection| to a DISCONNECTED state regardless of its initial 299 // Transitions |connection| to a DISCONNECTED state regardless of its initial
302 // state. 300 // state.
303 void Disconnect(MockBluetoothLowEnergyConnection* connection) { 301 void Disconnect(MockBluetoothLowEnergyWeaveClientConnection* connection) {
304 // A notify session was previously set. 302 // A notify session was previously set.
305 if (notify_session_alias_) 303 if (notify_session_alias_)
306 EXPECT_CALL(*notify_session_alias_, Stop(_)); 304 EXPECT_CALL(*notify_session_alias_, Stop(_));
307 305
308 connection->Disconnect(); 306 connection->Disconnect();
309 307
310 EXPECT_EQ(connection->sub_status(), 308 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
311 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
312 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 309 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
313 } 310 }
314 311
315 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) { 312 void InitializeConnection(
313 MockBluetoothLowEnergyWeaveClientConnection* connection,
314 uint32_t selected_packet_size) {
316 ConnectGatt(connection); 315 ConnectGatt(connection);
317 CharacteristicsFound(connection); 316 CharacteristicsFound(connection);
318 NotifySessionStarted(connection); 317 NotifySessionStarted(connection);
319 ResponseSignalReceived(connection); 318 ResponseSignalReceived(connection, selected_packet_size);
320 } 319 }
321 320
322 void RunWriteCharacteristicSuccessCallback() { 321 void RunWriteCharacteristicSuccessCallback() {
323 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); 322 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null());
324 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); 323 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null());
325 write_remote_characteristic_success_callback_.Run(); 324 write_remote_characteristic_success_callback_.Run();
326 } 325 }
327 326
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: 327 protected:
356 scoped_refptr<device::MockBluetoothAdapter> adapter_; 328 scoped_refptr<device::MockBluetoothAdapter> adapter_;
357 RemoteDevice remote_device_; 329 RemoteDevice remote_device_;
358 device::BluetoothUUID service_uuid_; 330 device::BluetoothUUID service_uuid_;
359 device::BluetoothUUID to_peripheral_char_uuid_; 331 device::BluetoothUUID tx_characteristic_uuid_;
360 device::BluetoothUUID from_peripheral_char_uuid_; 332 device::BluetoothUUID rx_characteristic_uuid_;
361 std::unique_ptr<device::MockBluetoothDevice> device_; 333 std::unique_ptr<device::MockBluetoothDevice> device_;
362 std::unique_ptr<device::MockBluetoothGattService> service_; 334 std::unique_ptr<device::MockBluetoothGattService> service_;
363 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; 335 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_;
364 std::unique_ptr<device::MockBluetoothGattCharacteristic> 336 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_;
365 from_peripheral_char_; 337 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_; 338 device::MockBluetoothGattNotifySession* notify_session_alias_;
368 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; 339 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_;
369 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 340 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
370 base::MessageLoop message_loop_; 341 base::MessageLoop message_loop_;
371 342
372 // Callbacks 343 // Callbacks
373 device::BluetoothDevice::GattConnectionCallback 344 device::BluetoothDevice::GattConnectionCallback
374 create_gatt_connection_success_callback_; 345 create_gatt_connection_success_callback_;
375 device::BluetoothDevice::ConnectErrorCallback 346 device::BluetoothDevice::ConnectErrorCallback
376 create_gatt_connection_error_callback_; 347 create_gatt_connection_error_callback_;
377 348
378 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback 349 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback
379 characteristics_finder_success_callback_; 350 characteristics_finder_success_callback_;
380 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback 351 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback
381 characteristics_finder_error_callback_; 352 characteristics_finder_error_callback_;
382 353
383 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback 354 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback
384 notify_session_success_callback_; 355 notify_session_success_callback_;
385 device::BluetoothRemoteGattCharacteristic::ErrorCallback 356 device::BluetoothRemoteGattCharacteristic::ErrorCallback
386 notify_session_error_callback_; 357 notify_session_error_callback_;
387 358
388 base::Closure write_remote_characteristic_success_callback_; 359 base::Closure write_remote_characteristic_success_callback_;
389 device::BluetoothRemoteGattCharacteristic::ErrorCallback 360 device::BluetoothRemoteGattCharacteristic::ErrorCallback
390 write_remote_characteristic_error_callback_; 361 write_remote_characteristic_error_callback_;
362
363 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> packet_generator_;
391 }; 364 };
392 365
393 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 366 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
394 CreateAndDestroyWithouthConnectCallDoesntCrash) { 367 CreateAndDestroyWithoutConnectCallDoesntCrash) {
395 BluetoothLowEnergyConnection connection( 368 BluetoothLowEnergyWeaveClientConnection connection(
396 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 369 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
397 kMaxNumberOfTries); 370 kMaxNumberOfTries);
398 } 371 }
399 372
400 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 373 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
401 Disconect_WithoutConnectDoesntCrash) { 374 DisconectWithoutConnectDoesntCrash) {
402 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 375 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
403 CreateConnection()); 376 CreateConnection());
404 Disconnect(connection.get()); 377 Disconnect(connection.get());
405 } 378 }
406 379
407 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { 380 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
408 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 381 ConnectSuccess) {
382 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
409 CreateConnection()); 383 CreateConnection());
410 ConnectGatt(connection.get()); 384 ConnectGatt(connection.get());
411 CharacteristicsFound(connection.get()); 385 CharacteristicsFound(connection.get());
412 NotifySessionStarted(connection.get()); 386 NotifySessionStarted(connection.get());
413 ResponseSignalReceived(connection.get()); 387 ResponseSignalReceived(connection.get(), kDefaultPacketSize);
414 } 388 }
415 389
416 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 390 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
417 Connect_Success_Disconnect) { 391 ConnectSuccessDisconnect) {
418 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 392 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
419 CreateConnection()); 393 CreateConnection());
420 InitializeConnection(connection.get()); 394 InitializeConnection(connection.get(), kDefaultPacketSize);
421 Disconnect(connection.get()); 395 Disconnect(connection.get());
422 } 396 }
423 397
424 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 398 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
425 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { 399 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) {
426 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 400 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
427 CreateConnection()); 401 CreateConnection());
428 ConnectGatt(connection.get()); 402 ConnectGatt(connection.get());
429 Disconnect(connection.get()); 403 Disconnect(connection.get());
430 } 404 }
431 405
432 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 406 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
433 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { 407 ConnectIncompleteDisconnectFromWaitingNotifySessionState) {
434 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 408 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
435 CreateConnection()); 409 CreateConnection());
436 ConnectGatt(connection.get()); 410 ConnectGatt(connection.get());
437 CharacteristicsFound(connection.get()); 411 CharacteristicsFound(connection.get());
438 Disconnect(connection.get()); 412 Disconnect(connection.get());
439 } 413 }
440 414
441 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 415 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
442 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { 416 ConnectIncompleteDisconnectFromWaitingResponseSignalState) {
443 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 417 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
444 CreateConnection()); 418 CreateConnection());
445 ConnectGatt(connection.get()); 419 ConnectGatt(connection.get());
446 CharacteristicsFound(connection.get()); 420 CharacteristicsFound(connection.get());
447 NotifySessionStarted(connection.get()); 421 NotifySessionStarted(connection.get());
448 Disconnect(connection.get()); 422 Disconnect(connection.get());
449 } 423 }
450 424
451 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 425 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
452 Connect_Fails_CharacteristicsNotFound) { 426 ConnectFailsCharacteristicsNotFound) {
453 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 427 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
454 CreateConnection()); 428 CreateConnection());
455 ConnectGatt(connection.get()); 429 ConnectGatt(connection.get());
456 430
457 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); 431 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0);
458 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); 432 EXPECT_FALSE(characteristics_finder_success_callback_.is_null());
459 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); 433 ASSERT_FALSE(characteristics_finder_error_callback_.is_null());
460 434
461 characteristics_finder_error_callback_.Run( 435 characteristics_finder_error_callback_.Run(
462 {to_peripheral_char_uuid_, kToPeripheralCharID}, 436 {tx_characteristic_uuid_, kToPeripheralCharID},
463 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 437 {rx_characteristic_uuid_, kFromPeripheralCharID});
464 438
465 EXPECT_EQ(connection->sub_status(), 439 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
466 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
467 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 440 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
468 } 441 }
469 442
470 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 443 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
471 Connect_Fails_NotifySessionError) { 444 ConnectFailsNotifySessionError) {
472 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 445 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
473 CreateConnection()); 446 CreateConnection());
474 ConnectGatt(connection.get()); 447 ConnectGatt(connection.get());
475 CharacteristicsFound(connection.get()); 448 CharacteristicsFound(connection.get());
476 449
477 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 450 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0);
478 .Times(0);
479 EXPECT_FALSE(notify_session_success_callback_.is_null()); 451 EXPECT_FALSE(notify_session_success_callback_.is_null());
480 ASSERT_FALSE(notify_session_error_callback_.is_null()); 452 ASSERT_FALSE(notify_session_error_callback_.is_null());
481 453
482 notify_session_error_callback_.Run( 454 notify_session_error_callback_.Run(
483 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 455 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
484 456
485 EXPECT_EQ(connection->sub_status(), 457 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
486 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
487 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 458 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
488 } 459 }
489 460
490 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 461 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
491 Connect_Fails_ErrorSendingInviteToConnectSignal) { 462 ConnectFailsErrorSendingConnectionRequest) {
492 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 463 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
493 CreateConnection()); 464 CreateConnection());
494 ConnectGatt(connection.get()); 465 ConnectGatt(connection.get());
495 CharacteristicsFound(connection.get()); 466 CharacteristicsFound(connection.get());
496 NotifySessionStarted(connection.get()); 467 NotifySessionStarted(connection.get());
497 468
498 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the 469 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the
499 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for 470 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for
500 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use 471 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use
501 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. 472 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement.
502 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); 473 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0);
503 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 474 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
504 .Times(kMaxNumberOfTries - 1) 475 .Times(kMaxNumberOfTries - 1)
505 .WillRepeatedly( 476 .WillRepeatedly(
506 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 477 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
507 SaveArg<1>(&write_remote_characteristic_success_callback_), 478 SaveArg<1>(&write_remote_characteristic_success_callback_),
508 SaveArg<2>(&write_remote_characteristic_error_callback_))); 479 SaveArg<2>(&write_remote_characteristic_error_callback_)));
509 480
510 for (int i = 0; i < kMaxNumberOfTries; i++) { 481 for (int i = 0; i < kMaxNumberOfTries; i++) {
511 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 482 const std::vector<uint8_t> request =
512 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 483 packet_generator_->CreateConnectionRequest();
513 kInviteToConnectSignal)); 484 EXPECT_EQ(last_value_written_on_tx_characteristic_, request);
514 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
515 kInviteToConnectSignal);
516 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); 485 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null());
517 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); 486 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null());
518 write_remote_characteristic_error_callback_.Run( 487 write_remote_characteristic_error_callback_.Run(
519 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 488 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
520 } 489 }
521 490
522 EXPECT_EQ(connection->sub_status(), 491 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED);
523 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
524 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 492 EXPECT_EQ(connection->status(), Connection::DISCONNECTED);
525 } 493 }
526 494
527 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 495 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
528 Receive_MessageSmallerThanCharacteristicSize) { 496 ReceiveMessageSmallerThanCharacteristicSize) {
529 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 497 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
530 CreateConnection()); 498 CreateConnection());
531 InitializeConnection(connection.get()); 499 InitializeConnection(connection.get(), kDefaultPacketSize);
532 500
533 std::string received_bytes; 501 std::string received_bytes;
534 EXPECT_CALL(*connection, OnBytesReceived(_)) 502 EXPECT_CALL(*connection, OnBytesReceived(_))
535 .WillOnce(SaveArg<0>(&received_bytes)); 503 .WillOnce(SaveArg<0>(&received_bytes));
536 504
537 // Message (bytes) that is going to be received. 505 // Message (bytes) that is going to be received.
538 std::string message(100, 'A'); 506 std::string message(kDefaultPacketSize - 1, 'A');
539 507
540 // Sending the |kSendSignal| + |message_size| + |message|. 508 std::vector<std::vector<uint8_t>> packets =
509 packet_generator_->EncodeDataMessage(message);
510
541 connection->GattCharacteristicValueChanged( 511 connection->GattCharacteristicValueChanged(
542 adapter_.get(), from_peripheral_char_.get(), 512 adapter_.get(), rx_characteristic_.get(), packets[0]);
543 CreateFirstCharacteristicValue(message, message.size()));
544 513
545 EXPECT_EQ(received_bytes, message); 514 EXPECT_EQ(received_bytes, message);
546 } 515 }
547 516
548 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 517 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
549 Receive_MessageLargerThanCharacteristicSize) { 518 ReceiveMessageLargerThanCharacteristicSize) {
550 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 519 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
551 CreateConnection()); 520 CreateConnection());
552 InitializeConnection(connection.get()); 521
522 const uint32_t selected_packet_size = 500;
523 InitializeConnection(connection.get(), selected_packet_size);
553 524
554 std::string received_bytes; 525 std::string received_bytes;
555 int chunk_size = 500;
556 EXPECT_CALL(*connection, OnBytesReceived(_)) 526 EXPECT_CALL(*connection, OnBytesReceived(_))
557 .WillOnce(SaveArg<0>(&received_bytes)); 527 .WillOnce(SaveArg<0>(&received_bytes));
558 528
559 // Message (bytes) that is going to be received. 529 // Message (bytes) that is going to be received.
560 int message_size = 600; 530 std::string message(600, 'A');
561 std::string message(message_size, 'A');
562 531
563 // Sending the |kSendSignal| + |message_size| + |message| (truncated at 532 std::vector<std::vector<uint8_t>> packets =
564 // |chunk_size|). 533 packet_generator_->EncodeDataMessage(message);
565 int first_write_payload_size =
566 chunk_size - CreateSendSignalWithSize(message_size).size();
567 connection->GattCharacteristicValueChanged(
568 adapter_.get(), from_peripheral_char_.get(),
569 CreateFirstCharacteristicValue(
570 message.substr(0, first_write_payload_size), message.size()));
571 534
572 // Sending the remaining bytes. 535 for (auto packet : packets) {
573 std::vector<uint8_t> value; 536 connection->GattCharacteristicValueChanged(
574 value.push_back(0); 537 adapter_.get(), rx_characteristic_.get(), packet);
575 value.insert(value.end(), message.begin() + first_write_payload_size, 538 }
576 message.end());
577 connection->GattCharacteristicValueChanged(
578 adapter_.get(), from_peripheral_char_.get(), value);
579
580 EXPECT_EQ(received_bytes, message); 539 EXPECT_EQ(received_bytes, message);
581 } 540 }
582 541
583 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 542 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
584 SendMessage_SmallerThanCharacteristicSize) { 543 SendMessageSmallerThanCharacteristicSize) {
585 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 544 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
586 CreateConnection()); 545 CreateConnection());
587 InitializeConnection(connection.get()); 546 InitializeConnection(connection.get(), kDefaultPacketSize);
588 547
589 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is 548 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is
590 // called. 549 // called.
591 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 550 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
592 .WillOnce( 551 .WillOnce(
593 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 552 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
594 SaveArg<1>(&write_remote_characteristic_success_callback_), 553 SaveArg<1>(&write_remote_characteristic_success_callback_),
595 SaveArg<2>(&write_remote_characteristic_error_callback_))); 554 SaveArg<2>(&write_remote_characteristic_error_callback_)));
596 555
597 // Message (bytes) that is going to be sent. 556 // Message (bytes) that is going to be sent.
598 int message_size = 100; 557 std::string message(kDefaultPacketSize - 1, 'A');
599 std::string message(message_size, 'A');
600 message[0] = 'B'; 558 message[0] = 'B';
601 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); 559 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message)));
560 std::vector<std::vector<uint8_t>> packets =
561 packet_generator_->EncodeDataMessage(message);
602 562
603 // Expecting that |kSendSignal| + |message_size| + |message| was written. 563 // Expecting that |kSendSignal| + |message_size| + |message| was written.
604 EXPECT_EQ(last_value_written_on_to_peripheral_char_, 564 EXPECT_EQ(last_value_written_on_tx_characteristic_, packets[0]);
605 CreateFirstCharacteristicValue(message, message.size()));
606 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); 565 EXPECT_CALL(*connection, OnDidSendMessage(_, _));
607 566
608 RunWriteCharacteristicSuccessCallback(); 567 RunWriteCharacteristicSuccessCallback();
609 } 568 }
610 569
611 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 570 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
612 SendMessage_LagerThanCharacteristicSize) { 571 SendMessageLargerThanCharacteristicSize) {
613 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 572 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
614 CreateConnection()); 573 CreateConnection());
615 InitializeConnection(connection.get()); 574
575 const uint32_t selected_packet_size = 500;
576 InitializeConnection(connection.get(), selected_packet_size);
616 577
617 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is 578 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is
618 // called. 579 // called.
619 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 580 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
620 .WillOnce( 581 .WillOnce(
621 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 582 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
622 SaveArg<1>(&write_remote_characteristic_success_callback_), 583 SaveArg<1>(&write_remote_characteristic_success_callback_),
623 SaveArg<2>(&write_remote_characteristic_error_callback_))); 584 SaveArg<2>(&write_remote_characteristic_error_callback_)));
624 585
625 // Message (bytes) that is going to be sent. 586 // Message (bytes) that is going to be sent.
626 int message_size = 600; 587 std::string message(600, 'A');
627 std::string message(message_size, 'A');
628 message[0] = 'B'; 588 message[0] = 'B';
629 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); 589 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message)));
630 590
631 // Expecting that |kSendSignal| + |message_size| was written in the first 8 591 std::vector<std::vector<uint8_t>> packets =
632 // bytes. 592 packet_generator_->EncodeDataMessage(message);
633 std::vector<uint8_t> prefix( 593 EXPECT_EQ(last_value_written_on_tx_characteristic_, packets[0]);
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( 594 std::vector<uint8_t> bytes_received(
638 last_value_written_on_to_peripheral_char_.begin() + 8, 595 last_value_written_on_tx_characteristic_.begin() + 1,
639 last_value_written_on_to_peripheral_char_.end()); 596 last_value_written_on_tx_characteristic_.end());
640 597
641 // Expecting a second call of WriteRemoteCharacteristic, after success 598 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _))
642 // callback is called.
643 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _))
644 .WillOnce( 599 .WillOnce(
645 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 600 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_),
646 SaveArg<1>(&write_remote_characteristic_success_callback_), 601 SaveArg<1>(&write_remote_characteristic_success_callback_),
647 SaveArg<2>(&write_remote_characteristic_error_callback_))); 602 SaveArg<2>(&write_remote_characteristic_error_callback_)));
648 603
649 RunWriteCharacteristicSuccessCallback(); 604 RunWriteCharacteristicSuccessCallback();
650 bytes_received.insert(bytes_received.end(), 605 bytes_received.insert(bytes_received.end(),
651 last_value_written_on_to_peripheral_char_.begin() + 1, 606 last_value_written_on_tx_characteristic_.begin() + 1,
652 last_value_written_on_to_peripheral_char_.end()); 607 last_value_written_on_tx_characteristic_.end());
653 608
654 // Expecting that the message was written. 609 // Expecting that the message was written.
655 std::vector<uint8_t> expected_value(message.begin(), message.end()); 610 std::vector<uint8_t> expected_value(message.begin(), message.end());
656 EXPECT_EQ(expected_value.size(), bytes_received.size()); 611 EXPECT_EQ(expected_value.size(), bytes_received.size());
657 EXPECT_EQ(expected_value, bytes_received); 612 EXPECT_EQ(expected_value, bytes_received);
658 613
659 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); 614 EXPECT_CALL(*connection, OnDidSendMessage(_, _));
660 RunWriteCharacteristicSuccessCallback(); 615 RunWriteCharacteristicSuccessCallback();
661 } 616 }
662 617
663 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 618 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest,
664 Connect_AfterADelayWhenThrottled) { 619 ConnectAfterDelayWhenThrottled) {
665 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 620 std::unique_ptr<MockBluetoothLowEnergyWeaveClientConnection> connection(
666 CreateConnection()); 621 CreateConnection());
667 622
668 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 623 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
669 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); 624 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1))));
670 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 625 EXPECT_CALL(*device_, CreateGattConnection(_, _))
671 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 626 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
672 SaveArg<1>(&create_gatt_connection_error_callback_))); 627 SaveArg<1>(&create_gatt_connection_error_callback_)));
673 628
674 // No GATT connection should be created before the delay. 629 // No GATT connection should be created before the delay.
675 connection->Connect(); 630 connection->Connect();
676 EXPECT_EQ(connection->sub_status(), 631 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION);
677 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
678 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 632 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
679 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); 633 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null());
680 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); 634 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null());
681 635
682 // A GATT connection should be created after the delay. 636 // A GATT connection should be created after the delay.
683 task_runner_->RunUntilIdle(); 637 task_runner_->RunUntilIdle();
684 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 638 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
685 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 639 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
686 640
687 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 641 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
688 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 642 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
689 .WillOnce(DoAll( 643 .WillOnce(DoAll(
690 SaveArg<0>(&characteristics_finder_success_callback_), 644 SaveArg<0>(&characteristics_finder_success_callback_),
691 SaveArg<1>(&characteristics_finder_error_callback_), 645 SaveArg<1>(&characteristics_finder_error_callback_),
692 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 646 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
693 647
694 create_gatt_connection_success_callback_.Run( 648 create_gatt_connection_success_callback_.Run(
695 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( 649 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>(
696 adapter_, kTestRemoteDeviceBluetoothAddress))); 650 adapter_, kTestRemoteDeviceBluetoothAddress)));
697 651
698 CharacteristicsFound(connection.get()); 652 CharacteristicsFound(connection.get());
699 NotifySessionStarted(connection.get()); 653 NotifySessionStarted(connection.get());
700 ResponseSignalReceived(connection.get()); 654 ResponseSignalReceived(connection.get(), kDefaultPacketSize);
701 } 655 }
702 656
703 } // namespace proximity_auth 657 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698