Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_blacklist.h" | 5 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 6 | 6 |
| 7 #include "device/bluetooth/bluetooth_uuid.h" | 7 #include "device/bluetooth/bluetooth_uuid.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 using device::BluetoothUUID; | 10 using device::BluetoothUUID; |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 std::string Canonicalize(const std::string& string) { | 16 base::Optional<BluetoothUUID> Canonicalize(const std::string& str) { |
| 17 return device::BluetoothUUID(string).canonical_value(); | 17 return base::make_optional(device::BluetoothUUID(str)); |
| 18 } | 18 } |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 class BluetoothBlacklistTest : public ::testing::Test { | 22 class BluetoothBlacklistTest : public ::testing::Test { |
| 23 public: | 23 public: |
| 24 BluetoothBlacklistTest() : list_(BluetoothBlacklist::Get()) { | 24 BluetoothBlacklistTest() : list_(BluetoothBlacklist::Get()) { |
| 25 // Because BluetoothBlacklist is used via a singleton instance, the data | 25 // Because BluetoothBlacklist is used via a singleton instance, the data |
| 26 // must be reset for each test. | 26 // must be reset for each test. |
| 27 list_.ResetToDefaultValuesForTest(); | 27 list_.ResetToDefaultValuesForTest(); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); | 225 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); |
| 226 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); | 226 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); |
| 227 { | 227 { |
| 228 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> empty_filters; | 228 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> empty_filters; |
| 229 EXPECT_FALSE(list_.IsExcluded(empty_filters)); | 229 EXPECT_FALSE(list_.IsExcluded(empty_filters)); |
| 230 } | 230 } |
| 231 { | 231 { |
| 232 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> single_empty_filter(1); | 232 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> single_empty_filter(1); |
| 233 | 233 |
| 234 single_empty_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); | 234 single_empty_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); |
| 235 single_empty_filter[0]->services = mojo::Array<mojo::String>(); | 235 single_empty_filter[0]->services = |
| 236 mojo::Array<base::Optional<BluetoothUUID>>(); | |
| 236 | 237 |
| 237 EXPECT_EQ(0u, single_empty_filter[0]->services.size()); | 238 EXPECT_EQ(0u, single_empty_filter[0]->services.size()); |
| 238 EXPECT_FALSE(list_.IsExcluded(single_empty_filter)); | 239 EXPECT_FALSE(list_.IsExcluded(single_empty_filter)); |
| 239 } | 240 } |
| 240 { | 241 { |
| 241 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> | 242 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> |
| 242 single_non_matching_filter(1); | 243 single_non_matching_filter(1); |
| 243 | 244 |
| 244 single_non_matching_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); | 245 single_non_matching_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); |
| 245 single_non_matching_filter[0]->services.push_back(Canonicalize("0000")); | 246 single_non_matching_filter[0]->services.push_back(Canonicalize("0000")); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 TEST_F(BluetoothBlacklistTest, RemoveExcludedUUIDs_NonMatching) { | 323 TEST_F(BluetoothBlacklistTest, RemoveExcludedUUIDs_NonMatching) { |
| 323 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); | 324 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); |
| 324 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); | 325 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); |
| 325 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); | 326 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); |
| 326 | 327 |
| 327 // options.optional_services should be the same before and after | 328 // options.optional_services should be the same before and after |
| 328 // RemoveExcludedUUIDs(). | 329 // RemoveExcludedUUIDs(). |
| 329 { | 330 { |
| 330 // Empty optional_services. | 331 // Empty optional_services. |
| 331 blink::mojom::WebBluetoothRequestDeviceOptions options; | 332 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 332 options.optional_services = mojo::Array<mojo::String>(); | 333 options.optional_services = mojo::Array<base::Optional<BluetoothUUID>>(); |
| 333 | 334 |
| 334 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 335 mojo::Array<base::Optional<BluetoothUUID>> expected = |
| 336 options.optional_services.Clone(); | |
| 335 | 337 |
| 336 list_.RemoveExcludedUUIDs(&options); | 338 list_.RemoveExcludedUUIDs(&options); |
| 337 EXPECT_TRUE(options.optional_services.Equals(expected)); | 339 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 338 } | 340 } |
| 339 { | 341 { |
| 340 // One non-matching service in optional_services. | 342 // One non-matching service in optional_services. |
| 341 blink::mojom::WebBluetoothRequestDeviceOptions options; | 343 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 342 options.optional_services.push_back(Canonicalize("0000")); | 344 options.optional_services.push_back(Canonicalize("0000")); |
| 343 | 345 |
| 344 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 346 mojo::Array<base::Optional<BluetoothUUID>> expected = |
| 347 options.optional_services.Clone(); | |
| 345 | 348 |
| 346 list_.RemoveExcludedUUIDs(&options); | 349 list_.RemoveExcludedUUIDs(&options); |
| 347 EXPECT_TRUE(options.optional_services.Equals(expected)); | 350 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 348 } | 351 } |
| 349 { | 352 { |
| 350 // Multiple non-matching services in optional_services. | 353 // Multiple non-matching services in optional_services. |
| 351 blink::mojom::WebBluetoothRequestDeviceOptions options; | 354 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 352 options.optional_services.push_back(Canonicalize("0000")); | 355 options.optional_services.push_back(Canonicalize("0000")); |
| 353 options.optional_services.push_back(Canonicalize("ee01")); | 356 options.optional_services.push_back(Canonicalize("ee01")); |
| 354 options.optional_services.push_back(Canonicalize("ee02")); | 357 options.optional_services.push_back(Canonicalize("ee02")); |
| 355 options.optional_services.push_back(Canonicalize("0003")); | 358 options.optional_services.push_back(Canonicalize("0003")); |
| 356 | 359 |
| 357 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 360 mojo::Array<base::Optional<BluetoothUUID>> expected = |
| 361 options.optional_services.Clone(); | |
| 358 | 362 |
| 359 list_.RemoveExcludedUUIDs(&options); | 363 list_.RemoveExcludedUUIDs(&options); |
| 360 EXPECT_TRUE(options.optional_services.Equals(expected)); | 364 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 361 } | 365 } |
| 362 } | 366 } |
| 363 | 367 |
| 364 TEST_F(BluetoothBlacklistTest, RemoveExcludedUuids_Matching) { | 368 TEST_F(BluetoothBlacklistTest, RemoveExcludedUuids_Matching) { |
| 365 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); | 369 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); |
| 366 list_.Add(BluetoothUUID("eee2"), BluetoothBlacklist::Value::EXCLUDE); | 370 list_.Add(BluetoothUUID("eee2"), BluetoothBlacklist::Value::EXCLUDE); |
| 367 list_.Add(BluetoothUUID("eee3"), BluetoothBlacklist::Value::EXCLUDE); | 371 list_.Add(BluetoothUUID("eee3"), BluetoothBlacklist::Value::EXCLUDE); |
| 368 list_.Add(BluetoothUUID("eee4"), BluetoothBlacklist::Value::EXCLUDE); | 372 list_.Add(BluetoothUUID("eee4"), BluetoothBlacklist::Value::EXCLUDE); |
| 369 { | 373 { |
| 370 // Single matching service in optional_services. | 374 // Single matching service in optional_services. |
| 371 blink::mojom::WebBluetoothRequestDeviceOptions options; | 375 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 372 options.optional_services.push_back(Canonicalize("eeee")); | 376 options.optional_services.push_back(Canonicalize("eeee")); |
| 373 | 377 |
| 374 mojo::Array<mojo::String> expected = mojo::Array<mojo::String>(); | 378 mojo::Array<base::Optional<BluetoothUUID>> expected = |
| 379 mojo::Array<base::Optional<BluetoothUUID>>(); | |
|
jbroman
2016/06/08 21:08:07
nit: Not added in this CL, but you could just defa
ortuno
2016/06/09 16:15:59
Done.
| |
| 375 | 380 |
| 376 list_.RemoveExcludedUUIDs(&options); | 381 list_.RemoveExcludedUUIDs(&options); |
| 377 | 382 |
| 378 EXPECT_TRUE(options.optional_services.Equals(expected)); | 383 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 379 } | 384 } |
| 380 { | 385 { |
| 381 // Single matching of many services in optional_services. | 386 // Single matching of many services in optional_services. |
| 382 blink::mojom::WebBluetoothRequestDeviceOptions options; | 387 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 383 options.optional_services.push_back(Canonicalize("0000")); | 388 options.optional_services.push_back(Canonicalize("0000")); |
| 384 options.optional_services.push_back(Canonicalize("eeee")); | 389 options.optional_services.push_back(Canonicalize("eeee")); |
| 385 options.optional_services.push_back(Canonicalize("0001")); | 390 options.optional_services.push_back(Canonicalize("0001")); |
| 386 | 391 |
| 387 mojo::Array<mojo::String> expected; | 392 mojo::Array<base::Optional<BluetoothUUID>> expected; |
| 388 expected.push_back(Canonicalize("0000")); | 393 expected.push_back(Canonicalize("0000")); |
| 389 expected.push_back(Canonicalize("0001")); | 394 expected.push_back(Canonicalize("0001")); |
| 390 | 395 |
| 391 list_.RemoveExcludedUUIDs(&options); | 396 list_.RemoveExcludedUUIDs(&options); |
| 392 EXPECT_TRUE(options.optional_services.Equals(expected)); | 397 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 393 } | 398 } |
| 394 { | 399 { |
| 395 // All matching of many services in optional_services. | 400 // All matching of many services in optional_services. |
| 396 blink::mojom::WebBluetoothRequestDeviceOptions options; | 401 blink::mojom::WebBluetoothRequestDeviceOptions options; |
| 397 options.optional_services.push_back(Canonicalize("eee2")); | 402 options.optional_services.push_back(Canonicalize("eee2")); |
| 398 options.optional_services.push_back(Canonicalize("eee4")); | 403 options.optional_services.push_back(Canonicalize("eee4")); |
| 399 options.optional_services.push_back(Canonicalize("eee3")); | 404 options.optional_services.push_back(Canonicalize("eee3")); |
| 400 options.optional_services.push_back(Canonicalize("eeee")); | 405 options.optional_services.push_back(Canonicalize("eeee")); |
| 401 | 406 |
| 402 mojo::Array<mojo::String> expected = mojo::Array<mojo::String>(); | 407 mojo::Array<base::Optional<BluetoothUUID>> expected = |
| 408 mojo::Array<base::Optional<BluetoothUUID>>(); | |
| 403 | 409 |
| 404 list_.RemoveExcludedUUIDs(&options); | 410 list_.RemoveExcludedUUIDs(&options); |
| 405 EXPECT_TRUE(options.optional_services.Equals(expected)); | 411 EXPECT_TRUE(options.optional_services.Equals(expected)); |
| 406 } | 412 } |
| 407 } | 413 } |
| 408 | 414 |
| 409 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { | 415 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { |
| 410 // When adding items to the blacklist the new values should be added in the | 416 // When adding items to the blacklist the new values should be added in the |
| 411 // tests below for each exclusion type. | 417 // tests below for each exclusion type. |
| 412 EXPECT_EQ(11u, list_.size()); | 418 EXPECT_EQ(11u, list_.size()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a02"))); | 453 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a02"))); |
| 448 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); | 454 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); |
| 449 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); | 455 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); |
| 450 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); | 456 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); |
| 451 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); | 457 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); |
| 452 EXPECT_TRUE(list_.IsExcludedFromWrites( | 458 EXPECT_TRUE(list_.IsExcludedFromWrites( |
| 453 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); | 459 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); |
| 454 } | 460 } |
| 455 | 461 |
| 456 } // namespace content | 462 } // namespace content |
| OLD | NEW |