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; |
375 | 379 |
376 list_.RemoveExcludedUUIDs(&options); | 380 list_.RemoveExcludedUUIDs(&options); |
377 | 381 |
378 EXPECT_TRUE(options.optional_services.Equals(expected)); | 382 EXPECT_TRUE(options.optional_services.Equals(expected)); |
379 } | 383 } |
380 { | 384 { |
381 // Single matching of many services in optional_services. | 385 // Single matching of many services in optional_services. |
382 blink::mojom::WebBluetoothRequestDeviceOptions options; | 386 blink::mojom::WebBluetoothRequestDeviceOptions options; |
383 options.optional_services.push_back(Canonicalize("0000")); | 387 options.optional_services.push_back(Canonicalize("0000")); |
384 options.optional_services.push_back(Canonicalize("eeee")); | 388 options.optional_services.push_back(Canonicalize("eeee")); |
385 options.optional_services.push_back(Canonicalize("0001")); | 389 options.optional_services.push_back(Canonicalize("0001")); |
386 | 390 |
387 mojo::Array<mojo::String> expected; | 391 mojo::Array<base::Optional<BluetoothUUID>> expected; |
388 expected.push_back(Canonicalize("0000")); | 392 expected.push_back(Canonicalize("0000")); |
389 expected.push_back(Canonicalize("0001")); | 393 expected.push_back(Canonicalize("0001")); |
390 | 394 |
391 list_.RemoveExcludedUUIDs(&options); | 395 list_.RemoveExcludedUUIDs(&options); |
392 EXPECT_TRUE(options.optional_services.Equals(expected)); | 396 EXPECT_TRUE(options.optional_services.Equals(expected)); |
393 } | 397 } |
394 { | 398 { |
395 // All matching of many services in optional_services. | 399 // All matching of many services in optional_services. |
396 blink::mojom::WebBluetoothRequestDeviceOptions options; | 400 blink::mojom::WebBluetoothRequestDeviceOptions options; |
397 options.optional_services.push_back(Canonicalize("eee2")); | 401 options.optional_services.push_back(Canonicalize("eee2")); |
398 options.optional_services.push_back(Canonicalize("eee4")); | 402 options.optional_services.push_back(Canonicalize("eee4")); |
399 options.optional_services.push_back(Canonicalize("eee3")); | 403 options.optional_services.push_back(Canonicalize("eee3")); |
400 options.optional_services.push_back(Canonicalize("eeee")); | 404 options.optional_services.push_back(Canonicalize("eeee")); |
401 | 405 |
402 mojo::Array<mojo::String> expected = mojo::Array<mojo::String>(); | 406 mojo::Array<base::Optional<BluetoothUUID>> expected; |
403 | 407 |
404 list_.RemoveExcludedUUIDs(&options); | 408 list_.RemoveExcludedUUIDs(&options); |
405 EXPECT_TRUE(options.optional_services.Equals(expected)); | 409 EXPECT_TRUE(options.optional_services.Equals(expected)); |
406 } | 410 } |
407 } | 411 } |
408 | 412 |
409 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { | 413 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { |
410 // When adding items to the blacklist the new values should be added in the | 414 // When adding items to the blacklist the new values should be added in the |
411 // tests below for each exclusion type. | 415 // tests below for each exclusion type. |
412 EXPECT_EQ(11u, list_.size()); | 416 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"))); | 451 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a02"))); |
448 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); | 452 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); |
449 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); | 453 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); |
450 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); | 454 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); |
451 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); | 455 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); |
452 EXPECT_TRUE(list_.IsExcludedFromWrites( | 456 EXPECT_TRUE(list_.IsExcludedFromWrites( |
453 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); | 457 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); |
454 } | 458 } |
455 | 459 |
456 } // namespace content | 460 } // namespace content |
OLD | NEW |