virusbtest.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/*
 * Copyright (C) 2013-2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Jan Tomko <jtomko@redhat.com>
 */

#include <config.h>
#include <stdlib.h>

#include "viralloc.h"
#include "virerror.h"
#include "virfile.h"
#include "virusb.h"

#include "testutils.h"

#define VIR_FROM_THIS VIR_FROM_NONE

typedef enum {
    FIND_BY_ALL,
    FIND_BY_VENDOR,
    FIND_BY_BUS
} testUSBFindFlags;

struct findTestInfo {
    const char *name;
    unsigned int vendor;
    unsigned int product;
    unsigned int bus;
    unsigned int devno;
    const char *vroot;
    bool mandatory;
    int how;
    bool expectFailure;
};

static int testDeviceFileActor(virUSBDevicePtr dev,
                               const char *path,
                               void *opaque ATTRIBUTE_UNUSED)
{
    char *str = NULL;
    int ret = 0;

    if (virAsprintf(&str, USB_DEVFS "%03d/%03d",
                    virUSBDeviceGetBus(dev),
                    virUSBDeviceGetDevno(dev)) < 0)
        return -1;

    if (STRNEQ(path, str)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Device path '%s' does not match expected '%s'",
                       path, str);
        ret = -1;
    }
    VIR_FREE(str);
    return ret;
}

static int testDeviceFind(const void *opaque)
{
    const struct findTestInfo *info = opaque;
    int ret = -1;
    virUSBDevicePtr dev = NULL;
    virUSBDeviceListPtr devs = NULL;
    int rv = 0;
    size_t i, ndevs = 0;

    switch (info->how) {
    case FIND_BY_ALL:
        rv = virUSBDeviceFind(info->vendor, info->product,
                              info->bus, info->devno,
                              info->vroot, info->mandatory, &dev);
        break;
    case FIND_BY_VENDOR:
        rv = virUSBDeviceFindByVendor(info->vendor, info->product,
                                      info->vroot, info->mandatory, &devs);
        break;
    case FIND_BY_BUS:
        rv = virUSBDeviceFindByBus(info->bus, info->devno,
                                   info->vroot, info->mandatory, &dev);
        break;
    }

    if (info->expectFailure) {
        if (rv == 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           "unexpected success");
        } else {
            ret = 0;
        }
        goto cleanup;
    } else if (rv < 0) {
        goto cleanup;
    }

    switch (info->how) {
    case FIND_BY_ALL:
    case FIND_BY_BUS:
        if (virUSBDeviceFileIterate(dev, testDeviceFileActor, NULL) < 0)
            goto cleanup;
        break;

    case FIND_BY_VENDOR:
        ndevs = virUSBDeviceListCount(devs);

        for (i = 0; i < ndevs; i++) {
            virUSBDevicePtr device = virUSBDeviceListGet(devs, i);
            if (virUSBDeviceFileIterate(device, testDeviceFileActor, NULL) < 0)
                goto cleanup;
        }
        break;
    }

    ret = 0;

130
 cleanup:
131 132 133 134 135 136
    virObjectUnref(devs);
    virUSBDeviceFree(dev);
    return ret;
}


137 138 139
static int
testCheckNdevs(const char* occasion,
               size_t got,
140 141
               size_t expected)
{
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    if (got != expected) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s: got %zu devices, expected %zu",
                       occasion, got, expected);
        return -1;
    }
    return 0;
}


static int
testUSBList(const void *opaque ATTRIBUTE_UNUSED)
{
    virUSBDeviceListPtr list = NULL;
    virUSBDeviceListPtr devlist = NULL;
    virUSBDevicePtr dev = NULL;
    int ret = -1;
    size_t i, ndevs;

    if (!(list = virUSBDeviceListNew()))
        goto cleanup;

#define EXPECTED_NDEVS_ONE 3
    if (virUSBDeviceFindByVendor(0x1d6b, 0x0002, NULL, true, &devlist) < 0)
        goto cleanup;

    ndevs = virUSBDeviceListCount(devlist);
    if (testCheckNdevs("After first find", ndevs, EXPECTED_NDEVS_ONE) < 0)
        goto cleanup;

    for (i = 0; i < ndevs; i++) {
        dev = virUSBDeviceListGet(devlist, 0);
        dev = virUSBDeviceListSteal(devlist, dev);

        if (virUSBDeviceListAdd(list, dev) < 0)
            goto cleanup;
        dev = NULL;
    }

    virObjectUnref(devlist);
    devlist = NULL;

    ndevs = virUSBDeviceListCount(list);
    if (testCheckNdevs("After first loop", ndevs, EXPECTED_NDEVS_ONE) < 0)
        goto cleanup;

#define EXPECTED_NDEVS_TWO 3
    if (virUSBDeviceFindByVendor(0x18d1, 0x4e22, NULL, true, &devlist) < 0)
        goto cleanup;

    ndevs = virUSBDeviceListCount(devlist);
    if (testCheckNdevs("After second find", ndevs, EXPECTED_NDEVS_TWO) < 0)
        goto cleanup;
    for (i = 0; i < ndevs; i++) {
        dev = virUSBDeviceListGet(devlist, 0);
        dev = virUSBDeviceListSteal(devlist, dev);

        if (virUSBDeviceListAdd(list, dev) < 0)
            goto cleanup;
        dev = NULL;
    }

    if (testCheckNdevs("After second loop",
                       virUSBDeviceListCount(list),
                       EXPECTED_NDEVS_ONE + EXPECTED_NDEVS_TWO) < 0)
        goto cleanup;

    if (virUSBDeviceFind(0x18d1, 0x4e22, 1, 20, NULL, true, &dev) < 0)
        goto cleanup;

    if (!virUSBDeviceListFind(list, dev)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Device '%s' not in list when it should be",
                       virUSBDeviceGetName(dev));
        goto cleanup;
    }

    virUSBDeviceListDel(list, dev);
