File: usb_policy_allowed_devices_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 (353 lines) | stat: -rw-r--r-- 12,174 bytes parent folder | download | duplicates (7)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2018 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_policy_allowed_devices.h"

#include <memory>
#include <string>
#include <utility>

#include "base/containers/contains.h"
#include "base/test/values_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "services/device/public/cpp/test/fake_usb_device_manager.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace {

using ::base::test::ParseJson;
using ::testing::UnorderedElementsAre;

class UsbPolicyAllowedDevicesTest : public testing::Test {
 public:
  UsbPolicyAllowedDevicesTest() = default;
  ~UsbPolicyAllowedDevicesTest() override = default;

  void SetWebUsbAllowDevicesForUrlsPrefValue(const base::Value& value) {
    profile_.GetPrefs()->Set(prefs::kManagedWebUsbAllowDevicesForUrls, value);
  }

 protected:
  Profile* profile() { return &profile_; }

  std::unique_ptr<UsbPolicyAllowedDevices> CreateUsbPolicyAllowedDevices() {
    return std::make_unique<UsbPolicyAllowedDevices>(profile()->GetPrefs());
  }

  device::FakeUsbDeviceManager device_manager_;

  const url::Origin kGoogleOrigin =
      url::Origin::Create(GURL("https://google.com"));
  const url::Origin kCrbugOrigin =
      url::Origin::Create(GURL("https://crbug.com"));
  const url::Origin kYoutubeOrigin =
      url::Origin::Create(GURL("https://www.youtube.com"));

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
};

}  // namespace

TEST_F(UsbPolicyAllowedDevicesTest, InitializeWithMissingPrefValue) {
  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  EXPECT_TRUE(usb_policy_allowed_devices->map().empty());
}

TEST_F(UsbPolicyAllowedDevicesTest, InitializeWithExistingEmptyPrefValue) {
  base::Value pref_value(base::Value::Type::LIST);

  SetWebUsbAllowDevicesForUrlsPrefValue(pref_value);

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  EXPECT_TRUE(usb_policy_allowed_devices->map().empty());
}

namespace {

constexpr char kPolicySetting[] = R"(
    [
      {
        "devices": [
          { "vendor_id": 1234, "product_id": 5678 },
          { "vendor_id": 4321 }
        ],
        "urls": [
          "https://google.com,https://google.com",
          "https://crbug.com"
        ]
      }, {
        "devices": [{}],
        "urls": ["https://www.youtube.com"]
      }
    ])";

}  // namespace

