Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: chrome/browser/usb/usb_chooser_context_unittest.cc

Issue 1382783002: Store USB device permissions in website settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address mlamouri@'s comments. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "chrome/browser/usb/usb_chooser_context.h"
6 #include "chrome/browser/usb/usb_chooser_context_factory.h"
7 #include "chrome/test/base/testing_profile.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "device/core/device_client.h"
10 #include "device/usb/mock_usb_device.h"
11 #include "device/usb/mock_usb_service.h"
12
13 using device::MockUsbDevice;
14 using device::UsbDevice;
15
16 namespace {
17
18 class TestDeviceClient : public device::DeviceClient {
19 public:
20 TestDeviceClient() {}
21 ~TestDeviceClient() override {}
22
23 device::MockUsbService& usb_service() { return usb_service_; }
24
25 private:
26 device::UsbService* GetUsbService() override { return &usb_service_; }
27
28 device::MockUsbService usb_service_;
29 };
30
31 } // namespace
32
33 class UsbChooserContextTest : public testing::Test {
34 public:
35 UsbChooserContextTest() {}
36 ~UsbChooserContextTest() override {}
37
38 protected:
39 Profile* profile() { return &profile_; }
40 device::MockUsbService& usb_service() { return device_client_.usb_service(); }
41
42 private:
43 content::TestBrowserThreadBundle thread_bundle_;
44 TestDeviceClient device_client_;
45 TestingProfile profile_;
46 };
47
48 TEST_F(UsbChooserContextTest, CheckGrantAndRevokePermission) {
49 GURL origin("https://www.google.com");
50 scoped_refptr<UsbDevice> device =
51 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
52 usb_service().AddDevice(device);
53 UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile());
54
55 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
56 store->GrantDevicePermission(origin, origin, device->guid());
57 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
58 store->RevokeDevicePermission(origin, origin, device->guid());
59 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
60 }
61
62 TEST_F(UsbChooserContextTest, CheckGrantAndRevokeEphemeralPermission) {
63 GURL origin("https://www.google.com");
64 scoped_refptr<UsbDevice> device =
65 new MockUsbDevice(0, 0, "Google", "Gizmo", "");
66 scoped_refptr<UsbDevice> other_device =
67 new MockUsbDevice(0, 0, "Google", "Gizmo", "");
68 usb_service().AddDevice(device);
69 UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile());
70
71 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
72 store->GrantDevicePermission(origin, origin, device->guid());
73 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
74 EXPECT_FALSE(
75 store->HasDevicePermission(origin, origin, other_device->guid()));
76 store->RevokeDevicePermission(origin, origin, device->guid());
77 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
78 }
79
80 TEST_F(UsbChooserContextTest, DisconnectDeviceWithPermission) {
81 GURL origin("https://www.google.com");
82 scoped_refptr<UsbDevice> device =
83 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
84 usb_service().AddDevice(device);
85 UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile());
86
87 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
88 store->GrantDevicePermission(origin, origin, device->guid());
89 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
90 usb_service().RemoveDevice(device);
91 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
92
93 scoped_refptr<UsbDevice> reconnected_device =
94 new MockUsbDevice(0, 0, "Google", "Gizmo", "123ABC");
95 usb_service().AddDevice(reconnected_device);
96 EXPECT_TRUE(
97 store->HasDevicePermission(origin, origin, reconnected_device->guid()));
98 }
99
100 TEST_F(UsbChooserContextTest, DisconnectDeviceWithEphemeralPermission) {
101 GURL origin("https://www.google.com");
102 scoped_refptr<UsbDevice> device =
103 new MockUsbDevice(0, 0, "Google", "Gizmo", "");
104 usb_service().AddDevice(device);
105 UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile());
106
107 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
108 store->GrantDevicePermission(origin, origin, device->guid());
109 EXPECT_TRUE(store->HasDevicePermission(origin, origin, device->guid()));
110 usb_service().RemoveDevice(device);
111 EXPECT_FALSE(store->HasDevicePermission(origin, origin, device->guid()));
112
113 scoped_refptr<UsbDevice> reconnected_device =
114 new MockUsbDevice(0, 0, "Google", "Gizmo", "");
115 usb_service().AddDevice(reconnected_device);
116 EXPECT_FALSE(
117 store->HasDevicePermission(origin, origin, reconnected_device->guid()));
118 }
OLDNEW
« no previous file with comments | « chrome/browser/usb/usb_chooser_context_factory.cc ('k') | chrome/browser/usb/web_usb_permission_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698