M
Michal Privoznik 已提交
220
    virUSBDeviceFree(dev);
221 222 223 224 225 226 227 228 229
    dev = NULL;

    if (testCheckNdevs("After deleting one",
                       virUSBDeviceListCount(list),
                       EXPECTED_NDEVS_ONE + EXPECTED_NDEVS_TWO - 1) < 0)
        goto cleanup;

    ret = 0;

230
 cleanup:
231 232 233 234 235 236 237
    virObjectUnref(list);
    virObjectUnref(devlist);
    virUSBDeviceFree(dev);
    return ret;
}


238 239 240 241 242 243
static int
mymain(void)
{
    int rv = 0;

#define DO_TEST_FIND_FULL(name, vend, prod, bus, devno, vroot, mand, how, fail) \
244 245 246 247 248 249
    do { \
        struct findTestInfo data = { name, vend, prod, bus, \
            devno, vroot, mand, how, fail \
        }; \
        if (virTestRun("USBDeviceFind " name, testDeviceFind, &data) < 0) \
            rv = -1; \
250 251
    } while (0)

252 253
#define DO_TEST_FIND(name, vend, prod, bus, devno) \
    DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \
254
                      FIND_BY_ALL, false)
255 256
#define DO_TEST_FIND_FAIL(name, vend, prod, bus, devno) \
    DO_TEST_FIND_FULL(name, vend, prod, bus, devno, NULL, true, \
257 258
                      FIND_BY_ALL, true)

259 260
#define DO_TEST_FIND_BY_BUS(name, bus, devno) \
    DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \
261
                      FIND_BY_BUS, false)
262 263
#define DO_TEST_FIND_BY_BUS_FAIL(name, bus, devno) \
    DO_TEST_FIND_FULL(name, 101, 202, bus, devno, NULL, true, \
264 265
                      FIND_BY_BUS, true)

266 267
#define DO_TEST_FIND_BY_VENDOR(name, vend, prod) \
    DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \
268
                      FIND_BY_VENDOR, false)
269 270
#define DO_TEST_FIND_BY_VENDOR_FAIL(name, vend, prod) \
    DO_TEST_FIND_FULL(name, vend, prod, 123, 456, NULL, true, \
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                      FIND_BY_VENDOR, true)

    DO_TEST_FIND("Nexus", 0x18d1, 0x4e22, 1, 20);
    DO_TEST_FIND_FAIL("Nexus wrong devnum", 0x18d1, 0x4e22, 1, 25);
    DO_TEST_FIND_FAIL("Bogus", 0xf00d, 0xbeef, 1024, 768);

    DO_TEST_FIND_BY_BUS("integrated camera", 1, 5);
    DO_TEST_FIND_BY_BUS_FAIL("wrong bus/devno combination", 2, 20);
    DO_TEST_FIND_BY_BUS_FAIL("missing bus", 5, 20);
    DO_TEST_FIND_BY_BUS_FAIL("missing devnum", 1, 158);

    DO_TEST_FIND_BY_VENDOR("Nexus (multiple results)", 0x18d1, 0x4e22);
    DO_TEST_FIND_BY_VENDOR_FAIL("Bogus vendor and product", 0xf00d, 0xbeef);
    DO_TEST_FIND_BY_VENDOR_FAIL("Valid vendor", 0x1d6b, 0xbeef);

286
    if (virTestRun("USB List test", testUSBList, NULL) < 0)
287 288
        rv = -1;

289 290 291 292 293
    if (rv < 0)
        return EXIT_FAILURE;
    return EXIT_SUCCESS;
}

294
VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virusbmock.so")