Re: [PATCH spice-gtk v4 20/29] test-cd-emu: Add base test for cd-emulation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Tue, Aug 27, 2019 at 10:22:37AM +0100, Frediano Ziglio wrote:
> Just allocate and free to test for base leaks and reference
> counting.
> 
> Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx>

Valgrind says ok here as well.

> ---
>  tests/cd-emu.c    | 92 +++++++++++++++++++++++++++++++++++++++++++++++
>  tests/meson.build |  4 +++
>  2 files changed, 96 insertions(+)
>  create mode 100644 tests/cd-emu.c
> 
> diff --git a/tests/cd-emu.c b/tests/cd-emu.c
> new file mode 100644
> index 00000000..7bf1fa3c
> --- /dev/null
> +++ b/tests/cd-emu.c
> @@ -0,0 +1,92 @@
> +/*
> +   Copyright (C) 2019 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/>.
> +*/
> +#include <gio/gio.h>
> +
> +#include "usb-device-cd.h"
> +#include "usb-emulation.h"
> +
> +static SpiceUsbBackendDevice *device = NULL;
> +
> +/* simple usb manager hotplug callback emulation. */
> +static void
> +test_hotplug_callback(void *user_data, SpiceUsbBackendDevice *dev, gboolean added)
> +{
> +    // ignore not emulated devices
> +    const UsbDeviceInformation *info = spice_usb_backend_device_get_info(dev);
> +    if (info->bus != BUS_NUMBER_FOR_EMULATED_USB) {
> +        return;
> +    }
> +
> +    if (added) {
> +        g_assert_null(device);
> +        device = spice_usb_backend_device_ref(dev);
> +    } else {
> +        g_assert_nonnull(device);
> +        g_assert(device == dev);
> +        spice_usb_backend_device_unref(dev);
> +        device = NULL;
> +    }
> +}
> +
> +static void multiple(const void *param)
> +{
> +    guint limit = GPOINTER_TO_UINT(param);
> +    CdEmulationParams params = { "test-cd-emu.iso", 1 };
> +    GError *err = NULL;
> +    SpiceUsbBackend * be = spice_usb_backend_new(&err);
> +    g_assert_nonnull(be);
> +    g_assert_null(err);
> +    spice_usb_backend_register_hotplug(be, NULL, test_hotplug_callback, &err);
> +    g_assert_null(err);
> +    for (int i = 0; i < limit; ++i) {
> +        // allocate a CD emulation device
> +        g_assert_true(create_emulated_cd(be, &params, &err));
> +        g_assert_null(err);
> +        g_assert_nonnull(device);
> +
> +        // emulate automatic CD ejection, this should release the
> +        // object
> +        spice_usb_backend_device_eject(be, device);
> +        g_assert_null(device);
> +    }
> +    spice_usb_backend_deregister_hotplug(be);
> +    spice_usb_backend_delete(be);
> +}
> +
> +static void
> +write_test_iso(void)
> +{
> +    uint8_t sector[2048];
> +    FILE *f = fopen("test-cd-emu.iso", "wb");
> +    g_assert_nonnull(f);
> +    memset(sector, 0, sizeof(sector));
> +    strcpy((char*) sector, "sector 0");
> +    fwrite(sector, sizeof(sector), 1, f);
> +    fclose(f);
> +}
> +
> +int main(int argc, char* argv[])
> +{
> +    write_test_iso();

We create the iso for testing but we don't remove it. There are a
few things that comes to mind in regards to creating this file
and removing it later that could be a problem. Ideally, we should
have some sort of SPICE_CD_ROM_MOCKING in the source code that
helps with unit test but honestly, i'm happy that we have a test
and it works fine enough form now ;)

Another way around this create but not removing is to use the
g_test_add with _set_up() and _tear_down() functions. That's more
verbose and slower than your approach but if I'm not mistaken,
_tear_down() should be called even if test fails.

> +    g_test_init(&argc, &argv, NULL);
> +
> +    g_test_add_data_func("/cd-emu/simple", GUINT_TO_POINTER(1), multiple);
> +    g_test_add_data_func("/cd-emu/multiple", GUINT_TO_POINTER(128), multiple);
> +
> +    return g_test_run();
> +}
> diff --git a/tests/meson.build b/tests/meson.build
> index 6c807762..39eac9c9 100644
> --- a/tests/meson.build
> +++ b/tests/meson.build
> @@ -10,6 +10,10 @@ if spice_gtk_has_phodav
>    tests_sources += 'pipe.c'
>  endif
>  
> +if spice_gtk_has_usbredir
> +  tests_sources += 'cd-emu.c'
> +endif
> +
>  if spice_gtk_has_polkit
>    tests_sources += [
>      'usb-acl-helper.c',
> -- 
> 2.20.1
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel@xxxxxxxxxxxxxxxxxxxxx
> https://lists.freedesktop.org/mailman/listinfo/spice-devel

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel

[Index of Archives]     [Linux Virtualization]     [Linux Virtualization]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]