TEST_F(UsbPolicyAllowedDevicesTest, InitializeWithExistingPrefValue) {
  SetWebUsbAllowDevicesForUrlsPrefValue(ParseJson(kPolicySetting));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const auto& map = usb_policy_allowed_devices->map();
  EXPECT_EQ(map.size(), 3ul);

  auto device_key = std::make_pair(1234, 5678);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& first = map.at(device_key);
  EXPECT_THAT(first, UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(4321, -1);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& second = map.at(device_key);
  EXPECT_THAT(second, UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(-1, -1);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& third = map.at(device_key);
  EXPECT_THAT(third, UnorderedElementsAre(kYoutubeOrigin));
}

TEST_F(UsbPolicyAllowedDevicesTest,
       InitializeWithMissingPolicyThenUpdatePolicy) {
  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();
  EXPECT_TRUE(usb_policy_allowed_devices->map().empty());

  // Ensure that the allowed devices can be dynamically updated.
  SetWebUsbAllowDevicesForUrlsPrefValue(ParseJson(kPolicySetting));

  const auto& map = usb_policy_allowed_devices->map();
  EXPECT_EQ(map.size(), 3ul);

  auto device_key = std::make_pair(1234, 5678);
  ASSERT_TRUE(base::Contains(map, device_key));
  EXPECT_THAT(map.at(device_key),
              UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(4321, -1);
  ASSERT_TRUE(base::Contains(map, device_key));
  EXPECT_THAT(map.at(device_key),
              UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(-1, -1);
  ASSERT_TRUE(base::Contains(map, device_key));
  EXPECT_THAT(map.at(device_key), UnorderedElementsAre(kYoutubeOrigin));
}

TEST_F(UsbPolicyAllowedDevicesTest,
       InitializeWithExistingPolicyThenRemovePolicy) {
  SetWebUsbAllowDevicesForUrlsPrefValue(ParseJson(kPolicySetting));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const auto& map = usb_policy_allowed_devices->map();
  EXPECT_EQ(map.size(), 3ul);

  auto device_key = std::make_pair(1234, 5678);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& first = map.at(device_key);
  EXPECT_THAT(first, UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(4321, -1);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& second = map.at(device_key);
  EXPECT_THAT(second, UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin));

  device_key = std::make_pair(-1, -1);
  ASSERT_TRUE(base::Contains(map, device_key));

  const auto& third = map.at(device_key);
  EXPECT_THAT(third, UnorderedElementsAre(kYoutubeOrigin));

  // Ensure that the allowed devices can be removed dynamically.
  SetWebUsbAllowDevicesForUrlsPrefValue(base::Value(base::Value::Type::LIST));

  EXPECT_TRUE(usb_policy_allowed_devices->map().empty());
}

namespace {

constexpr char kPolicySettingWithEntriesContainingDuplicateDevices[] = R"(
    [
      {
        "devices": [{ "vendor_id": 1234, "product_id": 5678 }],
        "urls": [
          "https://google.com",
          "https://crbug.com"
        ]
      }, {
        "devices": [{ "vendor_id": 1234, "product_id": 5678 }],
        "urls": ["https://www.youtube.com"]
      }
    ])";

}  // namespace

TEST_F(UsbPolicyAllowedDevicesTest,
       InitializeWithExistingPrefValueContainingDuplicateDevices) {
  SetWebUsbAllowDevicesForUrlsPrefValue(
      ParseJson(kPolicySettingWithEntriesContainingDuplicateDevices));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const auto& map = usb_policy_allowed_devices->map();
  ASSERT_EQ(map.size(), 1ul);

  auto device_key = std::make_pair(1234, 5678);
  ASSERT_TRUE(base::Contains(map, device_key));

  // Ensure a device has all of the URL patterns allowed to access it.
  const auto& policy = map.at(device_key);
  EXPECT_THAT(policy, UnorderedElementsAre(kGoogleOrigin, kCrbugOrigin,
                                           kYoutubeOrigin));
}

namespace {

constexpr char kPolicySettingWithEntriesMatchingMultipleDevices[] = R"(
    [
      {
        "devices": [{ "vendor_id": 1234, "product_id": 5678 }],
        "urls": ["https://google.com"]
      }, {
        "devices": [{ "vendor_id": 1234 }],
        "urls": ["https://www.youtube.com"]
      }, {
        "devices": [{}],
        "urls": ["https://chromium.org"]
      }
    ])";

}  // namespace

TEST_F(UsbPolicyAllowedDevicesTest, IsDeviceAllowed) {
  SetWebUsbAllowDevicesForUrlsPrefValue(
      ParseJson(kPolicySettingWithEntriesMatchingMultipleDevices));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const auto kGoogleOrigin = url::Origin::Create(GURL("https://google.com"));
  const auto kYoutubeOrigin =
      url::Origin::Create(GURL("https://www.youtube.com"));
  const auto kChromiumOrigin =
      url::Origin::Create(GURL("https://chromium.org"));

  auto specific_device_info = device_manager_.CreateAndAddDevice(
      1234, 5678, "Google", "Gizmo", "123ABC");
  auto vendor_device_info = device_manager_.CreateAndAddDevice(
      1234, 8765, "Google", "Gizmo", "ABC123");
  auto unrelated_device_info = device_manager_.CreateAndAddDevice(
      4321, 8765, "Chrome", "Gizmo", "987ZYX");

  // Check that the specific device is allowed for https://google.com but not
  // any other device.
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(
      kGoogleOrigin, *specific_device_info));
  EXPECT_FALSE(usb_policy_allowed_devices->IsDeviceAllowed(
      kGoogleOrigin, *vendor_device_info));
  EXPECT_FALSE(usb_policy_allowed_devices->IsDeviceAllowed(
      kGoogleOrigin, *unrelated_device_info));

  // Check that devices with a vendor ID of 1234 are allowed for
  // https://www.youtube.com, but not an unrelated device.
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(
      kYoutubeOrigin, *specific_device_info));
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(kYoutubeOrigin,
                                                          *vendor_device_info));
  EXPECT_FALSE(usb_policy_allowed_devices->IsDeviceAllowed(
      kYoutubeOrigin, *unrelated_device_info));

  // Check that any device is allowed for https://chromium.org.
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(
      kChromiumOrigin, *specific_device_info));
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(kChromiumOrigin,
                                                          *vendor_device_info));
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(
      kChromiumOrigin, *unrelated_device_info));
}

