| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "components/proximity_auth/ble/remote_attribute.h" | |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 13 #include "device/bluetooth/bluetooth_uuid.h" | |
| 14 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
| 15 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
| 16 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | |
| 17 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 using testing::_; | |
| 22 using testing::AtLeast; | |
| 23 using testing::NiceMock; | |
| 24 using testing::Return; | |
| 25 using testing::StrictMock; | |
| 26 using testing::SaveArg; | |
| 27 | |
| 28 namespace proximity_auth { | |
| 29 namespace { | |
| 30 | |
| 31 const char kDeviceName[] = "Device name"; | |
| 32 const char kBluetoothAddress[] = "11:22:33:44:55:66"; | |
| 33 | |
| 34 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | |
| 35 const char kToPeripheralCharUUID[] = "FBAE09F2-0482-11E5-8418-1697F925EC7B"; | |
| 36 const char kFromPeripheralCharUUID[] = "5539ED10-0483-11E5-8418-1697F925EC7B"; | |
| 37 | |
| 38 const char kToPeripheralCharID[] = "to peripheral id"; | |
| 39 const char kFromPeripheralCharID[] = "from peripheral id"; | |
| 40 | |
| 41 const device::BluetoothRemoteGattCharacteristic::Properties | |
| 42 kCharacteristicProperties = | |
| 43 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | | |
| 44 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | | |
| 45 device::BluetoothRemoteGattCharacteristic:: | |
| 46 PROPERTY_WRITE_WITHOUT_RESPONSE | | |
| 47 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; | |
| 48 | |
| 49 const char kOtherCharUUID[] = "09731422-048A-11E5-8418-1697F925EC7B"; | |
| 50 const char kOtherCharID[] = "other id"; | |
| 51 } // namespace | |
| 52 | |
| 53 class ProximityAuthBluetoothLowEnergyCharacteristicFinderTest | |
| 54 : public testing::Test { | |
| 55 protected: | |
| 56 ProximityAuthBluetoothLowEnergyCharacteristicFinderTest() | |
| 57 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | |
| 58 success_callback_(base::Bind( | |
| 59 &ProximityAuthBluetoothLowEnergyCharacteristicFinderTest:: | |
| 60 OnCharacteristicsFound, | |
| 61 base::Unretained(this))), | |
| 62 error_callback_(base::Bind( | |
| 63 &ProximityAuthBluetoothLowEnergyCharacteristicFinderTest:: | |
| 64 OnCharacteristicsFinderError, | |
| 65 base::Unretained(this))), | |
| 66 device_(new NiceMock<device::MockBluetoothDevice>(adapter_.get(), | |
| 67 0, | |
| 68 kDeviceName, | |
| 69 kBluetoothAddress, | |
| 70 false, | |
| 71 false)), | |
| 72 service_(new NiceMock<device::MockBluetoothGattService>( | |
| 73 device_.get(), | |
| 74 "", | |
| 75 device::BluetoothUUID(kServiceUUID), | |
| 76 true, | |
| 77 false)), | |
| 78 remote_service_({device::BluetoothUUID(kServiceUUID), ""}), | |
| 79 to_peripheral_char_({device::BluetoothUUID(kToPeripheralCharUUID), ""}), | |
| 80 from_peripheral_char_( | |
| 81 {device::BluetoothUUID(kFromPeripheralCharUUID), ""}) { | |
| 82 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
| 83 | |
| 84 // The default behavior for |device_| is to have no services discovered. Can | |
| 85 // be overrided later. | |
| 86 ON_CALL(*device_, GetGattServices()) | |
| 87 .WillByDefault( | |
| 88 Return(std::vector<device::BluetoothRemoteGattService*>())); | |
| 89 } | |
| 90 | |
| 91 void SetUp() { | |
| 92 EXPECT_CALL(*adapter_, AddObserver(_)); | |
| 93 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
| 94 } | |
| 95 | |
| 96 MOCK_METHOD3(OnCharacteristicsFound, | |
| 97 void(const RemoteAttribute&, | |
| 98 const RemoteAttribute&, | |
| 99 const RemoteAttribute&)); | |
| 100 MOCK_METHOD2(OnCharacteristicsFinderError, | |
| 101 void(const RemoteAttribute&, const RemoteAttribute&)); | |
| 102 | |
| 103 std::unique_ptr<device::MockBluetoothGattCharacteristic> | |
| 104 ExpectToFindCharacteristic(const device::BluetoothUUID& uuid, | |
| 105 const std::string& id, | |
| 106 bool valid) { | |
| 107 std::unique_ptr<device::MockBluetoothGattCharacteristic> characteristic( | |
| 108 new NiceMock<device::MockBluetoothGattCharacteristic>( | |
| 109 service_.get(), id, uuid, true, kCharacteristicProperties, | |
| 110 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); | |
| 111 | |
| 112 ON_CALL(*characteristic.get(), GetUUID()).WillByDefault(Return(uuid)); | |
| 113 if (valid) | |
| 114 ON_CALL(*characteristic.get(), GetIdentifier()).WillByDefault(Return(id)); | |
| 115 ON_CALL(*characteristic.get(), GetService()) | |
| 116 .WillByDefault(Return(service_.get())); | |
| 117 return characteristic; | |
| 118 } | |
| 119 | |
| 120 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
| 121 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback success_callback_; | |
| 122 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback error_callback_; | |
| 123 std::unique_ptr<device::MockBluetoothDevice> device_; | |
| 124 std::unique_ptr<device::MockBluetoothGattService> service_; | |
| 125 RemoteAttribute remote_service_; | |
| 126 RemoteAttribute to_peripheral_char_; | |
| 127 RemoteAttribute from_peripheral_char_; | |
| 128 }; | |
| 129 | |
| 130 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 131 ConstructAndDestroyDontCrash) { | |
| 132 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 133 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 134 from_peripheral_char_, success_callback_, error_callback_); | |
| 135 } | |
| 136 | |
| 137 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 138 FindRightCharacteristics) { | |
| 139 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 140 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 141 from_peripheral_char_, success_callback_, error_callback_); | |
| 142 // Upcasting |characteristic_finder| to access the virtual protected methods | |
| 143 // from Observer: GattCharacteristicAdded() and | |
| 144 // GattDiscoveryCompleteForService(). | |
| 145 device::BluetoothAdapter::Observer* observer = | |
| 146 static_cast<device::BluetoothAdapter::Observer*>(&characteristic_finder); | |
| 147 | |
| 148 RemoteAttribute found_to_char, found_from_char; | |
| 149 EXPECT_CALL(*this, OnCharacteristicsFound(_, _, _)) | |
| 150 .WillOnce( | |
| 151 DoAll(SaveArg<1>(&found_to_char), SaveArg<2>(&found_from_char))); | |
| 152 EXPECT_CALL(*this, OnCharacteristicsFinderError(_, _)).Times(0); | |
| 153 | |
| 154 std::unique_ptr<device::MockBluetoothGattCharacteristic> from_char = | |
| 155 ExpectToFindCharacteristic(device::BluetoothUUID(kFromPeripheralCharUUID), | |
| 156 kFromPeripheralCharID, true); | |
| 157 observer->GattCharacteristicAdded(adapter_.get(), from_char.get()); | |
| 158 | |
| 159 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_char = | |
| 160 ExpectToFindCharacteristic(device::BluetoothUUID(kToPeripheralCharUUID), | |
| 161 kToPeripheralCharID, true); | |
| 162 observer->GattCharacteristicAdded(adapter_.get(), to_char.get()); | |
| 163 | |
| 164 EXPECT_EQ(kToPeripheralCharID, found_to_char.id); | |
| 165 EXPECT_EQ(kFromPeripheralCharID, found_from_char.id); | |
| 166 | |
| 167 EXPECT_CALL(*service_, GetUUID()) | |
| 168 .WillOnce(Return(device::BluetoothUUID(kServiceUUID))); | |
| 169 observer->GattDiscoveryCompleteForService(adapter_.get(), service_.get()); | |
| 170 } | |
| 171 | |
| 172 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 173 DidntFindRightCharacteristics) { | |
| 174 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 175 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 176 from_peripheral_char_, success_callback_, error_callback_); | |
| 177 device::BluetoothAdapter::Observer* observer = | |
| 178 static_cast<device::BluetoothAdapter::Observer*>(&characteristic_finder); | |
| 179 | |
| 180 EXPECT_CALL(*this, OnCharacteristicsFound(_, _, _)).Times(0); | |
| 181 EXPECT_CALL(*this, OnCharacteristicsFinderError(_, _)); | |
| 182 | |
| 183 std::unique_ptr<device::MockBluetoothGattCharacteristic> other_char = | |
| 184 ExpectToFindCharacteristic(device::BluetoothUUID(kOtherCharUUID), | |
| 185 kOtherCharID, false); | |
| 186 observer->GattCharacteristicAdded(adapter_.get(), other_char.get()); | |
| 187 | |
| 188 EXPECT_CALL(*service_, GetUUID()) | |
| 189 .WillOnce(Return(device::BluetoothUUID(kServiceUUID))); | |
| 190 observer->GattDiscoveryCompleteForService(adapter_.get(), service_.get()); | |
| 191 } | |
| 192 | |
| 193 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 194 FindOnlyOneRightCharacteristic) { | |
| 195 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 196 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 197 from_peripheral_char_, success_callback_, error_callback_); | |
| 198 device::BluetoothAdapter::Observer* observer = | |
| 199 static_cast<device::BluetoothAdapter::Observer*>(&characteristic_finder); | |
| 200 | |
| 201 RemoteAttribute found_to_char, found_from_char; | |
| 202 EXPECT_CALL(*this, OnCharacteristicsFound(_, _, _)).Times(0); | |
| 203 EXPECT_CALL(*this, OnCharacteristicsFinderError(_, _)) | |
| 204 .WillOnce( | |
| 205 DoAll(SaveArg<0>(&found_to_char), SaveArg<1>(&found_from_char))); | |
| 206 | |
| 207 std::unique_ptr<device::MockBluetoothGattCharacteristic> from_char = | |
| 208 ExpectToFindCharacteristic(device::BluetoothUUID(kFromPeripheralCharUUID), | |
| 209 kFromPeripheralCharID, true); | |
| 210 observer->GattCharacteristicAdded(adapter_.get(), from_char.get()); | |
| 211 | |
| 212 EXPECT_CALL(*service_, GetUUID()) | |
| 213 .WillOnce(Return(device::BluetoothUUID(kServiceUUID))); | |
| 214 observer->GattDiscoveryCompleteForService(adapter_.get(), service_.get()); | |
| 215 EXPECT_EQ(kFromPeripheralCharID, found_from_char.id); | |
| 216 } | |
| 217 | |
| 218 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 219 FindWrongCharacteristic_FindRightCharacteristics) { | |
| 220 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 221 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 222 from_peripheral_char_, success_callback_, error_callback_); | |
| 223 device::BluetoothAdapter::Observer* observer = | |
| 224 static_cast<device::BluetoothAdapter::Observer*>(&characteristic_finder); | |
| 225 | |
| 226 RemoteAttribute found_to_char, found_from_char; | |
| 227 EXPECT_CALL(*this, OnCharacteristicsFound(_, _, _)) | |
| 228 .WillOnce( | |
| 229 DoAll(SaveArg<1>(&found_to_char), SaveArg<2>(&found_from_char))); | |
| 230 EXPECT_CALL(*this, OnCharacteristicsFinderError(_, _)).Times(0); | |
| 231 | |
| 232 std::unique_ptr<device::MockBluetoothGattCharacteristic> other_char = | |
| 233 ExpectToFindCharacteristic(device::BluetoothUUID(kOtherCharUUID), | |
| 234 kOtherCharID, false); | |
| 235 observer->GattCharacteristicAdded(adapter_.get(), other_char.get()); | |
| 236 | |
| 237 std::unique_ptr<device::MockBluetoothGattCharacteristic> from_char = | |
| 238 ExpectToFindCharacteristic(device::BluetoothUUID(kFromPeripheralCharUUID), | |
| 239 kFromPeripheralCharID, true); | |
| 240 observer->GattCharacteristicAdded(adapter_.get(), from_char.get()); | |
| 241 | |
| 242 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_char = | |
| 243 ExpectToFindCharacteristic(device::BluetoothUUID(kToPeripheralCharUUID), | |
| 244 kToPeripheralCharID, true); | |
| 245 observer->GattCharacteristicAdded(adapter_.get(), to_char.get()); | |
| 246 | |
| 247 EXPECT_EQ(kToPeripheralCharID, found_to_char.id); | |
| 248 EXPECT_EQ(kFromPeripheralCharID, found_from_char.id); | |
| 249 | |
| 250 EXPECT_CALL(*service_, GetUUID()) | |
| 251 .WillOnce(Return(device::BluetoothUUID(kServiceUUID))); | |
| 252 observer->GattDiscoveryCompleteForService(adapter_.get(), service_.get()); | |
| 253 } | |
| 254 | |
| 255 TEST_F(ProximityAuthBluetoothLowEnergyCharacteristicFinderTest, | |
| 256 RightCharacteristicsAlreadyPresent) { | |
| 257 RemoteAttribute found_to_char, found_from_char; | |
| 258 EXPECT_CALL(*this, OnCharacteristicsFound(_, _, _)) | |
| 259 .WillOnce( | |
| 260 DoAll(SaveArg<1>(&found_to_char), SaveArg<2>(&found_from_char))); | |
| 261 EXPECT_CALL(*this, OnCharacteristicsFinderError(_, _)).Times(0); | |
| 262 | |
| 263 std::unique_ptr<device::MockBluetoothGattCharacteristic> from_char = | |
| 264 ExpectToFindCharacteristic(device::BluetoothUUID(kFromPeripheralCharUUID), | |
| 265 kFromPeripheralCharID, true); | |
| 266 | |
| 267 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_char = | |
| 268 ExpectToFindCharacteristic(device::BluetoothUUID(kToPeripheralCharUUID), | |
| 269 kToPeripheralCharID, true); | |
| 270 | |
| 271 std::vector<device::BluetoothRemoteGattService*> services; | |
| 272 services.push_back(service_.get()); | |
| 273 ON_CALL(*device_, GetGattServices()).WillByDefault(Return(services)); | |
| 274 | |
| 275 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics; | |
| 276 characteristics.push_back(from_char.get()); | |
| 277 characteristics.push_back(to_char.get()); | |
| 278 ON_CALL(*service_, GetCharacteristics()) | |
| 279 .WillByDefault(Return(characteristics)); | |
| 280 | |
| 281 BluetoothLowEnergyCharacteristicsFinder characteristic_finder( | |
| 282 adapter_, device_.get(), remote_service_, to_peripheral_char_, | |
| 283 from_peripheral_char_, success_callback_, error_callback_); | |
| 284 device::BluetoothAdapter::Observer* observer = | |
| 285 static_cast<device::BluetoothAdapter::Observer*>(&characteristic_finder); | |
| 286 | |
| 287 EXPECT_EQ(kToPeripheralCharID, found_to_char.id); | |
| 288 EXPECT_EQ(kFromPeripheralCharID, found_from_char.id); | |
| 289 | |
| 290 EXPECT_CALL(*service_, GetUUID()) | |
| 291 .WillOnce(Return(device::BluetoothUUID(kServiceUUID))); | |
| 292 observer->GattDiscoveryCompleteForService(adapter_.get(), service_.get()); | |
| 293 } | |
| 294 | |
| 295 } // namespace proximity_auth | |
| OLD | NEW |