2 * Copyright (C) 2013-2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
18 * Author: Jan Tomko <jtomko@redhat.com>
29 #include "testutils.h"
31 #define VIR_FROM_THIS VIR_FROM_NONE
51 static int testDeviceFileActor(virUSBDevicePtr dev,
53 void *opaque ATTRIBUTE_UNUSED)
58 if (virAsprintf(&str, USB_DEVFS "%03d/%03d",
59 virUSBDeviceGetBus(dev),
60 virUSBDeviceGetDevno(dev)) < 0)
63 if (STRNEQ(path, str)) {
64 virReportError(VIR_ERR_INTERNAL_ERROR,
65 "Device path '%s' does not match expected '%s'",
73 static int testDeviceFind(const void *opaque)
75 const struct findTestInfo *info = opaque;
77 virUSBDevicePtr dev = NULL;
78 virUSBDeviceListPtr devs = NULL;
84 rv = virUSBDeviceFind(info->vendor, info->product,
85 info->bus, info->devno,
86 info->vroot, info->mandatory, &dev);
89 rv = virUSBDeviceFindByVendor(info->vendor, info->product,
90 info->vroot, info->mandatory, &devs);
93 rv = virUSBDeviceFindByBus(info->bus, info->devno,
94 info->vroot, info->mandatory, &dev);
98 if (info->expectFailure) {
100 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
101 "unexpected success");
113 if (virUSBDeviceFileIterate(dev, testDeviceFileActor, NULL) < 0)
118 ndevs = virUSBDeviceListCount(devs);
120 for (i = 0; i < ndevs; i++) {
121 virUSBDevicePtr device = virUSBDeviceListGet(devs, i);
122 if (virUSBDeviceFileIterate(device, testDeviceFileActor, NULL) < 0)
131 virObjectUnref(devs);
132 virUSBDeviceFree(dev);
138 testCheckNdevs(const char* occasion,
142 if (got != expected) {
143 virReportError(VIR_ERR_INTERNAL_ERROR,
144 "%s: got %zu devices, expected %zu",
145 occasion, got, expected);
153 testUSBList(const void *opaque ATTRIBUTE_UNUSED)
155 virUSBDeviceListPtr list = NULL;
156 virUSBDeviceListPtr devlist = NULL;
157 virUSBDevicePtr dev = NULL;
161 if (!(list = virUSBDeviceListNew()))
164 #define EXPECTED_NDEVS_ONE 3
165 if (virUSBDeviceFindByVendor(0x1d6b, 0x0002, NULL, true, &devlist) < 0)
168 ndevs = virUSBDeviceListCount(devlist);
169 if (testCheckNdevs("After first find", ndevs, EXPECTED_NDEVS_ONE) < 0)
172 for (i = 0; i < ndevs; i++) {
173 dev = virUSBDeviceListGet(devlist, 0);
174 dev = virUSBDeviceListSteal(devlist, dev);
176 if (virUSBDeviceListAdd(list, dev) < 0)
181 virObjectUnref(devlist);
184 ndevs = virUSBDeviceListCount(list);
185 if (testCheckNdevs("After first loop", ndevs, EXPECTED_NDEVS_ONE) < 0)
188 #define EXPECTED_NDEVS_TWO 3
189 if (virUSBDeviceFindByVendor(0x18d1, 0x4e22, NULL, true, &devlist) < 0)
192 ndevs = virUSBDeviceListCount(devlist);
193 if (testCheckNdevs("After second find", ndevs, EXPECTED_NDEVS_TWO) < 0)
195 for (i = 0; i < ndevs; i++) {
196 dev = virUSBDeviceListGet(devlist, 0);
197 dev = virUSBDeviceListSteal(devlist, dev);
199 if (virUSBDeviceListAdd(list, dev) < 0)
204 if (testCheckNdevs("After second loop",
205 virUSBDeviceListCount(list),
206 EXPECTED_NDEVS_ONE + EXPECTED_NDEVS_TWO) < 0)
209 if (virUSBDeviceFind(0x18d1, 0x4e22, 1, 20, NULL, true, &dev) < 0)
212 if (!virUSBDeviceListFind(list, dev)) {
213 virReportError(VIR_ERR_INTERNAL_ERROR,
214 "Device '%s' not in list when it should be",
215 virUSBDeviceGetName(dev));
219 virUSBDeviceListDel(list, dev);
222 if (testCheckNdevs("After deleting one",
223 virUSBDeviceListCount(list),
224 EXPECTED_NDEVS_ONE + EXPECTED_NDEVS_TWO - 1) < 0)
230 virObjectUnref(list);
231 virObjectUnref(devlist);
232 virUSBDeviceFree(dev);
242 #define DO_TEST_FIND_FULL(name, vend, prod, bus, devno, vroot, mand, how, fail) \
244 struct findTestInfo data = { name, vend, prod, bus, \
245 devno, vroot, mand, how, fail \
247 if (virtTestRun("USBDeviceFind " name, testDeviceFind, &data) < 0) \
251 #define DO_TEST_FIND(name, vend, prod, bus, devno) \
252 DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \
254 #define DO_TEST_FIND_FAIL(name, vend, prod, bus, devno) \
255 DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \
258 #define DO_TEST_FIND_BY_BUS(name, bus, devno) \
259 DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \
261 #define DO_TEST_FIND_BY_BUS_FAIL(name, bus, devno) \
262 DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \
265 #define DO_TEST_FIND_BY_VENDOR(name, vend, prod) \
266 DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \
267 FIND_BY_VENDOR, false)
268 #define DO_TEST_FIND_BY_VENDOR_FAIL(name, vend, prod) \
269 DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \
270 FIND_BY_VENDOR, true)
272 DO_TEST_FIND("Nexus", 0x18d1, 0x4e22, 1, 20);
273 DO_TEST_FIND_FAIL("Nexus wrong devnum", 0x18d1, 0x4e22, 1, 25);
274 DO_TEST_FIND_FAIL("Bogus", 0xf00d, 0xbeef, 1024, 768);
276 DO_TEST_FIND_BY_BUS("integrated camera", 1, 5);
277 DO_TEST_FIND_BY_BUS_FAIL("wrong bus/devno combination", 2, 20);
278 DO_TEST_FIND_BY_BUS_FAIL("missing bus", 5, 20);
279 DO_TEST_FIND_BY_BUS_FAIL("missing devnum", 1, 158);
281 DO_TEST_FIND_BY_VENDOR("Nexus (multiple results)", 0x18d1, 0x4e22);
282 DO_TEST_FIND_BY_VENDOR_FAIL("Bogus vendor and product", 0xf00d, 0xbeef);
283 DO_TEST_FIND_BY_VENDOR_FAIL("Valid vendor", 0x1d6b, 0xbeef);
285 if (virtTestRun("USB List test", testUSBList, NULL) < 0)
293 VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virusbmock.so")