| 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 "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
| 6 | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "content/common/bluetooth/web_bluetooth_device_id.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "url/gurl.h" | |
| 11 | |
| 12 using device::BluetoothUUID; | |
| 13 | |
| 14 namespace content { | |
| 15 namespace { | |
| 16 const url::Origin kTestOrigin1(GURL("https://www.example1.com")); | |
| 17 const url::Origin kTestOrigin2(GURL("https://www.example2.com")); | |
| 18 | |
| 19 const std::string kDeviceAddress1 = "00:00:00"; | |
| 20 const std::string kDeviceAddress2 = "11:11:11"; | |
| 21 | |
| 22 const std::string kDeviceName = "TestName"; | |
| 23 | |
| 24 const char kGlucoseUUIDString[] = "00001808-0000-1000-8000-00805f9b34fb"; | |
| 25 const char kHeartRateUUIDString[] = "0000180d-0000-1000-8000-00805f9b34fb"; | |
| 26 const char kBatteryServiceUUIDString[] = "0000180f-0000-1000-8000-00805f9b34fb"; | |
| 27 const char kBloodPressureUUIDString[] = "00001813-0000-1000-8000-00805f9b34fb"; | |
| 28 const char kCyclingPowerUUIDString[] = "00001818-0000-1000-8000-00805f9b34fb"; | |
| 29 const BluetoothUUID kGlucoseUUID(kGlucoseUUIDString); | |
| 30 const BluetoothUUID kHeartRateUUID(kHeartRateUUIDString); | |
| 31 const BluetoothUUID kBatteryServiceUUID(kBatteryServiceUUIDString); | |
| 32 const BluetoothUUID kBloodPressureUUID(kBloodPressureUUIDString); | |
| 33 const BluetoothUUID kCyclingPowerUUID(kCyclingPowerUUIDString); | |
| 34 | |
| 35 class BluetoothAllowedDevicesMapTest : public testing::Test { | |
| 36 protected: | |
| 37 BluetoothAllowedDevicesMapTest() { | |
| 38 empty_options_ = blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 39 } | |
| 40 | |
| 41 ~BluetoothAllowedDevicesMapTest() override {} | |
| 42 | |
| 43 blink::mojom::WebBluetoothRequestDeviceOptionsPtr empty_options_; | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 TEST_F(BluetoothAllowedDevicesMapTest, UniqueOriginNotSupported) { | |
| 49 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 50 | |
| 51 EXPECT_DEATH_IF_SUPPORTED(allowed_devices_map.AddDevice( | |
| 52 url::Origin(), kDeviceAddress1, empty_options_), | |
| 53 ""); | |
| 54 } | |
| 55 | |
| 56 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMap) { | |
| 57 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 58 | |
| 59 const WebBluetoothDeviceId& device_id = allowed_devices_map.AddDevice( | |
| 60 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 61 | |
| 62 // Test that we can retrieve the device address/id. | |
| 63 EXPECT_EQ(device_id, | |
| 64 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | |
| 65 EXPECT_EQ(kDeviceAddress1, | |
| 66 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); | |
| 67 } | |
| 68 | |
| 69 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceToMapTwice) { | |
| 70 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 71 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( | |
| 72 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 73 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( | |
| 74 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 75 | |
| 76 EXPECT_EQ(device_id1, device_id2); | |
| 77 | |
| 78 // Test that we can retrieve the device address/id. | |
| 79 EXPECT_EQ(device_id1, | |
| 80 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | |
| 81 EXPECT_EQ(kDeviceAddress1, | |
| 82 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | |
| 83 } | |
| 84 | |
| 85 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromSameOriginToMap) { | |
| 86 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 87 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( | |
| 88 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 89 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( | |
| 90 kTestOrigin1, kDeviceAddress2, empty_options_); | |
| 91 | |
| 92 EXPECT_NE(device_id1, device_id2); | |
| 93 | |
| 94 // Test that we can retrieve the device address/id. | |
| 95 EXPECT_EQ(device_id1, | |
| 96 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | |
| 97 EXPECT_EQ(device_id2, | |
| 98 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | |
| 99 | |
| 100 EXPECT_EQ(kDeviceAddress1, | |
| 101 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | |
| 102 EXPECT_EQ(kDeviceAddress2, | |
| 103 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | |
| 104 } | |
| 105 | |
| 106 TEST_F(BluetoothAllowedDevicesMapTest, AddTwoDevicesFromTwoOriginsToMap) { | |
| 107 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 108 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( | |
| 109 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 110 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( | |
| 111 kTestOrigin2, kDeviceAddress2, empty_options_); | |
| 112 | |
| 113 EXPECT_NE(device_id1, device_id2); | |
| 114 | |
| 115 // Test that the wrong origin doesn't have access to the device. | |
| 116 | |
| 117 EXPECT_EQ(nullptr, | |
| 118 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress2)); | |
| 119 EXPECT_EQ(nullptr, | |
| 120 allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress1)); | |
| 121 | |
| 122 EXPECT_EQ(base::EmptyString(), | |
| 123 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | |
| 124 EXPECT_EQ(base::EmptyString(), | |
| 125 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | |
| 126 | |
| 127 // Test that we can retrieve the device address/id. | |
| 128 EXPECT_EQ(device_id1, | |
| 129 *allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | |
| 130 EXPECT_EQ(device_id2, | |
| 131 *allowed_devices_map.GetDeviceId(kTestOrigin2, kDeviceAddress2)); | |
| 132 | |
| 133 EXPECT_EQ(kDeviceAddress1, | |
| 134 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id1)); | |
| 135 EXPECT_EQ(kDeviceAddress2, | |
| 136 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id2)); | |
| 137 } | |
| 138 | |
| 139 TEST_F(BluetoothAllowedDevicesMapTest, AddDeviceFromTwoOriginsToMap) { | |
| 140 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 141 const WebBluetoothDeviceId& device_id1 = allowed_devices_map.AddDevice( | |
| 142 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 143 const WebBluetoothDeviceId& device_id2 = allowed_devices_map.AddDevice( | |
| 144 kTestOrigin2, kDeviceAddress1, empty_options_); | |
| 145 | |
| 146 EXPECT_NE(device_id1, device_id2); | |
| 147 | |
| 148 // Test that the wrong origin doesn't have access to the device. | |
| 149 EXPECT_EQ(base::EmptyString(), | |
| 150 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id2)); | |
| 151 EXPECT_EQ(base::EmptyString(), | |
| 152 allowed_devices_map.GetDeviceAddress(kTestOrigin2, device_id1)); | |
| 153 } | |
| 154 | |
| 155 TEST_F(BluetoothAllowedDevicesMapTest, AddRemoveAddDeviceToMap) { | |
| 156 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 157 const WebBluetoothDeviceId device_id_first_time = | |
| 158 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, | |
| 159 empty_options_); | |
| 160 | |
| 161 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | |
| 162 | |
| 163 const WebBluetoothDeviceId device_id_second_time = | |
| 164 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, | |
| 165 empty_options_); | |
| 166 | |
| 167 EXPECT_NE(device_id_first_time, device_id_second_time); | |
| 168 } | |
| 169 | |
| 170 TEST_F(BluetoothAllowedDevicesMapTest, RemoveDeviceFromMap) { | |
| 171 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 172 | |
| 173 const WebBluetoothDeviceId device_id = allowed_devices_map.AddDevice( | |
| 174 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 175 | |
| 176 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | |
| 177 | |
| 178 EXPECT_EQ(nullptr, | |
| 179 allowed_devices_map.GetDeviceId(kTestOrigin1, kDeviceAddress1)); | |
| 180 EXPECT_EQ(base::EmptyString(), | |
| 181 allowed_devices_map.GetDeviceAddress(kTestOrigin1, device_id)); | |
| 182 } | |
| 183 | |
| 184 TEST_F(BluetoothAllowedDevicesMapTest, NoPermissionForAnyService) { | |
| 185 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 186 | |
| 187 // Setup device. | |
| 188 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | |
| 189 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 190 blink::mojom::WebBluetoothScanFilterPtr scan_filter = | |
| 191 blink::mojom::WebBluetoothScanFilter::New(); | |
| 192 | |
| 193 scan_filter->name = kDeviceName; | |
| 194 options->filters.emplace(); | |
| 195 options->filters->push_back({scan_filter.Clone()}); | |
| 196 | |
| 197 // Add to map. | |
| 198 const WebBluetoothDeviceId device_id = | |
| 199 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | |
| 200 | |
| 201 // Try to access at least one service. | |
| 202 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 203 kTestOrigin1, device_id)); | |
| 204 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 205 kTestOrigin1, device_id, kGlucoseUUID)); | |
| 206 } | |
| 207 | |
| 208 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { | |
| 209 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 210 | |
| 211 // Setup device. | |
| 212 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | |
| 213 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 214 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | |
| 215 blink::mojom::WebBluetoothScanFilter::New(); | |
| 216 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | |
| 217 blink::mojom::WebBluetoothScanFilter::New(); | |
| 218 | |
| 219 scan_filter1->services.emplace(); | |
| 220 scan_filter1->services->push_back(kGlucoseUUID); | |
| 221 options->filters.emplace(); | |
| 222 options->filters->push_back(scan_filter1.Clone()); | |
| 223 | |
| 224 scan_filter2->services.emplace(); | |
| 225 scan_filter2->services->push_back(kHeartRateUUID); | |
| 226 options->filters->push_back(scan_filter2.Clone()); | |
| 227 | |
| 228 options->optional_services.push_back(kBatteryServiceUUID); | |
| 229 options->optional_services.push_back(kHeartRateUUID); | |
| 230 | |
| 231 // Add to map. | |
| 232 const WebBluetoothDeviceId device_id1 = | |
| 233 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | |
| 234 | |
| 235 // Access allowed services. | |
| 236 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 237 kTestOrigin1, device_id1)); | |
| 238 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 239 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 240 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 241 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 242 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 243 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 244 | |
| 245 // Try to access a non-allowed service. | |
| 246 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 247 kTestOrigin1, device_id1, kBloodPressureUUID)); | |
| 248 | |
| 249 // Try to access allowed services after removing device. | |
| 250 allowed_devices_map.RemoveDevice(kTestOrigin1, kDeviceAddress1); | |
| 251 | |
| 252 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 253 kTestOrigin1, device_id1)); | |
| 254 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 255 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 256 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 257 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 258 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 259 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 260 | |
| 261 // Add device back. | |
| 262 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | |
| 263 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 264 | |
| 265 options2->filters.emplace(); | |
| 266 options2->filters->push_back(scan_filter1.Clone()); | |
| 267 options2->filters->push_back(scan_filter2.Clone()); | |
| 268 | |
| 269 const WebBluetoothDeviceId device_id2 = | |
| 270 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | |
| 271 | |
| 272 // Access allowed services. | |
| 273 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 274 kTestOrigin1, device_id2)); | |
| 275 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 276 kTestOrigin1, device_id2, kGlucoseUUID)); | |
| 277 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 278 kTestOrigin1, device_id2, kHeartRateUUID)); | |
| 279 | |
| 280 // Try to access a non-allowed service. | |
| 281 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 282 kTestOrigin1, device_id2, kBatteryServiceUUID)); | |
| 283 | |
| 284 // Try to access services from old device. | |
| 285 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 286 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 287 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 288 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 289 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 290 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 291 } | |
| 292 | |
| 293 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginTwoDevices) { | |
| 294 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 295 | |
| 296 // Setup request for device #1. | |
| 297 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | |
| 298 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 299 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | |
| 300 blink::mojom::WebBluetoothScanFilter::New(); | |
| 301 | |
| 302 scan_filter1->services.emplace(); | |
| 303 scan_filter1->services->push_back(kGlucoseUUID); | |
| 304 options1->filters.emplace(); | |
| 305 options1->filters->push_back(std::move(scan_filter1)); | |
| 306 | |
| 307 options1->optional_services.push_back(kHeartRateUUID); | |
| 308 | |
| 309 // Setup request for device #2. | |
| 310 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | |
| 311 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 312 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | |
| 313 blink::mojom::WebBluetoothScanFilter::New(); | |
| 314 | |
| 315 scan_filter2->services.emplace(); | |
| 316 scan_filter2->services->push_back(kBatteryServiceUUID); | |
| 317 options2->filters.emplace(); | |
| 318 options2->filters->push_back(std::move(scan_filter2)); | |
| 319 | |
| 320 options2->optional_services.push_back(kBloodPressureUUID); | |
| 321 | |
| 322 // Add devices to map. | |
| 323 const WebBluetoothDeviceId& device_id1 = | |
| 324 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | |
| 325 const WebBluetoothDeviceId& device_id2 = | |
| 326 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); | |
| 327 | |
| 328 // Access allowed services. | |
| 329 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 330 kTestOrigin1, device_id1)); | |
| 331 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 332 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 333 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 334 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 335 | |
| 336 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 337 kTestOrigin1, device_id2)); | |
| 338 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 339 kTestOrigin1, device_id2, kBatteryServiceUUID)); | |
| 340 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 341 kTestOrigin1, device_id2, kBloodPressureUUID)); | |
| 342 | |
| 343 // Try to access non-allowed services. | |
| 344 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 345 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 346 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 347 kTestOrigin1, device_id1, kBloodPressureUUID)); | |
| 348 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 349 kTestOrigin1, device_id1, kCyclingPowerUUID)); | |
| 350 | |
| 351 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 352 kTestOrigin1, device_id2, kGlucoseUUID)); | |
| 353 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 354 kTestOrigin1, device_id2, kHeartRateUUID)); | |
| 355 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 356 kTestOrigin1, device_id2, kCyclingPowerUUID)); | |
| 357 } | |
| 358 | |
| 359 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { | |
| 360 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 361 // Setup request #1 for device. | |
| 362 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | |
| 363 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 364 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | |
| 365 blink::mojom::WebBluetoothScanFilter::New(); | |
| 366 | |
| 367 scan_filter1->services.emplace(); | |
| 368 scan_filter1->services->push_back(kGlucoseUUID); | |
| 369 options1->filters.emplace(); | |
| 370 options1->filters->push_back(std::move(scan_filter1)); | |
| 371 | |
| 372 options1->optional_services.push_back(kHeartRateUUID); | |
| 373 | |
| 374 // Setup request #2 for device. | |
| 375 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | |
| 376 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 377 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | |
| 378 blink::mojom::WebBluetoothScanFilter::New(); | |
| 379 | |
| 380 scan_filter2->services.emplace(); | |
| 381 scan_filter2->services->push_back(kBatteryServiceUUID); | |
| 382 options2->filters.emplace(); | |
| 383 options2->filters->push_back(std::move(scan_filter2)); | |
| 384 | |
| 385 options2->optional_services.push_back(kBloodPressureUUID); | |
| 386 | |
| 387 // Add devices to map. | |
| 388 const WebBluetoothDeviceId& device_id1 = | |
| 389 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | |
| 390 const WebBluetoothDeviceId& device_id2 = | |
| 391 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); | |
| 392 | |
| 393 // Access allowed services. | |
| 394 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 395 kTestOrigin1, device_id1)); | |
| 396 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 397 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 398 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 399 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 400 | |
| 401 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 402 kTestOrigin2, device_id2)); | |
| 403 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 404 kTestOrigin2, device_id2, kBatteryServiceUUID)); | |
| 405 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 406 kTestOrigin2, device_id2, kBloodPressureUUID)); | |
| 407 | |
| 408 // Try to access non-allowed services. | |
| 409 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 410 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 411 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 412 kTestOrigin1, device_id1, kBloodPressureUUID)); | |
| 413 | |
| 414 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 415 kTestOrigin1, device_id2)); | |
| 416 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 417 kTestOrigin1, device_id2, kGlucoseUUID)); | |
| 418 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 419 kTestOrigin1, device_id2, kHeartRateUUID)); | |
| 420 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 421 kTestOrigin1, device_id2, kBatteryServiceUUID)); | |
| 422 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 423 kTestOrigin1, device_id2, kBloodPressureUUID)); | |
| 424 | |
| 425 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 426 kTestOrigin2, device_id2, kGlucoseUUID)); | |
| 427 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 428 kTestOrigin2, device_id2, kHeartRateUUID)); | |
| 429 | |
| 430 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 431 kTestOrigin2, device_id1)); | |
| 432 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 433 kTestOrigin2, device_id1, kGlucoseUUID)); | |
| 434 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 435 kTestOrigin2, device_id1, kHeartRateUUID)); | |
| 436 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 437 kTestOrigin2, device_id1, kBatteryServiceUUID)); | |
| 438 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 439 kTestOrigin2, device_id1, kBloodPressureUUID)); | |
| 440 } | |
| 441 | |
| 442 TEST_F(BluetoothAllowedDevicesMapTest, MergeServices) { | |
| 443 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 444 | |
| 445 // Setup first request. | |
| 446 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | |
| 447 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 448 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | |
| 449 blink::mojom::WebBluetoothScanFilter::New(); | |
| 450 | |
| 451 scan_filter1->services.emplace(); | |
| 452 scan_filter1->services->push_back(kGlucoseUUID); | |
| 453 options1->filters.emplace(); | |
| 454 options1->filters->push_back(std::move(scan_filter1)); | |
| 455 | |
| 456 options1->optional_services.push_back(kBatteryServiceUUID); | |
| 457 | |
| 458 // Add to map. | |
| 459 const WebBluetoothDeviceId device_id1 = | |
| 460 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | |
| 461 | |
| 462 // Setup second request. | |
| 463 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | |
| 464 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 465 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | |
| 466 blink::mojom::WebBluetoothScanFilter::New(); | |
| 467 | |
| 468 scan_filter2->services.emplace(); | |
| 469 scan_filter2->services->push_back(kHeartRateUUID); | |
| 470 options2->filters.emplace(); | |
| 471 options2->filters->push_back(std::move(scan_filter2)); | |
| 472 | |
| 473 options2->optional_services.push_back(kBloodPressureUUID); | |
| 474 | |
| 475 // Add to map again. | |
| 476 const WebBluetoothDeviceId device_id2 = | |
| 477 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | |
| 478 | |
| 479 EXPECT_EQ(device_id1, device_id2); | |
| 480 | |
| 481 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 482 kTestOrigin1, device_id1)); | |
| 483 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 484 kTestOrigin1, device_id1, kGlucoseUUID)); | |
| 485 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 486 kTestOrigin1, device_id1, kBatteryServiceUUID)); | |
| 487 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 488 kTestOrigin1, device_id1, kHeartRateUUID)); | |
| 489 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | |
| 490 kTestOrigin1, device_id1, kBloodPressureUUID)); | |
| 491 } | |
| 492 | |
| 493 TEST_F(BluetoothAllowedDevicesMapTest, CorrectIdFormat) { | |
| 494 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 495 | |
| 496 const WebBluetoothDeviceId& device_id = allowed_devices_map.AddDevice( | |
| 497 kTestOrigin1, kDeviceAddress1, empty_options_); | |
| 498 | |
| 499 EXPECT_TRUE(WebBluetoothDeviceId::IsValid(device_id.str())); | |
| 500 } | |
| 501 | |
| 502 TEST_F(BluetoothAllowedDevicesMapTest, NoFilterServices) { | |
| 503 BluetoothAllowedDevicesMap allowed_devices_map; | |
| 504 | |
| 505 // Setup request. | |
| 506 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | |
| 507 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | |
| 508 blink::mojom::WebBluetoothScanFilterPtr scan_filter = | |
| 509 blink::mojom::WebBluetoothScanFilter::New(); | |
| 510 | |
| 511 options->filters.emplace(); | |
| 512 options->filters->push_back(std::move(scan_filter)); | |
| 513 | |
| 514 // Add to map. | |
| 515 const WebBluetoothDeviceId device_id = | |
| 516 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | |
| 517 | |
| 518 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | |
| 519 kTestOrigin1, device_id)); | |
| 520 } | |
| 521 | |
| 522 } // namespace content | |
| OLD | NEW |