1 | /* $Id: tstUSBProxyLinux.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USBProxyBackendLinux test case.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2022 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 "USBGetDevices.h"
|
---|
24 |
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/env.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/test.h>
|
---|
30 |
|
---|
31 | /*** BEGIN STUBS ***/
|
---|
32 |
|
---|
33 | static struct
|
---|
34 | {
|
---|
35 | const char *pcszEnvUsb;
|
---|
36 | const char *pcszEnvUsbRoot;
|
---|
37 | const char *pcszDevicesRoot;
|
---|
38 | bool fDevicesAccessible;
|
---|
39 | const char *pcszUsbfsRoot;
|
---|
40 | bool fUsbfsAccessible;
|
---|
41 | int rcMethodInit;
|
---|
42 | const char *pcszDevicesRootExpected;
|
---|
43 | bool fUsingUsbfsExpected;
|
---|
44 | int rcExpected;
|
---|
45 | } s_testEnvironment[] =
|
---|
46 | {
|
---|
47 | /* "sysfs" and valid root in the environment */
|
---|
48 | { "sysfs", "/dev/bus/usb", "/dev/bus/usb", true, NULL, false, VINF_SUCCESS, "/dev/bus/usb", false, VINF_SUCCESS },
|
---|
49 | /* "sysfs" and bad root in the environment */
|
---|
50 | { "sysfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
|
---|
51 | /* "sysfs" and no root in the environment */
|
---|
52 | { "sysfs", NULL, "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
53 | /* "usbfs" and valid root in the environment */
|
---|
54 | { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VINF_SUCCESS, "/dev/bus/usb", true, VINF_SUCCESS },
|
---|
55 | /* "usbfs" and bad root in the environment */
|
---|
56 | { "usbfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
|
---|
57 | /* "usbfs" and no root in the environment */
|
---|
58 | { "usbfs", NULL, NULL, false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
59 | /* invalid method in the environment, sysfs available */
|
---|
60 | { "invalid", "/dev/bus/usb", "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
61 | /* invalid method in the environment, usbfs available */
|
---|
62 | { "invalid", "/dev/bus/usb", NULL, true, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
63 | /* invalid method in the environment, sysfs inaccessible */
|
---|
64 | { "invalid", "/dev/bus/usb", "/dev/vboxusb", false, NULL, false, VINF_SUCCESS, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
65 | /* invalid method in the environment, usbfs inaccessible */
|
---|
66 | { "invalid", "/dev/bus/usb", NULL, false, "/proc/bus/usb", false, VINF_SUCCESS, "", true, VERR_VUSB_USBFS_PERMISSION },
|
---|
67 | /* No environment, sysfs and usbfs available but without access permissions. */
|
---|
68 | { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
69 | /* No environment, sysfs and usbfs available, access permissions for sysfs. */
|
---|
70 | { NULL, NULL, "/dev/vboxusb", true, "/proc/bus/usb", false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
|
---|
71 | /* No environment, sysfs and usbfs available, access permissions for usbfs. */
|
---|
72 | { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
|
---|
73 | /* No environment, sysfs available but without access permissions. */
|
---|
74 | { NULL, NULL, "/dev/vboxusb", false, NULL, false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
|
---|
75 | /* No environment, usbfs available but without access permissions. */
|
---|
76 | { NULL, NULL, NULL, false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USBFS_PERMISSION },
|
---|
77 | };
|
---|
78 |
|
---|
79 | static void testInit(RTTEST hTest)
|
---|
80 | {
|
---|
81 | RTTestSub(hTest, "Testing USBProxyLinuxChooseMethod");
|
---|
82 | for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
|
---|
83 | {
|
---|
84 | bool fUsingUsbfs = true;
|
---|
85 | const char *pcszDevicesRoot = "";
|
---|
86 |
|
---|
87 | TestUSBSetEnv(s_testEnvironment[i].pcszEnvUsb,
|
---|
88 | s_testEnvironment[i].pcszEnvUsbRoot);
|
---|
89 | TestUSBSetupInit(s_testEnvironment[i].pcszUsbfsRoot,
|
---|
90 | s_testEnvironment[i].fUsbfsAccessible,
|
---|
91 | s_testEnvironment[i].pcszDevicesRoot,
|
---|
92 | s_testEnvironment[i].fDevicesAccessible,
|
---|
93 | s_testEnvironment[i].rcMethodInit);
|
---|
94 | int rc = USBProxyLinuxChooseMethod(&fUsingUsbfs, &pcszDevicesRoot);
|
---|
95 | RTTESTI_CHECK_MSG(rc == s_testEnvironment[i].rcExpected,
|
---|
96 | ("rc=%Rrc (test index %i) instead of %Rrc!\n",
|
---|
97 | rc, i, s_testEnvironment[i].rcExpected));
|
---|
98 | RTTESTI_CHECK_MSG(!RTStrCmp(pcszDevicesRoot,
|
---|
99 | s_testEnvironment[i].pcszDevicesRootExpected),
|
---|
100 | ("testGetDevicesRoot() returned %s (test index %i) instead of %s!\n",
|
---|
101 | pcszDevicesRoot, i,
|
---|
102 | s_testEnvironment[i].pcszDevicesRootExpected));
|
---|
103 | RTTESTI_CHECK_MSG( fUsingUsbfs
|
---|
104 | == s_testEnvironment[i].fUsingUsbfsExpected,
|
---|
105 | ("testGetUsingUsbfs() returned %RTbool (test index %i) instead of %RTbool!\n",
|
---|
106 | fUsingUsbfs, i,
|
---|
107 | s_testEnvironment[i].fUsingUsbfsExpected));
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | static struct
|
---|
112 | {
|
---|
113 | const char *pacszDeviceAddresses[16];
|
---|
114 | const char *pacszAccessibleFiles[16];
|
---|
115 | const char *pcszRoot;
|
---|
116 | bool fIsDeviceNodes;
|
---|
117 | bool fAvailableExpected;
|
---|
118 | } s_testCheckDeviceRoot[] =
|
---|
119 | {
|
---|
120 | /* /dev/vboxusb accessible -> device nodes method available */
|
---|
121 | { { NULL }, { "/dev/vboxusb" }, "/dev/vboxusb", true, true },
|
---|
122 | /* /dev/vboxusb present but not accessible -> device nodes method not
|
---|
123 | * available */
|
---|
124 | { { NULL }, { NULL }, "/dev/vboxusb", true, false },
|
---|
125 | /* /proc/bus/usb available but empty -> usbfs method available (we can't
|
---|
126 | * really check in this case) */
|
---|
127 | { { NULL }, { "/proc/bus/usb" }, "/proc/bus/usb", false, true },
|
---|
128 | /* /proc/bus/usb not available or not accessible -> usbfs method not available */
|
---|
129 | { { NULL }, { NULL }, "/proc/bus/usb", false, false },
|
---|
130 | /* /proc/bus/usb available, one inaccessible device -> usbfs method not
|
---|
131 | * available */
|
---|
132 | { { "/proc/bus/usb/001/001" }, { "/proc/bus/usb" }, "/proc/bus/usb", false, false },
|
---|
133 | /* /proc/bus/usb available, one device of two inaccessible -> usbfs method
|
---|
134 | * not available */
|
---|
135 | { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
136 | { "/proc/bus/usb", "/proc/bus/usb/001/001" }, "/proc/bus/usb", false, false },
|
---|
137 | /* /proc/bus/usb available, two accessible devices -> usbfs method
|
---|
138 | * available */
|
---|
139 | { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
140 | { "/proc/bus/usb", "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
|
---|
141 | "/proc/bus/usb", false, true }
|
---|
142 | };
|
---|
143 |
|
---|
144 | static void testCheckDeviceRoot(RTTEST hTest)
|
---|
145 | {
|
---|
146 | RTTestSub(hTest, "Testing the USBProxyLinuxCheckDeviceRoot API");
|
---|
147 | for (unsigned i = 0; i < RT_ELEMENTS(s_testCheckDeviceRoot); ++i)
|
---|
148 | {
|
---|
149 | TestUSBSetAvailableUsbfsDevices(s_testCheckDeviceRoot[i]
|
---|
150 | .pacszDeviceAddresses);
|
---|
151 | TestUSBSetAccessibleFiles(s_testCheckDeviceRoot[i]
|
---|
152 | .pacszAccessibleFiles);
|
---|
153 | bool fAvailable = USBProxyLinuxCheckDeviceRoot
|
---|
154 | (s_testCheckDeviceRoot[i].pcszRoot,
|
---|
155 | s_testCheckDeviceRoot[i].fIsDeviceNodes);
|
---|
156 | RTTESTI_CHECK_MSG( fAvailable
|
---|
157 | == s_testCheckDeviceRoot[i].fAvailableExpected,
|
---|
158 | ("USBProxyLinuxCheckDeviceRoot() returned %RTbool (test index %i) instead of %RTbool!\n",
|
---|
159 | fAvailable, i,
|
---|
160 | s_testCheckDeviceRoot[i].fAvailableExpected));
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | int main(void)
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * Init the runtime, test and say hello.
|
---|
168 | */
|
---|
169 | RTTEST hTest;
|
---|
170 | RTEXITCODE rcExit = RTTestInitAndCreate("tstUSBProxyLinux", &hTest);
|
---|
171 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
172 | return rcExit;
|
---|
173 | RTTestBanner(hTest);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Run the tests.
|
---|
177 | */
|
---|
178 | testInit(hTest);
|
---|
179 | testCheckDeviceRoot(hTest);
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Summary
|
---|
183 | */
|
---|
184 | return RTTestSummaryAndDestroy(hTest);
|
---|
185 | }
|
---|