TEST_F(UsbPolicyAllowedDevicesTest, IsDeviceAllowedForUrlsNotInPref) {
  SetWebUsbAllowDevicesForUrlsPrefValue(
      ParseJson(kPolicySettingWithEntriesMatchingMultipleDevices));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const url::Origin origins[] = {
      url::Origin::Create(GURL("https://evil.com")),
      url::Origin::Create(GURL("https://very.evil.com")),
      url::Origin::Create(GURL("https://chromium.deceptive.org"))};

  auto device_info = device_manager_.CreateAndAddDevice(1234, 5678, "Google",
                                                        "Gizmo", "123ABC");
  for (const url::Origin& origin : origins) {
    EXPECT_FALSE(
        usb_policy_allowed_devices->IsDeviceAllowed(origin, *device_info));
  }
}

TEST_F(UsbPolicyAllowedDevicesTest, IsDeviceAllowedForDeviceNotInPref) {
  SetWebUsbAllowDevicesForUrlsPrefValue(
      ParseJson(kPolicySettingWithEntriesMatchingMultipleDevices));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const url::Origin origins[] = {
      url::Origin::Create(GURL("https://google.com")),
      url::Origin::Create(GURL("https://www.youtube.com"))};

  auto device_info = device_manager_.CreateAndAddDevice(4321, 8765, "Google",
                                                        "Gizmo", "123ABC");
  for (const url::Origin& origin : origins) {
    EXPECT_FALSE(
        usb_policy_allowed_devices->IsDeviceAllowed(origin, *device_info));
  }
}

namespace {

constexpr char kPolicySettingWithUrlContainingEmbeddingOrigin[] = R"(
    [
      {
        "devices": [{ "vendor_id": 1234, "product_id": 5678 }],
        "urls": [
          "https://requesting.com,https://embedding.com"
        ]
      }
    ])";

}  // namespace

TEST_F(UsbPolicyAllowedDevicesTest,
       IsDeviceAllowedForUrlContainingEmbeddingOrigin) {
  SetWebUsbAllowDevicesForUrlsPrefValue(
      ParseJson(kPolicySettingWithUrlContainingEmbeddingOrigin));

  auto usb_policy_allowed_devices = CreateUsbPolicyAllowedDevices();

  const auto requesting_origin =
      url::Origin::Create(GURL("https://requesting.com"));
  const auto embedding_origin =
      url::Origin::Create(GURL("https://embedding.com"));

  auto device_info = device_manager_.CreateAndAddDevice(1234, 5678, "Google",
                                                        "Gizmo", "123ABC");
  EXPECT_TRUE(usb_policy_allowed_devices->IsDeviceAllowed(embedding_origin,
                                                          *device_info));
  EXPECT_FALSE(usb_policy_allowed_devices->IsDeviceAllowed(requesting_origin,
                                                           *device_info));
}