OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/metrics/field_trial.h" |
| 6 #include "chrome/browser/usb/usb_blocklist.h" |
| 7 #include "components/variations/variations_associated_data.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 class UsbBlocklistTest : public testing::Test { |
| 11 public: |
| 12 UsbBlocklistTest() |
| 13 : field_trial_list_(new base::FieldTrialList(nullptr)), |
| 14 list_(UsbBlocklist::Get()) {} |
| 15 |
| 16 void TearDown() override { |
| 17 // Because UsbBlocklist is a singleton it must be cleared after tests run |
| 18 // to prevent leakage between tests. |
| 19 field_trial_list_.reset(); |
| 20 list_.ResetToDefaultValuesForTest(); |
| 21 } |
| 22 |
| 23 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 24 UsbBlocklist& list_; |
| 25 }; |
| 26 |
| 27 TEST_F(UsbBlocklistTest, BasicExclusions) { |
| 28 list_.Exclude({0x18D1, 0x58F0, 0x0100}); |
| 29 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x0100})); |
| 30 EXPECT_FALSE(list_.IsExcluded({0x18D1, 0x58F1, 0x0100})); |
| 31 EXPECT_FALSE(list_.IsExcluded({0x18D0, 0x58F0, 0x0100})); |
| 32 EXPECT_FALSE(list_.IsExcluded({0x18D1, 0x58F0, 0x0200})); |
| 33 } |
| 34 |
| 35 TEST_F(UsbBlocklistTest, StringsWithNoValidEntries) { |
| 36 size_t previous_list_size = list_.size(); |
| 37 list_.Exclude(""); |
| 38 list_.Exclude("~!@#$%^&*()-_=+[]{}/*-"); |
| 39 list_.Exclude(":"); |
| 40 list_.Exclude("::"); |
| 41 list_.Exclude(","); |
| 42 list_.Exclude(",,"); |
| 43 list_.Exclude(",::,"); |
| 44 list_.Exclude("1:2:3"); |
| 45 list_.Exclude("18D1:2:3000"); |
| 46 list_.Exclude("☯"); |
| 47 EXPECT_EQ(previous_list_size, list_.size()); |
| 48 } |
| 49 |
| 50 TEST_F(UsbBlocklistTest, StringsWithOneValidEntry) { |
| 51 size_t previous_list_size = list_.size(); |
| 52 list_.Exclude("18D1:58F0:0101"); |
| 53 EXPECT_EQ(++previous_list_size, list_.size()); |
| 54 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x0101})); |
| 55 |
| 56 list_.Exclude(" 18D1:58F0:0200 "); |
| 57 EXPECT_EQ(++previous_list_size, list_.size()); |
| 58 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x0200})); |
| 59 |
| 60 list_.Exclude(", 18D1:58F0:0201, "); |
| 61 EXPECT_EQ(++previous_list_size, list_.size()); |
| 62 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x0201})); |
| 63 |
| 64 list_.Exclude("18D1:58F0:0202, 0000:1:0000"); |
| 65 EXPECT_EQ(++previous_list_size, list_.size()); |
| 66 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x0202})); |
| 67 } |
| 68 |
| 69 TEST_F(UsbBlocklistTest, ServerProvidedBlocklist) { |
| 70 if (base::FieldTrialList::TrialExists("WebUSBBlocklist")) { |
| 71 // This code checks to make sure that when a field trial is launched it |
| 72 // still contains our test data. |
| 73 LOG(INFO) << "WebUSBBlocklist field trial already configured."; |
| 74 ASSERT_NE(variations::GetVariationParamValue("WebUSBBlocklist", |
| 75 "blocklist_additions") |
| 76 .find("18D1:58F0:1BAD"), |
| 77 std::string::npos) |
| 78 << "ERROR: A WebUSBBlocklist field trial has been configured in\n" |
| 79 "testing/variations/fieldtrial_testing_config.json and must\n" |
| 80 "include this test's excluded device ID '18D1:58F0:1BAD' in\n" |
| 81 "blocklist_additions.\n"; |
| 82 } else { |
| 83 LOG(INFO) << "Creating WebUSBBlocklist field trial for test."; |
| 84 // Create a field trial with test parameter. |
| 85 std::map<std::string, std::string> params; |
| 86 params["blocklist_additions"] = "18D1:58F0:1BAD"; |
| 87 variations::AssociateVariationParams("WebUSBBlocklist", "TestGroup", |
| 88 params); |
| 89 base::FieldTrialList::CreateFieldTrial("WebUSBBlocklist", "TestGroup"); |
| 90 // Refresh the blocklist based on the new field trial. |
| 91 list_.ResetToDefaultValuesForTest(); |
| 92 } |
| 93 |
| 94 EXPECT_TRUE(list_.IsExcluded({0x18D1, 0x58F0, 0x1BAD})); |
| 95 EXPECT_FALSE(list_.IsExcluded({0x18D1, 0x58F0, 0x0100})); |
| 96 } |
OLD | NEW |