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 |
|
---|
32 | USBProxyService::USBProxyService(Host*) {}
|
---|
33 | USBProxyService::~USBProxyService() {}
|
---|
34 | HRESULT USBProxyService::init() { return S_OK; }
|
---|
35 | int USBProxyService::start() { return VINF_SUCCESS; }
|
---|
36 | int USBProxyService::stop() { return VINF_SUCCESS; }
|
---|
37 | RWLockHandle *USBProxyService::lockHandle() const { return NULL; }
|
---|
38 | void *USBProxyService::insertFilter(USBFILTER const*) { return NULL; }
|
---|
39 | void USBProxyService::removeFilter(void*) {}
|
---|
40 | int USBProxyService::captureDevice(HostUSBDevice*) { return VINF_SUCCESS; }
|
---|
41 | void USBProxyService::captureDeviceCompleted(HostUSBDevice*, bool) {}
|
---|
42 | void USBProxyService::detachingDevice(HostUSBDevice*) {}
|
---|
43 | int USBProxyService::releaseDevice(HostUSBDevice*) { return VINF_SUCCESS; }
|
---|
44 | void USBProxyService::releaseDeviceCompleted(HostUSBDevice*, bool) {}
|
---|
45 | void USBProxyService::serviceThreadInit() {}
|
---|
46 | void USBProxyService::serviceThreadTerm() {}
|
---|
47 | int USBProxyService::wait(unsigned int) { return VINF_SUCCESS; }
|
---|
48 | int USBProxyService::interruptWait() { return VINF_SUCCESS; }
|
---|
49 | PUSBDEVICE USBProxyService::getDevices() { return NULL; }
|
---|
50 | void USBProxyService::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice) {}
|
---|
51 | void USBProxyService::deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice) {}
|
---|
52 | void USBProxyService::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList*, SessionMachine*) {}
|
---|
53 | bool USBProxyService::updateDeviceState(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
|
---|
54 | bool USBProxyService::updateDeviceStateFake(HostUSBDevice*, USBDEVICE*, bool*, SessionMachine**) { return true; }
|
---|
55 | bool USBProxyService::isActive() { return true; }
|
---|
56 |
|
---|
57 | VBoxMainHotplugWaiter::VBoxMainHotplugWaiter(char const*) {}
|
---|
58 |
|
---|
59 | com::Utf8Str HostUSBDevice::getName()
|
---|
60 | {
|
---|
61 | return Utf8Str();
|
---|
62 | }
|
---|
63 |
|
---|
64 | static USBDEVTREELOCATION s_deviceRoot = { "", false };
|
---|
65 | static bool s_fGetDeviceRootPreferSysfs = false;
|
---|
66 |
|
---|
67 | PCUSBDEVTREELOCATION USBProxyLinuxGetDeviceRoot(bool fPreferSysfs)
|
---|
68 | {
|
---|
69 | s_fGetDeviceRootPreferSysfs = fPreferSysfs;
|
---|
70 | return &s_deviceRoot;
|
---|
71 | }
|
---|
72 |
|
---|
73 | static struct
|
---|
74 | {
|
---|
75 | const char *pcszDevicesRoot;
|
---|
76 | bool fUseSysfs;
|
---|
77 | } s_getDevices;
|
---|
78 |
|
---|
79 | PUSBDEVICE USBProxyLinuxGetDevices(const char *pcszDevicesRoot,
|
---|
80 | bool fUseSysfs)
|
---|
81 | {
|
---|
82 | s_getDevices.pcszDevicesRoot = pcszDevicesRoot;
|
---|
83 | s_getDevices.fUseSysfs = fUseSysfs;
|
---|
84 | return NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void SysFreeString(BSTR bstr)
|
---|
88 | {
|
---|
89 | Assert(0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*** END STUBS ***/
|
---|
93 |
|
---|
94 | class tstUSBProxyLinux : public USBProxyServiceLinux
|
---|
95 | {
|
---|
96 | protected:
|
---|
97 | virtual int initUsbfs(void) { return VINF_SUCCESS; }
|
---|
98 | virtual int initSysfs(void) { return VINF_SUCCESS; }
|
---|
99 | public:
|
---|
100 | tstUSBProxyLinux(void) : USBProxyServiceLinux(NULL) {}
|
---|
101 | PUSBDEVICE getDevices(void)
|
---|
102 | {
|
---|
103 | return USBProxyServiceLinux::getDevices();
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 | static 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. */
|
---|
140 | static 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 |
|
---|
171 | int 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 |
|
---|