1 | /* $Id: tstUSBProxyLinux.cpp 59117 2015-12-14 14:04:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USBProxyBackendLinux test case.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 |
|
---|
23 | #include "USBProxyBackend.h"
|
---|
24 | #include "USBGetDevices.h"
|
---|
25 |
|
---|
26 | #include <VBox/err.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/env.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/test.h>
|
---|
31 |
|
---|
32 | /*** BEGIN STUBS ***/
|
---|
33 |
|
---|
34 | USBProxyBackend::USBProxyBackend(USBProxyService*) {}
|
---|
35 | USBProxyBackend::~USBProxyBackend() {}
|
---|
36 | int USBProxyBackend::init() { return VINF_SUCCESS; }
|
---|
37 | int USBProxyBackend::start() { return VINF_SUCCESS; }
|
---|
38 | int USBProxyBackend::stop() { return VINF_SUCCESS; }
|
---|
39 | RWLockHandle *USBProxyBackend::lockHandle() const { return NULL; }
|
---|
40 | void *USBProxyBackend::insertFilter(USBFILTER const*) { return NULL; }
|
---|
41 | void USBProxyBackend::removeFilter(void*) {}
|
---|
42 | int USBProxyBackend::captureDevice(HostUSBDevice*) { return VINF_SUCCESS; }
|
---|
43 | void USBProxyBackend::captureDeviceCompleted(HostUSBDevice*, bool) {}
|
---|
44 | void USBProxyBackend::detachingDevice(HostUSBDevice*) {}
|
---|
45 | int USBProxyBackend::releaseDevice(HostUSBDevice*) { return VINF_SUCCESS; }
|
---|
46 | void USBProxyBackend::releaseDeviceCompleted(HostUSBDevice*, bool) {}
|
---|
47 | void USBProxyBackend::serviceThreadInit() {}
|
---|
48 | void USBProxyBackend::serviceThreadTerm() {}
|
---|
49 | int USBProxyBackend::wait(unsigned int) { return VINF_SUCCESS; }
|
---|
50 | int USBProxyBackend::interruptWait() { return VINF_SUCCESS; }
|
---|
51 | PUSBDEVICE USBProxyBackend::getDevices() { return NULL; }
|
---|
52 | void USBProxyBackend::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice) {}
|
---|
53 | void USBProxyBackend::deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice) {}
|
---|
54 | void USBProxyBackend::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList*, SessionMachine*) {}
|
---|
55 | bool USBProxyBackend::updateDeviceState(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
|
---|
56 | bool USBProxyBackend::updateDeviceStateFake(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
|
---|
57 | bool USBProxyBackend::isActive() { return true; }
|
---|
58 |
|
---|
59 | VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(char const*) {}
|
---|
60 |
|
---|
61 | com::Utf8Str HostUSBDevice::i_getName()
|
---|
62 | {
|
---|
63 | return Utf8Str();
|
---|
64 | }
|
---|
65 |
|
---|
66 | void SysFreeString(BSTR bstr)
|
---|
67 | {
|
---|
68 | Assert(0);
|
---|
69 | }
|
---|
70 |
|
---|
71 | static struct
|
---|
72 | {
|
---|
73 | const char *pcszEnvUsb;
|
---|
74 | const char *pcszEnvUsbRoot;
|
---|
75 | const char *pcszDevicesRoot;
|
---|
76 | bool fDevicesAccessible;
|
---|
77 | const char *pcszUsbfsRoot;
|
---|
78 | bool fUsbfsAccessible;
|
---|
79 | int rcMethodInit;
|
---|
80 | const char *pcszDevicesRootExpected;
|
---|
81 | bool fUsingUsbfsExpected;
|
---|
82 | int rcExpected;
|
---|
83 | } s_testEnvironment[] =
|
---|
84 | {
|
---|
85 | /* "sysfs" and valid root in the environment */
|
---|
86 | { "sysfs", "/dev/bus/usb", "/dev/bus/usb", true, NULL, false, VINF_SUCCESS, "/dev/bus/usb", false, VINF_SUCCESS },
|
---|
87 | /* "sysfs" and bad root in the environment */
|
---|
88 | { "sysfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
|
---|
89 | /* "sysfs" and no root in the environment */
|
---|
90 | { "sysfs", NULL, "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
91 | /* "usbfs" and valid root in the environment */
|
---|
92 | { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VINF_SUCCESS, "/dev/bus/usb", true, VINF_SUCCESS },
|
---|
93 | /* "usbfs" and bad root in the environment */
|
---|
94 | { "usbfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
|
---|
95 | /* "usbfs" and no root in the environment */
|
---|
96 | { "usbfs", NULL, NULL, false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
97 | /* invalid method in the environment, sysfs available */
|
---|
98 | { "invalid", "/dev/bus/usb", "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
99 | /* invalid method in the environment, usbfs available */
|
---|
100 | { "invalid", "/dev/bus/usb", NULL, true, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
101 | /* invalid method in the environment, sysfs inaccessible */
|
---|
102 | { "invalid", "/dev/bus/usb", "/dev/vboxusb", false, NULL, false, VINF_SUCCESS, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
103 | /* invalid method in the environment, usbfs inaccessible */
|
---|
104 | { "invalid", "/dev/bus/usb", NULL, false, "/proc/bus/usb", false, VINF_SUCCESS, "", true, VERR_VUSB_USBFS_PERMISSION },
|
---|
105 | /* No environment, sysfs and usbfs available but without access permissions. */
|
---|
106 | { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
107 | /* No environment, sysfs and usbfs available, access permissions for sysfs. */
|
---|
108 | { NULL, NULL, "/dev/vboxusb", true, "/proc/bus/usb", false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
109 | /* No environment, sysfs and usbfs available, access permissions for usbfs. */
|
---|
110 | { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
111 | /* No environment, sysfs available but without access permissions. */
|
---|
112 | { NULL, NULL, "/dev/vboxusb", false, NULL, false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
113 | /* No environment, usbfs available but without access permissions. */
|
---|
114 | { NULL, NULL, NULL, false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USBFS_PERMISSION },
|
---|
115 | };
|
---|
116 |
|
---|
117 | static void testInit(RTTEST hTest)
|
---|
118 | {
|
---|
119 | RTTestSub(hTest, "Testing USBProxyLinuxChooseMethod");
|
---|
120 | for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
|
---|
121 | {
|
---|
122 | bool fUsingUsbfs = true;
|
---|
123 | const char *pcszDevicesRoot = "";
|
---|
124 |
|
---|
125 | TestUSBSetEnv(s_testEnvironment[i].pcszEnvUsb,
|
---|
126 | s_testEnvironment[i].pcszEnvUsbRoot);
|
---|
127 | TestUSBSetupInit(s_testEnvironment[i].pcszUsbfsRoot,
|
---|
128 | s_testEnvironment[i].fUsbfsAccessible,
|
---|
129 | s_testEnvironment[i].pcszDevicesRoot,
|
---|
130 | s_testEnvironment[i].fDevicesAccessible,
|
---|
131 | s_testEnvironment[i].rcMethodInit);
|
---|
132 | int rc = USBProxyLinuxChooseMethod(&fUsingUsbfs, &pcszDevicesRoot);
|
---|
133 | RTTESTI_CHECK_MSG(rc == s_testEnvironment[i].rcExpected,
|
---|
134 | ("rc=%Rrc (test index %i) instead of %Rrc!\n",
|
---|
135 | rc, i, s_testEnvironment[i].rcExpected));
|
---|
136 | RTTESTI_CHECK_MSG(!RTStrCmp(pcszDevicesRoot,
|
---|
137 | s_testEnvironment[i].pcszDevicesRootExpected),
|
---|
138 | ("testGetDevicesRoot() returned %s (test index %i) instead of %s!\n",
|
---|
139 | pcszDevicesRoot, i,
|
---|
140 | s_testEnvironment[i].pcszDevicesRootExpected));
|
---|
141 | RTTESTI_CHECK_MSG( fUsingUsbfs
|
---|
142 | == s_testEnvironment[i].fUsingUsbfsExpected,
|
---|
143 | ("testGetUsingUsbfs() returned %RTbool (test index %i) instead of %RTbool!\n",
|
---|
144 | fUsingUsbfs, i,
|
---|
145 | s_testEnvironment[i].fUsingUsbfsExpected));
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | static struct
|
---|
150 | {
|
---|
151 | const char *pacszDeviceAddresses[16];
|
---|
152 | const char *pacszAccessibleFiles[16];
|
---|
153 | const char *pcszRoot;
|
---|
154 | bool fIsDeviceNodes;
|
---|
155 | bool fAvailableExpected;
|
---|
156 | } s_testCheckDeviceRoot[] =
|
---|
157 | {
|
---|
158 | /* /dev/vboxusb accessible -> device nodes method available */
|
---|
159 | { { NULL }, { "/dev/vboxusb" }, "/dev/vboxusb", true, true },
|
---|
160 | /* /dev/vboxusb present but not accessible -> device nodes method not
|
---|
161 | * available */
|
---|
162 | { { NULL }, { NULL }, "/dev/vboxusb", true, false },
|
---|
163 | /* /proc/bus/usb available but empty -> usbfs method available (we can't
|
---|
164 | * really check in this case) */
|
---|
165 | { { NULL }, { "/proc/bus/usb" }, "/proc/bus/usb", false, true },
|
---|
166 | /* /proc/bus/usb not available or not accessible -> usbfs method not available */
|
---|
167 | { { NULL }, { NULL }, "/proc/bus/usb", false, false },
|
---|
168 | /* /proc/bus/usb available, one inaccessible device -> usbfs method not
|
---|
169 | * available */
|
---|
170 | { { "/proc/bus/usb/001/001" }, { "/proc/bus/usb" }, "/proc/bus/usb", false, false },
|
---|
171 | /* /proc/bus/usb available, one device of two inaccessible -> usbfs method
|
---|
172 | * not available */
|
---|
173 | { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
174 | { "/proc/bus/usb", "/proc/bus/usb/001/001" }, "/proc/bus/usb", false, false },
|
---|
175 | /* /proc/bus/usb available, two accessible devices -> usbfs method
|
---|
176 | * available */
|
---|
177 | { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
178 | { "/proc/bus/usb", "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
179 | "/proc/bus/usb", false, true }
|
---|
180 | };
|
---|
181 |
|
---|
182 | static void testCheckDeviceRoot(RTTEST hTest)
|
---|
183 | {
|
---|
184 | RTTestSub(hTest, "Testing the USBProxyLinuxCheckDeviceRoot API");
|
---|
185 | for (unsigned i = 0; i < RT_ELEMENTS(s_testCheckDeviceRoot); ++i)
|
---|
186 | {
|
---|
187 | TestUSBSetAvailableUsbfsDevices(s_testCheckDeviceRoot[i]
|
---|
188 | .pacszDeviceAddresses);
|
---|
189 | TestUSBSetAccessibleFiles(s_testCheckDeviceRoot[i]
|
---|
190 | .pacszAccessibleFiles);
|
---|
191 | bool fAvailable = USBProxyLinuxCheckDeviceRoot
|
---|
192 | (s_testCheckDeviceRoot[i].pcszRoot,
|
---|
193 | s_testCheckDeviceRoot[i].fIsDeviceNodes);
|
---|
194 | RTTESTI_CHECK_MSG( fAvailable
|
---|
195 | == s_testCheckDeviceRoot[i].fAvailableExpected,
|
---|
196 | ("USBProxyLinuxCheckDeviceRoot() returned %RTbool (test index %i) instead of %RTbool!\n",
|
---|
197 | fAvailable, i,
|
---|
198 | s_testCheckDeviceRoot[i].fAvailableExpected));
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | int main(void)
|
---|
203 | {
|
---|
204 | /*
|
---|
205 | * Init the runtime, test and say hello.
|
---|
206 | */
|
---|
207 | RTTEST hTest;
|
---|
208 | RTEXITCODE rcExit = RTTestInitAndCreate("tstUSBProxyLinux", &hTest);
|
---|
209 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
210 | return rcExit;
|
---|
211 | RTTestBanner(hTest);
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Run the tests.
|
---|
215 | */
|
---|
216 | testInit(hTest);
|
---|
217 | testCheckDeviceRoot(hTest);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Summary
|
---|
221 | */
|
---|
222 | return RTTestSummaryAndDestroy(hTest);
|
---|
223 | }
|
---|