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 std::unique_ptr<BluetoothUUID> Canonicalize(const std::string& string) { |
17 return device::BluetoothUUID(string).canonical_value(); | 17 return base::WrapUnique(new device::BluetoothUUID(string)); |
18 } | |
19 | |
20 bool OptionalServicesEqual( | |
Jeffrey Yasskin
2016/05/28 04:38:06
Write this to return an AssertionResult, and then
ortuno
2016/05/31 17:30:47
Now that we are using Optional we can just use the
Jeffrey Yasskin
2016/05/31 21:52:57
SGTM
| |
21 const mojo::Array<std::unique_ptr<BluetoothUUID>>& optional_services, | |
22 const mojo::Array<std::unique_ptr<BluetoothUUID>>& expected) { | |
23 if (optional_services.size() != expected.size()) { | |
24 return false; | |
25 } | |
26 for (size_t i = 0; i < optional_services.size(); i++) { | |
27 if (*(optional_services[0]) != *(expected[0])) { | |
28 return false; | |
29 } | |
30 } | |
31 return true; | |
18 } | 32 } |
19 | 33 |
20 } // namespace | 34 } // namespace |
21 | 35 |
22 class BluetoothBlacklistTest : public ::testing::Test { | 36 class BluetoothBlacklistTest : public ::testing::Test { |
23 public: | 37 public: |
24 BluetoothBlacklistTest() : list_(BluetoothBlacklist::Get()) { | 38 BluetoothBlacklistTest() : list_(BluetoothBlacklist::Get()) { |
25 // Because BluetoothBlacklist is used via a singleton instance, the data | 39 // Because BluetoothBlacklist is used via a singleton instance, the data |
26 // must be reset for each test. | 40 // must be reset for each test. |
27 list_.ResetToDefaultValuesForTest(); | 41 list_.ResetToDefaultValuesForTest(); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); | 239 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); |
226 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); | 240 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); |
227 { | 241 { |
228 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> empty_filters; | 242 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> empty_filters; |
229 EXPECT_FALSE(list_.IsExcluded(empty_filters)); | 243 EXPECT_FALSE(list_.IsExcluded(empty_filters)); |
230 } | 244 } |
231 { | 245 { |
232 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> single_empty_filter(1); | 246 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> single_empty_filter(1); |
233 | 247 |
234 single_empty_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); | 248 single_empty_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); |
235 single_empty_filter[0]->services = mojo::Array<mojo::String>(); | 249 single_empty_filter[0]->services = |
250 mojo::Array<std::unique_ptr<BluetoothUUID>>(); | |
236 | 251 |
237 EXPECT_EQ(0u, single_empty_filter[0]->services.size()); | 252 EXPECT_EQ(0u, single_empty_filter[0]->services.size()); |
238 EXPECT_FALSE(list_.IsExcluded(single_empty_filter)); | 253 EXPECT_FALSE(list_.IsExcluded(single_empty_filter)); |
239 } | 254 } |
240 { | 255 { |
241 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> | 256 mojo::Array<blink::mojom::WebBluetoothScanFilterPtr> |
242 single_non_matching_filter(1); | 257 single_non_matching_filter(1); |
243 | 258 |
244 single_non_matching_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); | 259 single_non_matching_filter[0] = blink::mojom::WebBluetoothScanFilter::New(); |
245 single_non_matching_filter[0]->services.push_back(Canonicalize("0000")); | 260 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) { | 337 TEST_F(BluetoothBlacklistTest, RemoveExcludedUUIDs_NonMatching) { |
323 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); | 338 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); |
324 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); | 339 list_.Add(BluetoothUUID("ee01"), BluetoothBlacklist::Value::EXCLUDE_READS); |
325 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); | 340 list_.Add(BluetoothUUID("ee02"), BluetoothBlacklist::Value::EXCLUDE_WRITES); |
326 | 341 |
327 // options.optional_services should be the same before and after | 342 // options.optional_services should be the same before and after |
328 // RemoveExcludedUUIDs(). | 343 // RemoveExcludedUUIDs(). |
329 { | 344 { |
330 // Empty optional_services. | 345 // Empty optional_services. |
331 blink::mojom::WebBluetoothRequestDeviceOptions options; | 346 blink::mojom::WebBluetoothRequestDeviceOptions options; |
332 options.optional_services = mojo::Array<mojo::String>(); | 347 options.optional_services = mojo::Array<std::unique_ptr<BluetoothUUID>>(); |
333 | 348 |
334 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 349 mojo::Array<std::unique_ptr<BluetoothUUID>> expected = |
350 mojo::Array<std::unique_ptr<BluetoothUUID>>(); | |
335 | 351 |
336 list_.RemoveExcludedUUIDs(&options); | 352 list_.RemoveExcludedUUIDs(&options); |
337 EXPECT_TRUE(options.optional_services.Equals(expected)); | 353 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
338 } | 354 } |
339 { | 355 { |
340 // One non-matching service in optional_services. | 356 // One non-matching service in optional_services. |
341 blink::mojom::WebBluetoothRequestDeviceOptions options; | 357 blink::mojom::WebBluetoothRequestDeviceOptions options; |
342 options.optional_services.push_back(Canonicalize("0000")); | 358 options.optional_services.push_back(Canonicalize("0000")); |
343 | 359 |
344 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 360 mojo::Array<std::unique_ptr<BluetoothUUID>> expected; |
361 expected.push_back(Canonicalize("0000")); | |
345 | 362 |
346 list_.RemoveExcludedUUIDs(&options); | 363 list_.RemoveExcludedUUIDs(&options); |
347 EXPECT_TRUE(options.optional_services.Equals(expected)); | 364 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
348 } | 365 } |
349 { | 366 { |
350 // Multiple non-matching services in optional_services. | 367 // Multiple non-matching services in optional_services. |
351 blink::mojom::WebBluetoothRequestDeviceOptions options; | 368 blink::mojom::WebBluetoothRequestDeviceOptions options; |
352 options.optional_services.push_back(Canonicalize("0000")); | 369 options.optional_services.push_back(Canonicalize("0000")); |
353 options.optional_services.push_back(Canonicalize("ee01")); | 370 options.optional_services.push_back(Canonicalize("ee01")); |
354 options.optional_services.push_back(Canonicalize("ee02")); | 371 options.optional_services.push_back(Canonicalize("ee02")); |
355 options.optional_services.push_back(Canonicalize("0003")); | 372 options.optional_services.push_back(Canonicalize("0003")); |
356 | 373 |
357 mojo::Array<mojo::String> expected = options.optional_services.Clone(); | 374 mojo::Array<std::unique_ptr<BluetoothUUID>> expected; |
375 expected.push_back(Canonicalize("0000")); | |
376 expected.push_back(Canonicalize("ee01")); | |
377 expected.push_back(Canonicalize("ee02")); | |
378 expected.push_back(Canonicalize("0003")); | |
358 | 379 |
359 list_.RemoveExcludedUUIDs(&options); | 380 list_.RemoveExcludedUUIDs(&options); |
360 EXPECT_TRUE(options.optional_services.Equals(expected)); | 381 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
361 } | 382 } |
362 } | 383 } |
363 | 384 |
364 TEST_F(BluetoothBlacklistTest, RemoveExcludedUuids_Matching) { | 385 TEST_F(BluetoothBlacklistTest, RemoveExcludedUuids_Matching) { |
365 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); | 386 list_.Add(BluetoothUUID("eeee"), BluetoothBlacklist::Value::EXCLUDE); |
366 list_.Add(BluetoothUUID("eee2"), BluetoothBlacklist::Value::EXCLUDE); | 387 list_.Add(BluetoothUUID("eee2"), BluetoothBlacklist::Value::EXCLUDE); |
367 list_.Add(BluetoothUUID("eee3"), BluetoothBlacklist::Value::EXCLUDE); | 388 list_.Add(BluetoothUUID("eee3"), BluetoothBlacklist::Value::EXCLUDE); |
368 list_.Add(BluetoothUUID("eee4"), BluetoothBlacklist::Value::EXCLUDE); | 389 list_.Add(BluetoothUUID("eee4"), BluetoothBlacklist::Value::EXCLUDE); |
369 { | 390 { |
370 // Single matching service in optional_services. | 391 // Single matching service in optional_services. |
371 blink::mojom::WebBluetoothRequestDeviceOptions options; | 392 blink::mojom::WebBluetoothRequestDeviceOptions options; |
372 options.optional_services.push_back(Canonicalize("eeee")); | 393 options.optional_services.push_back(Canonicalize("eeee")); |
373 | 394 |
374 mojo::Array<mojo::String> expected = mojo::Array<mojo::String>(); | 395 mojo::Array<std::unique_ptr<BluetoothUUID>> expected = |
396 mojo::Array<std::unique_ptr<BluetoothUUID>>(); | |
375 | 397 |
376 list_.RemoveExcludedUUIDs(&options); | 398 list_.RemoveExcludedUUIDs(&options); |
377 | 399 |
378 EXPECT_TRUE(options.optional_services.Equals(expected)); | 400 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
379 } | 401 } |
380 { | 402 { |
381 // Single matching of many services in optional_services. | 403 // Single matching of many services in optional_services. |
382 blink::mojom::WebBluetoothRequestDeviceOptions options; | 404 blink::mojom::WebBluetoothRequestDeviceOptions options; |
383 options.optional_services.push_back(Canonicalize("0000")); | 405 options.optional_services.push_back(Canonicalize("0000")); |
384 options.optional_services.push_back(Canonicalize("eeee")); | 406 options.optional_services.push_back(Canonicalize("eeee")); |
385 options.optional_services.push_back(Canonicalize("0001")); | 407 options.optional_services.push_back(Canonicalize("0001")); |
386 | 408 |
387 mojo::Array<mojo::String> expected; | 409 mojo::Array<std::unique_ptr<BluetoothUUID>> expected; |
388 expected.push_back(Canonicalize("0000")); | 410 expected.push_back(Canonicalize("0000")); |
389 expected.push_back(Canonicalize("0001")); | 411 expected.push_back(Canonicalize("0001")); |
390 | 412 |
391 list_.RemoveExcludedUUIDs(&options); | 413 list_.RemoveExcludedUUIDs(&options); |
392 EXPECT_TRUE(options.optional_services.Equals(expected)); | 414 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
393 } | 415 } |
394 { | 416 { |
395 // All matching of many services in optional_services. | 417 // All matching of many services in optional_services. |
396 blink::mojom::WebBluetoothRequestDeviceOptions options; | 418 blink::mojom::WebBluetoothRequestDeviceOptions options; |
397 options.optional_services.push_back(Canonicalize("eee2")); | 419 options.optional_services.push_back(Canonicalize("eee2")); |
398 options.optional_services.push_back(Canonicalize("eee4")); | 420 options.optional_services.push_back(Canonicalize("eee4")); |
399 options.optional_services.push_back(Canonicalize("eee3")); | 421 options.optional_services.push_back(Canonicalize("eee3")); |
400 options.optional_services.push_back(Canonicalize("eeee")); | 422 options.optional_services.push_back(Canonicalize("eeee")); |
401 | 423 |
402 mojo::Array<mojo::String> expected = mojo::Array<mojo::String>(); | 424 mojo::Array<std::unique_ptr<BluetoothUUID>> expected = |
425 mojo::Array<std::unique_ptr<BluetoothUUID>>(); | |
403 | 426 |
404 list_.RemoveExcludedUUIDs(&options); | 427 list_.RemoveExcludedUUIDs(&options); |
405 EXPECT_TRUE(options.optional_services.Equals(expected)); | 428 EXPECT_TRUE(OptionalServicesEqual(options.optional_services, expected)); |
406 } | 429 } |
407 } | 430 } |
408 | 431 |
409 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { | 432 TEST_F(BluetoothBlacklistTest, VerifyDefaultBlacklistSize) { |
410 // When adding items to the blacklist the new values should be added in the | 433 // When adding items to the blacklist the new values should be added in the |
411 // tests below for each exclusion type. | 434 // tests below for each exclusion type. |
412 EXPECT_EQ(11u, list_.size()); | 435 EXPECT_EQ(11u, list_.size()); |
413 } | 436 } |
414 | 437 |
415 TEST_F(BluetoothBlacklistTest, VerifyDefaultExcludeList) { | 438 TEST_F(BluetoothBlacklistTest, VerifyDefaultExcludeList) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a02"))); | 470 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a02"))); |
448 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); | 471 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a03"))); |
449 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); | 472 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2a25"))); |
450 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); | 473 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2902"))); |
451 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); | 474 EXPECT_TRUE(list_.IsExcludedFromWrites(BluetoothUUID("2903"))); |
452 EXPECT_TRUE(list_.IsExcludedFromWrites( | 475 EXPECT_TRUE(list_.IsExcludedFromWrites( |
453 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); | 476 BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"))); |
454 } | 477 } |
455 | 478 |
456 } // namespace content | 479 } // namespace content |
OLD | NEW |