VirtualBox

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

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

Main/USB/linux: move USBProxyLinuxChooseMethod to USBGetDevices

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1/* $Id: tstUSBProxyLinux.cpp 37618 2011-06-23 17:16:39Z 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 bad root in the environment */
92 { "sysfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
93 /* "sysfs" and no root in the environment */
94 { "sysfs", NULL, "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
95 /* "usbfs" and valid root in the environment */
96 { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VINF_SUCCESS, "/dev/bus/usb", true, VINF_SUCCESS },
97 /* "usbfs" and bad root in the environment */
98 { "usbfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
99 /* "usbfs" and no root in the environment */
100 { "usbfs", NULL, NULL, false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
101 /* invalid method in the environment, sysfs available */
102 { "invalid", "/dev/bus/usb", "/dev/vboxusb", true, NULL, false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
103 /* invalid method in the environment, usbfs available */
104 { "invalid", "/dev/bus/usb", NULL, true, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
105 /* invalid method in the environment, sysfs inaccessible */
106 { "invalid", "/dev/bus/usb", "/dev/vboxusb", false, NULL, false, VINF_SUCCESS, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
107 /* invalid method in the environment, usbfs inaccessible */
108 { "invalid", "/dev/bus/usb", NULL, false, "/proc/bus/usb", false, VINF_SUCCESS, "", true, VERR_VUSB_USBFS_PERMISSION },
109 /* No environment, sysfs and usbfs available but without access permissions. */
110 { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
111 /* No environment, sysfs and usbfs available, access permissions for sysfs. */
112 { NULL, NULL, "/dev/vboxusb", true, "/proc/bus/usb", false, VINF_SUCCESS, "/dev/vboxusb", false, VINF_SUCCESS },
113 /* No environment, sysfs and usbfs available, access permissions for usbfs. */
114 { NULL, NULL, "/dev/vboxusb", false, "/proc/bus/usb", true, VINF_SUCCESS, "/proc/bus/usb", true, VINF_SUCCESS },
115 /* No environment, sysfs available but without access permissions. */
116 { NULL, NULL, "/dev/vboxusb", false, NULL, false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
117 /* No environment, usbfs available but without access permissions. */
118 { NULL, NULL, NULL, false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USBFS_PERMISSION },
119};
120
121static void testInit(RTTEST hTest)
122{
123 RTTestSub(hTest, "Testing USBProxyLinuxChooseMethod");
124 for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
125 {
126 bool fUsingUsbfs = true;
127 const char *pcszDevicesRoot = "";
128
129 TestUSBSetEnv(s_testEnvironment[i].pcszEnvUsb,
130 s_testEnvironment[i].pcszEnvUsbRoot);
131 TestUSBSetupInit(s_testEnvironment[i].pcszUsbfsRoot,
132 s_testEnvironment[i].fUsbfsAccessible,
133 s_testEnvironment[i].pcszDevicesRoot,
134 s_testEnvironment[i].fDevicesAccessible,
135 s_testEnvironment[i].rcMethodInit);
136 int rc = USBProxyLinuxChooseMethod(&fUsingUsbfs, &pcszDevicesRoot);
137 RTTESTI_CHECK_MSG(rc == s_testEnvironment[i].rcExpected,
138 ("rc=%Rrc (test index %i) instead of %Rrc!\n",
139 rc, i, s_testEnvironment[i].rcExpected));
140 RTTESTI_CHECK_MSG(!RTStrCmp(pcszDevicesRoot,
141 s_testEnvironment[i].pcszDevicesRootExpected),
142 ("testGetDevicesRoot() returned %s (test index %i) instead of %s!\n",
143 pcszDevicesRoot, i,
144 s_testEnvironment[i].pcszDevicesRootExpected));
145 RTTESTI_CHECK_MSG( fUsingUsbfs
146 == s_testEnvironment[i].fUsingUsbfsExpected,
147 ("testGetUsingUsbfs() returned %RTbool (test index %i) instead of %RTbool!\n",
148 fUsingUsbfs, i,
149 s_testEnvironment[i].fUsingUsbfsExpected));
150 }
151}
152
153static struct
154{
155 const char *pacszDeviceAddresses[16];
156 const char *pacszAccessibleFiles[16];
157 const char *pcszRoot;
158 bool fIsDeviceNodes;
159 bool fAvailableExpected;
160} s_testCheckDeviceRoot[] =
161{
162 /* /dev/vboxusb accessible -> device nodes method available */
163 { { NULL }, { "/dev/vboxusb" }, "/dev/vboxusb", true, true },
164 /* /dev/vboxusb present but not accessible -> device nodes method not
165 * available */
166 { { NULL }, { NULL }, "/dev/vboxusb", true, false },
167 /* /proc/bus/usb available but empty -> usbfs method available (we can't
168 * really check in this case) */
169 { { NULL }, { "/proc/bus/usb" }, "/proc/bus/usb", false, true },
170 /* /proc/bus/usb not available or not accessible -> usbfs method not available */
171 { { NULL }, { NULL }, "/proc/bus/usb", false, false },
172 /* /proc/bus/usb available, one inaccessible device -> usbfs method not
173 * available */
174 { { "/proc/bus/usb/001/001" }, { "/proc/bus/usb" }, "/proc/bus/usb", false, false },
175 /* /proc/bus/usb available, one device of two inaccessible -> usbfs method
176 * not available */
177 { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
178 { "/proc/bus/usb", "/proc/bus/usb/001/001" }, "/proc/bus/usb", false, false },
179 /* /proc/bus/usb available, two accessible devices -> usbfs method
180 * available */
181 { { "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
182 { "/proc/bus/usb", "/proc/bus/usb/001/001", "/proc/bus/usb/002/002" },
183 "/proc/bus/usb", false, true }
184};
185
186static void testCheckDeviceRoot(RTTEST hTest)
187{
188 RTTestSub(hTest, "Testing the USBProxyLinuxCheckDeviceRoot API");
189 for (unsigned i = 0; i < RT_ELEMENTS(s_testCheckDeviceRoot); ++i)
190 {
191 TestUSBSetAvailableUsbfsDevices(s_testCheckDeviceRoot[i]
192 .pacszDeviceAddresses);
193 TestUSBSetAccessibleFiles(s_testCheckDeviceRoot[i]
194 .pacszAccessibleFiles);
195 bool fAvailable = USBProxyLinuxCheckDeviceRoot
196 (s_testCheckDeviceRoot[i].pcszRoot,
197 s_testCheckDeviceRoot[i].fIsDeviceNodes);
198 RTTESTI_CHECK_MSG( fAvailable
199 == s_testCheckDeviceRoot[i].fAvailableExpected,
200 ("USBProxyLinuxCheckDeviceRoot() returned %RTbool (test index %i) instead of %RTbool!\n",
201 fAvailable, i,
202 s_testCheckDeviceRoot[i].fAvailableExpected));
203 }
204}
205
206int main(void)
207{
208 /*
209 * Init the runtime, test and say hello.
210 */
211 RTTEST hTest;
212 RTEXITCODE rcExit = RTTestInitAndCreate("tstUSBProxyLinux", &hTest);
213 if (rcExit != RTEXITCODE_SUCCESS)
214 return rcExit;
215 RTTestBanner(hTest);
216
217 /*
218 * Run the tests.
219 */
220 testInit(hTest);
221 testCheckDeviceRoot(hTest);
222
223 /*
224 * Summary
225 */
226 return RTTestSummaryAndDestroy(hTest);
227}
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