[libvirt] [PATCH 2/2] Add a test for virUSBDeviceList functions

Michal Privoznik mprivozn at redhat.com
Thu Feb 27 13:27:43 UTC 2014


On 26.02.2014 17:54, Ján Tomko wrote:
> Most of them are already tested in a limited way
> by testing virUSBDeviceFind.
> ---
>   tests/virusbtest.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 75 insertions(+)
>
> diff --git a/tests/virusbtest.c b/tests/virusbtest.c
> index f9104bf..74b9a5e 100644
> --- a/tests/virusbtest.c
> +++ b/tests/virusbtest.c
> @@ -204,6 +204,78 @@ cleanup:
>
>
>   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;
> +
> +    if (virUSBDeviceFindByVendor(0x1d6b, 0x0002, NULL, true, &devlist) < 0)
> +        goto cleanup;
> +
> +    ndevs = virUSBDeviceListCount(devlist);
> +    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;

I'd add here:
if (ndevs != virUSBDeviceListCount(list)) {error(); goto cleanup;}

> +
> +    if (virUSBDeviceFindByVendor(0x18d1, 0x4e22, NULL, true, &devlist) < 0)
> +        goto cleanup;
> +
> +    ndevs = virUSBDeviceListCount(devlist);
> +    for (i = 0; i < ndevs; i++) {
> +        dev = virUSBDeviceListGet(devlist, 0);
> +        dev = virUSBDeviceListSteal(devlist, dev);
> +
> +        if (virUSBDeviceListAdd(list, dev) < 0)
> +            goto cleanup;
> +        dev = NULL;
> +    }
> +

Same here.

> +    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);
> +    dev = NULL;
> +
> +    if (virUSBDeviceListCount(list) != 5) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       "Wrong device count %zu expected %d",
> +                       virUSBDeviceListCount(list),
> +                       5);
> +        goto cleanup;
> +    }
> +
> +    ret = 0;
> +
> +cleanup:
> +    virObjectUnref(list);
> +    virObjectUnref(devlist);
> +    virUSBDeviceFree(dev);
> +    return ret;
> +}
> +
> +
> +static int
>   mymain(void)
>   {
>       int rv = 0;
> @@ -251,6 +323,9 @@ mymain(void)
>       DO_TEST_FIND_BY_VENDOR_FAIL("Bogus vendor and product", 0xf00d, 0xbeef);
>       DO_TEST_FIND_BY_VENDOR_FAIL("Valid vendor", 0x1d6b, 0xbeef);
>
> +    if (virtTestRun("USB List test", testUSBList, NULL) < 0)
> +        rv = -1;
> +
>       if (rv < 0)
>           return EXIT_FAILURE;
>       return EXIT_SUCCESS;
>

ACK

Michal




More information about the libvir-list mailing list