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