VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstUSBProxyLinux.cpp@ 36997

Last change on this file since 36997 was 36997, checked in by vboxsync, 14 years ago

Main/linux/usb: no reason not to check root paths passed in the environment

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: tstUSBProxyLinux.cpp 36997 2011-05-06 23:30:23Z vboxsync $ */
2/** @file
3 * USBProxyServiceLinux 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* Header Files *
20******************************************************************************/
21
22#include "USBProxyService.h"
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
33USBProxyService::USBProxyService(Host*) {}
34USBProxyService::~USBProxyService() {}
35HRESULT USBProxyService::init() { return S_OK; }
36int USBProxyService::start() { return VINF_SUCCESS; }
37int USBProxyService::stop() { return VINF_SUCCESS; }
38RWLockHandle *USBProxyService::lockHandle() const { return NULL; }
39void *USBProxyService::insertFilter(USBFILTER const*) { return NULL; }
40void USBProxyService::removeFilter(void*) {}
41int USBProxyService::captureDevice(HostUSBDevice*) { return VINF_SUCCESS; }
42void USBProxyService::captureDeviceCompleted(HostUSBDevice*, bool) {}
43void USBProxyService::detachingDevice(HostUSBDevice*) {}
44int USBProxyService::releaseDevice(HostUSBDevice*) { return VINF_SUCCESS; }
45void USBProxyService::releaseDeviceCompleted(HostUSBDevice*, bool) {}
46void USBProxyService::serviceThreadInit() {}
47void USBProxyService::serviceThreadTerm() {}
48int USBProxyService::wait(unsigned int) { return VINF_SUCCESS; }
49int USBProxyService::interruptWait() { return VINF_SUCCESS; }
50PUSBDEVICE USBProxyService::getDevices() { return NULL; }
51void USBProxyService::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice) {}
52void USBProxyService::deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice) {}
53void USBProxyService::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList*, SessionMachine*) {}
54bool USBProxyService::updateDeviceState(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
55bool USBProxyService::updateDeviceStateFake(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
56bool USBProxyService::isActive() { return true; }
57
58VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(char const*) {}
59
60com::Utf8Str HostUSBDevice::getName()
61{
62 return Utf8Str();
63}
64
65int USBProxyService::getLastError(void)
66{
67 return mLastError;
68}
69
70void SysFreeString(BSTR bstr)
71{
72 Assert(0);
73}
74
75static struct
76{
77 const char *pcszEnvUsb;
78 const char *pcszEnvUsbRoot;
79 const char *pcszDevicesRoot;
80 bool fDevicesAccessible;
81 const char *pcszUsbfsRoot;
82 bool fUsbfsAccessible;
83 int rcMethodInit;
84 const char *pcszDevicesRootExpected;
85 bool fUsingUsbfsExpected;
86 int rcExpected;
87} s_testEnvironment[] =
88{
89 /* "sysfs" and valid root in the environment */
90 { "sysfs", "/dev/bus/usb", "/dev/bus/usb", true, NULL, false, VINF_SUCCESS, "/dev/bus/usb", false, VINF_SUCCESS },
91 /* "sysfs" and valid root in the environment, method-specific init failed */
92 { "sysfs", "/dev/bus/usb", "/dev/bus/usb", true, NULL, false, VERR_NO_MEMORY, "/dev/bus/usb", false, VERR_NO_MEMORY },
93 /* "sysfs" and bad root in the environment */
94 { "sysfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
95 /* "sysfs" and no root in the environment */
96 { "sysfs", NULL, "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
97 /* "usbfs" and valid root in the environment */
98 { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VINF_SUCCESS, "/dev/bus/usb", true, VINF_SUCCESS },
99 /* "usbfs" and valid root in the environment, method-specific init failed */
100 { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VERR_NO_MEMORY, "/dev/bus/usb", true, VERR_NO_MEMORY },
101 /* "usbfs" and bad root in the environment */
102 { "usbfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
103 /* "usbfs" and no root in the environment */
104 { "usbfs", NULL, NULL, false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
105 /* invalid method in the environment, sysfs available */
106 { "invalid", "/dev/bus/usb", "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
107 /* invalid method in the environment, usbfs available */
108 { "invalid", "/dev/bus/usb", NULL, true, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
109 /* invalid method in the environment, sysfs inaccessible */
110 { "invalid", "/dev/bus/usb", "/dev/vboxusb", false, NULL, false, VINF_SUCCESS, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
111 /* invalid method in the environment, usbfs inaccessible */
112 { "invalid", "/dev/bus/usb", NULL, false, "/proc/bus/usb", false, VINF_SUCCESS, "", true, VERR_VUSB_USBFS_PERMISSION },
113 /* No environment, sysfs and usbfs available but without access permissions. */
114 { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
115 /* No environment, sysfs and usbfs available, access permissions for sysfs. */
116 { NULL, NULL, "/dev/vboxusb", true, "/proc/bus/usb", false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
117 /* No environment, sysfs and usbfs available, access permissions for usbfs. */
118 { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
119 /* No environment, sysfs available but without access permissions. */
120 { NULL, NULL, "/dev/vboxusb", false, NULL, false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
121 /* No environment, sysfs available with access permissions, method-specific init failed. */
122 { NULL, NULL, "/dev/vboxusb", true, NULL, false, VERR_NO_MEMORY, "/dev/vboxusb", false, VERR_NO_MEMORY },
123 /* No environment, usbfs available but without access permissions. */
124 { NULL, NULL, NULL, false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USBFS_PERMISSION },
125 /* No environment, usbfs available with access permissions, method-specific
126 * init failed. */
127 { NULL, NULL, NULL, false, "/proc/bus/usb", true, VERR_NO_MEMORY, "/proc/bus/usb", true, VERR_NO_MEMORY }
128};
129
130static void testInit(RTTEST hTest)
131{
132 RTTestSub(hTest, "Testing USBProxyServiceLinux initialisation");
133 for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
134 {
135 USBProxyServiceLinux test(NULL);
136 test.testSetEnv(s_testEnvironment[i].pcszEnvUsb,
137 s_testEnvironment[i].pcszEnvUsbRoot);
138 test.testSetupInit(s_testEnvironment[i].pcszUsbfsRoot,
139 s_testEnvironment[i].fUsbfsAccessible,
140 s_testEnvironment[i].pcszDevicesRoot,
141 s_testEnvironment[i].fDevicesAccessible,
142 s_testEnvironment[i].rcMethodInit);
143 HRESULT hrc = test.init();
144 RTTESTI_CHECK_MSG(hrc == S_OK,
145 ("init() returned 0x%x (test index %i)!\n", hrc, i));
146 int rc = test.getLastError();
147 RTTESTI_CHECK_MSG(rc == s_testEnvironment[i].rcExpected,
148 ("getLastError() returned %Rrc (test index %i) instead of %Rrc!\n",
149 rc, i, s_testEnvironment[i].rcExpected));
150 const char *pcszDevicesRoot = test.testGetDevicesRoot();
151 RTTESTI_CHECK_MSG(!RTStrCmp(pcszDevicesRoot,
152 s_testEnvironment[i].pcszDevicesRootExpected),
153 ("testGetDevicesRoot() returned %s (test index %i) instead of %s!\n",
154 pcszDevicesRoot, i,
155 s_testEnvironment[i].pcszDevicesRootExpected));
156 bool fUsingUsbfs = test.testGetUsingUsbfs();
157 RTTESTI_CHECK_MSG( fUsingUsbfs
158 == s_testEnvironment[i].fUsingUsbfsExpected,
159 ("testGetUsingUsbfs() returned %RTbool (test index %i) instead of %RTbool!\n",
160 fUsingUsbfs, i,
161 s_testEnvironment[i].fUsingUsbfsExpected));
162 }
163}
164
165static struct
166{
167 const char *pacszDeviceAddresses[16];
168 const char *pacszAccessibleFiles[16];
169 const char *pcszRoot;
170 bool fIsDeviceNodes;
171 bool fAvailableExpected;
172} s_testCheckDeviceRoot[] =
173{
174 /* /dev/vboxusb accessible -> device nodes method available */
175 { { NULL }, { "/dev/vboxusb" }, "/dev/vboxusb", true, true },
176 /* /dev/vboxusb present but not accessible -> device nodes method not
177 * available */
178 { { NULL }, { NULL }, "/dev/vboxusb", true, false },
179 /* /proc/bus/usb available but empty -> usbfs method available (we can't
180 * really check in this case) */
181 { { NULL }, { NULL }, "/proc/bus/usb", false, true },
182 /* /proc/bus/usb available, one inaccessible device -> usbfs method not
183 * available */
184 { { "/proc/bus/usb/001/001" }, { NULL }, "/proc/bus/usb", false, false },
185 /* /proc/bus/usb available, one device of two inaccessible -> usbfs method
186 * not available */
187 { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
188 { "/proc/bus/usb/001/001" }, "/proc/bus/usb", false, false },
189 /* /proc/bus/usb available, two accessible devices -> usbfs method
190 * available */
191 { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
192 { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
193 "/proc/bus/usb", false, true }
194};
195
196static void testCheckDeviceRoot(RTTEST hTest)
197{
198 RTTestSub(hTest, "Testing the USBProxyLinuxCheckDeviceRoot API");
199 for (unsigned i = 0; i < RT_ELEMENTS(s_testCheckDeviceRoot); ++i)
200 {
201 TestUSBSetAvailableUsbfsDevices(s_testCheckDeviceRoot[i]
202 .pacszDeviceAddresses);
203 TestUSBSetAccessibleFiles(s_testCheckDeviceRoot[i]
204 .pacszAccessibleFiles);
205 bool fAvailable = USBProxyLinuxCheckDeviceRoot
206 (s_testCheckDeviceRoot[i].pcszRoot,
207 s_testCheckDeviceRoot[i].fIsDeviceNodes);
208 RTTESTI_CHECK_MSG( fAvailable
209 == s_testCheckDeviceRoot[i].fAvailableExpected,
210 ("USBProxyLinuxCheckDeviceRoot() returned %RTbool (test index %i) instead of %RTbool!\n",
211 fAvailable, i,
212 s_testCheckDeviceRoot[i].fAvailableExpected));
213 }
214}
215
216int main(void)
217{
218 /*
219 * Init the runtime, test and say hello.
220 */
221 RTTEST hTest;
222 RTEXITCODE rcExit = RTTestInitAndCreate("tstUSBProxyLinux", &hTest);
223 if (rcExit != RTEXITCODE_SUCCESS)
224 return rcExit;
225 RTTestBanner(hTest);
226
227 /*
228 * Run the tests.
229 */
230 testInit(hTest);
231 testCheckDeviceRoot(hTest);
232
233 /*
234 * Summary
235 */
236 return RTTestSummaryAndDestroy(hTest);
237}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette