VirtualBox

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

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

Main/USB/Linux: extend environment variables for selecting USB probing method and add skeleton testcase

  • 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 36419 2011-03-24 22:53:54Z 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 USBDEVTREELOCATION s_deviceRoot = { "", false };
65static bool s_fGetDeviceRootPreferSysfs = false;
66
67PCUSBDEVTREELOCATION USBProxyLinuxGetDeviceRoot(bool fPreferSysfs)
68{
69 s_fGetDeviceRootPreferSysfs = fPreferSysfs;
70 return &s_deviceRoot;
71}
72
73static struct
74{
75 const char *pcszDevicesRoot;
76 bool fUseSysfs;
77} s_getDevices;
78
79PUSBDEVICE USBProxyLinuxGetDevices(const char *pcszDevicesRoot,
80 bool fUseSysfs)
81{
82 s_getDevices.pcszDevicesRoot = pcszDevicesRoot;
83 s_getDevices.fUseSysfs = fUseSysfs;
84 return NULL;
85}
86
87void SysFreeString(BSTR bstr)
88{
89 Assert(0);
90}
91
92/*** END STUBS ***/
93
94class tstUSBProxyLinux : public USBProxyServiceLinux
95{
96protected:
97 virtual int initUsbfs(void) { return VINF_SUCCESS; }
98 virtual int initSysfs(void) { return VINF_SUCCESS; }
99public:
100 tstUSBProxyLinux(void) : USBProxyServiceLinux(NULL) {}
101 PUSBDEVICE getDevices(void)
102 {
103 return USBProxyServiceLinux::getDevices();
104 }
105};
106
107static struct
108{
109 const char *pcszVBOX_USB;
110 const char *pcszVBOX_USB_ROOT;
111 const char *pcszReturnedRoot;
112 bool fReturnedUseSysfs;
113 bool fRequestedUseSysfs;
114 const char *pcszFinalRoot;
115 bool fFinalUseSysfs;
116} s_testEnvironment[] =
117{
118 { "sysfs", "/dev/bus/usb", "/proc/bus/usb", false, true, "/dev/bus/usb", true },
119 { "sysfs", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/bus/usb", true },
120 { "sysfs", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
121 { "sysfs", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
122 { "usbfs", "/dev/bus/usb", "/proc/bus/usb", false, false, "/dev/bus/usb", false },
123 { "usbfs", "/dev/bus/usb", "/dev/vboxusb", true, false, "/dev/bus/usb", false },
124 { "usbfs", NULL, "/proc/bus/usb", false, false, "/proc/bus/usb", false },
125 { "usbfs", NULL, "/dev/vboxusb", true, false, "/dev/vboxusb", true },
126 { "nofs", "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
127 { "nofs", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true },
128 { "nofs", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
129 { "nofs", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
130 { "", "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
131 { "", "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true },
132 { "", NULL, "/proc/bus/usb", false, true, "/proc/bus/usb", false },
133 { "", NULL, "/dev/vboxusb", true, true, "/dev/vboxusb", true },
134 { NULL, "/dev/bus/usb", "/proc/bus/usb", false, true, "/proc/bus/usb", false },
135 { NULL, "/dev/bus/usb", "/dev/vboxusb", true, true, "/dev/vboxusb", true }
136};
137
138/** @note Fiddling with the real environment for this is not nice, in my
139 * opinion at least. */
140static void testEnvironment(RTTEST hTest)
141{
142 RTTestSub(hTest, "Testing environment variable handling");
143 for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
144 {
145 tstUSBProxyLinux test;
146 if (s_testEnvironment[i].pcszVBOX_USB)
147 RTEnvSet("VBOX_USB", s_testEnvironment[i].pcszVBOX_USB);
148 else
149 RTEnvUnset("VBOX_USB");
150 if (s_testEnvironment[i].pcszVBOX_USB_ROOT)
151 RTEnvSet("VBOX_USB_ROOT",
152 s_testEnvironment[i].pcszVBOX_USB_ROOT);
153 else
154 RTEnvUnset("VBOX_USB_ROOT");
155 strcpy(s_deviceRoot.szDevicesRoot,
156 s_testEnvironment[i].pcszReturnedRoot);
157 s_deviceRoot.fUseSysfs = 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