File: usb_blocklist_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,613 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/usb/usb_blocklist.h"

#include <string_view>

#include "base/memory/raw_ref.h"
#include "base/test/scoped_feature_list.h"
#include "services/device/public/cpp/device_features.h"
#include "testing/gtest/include/gtest/gtest.h"

class UsbBlocklistTest : public testing::Test {
 public:
  UsbBlocklistTest() : blocklist_(UsbBlocklist::Get()) {}

  const UsbBlocklist& list() { return *blocklist_; }

  void SetDynamicBlocklist(std::string_view list) {
    feature_list_.Reset();

    std::map<std::string, std::string> params;
    params["blocklist_additions"] = std::string(list);
    feature_list_.InitAndEnableFeatureWithParameters(features::kWebUsbBlocklist,
                                                     params);
    blocklist_->ResetToDefaultValuesForTest();
  }

 private:
  void TearDown() override {
    // Because UsbBlocklist is a singleton it must be cleared after tests run
    // to prevent leakage between tests.
    feature_list_.Reset();
    blocklist_->ResetToDefaultValuesForTest();
  }

  base::test::ScopedFeatureList feature_list_;
  const raw_ref<UsbBlocklist> blocklist_;
};

TEST_F(UsbBlocklistTest, BasicExclusions) {
  SetDynamicBlocklist("18D1:58F0:0100");
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0100}));
  // An older device version is also blocked.
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0090}));
  // A newer device version is not blocked.
  EXPECT_FALSE(list().IsExcluded({0x18D1, 0x58F0, 0x0200}));
  // Other devices with nearby vendor and product IDs are not blocked.
  EXPECT_FALSE(list().IsExcluded({0x18D1, 0x58F1, 0x0100}));
  EXPECT_FALSE(list().IsExcluded({0x18D1, 0x58EF, 0x0100}));
  EXPECT_FALSE(list().IsExcluded({0x18D0, 0x58F0, 0x0100}));
  EXPECT_FALSE(list().IsExcluded({0x18D2, 0x58F0, 0x0100}));
}

TEST_F(UsbBlocklistTest, StringsWithNoValidEntries) {
  SetDynamicBlocklist("");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("~!@#$%^&*()-_=+[]{}/*-");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist(":");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("::");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist(",");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist(",,");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist(",::,");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("1:2:3");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("18D1:2:3000");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("0000:0x00:0000");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("0000:   0:0000");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("000g:0000:0000");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());

  SetDynamicBlocklist("☯");
  EXPECT_EQ(0u, list().GetDynamicEntryCountForTest());
}

TEST_F(UsbBlocklistTest, StringsWithOneValidEntry) {
  SetDynamicBlocklist("18D1:58F0:0101");
  EXPECT_EQ(1u, list().GetDynamicEntryCountForTest());
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0101}));

  SetDynamicBlocklist(" 18D1:58F0:0200  ");
  EXPECT_EQ(1u, list().GetDynamicEntryCountForTest());
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0200}));

  SetDynamicBlocklist(", 18D1:58F0:0201,  ");
  EXPECT_EQ(1u, list().GetDynamicEntryCountForTest());
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0201}));

  SetDynamicBlocklist("18D1:58F0:0202, 0000:1:0000");
  EXPECT_EQ(1u, list().GetDynamicEntryCountForTest());
  EXPECT_TRUE(list().IsExcluded({0x18D1, 0x58F0, 0x0202}));
}

TEST_F(UsbBlocklistTest, StaticEntries) {
  // Yubikey devices. https://crbug.com/818807
  //
  // The specific versions of these devices that we want to block are unknown.
  // The device versions listed here are abitrary chosen to test that any device
  // will be matched.
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0010, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0018, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0030, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0110, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0111, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0112, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0113, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0114, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0115, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0116, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0120, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0200, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0211, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0401, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0402, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0403, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0404, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0405, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0406, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0407, 0x0100}));
  EXPECT_TRUE(list().IsExcluded({0x1050, 0x0410, 0x0100}));

  // Check that various devices around the Yubikey range are not blocked.
  EXPECT_FALSE(list().IsExcluded({0x104F, 0x0200, 0x0100}));
  EXPECT_FALSE(list().IsExcluded({0x1051, 0x0200, 0x0100}));
}