VirtualBox

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

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

Main/linux/USB: better permissions checking and code cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: tstUSBProxyLinux.cpp 36958 2011-05-04 14:50:32Z 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 <iprt/assert.h>
26#include <iprt/err.h>
27#include <iprt/env.h>
28#include <iprt/test.h>
29
30/*** BEGIN STUBS ***/
31
32USBProxyService::USBProxyService(Host*) {}
33USBProxyService::~USBProxyService() {}
34HRESULT USBProxyService::init() { return S_OK; }
35int USBProxyService::start() { return VINF_SUCCESS; }
36int USBProxyService::stop() { return VINF_SUCCESS; }
37RWLockHandle *USBProxyService::lockHandle() const { return NULL; }
38void *USBProxyService::insertFilter(USBFILTER const*) { return NULL; }
39void USBProxyService::removeFilter(void*) {}
40int USBProxyService::captureDevice(HostUSBDevice*) { return VINF_SUCCESS; }
41void USBProxyService::captureDeviceCompleted(HostUSBDevice*, bool) {}
42void USBProxyService::detachingDevice(HostUSBDevice*) {}
43int USBProxyService::releaseDevice(HostUSBDevice*) { return VINF_SUCCESS; }
44void USBProxyService::releaseDeviceCompleted(HostUSBDevice*, bool) {}
45void USBProxyService::serviceThreadInit() {}
46void USBProxyService::serviceThreadTerm() {}
47int USBProxyService::wait(unsigned int) { return VINF_SUCCESS; }
48int USBProxyService::interruptWait() { return VINF_SUCCESS; }
49PUSBDEVICE USBProxyService::getDevices() { return NULL; }
50void USBProxyService::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice) {}
51void USBProxyService::deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice) {}
52void USBProxyService::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList*, SessionMachine*) {}
53bool USBProxyService::updateDeviceState(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
54bool USBProxyService::updateDeviceStateFake(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
55bool USBProxyService::isActive() { return true; }
56
57VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(char const*) {}
58
59com::Utf8Str HostUSBDevice::getName()
60{
61 return Utf8Str();
62}
63
64static const char *s_pcszDeviceRoot = "";
65static bool s_fIsDeviceNodes = false;
66
67bool USBProxyLinuxCheckDeviceRoot(const char *pcszRoot,
68 bool fIsDeviceNodes)
69{
70 return ( (!strcmp(s_pcszDeviceRoot, pcszRoot))
71 && (s_fIsDeviceNodes == fIsDeviceNodes));
72}
73
74static struct
75{
76 const char *pcszDevicesRoot;
77 bool fUseSysfs;
78} s_getDevices;
79
80PUSBDEVICE USBProxyLinuxGetDevices(const char *pcszDevicesRoot,
81 bool fUseSysfs)
82{
83 s_getDevices.pcszDevicesRoot = pcszDevicesRoot;
84 s_getDevices.fUseSysfs = fUseSysfs;
85 return NULL;
86}
87
88void SysFreeString(BSTR bstr)
89{
90 Assert(0);
91}
92
93/*** END STUBS ***/
94
95class tstUSBProxyLinux : public USBProxyServiceLinux
96{
97protected:
98 virtual int initUsbfs(void) { return VINF_SUCCESS; }
99 virtual int initSysfs(void) { return VINF_SUCCESS; }
100public:
101 tstUSBProxyLinux(void) : USBProxyServiceLinux(NULL) {}
102 PUSBDEVICE getDevices(void)
103 {
104 return USBProxyServiceLinux::getDevices();
105 }
106};
107
108static struct
109{
110 const char *pcszVBOX_USB;
111 const char *pcszVBOX_USB_ROOT;
112 const char *pcszReturnedRoot;
113 bool fReturnedUseSysfs;
114 bool fRequestedUseSysfs;
115 const char *pcszFinalRoot;
116 bool fFinalUseSysfs;
117} s_testEnvironment[] =
118{
119 { "sysfs", "/dev/bus/usb", "/proc/bus/usb", false, true, "/dev/bus/usb", true },
120 { "sysfs", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/bus/usb", true },
121 { "sysfs", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
122 { "sysfs", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
123 { "usbfs", "/dev/bus/usb", "/proc/bus/usb", false, false, "/dev/bus/usb", false },
124 { "usbfs", "/dev/bus/usb", "/dev/vboxusb", true, false, "/dev/bus/usb", false },
125 { "usbfs", NULL, "/proc/bus/usb", false, false, "/proc/bus/usb", false },
126 { "usbfs", NULL, "/dev/vboxusb", true, false, "/dev/vboxusb", true },
127 { "nofs", "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
128 { "nofs", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true },
129 { "nofs", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
130 { "nofs", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
131 { "", "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
132 { "", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true },
133 { "", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
134 { "", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
135 { NULL, "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
136 { NULL, "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true }
137};
138
139/** @note Fiddling with the real environment for this is not nice, in my
140 * opinion at least. */
141static void testEnvironment(RTTEST hTest)
142{
143 RTTestSub(hTest, "Testing environment variable handling");
144 for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
145 {
146 tstUSBProxyLinux test;
147 if (s_testEnvironment[i].pcszVBOX_USB)
148 RTEnvSet("VBOX_USB", s_testEnvironment[i].pcszVBOX_USB);
149 else
150 RTEnvUnset("VBOX_USB");
151 if (s_testEnvironment[i].pcszVBOX_USB_ROOT)
152 RTEnvSet("VBOX_USB_ROOT",
153 s_testEnvironment[i].pcszVBOX_USB_ROOT);
154 else
155 RTEnvUnset("VBOX_USB_ROOT");
156 s_pcszDeviceRoot = s_testEnvironment[i].pcszReturnedRoot;
157 s_fIsDeviceNodes = s_testEnvironment[i].fReturnedUseSysfs;
158 RTTESTI_CHECK(test.init() == S_OK);
159 test.getDevices();
160 RTTESTI_CHECK_MSG(!strcmp(s_getDevices.pcszDevicesRoot,
161 s_testEnvironment[i].pcszFinalRoot),
162 ("i=%u: %s, %s\n", i, s_getDevices.pcszDevicesRoot,
163 s_testEnvironment[i].pcszFinalRoot));
164 RTTESTI_CHECK_MSG( s_getDevices.fUseSysfs
165 == s_testEnvironment[i].fFinalUseSysfs,
166 ("i=%u, %d, %d\n", i, s_getDevices.fUseSysfs,
167 s_testEnvironment[i].fFinalUseSysfs));
168 }
169}
170
171int main(void)
172{
173 /*
174 * Init the runtime, test and say hello.
175 */
176 RTTEST hTest;
177 RTEXITCODE rcExit = RTTestInitAndCreate("tstUSBProxyLinux", &hTest);
178 if (rcExit != RTEXITCODE_SUCCESS)
179 return rcExit;
180 RTTestBanner(hTest);
181
182 /*
183 * Run the tests.
184 */
185 testEnvironment(hTest);
186
187 /*
188 * Summary
189 */
190 return RTTestSummaryAndDestroy(hTest);
191}
192
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