| 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 "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "content/common/bluetooth/web_bluetooth_device_id.h" | 8 #include "content/common/bluetooth/web_bluetooth_device_id.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 TEST_F(BluetoothAllowedDevicesMapTest, NoPermissionForAnyService) { | 184 TEST_F(BluetoothAllowedDevicesMapTest, NoPermissionForAnyService) { |
| 185 BluetoothAllowedDevicesMap allowed_devices_map; | 185 BluetoothAllowedDevicesMap allowed_devices_map; |
| 186 | 186 |
| 187 // Setup device. | 187 // Setup device. |
| 188 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | 188 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
| 189 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 189 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 190 blink::mojom::WebBluetoothScanFilterPtr scan_filter = | 190 blink::mojom::WebBluetoothScanFilterPtr scan_filter = |
| 191 blink::mojom::WebBluetoothScanFilter::New(); | 191 blink::mojom::WebBluetoothScanFilter::New(); |
| 192 | 192 |
| 193 scan_filter->name = kDeviceName; | 193 scan_filter->name = kDeviceName; |
| 194 options->filters.push_back(scan_filter.Clone()); | 194 options->filters.emplace(); |
| 195 options->filters->push_back({scan_filter.Clone()}); |
| 195 | 196 |
| 196 // Add to map. | 197 // Add to map. |
| 197 const WebBluetoothDeviceId device_id = | 198 const WebBluetoothDeviceId device_id = |
| 198 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | 199 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
| 199 | 200 |
| 200 // Try to access at least one service. | 201 // Try to access at least one service. |
| 201 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | 202 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( |
| 202 kTestOrigin1, device_id)); | 203 kTestOrigin1, device_id)); |
| 203 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 204 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 204 kTestOrigin1, device_id, kGlucoseUUID)); | 205 kTestOrigin1, device_id, kGlucoseUUID)); |
| 205 } | 206 } |
| 206 | 207 |
| 207 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { | 208 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_OneOriginOneDevice) { |
| 208 BluetoothAllowedDevicesMap allowed_devices_map; | 209 BluetoothAllowedDevicesMap allowed_devices_map; |
| 209 | 210 |
| 210 // Setup device. | 211 // Setup device. |
| 211 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | 212 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
| 212 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 213 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 213 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | 214 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = |
| 214 blink::mojom::WebBluetoothScanFilter::New(); | 215 blink::mojom::WebBluetoothScanFilter::New(); |
| 215 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | 216 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = |
| 216 blink::mojom::WebBluetoothScanFilter::New(); | 217 blink::mojom::WebBluetoothScanFilter::New(); |
| 217 | 218 |
| 218 scan_filter1->services.emplace(); | 219 scan_filter1->services.emplace(); |
| 219 scan_filter1->services->push_back(kGlucoseUUID); | 220 scan_filter1->services->push_back(kGlucoseUUID); |
| 220 options->filters.push_back(scan_filter1.Clone()); | 221 options->filters.emplace(); |
| 222 options->filters->push_back(scan_filter1.Clone()); |
| 221 | 223 |
| 222 scan_filter2->services.emplace(); | 224 scan_filter2->services.emplace(); |
| 223 scan_filter2->services->push_back(kHeartRateUUID); | 225 scan_filter2->services->push_back(kHeartRateUUID); |
| 224 options->filters.push_back(scan_filter2.Clone()); | 226 options->filters->push_back(scan_filter2.Clone()); |
| 225 | 227 |
| 226 options->optional_services.push_back(kBatteryServiceUUID); | 228 options->optional_services.push_back(kBatteryServiceUUID); |
| 227 options->optional_services.push_back(kHeartRateUUID); | 229 options->optional_services.push_back(kHeartRateUUID); |
| 228 | 230 |
| 229 // Add to map. | 231 // Add to map. |
| 230 const WebBluetoothDeviceId device_id1 = | 232 const WebBluetoothDeviceId device_id1 = |
| 231 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | 233 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
| 232 | 234 |
| 233 // Access allowed services. | 235 // Access allowed services. |
| 234 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | 236 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 253 kTestOrigin1, device_id1, kGlucoseUUID)); | 255 kTestOrigin1, device_id1, kGlucoseUUID)); |
| 254 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 256 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 255 kTestOrigin1, device_id1, kHeartRateUUID)); | 257 kTestOrigin1, device_id1, kHeartRateUUID)); |
| 256 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( | 258 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 257 kTestOrigin1, device_id1, kBatteryServiceUUID)); | 259 kTestOrigin1, device_id1, kBatteryServiceUUID)); |
| 258 | 260 |
| 259 // Add device back. | 261 // Add device back. |
| 260 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 262 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 261 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 263 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 262 | 264 |
| 263 options2->filters.push_back(scan_filter1.Clone()); | 265 options2->filters.emplace(); |
| 264 options2->filters.push_back(scan_filter2.Clone()); | 266 options2->filters->push_back(scan_filter1.Clone()); |
| 267 options2->filters->push_back(scan_filter2.Clone()); |
| 265 | 268 |
| 266 const WebBluetoothDeviceId device_id2 = | 269 const WebBluetoothDeviceId device_id2 = |
| 267 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | 270 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
| 268 | 271 |
| 269 // Access allowed services. | 272 // Access allowed services. |
| 270 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | 273 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( |
| 271 kTestOrigin1, device_id2)); | 274 kTestOrigin1, device_id2)); |
| 272 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 275 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| 273 kTestOrigin1, device_id2, kGlucoseUUID)); | 276 kTestOrigin1, device_id2, kGlucoseUUID)); |
| 274 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( | 277 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessService( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 291 BluetoothAllowedDevicesMap allowed_devices_map; | 294 BluetoothAllowedDevicesMap allowed_devices_map; |
| 292 | 295 |
| 293 // Setup request for device #1. | 296 // Setup request for device #1. |
| 294 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 297 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
| 295 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 298 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 296 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | 299 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = |
| 297 blink::mojom::WebBluetoothScanFilter::New(); | 300 blink::mojom::WebBluetoothScanFilter::New(); |
| 298 | 301 |
| 299 scan_filter1->services.emplace(); | 302 scan_filter1->services.emplace(); |
| 300 scan_filter1->services->push_back(kGlucoseUUID); | 303 scan_filter1->services->push_back(kGlucoseUUID); |
| 301 options1->filters.push_back(std::move(scan_filter1)); | 304 options1->filters.emplace(); |
| 305 options1->filters->push_back(std::move(scan_filter1)); |
| 302 | 306 |
| 303 options1->optional_services.push_back(kHeartRateUUID); | 307 options1->optional_services.push_back(kHeartRateUUID); |
| 304 | 308 |
| 305 // Setup request for device #2. | 309 // Setup request for device #2. |
| 306 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 310 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 307 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 311 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 308 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | 312 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = |
| 309 blink::mojom::WebBluetoothScanFilter::New(); | 313 blink::mojom::WebBluetoothScanFilter::New(); |
| 310 | 314 |
| 311 scan_filter2->services.emplace(); | 315 scan_filter2->services.emplace(); |
| 312 scan_filter2->services->push_back(kBatteryServiceUUID); | 316 scan_filter2->services->push_back(kBatteryServiceUUID); |
| 313 options2->filters.push_back(std::move(scan_filter2)); | 317 options2->filters.emplace(); |
| 318 options2->filters->push_back(std::move(scan_filter2)); |
| 314 | 319 |
| 315 options2->optional_services.push_back(kBloodPressureUUID); | 320 options2->optional_services.push_back(kBloodPressureUUID); |
| 316 | 321 |
| 317 // Add devices to map. | 322 // Add devices to map. |
| 318 const WebBluetoothDeviceId& device_id1 = | 323 const WebBluetoothDeviceId& device_id1 = |
| 319 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 324 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 320 const WebBluetoothDeviceId& device_id2 = | 325 const WebBluetoothDeviceId& device_id2 = |
| 321 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); | 326 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress2, options2); |
| 322 | 327 |
| 323 // Access allowed services. | 328 // Access allowed services. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 354 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { | 359 TEST_F(BluetoothAllowedDevicesMapTest, AllowedServices_TwoOriginsOneDevice) { |
| 355 BluetoothAllowedDevicesMap allowed_devices_map; | 360 BluetoothAllowedDevicesMap allowed_devices_map; |
| 356 // Setup request #1 for device. | 361 // Setup request #1 for device. |
| 357 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 362 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
| 358 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 363 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 359 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | 364 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = |
| 360 blink::mojom::WebBluetoothScanFilter::New(); | 365 blink::mojom::WebBluetoothScanFilter::New(); |
| 361 | 366 |
| 362 scan_filter1->services.emplace(); | 367 scan_filter1->services.emplace(); |
| 363 scan_filter1->services->push_back(kGlucoseUUID); | 368 scan_filter1->services->push_back(kGlucoseUUID); |
| 364 options1->filters.push_back(std::move(scan_filter1)); | 369 options1->filters.emplace(); |
| 370 options1->filters->push_back(std::move(scan_filter1)); |
| 365 | 371 |
| 366 options1->optional_services.push_back(kHeartRateUUID); | 372 options1->optional_services.push_back(kHeartRateUUID); |
| 367 | 373 |
| 368 // Setup request #2 for device. | 374 // Setup request #2 for device. |
| 369 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 375 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 370 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 376 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 371 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | 377 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = |
| 372 blink::mojom::WebBluetoothScanFilter::New(); | 378 blink::mojom::WebBluetoothScanFilter::New(); |
| 373 | 379 |
| 374 scan_filter2->services.emplace(); | 380 scan_filter2->services.emplace(); |
| 375 scan_filter2->services->push_back(kBatteryServiceUUID); | 381 scan_filter2->services->push_back(kBatteryServiceUUID); |
| 376 options2->filters.push_back(std::move(scan_filter2)); | 382 options2->filters.emplace(); |
| 383 options2->filters->push_back(std::move(scan_filter2)); |
| 377 | 384 |
| 378 options2->optional_services.push_back(kBloodPressureUUID); | 385 options2->optional_services.push_back(kBloodPressureUUID); |
| 379 | 386 |
| 380 // Add devices to map. | 387 // Add devices to map. |
| 381 const WebBluetoothDeviceId& device_id1 = | 388 const WebBluetoothDeviceId& device_id1 = |
| 382 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 389 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 383 const WebBluetoothDeviceId& device_id2 = | 390 const WebBluetoothDeviceId& device_id2 = |
| 384 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); | 391 allowed_devices_map.AddDevice(kTestOrigin2, kDeviceAddress1, options2); |
| 385 | 392 |
| 386 // Access allowed services. | 393 // Access allowed services. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 BluetoothAllowedDevicesMap allowed_devices_map; | 443 BluetoothAllowedDevicesMap allowed_devices_map; |
| 437 | 444 |
| 438 // Setup first request. | 445 // Setup first request. |
| 439 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = | 446 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options1 = |
| 440 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 447 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 441 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = | 448 blink::mojom::WebBluetoothScanFilterPtr scan_filter1 = |
| 442 blink::mojom::WebBluetoothScanFilter::New(); | 449 blink::mojom::WebBluetoothScanFilter::New(); |
| 443 | 450 |
| 444 scan_filter1->services.emplace(); | 451 scan_filter1->services.emplace(); |
| 445 scan_filter1->services->push_back(kGlucoseUUID); | 452 scan_filter1->services->push_back(kGlucoseUUID); |
| 446 options1->filters.push_back(std::move(scan_filter1)); | 453 options1->filters.emplace(); |
| 454 options1->filters->push_back(std::move(scan_filter1)); |
| 447 | 455 |
| 448 options1->optional_services.push_back(kBatteryServiceUUID); | 456 options1->optional_services.push_back(kBatteryServiceUUID); |
| 449 | 457 |
| 450 // Add to map. | 458 // Add to map. |
| 451 const WebBluetoothDeviceId device_id1 = | 459 const WebBluetoothDeviceId device_id1 = |
| 452 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); | 460 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options1); |
| 453 | 461 |
| 454 // Setup second request. | 462 // Setup second request. |
| 455 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = | 463 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options2 = |
| 456 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 464 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 457 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = | 465 blink::mojom::WebBluetoothScanFilterPtr scan_filter2 = |
| 458 blink::mojom::WebBluetoothScanFilter::New(); | 466 blink::mojom::WebBluetoothScanFilter::New(); |
| 459 | 467 |
| 460 scan_filter2->services.emplace(); | 468 scan_filter2->services.emplace(); |
| 461 scan_filter2->services->push_back(kHeartRateUUID); | 469 scan_filter2->services->push_back(kHeartRateUUID); |
| 462 options2->filters.push_back(std::move(scan_filter2)); | 470 options2->filters.emplace(); |
| 471 options2->filters->push_back(std::move(scan_filter2)); |
| 463 | 472 |
| 464 options2->optional_services.push_back(kBloodPressureUUID); | 473 options2->optional_services.push_back(kBloodPressureUUID); |
| 465 | 474 |
| 466 // Add to map again. | 475 // Add to map again. |
| 467 const WebBluetoothDeviceId device_id2 = | 476 const WebBluetoothDeviceId device_id2 = |
| 468 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); | 477 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options2); |
| 469 | 478 |
| 470 EXPECT_EQ(device_id1, device_id2); | 479 EXPECT_EQ(device_id1, device_id2); |
| 471 | 480 |
| 472 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | 481 EXPECT_TRUE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 492 | 501 |
| 493 TEST_F(BluetoothAllowedDevicesMapTest, NoFilterServices) { | 502 TEST_F(BluetoothAllowedDevicesMapTest, NoFilterServices) { |
| 494 BluetoothAllowedDevicesMap allowed_devices_map; | 503 BluetoothAllowedDevicesMap allowed_devices_map; |
| 495 | 504 |
| 496 // Setup request. | 505 // Setup request. |
| 497 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = | 506 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options = |
| 498 blink::mojom::WebBluetoothRequestDeviceOptions::New(); | 507 blink::mojom::WebBluetoothRequestDeviceOptions::New(); |
| 499 blink::mojom::WebBluetoothScanFilterPtr scan_filter = | 508 blink::mojom::WebBluetoothScanFilterPtr scan_filter = |
| 500 blink::mojom::WebBluetoothScanFilter::New(); | 509 blink::mojom::WebBluetoothScanFilter::New(); |
| 501 | 510 |
| 502 options->filters.push_back(std::move(scan_filter)); | 511 options->filters.emplace(); |
| 512 options->filters->push_back(std::move(scan_filter)); |
| 503 | 513 |
| 504 // Add to map. | 514 // Add to map. |
| 505 const WebBluetoothDeviceId device_id = | 515 const WebBluetoothDeviceId device_id = |
| 506 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); | 516 allowed_devices_map.AddDevice(kTestOrigin1, kDeviceAddress1, options); |
| 507 | 517 |
| 508 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( | 518 EXPECT_FALSE(allowed_devices_map.IsOriginAllowedToAccessAtLeastOneService( |
| 509 kTestOrigin1, device_id)); | 519 kTestOrigin1, device_id)); |
| 510 } | 520 } |
| 511 | 521 |
| 512 } // namespace content | 522 } // namespace content |
| OLD | NEW |