On Tue, 14 Feb 2017 22:15:49 -0800
b...@skyportsystems.com wrote:

> From: Ben Warren <b...@skyportsystems.com>
> 
> The following tests are implemented:
> * test that a GUID passed in by command line is propagated to the guest.
> * test that changing the GUID at runtime via the monitor is reflected in
>   the guest.
> * test that the "auto" argument to the GUID generates a different, and
>   correct GUID as seen by the guest.
> 
>   This patch is loosely based on a previous patch from:
>   Gal Hammer <gham...@redhat.com>  and Igor Mammedov <imamm...@redhat.com>
> 
> Signed-off-by: Ben Warren <b...@skyportsystems.com>
> ---
>  tests/Makefile.include |   2 +
>  tests/vmgenid-test.c   | 195 
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 197 insertions(+)
>  create mode 100644 tests/vmgenid-test.c
> 
> diff --git a/tests/Makefile.include b/tests/Makefile.include
> index 634394a..ca4b3f7 100644
> --- a/tests/Makefile.include
> +++ b/tests/Makefile.include
> @@ -241,6 +241,7 @@ check-qtest-i386-y += tests/usb-hcd-xhci-test$(EXESUF)
>  gcov-files-i386-y += hw/usb/hcd-xhci.c
>  check-qtest-i386-y += tests/pc-cpu-test$(EXESUF)
>  check-qtest-i386-y += tests/q35-test$(EXESUF)
> +check-qtest-i386-y += tests/vmgenid-test$(EXESUF)
>  gcov-files-i386-y += hw/pci-host/q35.c
>  check-qtest-i386-$(CONFIG_VHOST_NET_TEST_i386) += 
> tests/vhost-user-test$(EXESUF)
>  ifeq ($(CONFIG_VHOST_NET_TEST_i386),)
> @@ -726,6 +727,7 @@ tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o 
> contrib/ivshmem-server/ivshmem
>  tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o 
> contrib/libvhost-user/libvhost-user.o $(test-util-obj-y)
>  tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y)
>  tests/test-arm-mptimer$(EXESUF): tests/test-arm-mptimer.o
> +tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o
>  
>  tests/migration/stress$(EXESUF): tests/migration/stress.o
>       $(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< 
> ,"LINK","$(TARGET_DIR)$@")
> diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c
> new file mode 100644
> index 0000000..721ba05
> --- /dev/null
> +++ b/tests/vmgenid-test.c
> @@ -0,0 +1,195 @@
> +/*
> + * QTest testcase for VM Generation ID
> + *
> + * Copyright (c) 2016 Red Hat, Inc.
> + * Copyright (c) 2017 Skyport Systems
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include <glib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include "qemu/osdep.h"
> +#include "qemu/bitmap.h"
> +#include "qemu/uuid.h"
> +#include "hw/acpi/acpi-defs.h"
> +#include "acpi-utils.h"
> +#include "libqtest.h"
> +
> +#define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
> +#define VMGENID_GUID_OFFSET      40   /* allow space for
> +                                       * OVMF SDT Header Probe Supressor
> +                                       */
> +
> +static uint32_t vgia;
> +
> +typedef struct {
> +    AcpiTableHeader header;
> +    gchar name_op;
> +    gchar vgia[4];
> +    gchar val_op;
> +    uint32_t vgia_val;
> +} QEMU_PACKED VgidTable;
> +
> +static uint32_t find_vgia(void)
> +{
[...]

===
> +    /* First, find the RSDP */
> +    for (off = 0xf0000; off < 0x100000; off += 0x10) {
> +        uint8_t sig[] = "RSD PTR ";
> +
> +        for (i = 0; i < sizeof sig - 1; ++i) {
> +            sig[i] = readb(off + i);
> +        }
> +
> +        if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
> +            break;
> +        }
> +    }
> +    g_assert_cmphex(off, <, 0x100000);
> +
> +    /* Parse the RSDP header so we can find the RSDT */
> +    ACPI_READ_FIELD(rsdp_table.signature, off);
> +    ACPI_ASSERT_CMP64(rsdp_table.signature, "RSD PTR ");
> +
> +    ACPI_READ_FIELD(rsdp_table.checksum, off);
> +    ACPI_READ_ARRAY(rsdp_table.oem_id, off);
> +    ACPI_READ_FIELD(rsdp_table.revision, off);
> +    ACPI_READ_FIELD(rsdp_table.rsdt_physical_address, off);
> +
> +    rsdt = rsdp_table.rsdt_physical_address;
> +    /* read the header */
> +    ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt);
> +    ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");
> +
> +    /* compute the table entries in rsdt */
> +    tables_nr = (rsdt_table.length - sizeof(AcpiRsdtDescriptorRev1)) /
> +                sizeof(uint32_t);
> +    g_assert_cmpint(tables_nr, >, 0);
> +
> +    /* get the addresses of the tables pointed by rsdt */
> +    tables = g_new0(uint32_t, tables_nr);
> +    ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt);
===
above is duplicated code from bios-tables-test.c
please extract it into separate functions and use them in both tests.

> +    for (i = 0; i < tables_nr; i++) {
> +        ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]);
> +        if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) {
> +            /* the first entry in the table should be VGIA
> +             * That's all we need
> +             */
> +            ACPI_READ_FIELD(vgid_table.name_op, tables[i]);
> +            g_assert(vgid_table.name_op == 0x08);  /* name */
> +            ACPI_READ_ARRAY(vgid_table.vgia, tables[i]);
> +            g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
> +            ACPI_READ_FIELD(vgid_table.val_op, tables[i]);
> +            g_assert(vgid_table.val_op == 0x0C);  /* dword */
> +            ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]);
> +            /* The GUID is written at a fixed offset into the fw_cfg file
> +             * in order to implement the "OVMF SDT Header probe suppressor"
> +             * see docs/specs/vmgenid.txt for more details
> +             */
> +            return vgid_table.vgia_val + VMGENID_GUID_OFFSET;
> +        }
> +    }
> +    return 0;
> +}
> +
> +static void vmgenid_read_guid(QemuUUID *guid)
> +{
> +    int i;
> +
> +    if (vgia == 0) {
> +        vgia = find_vgia();
> +    }
> +    g_assert(vgia);
> +
> +    /* Read the GUID directly from guest memory */
> +    for (i = 0; i < 16; i++) {
> +        guid->data[i] = readb(vgia + i);
> +    }
> +    /* The GUID is in little-endian format in the guest, while QEMU
> +     * uses big-endian.  Swap after reading.
> +     */
> +    qemu_uuid_bswap(guid);
> +}
> +
> +static void vmgenid_test(void)
> +{
> +    QemuUUID expected, measured;
> +    g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
> +    vmgenid_read_guid(&measured);
> +    g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 
> 0);
> +}
> +
> +static void vmgenid_set_guid_test(void)
> +{
> +    QDict *response;
> +    gchar *cmd;
> +    QemuUUID expected, measured;
> +    g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
> +    /* Change the GUID slightly */
> +    expected.data[0] += 1;
> +
> +    cmd = g_strdup_printf("{ 'execute': 'qom-set', 'arguments': { "
> +                   "'path': '/machine/peripheral/testvgid', "
> +                   "'property': 'guid', 'value': '%s' } }",
> +                   qemu_uuid_unparse_strdup(&expected));
> +    response = qmp(cmd);
> +    g_assert(qdict_haskey(response, "return"));
> +    QDECREF(response);
> +
> +    vmgenid_read_guid(&measured);
> +    g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 
> 0);
> +}
> +
> +static void vmgenid_set_guid_auto_test(void)
> +{
> +    QDict *response;
> +    QemuUUID expected, measured;
> +
> +    /* Read the initial value */
> +    vmgenid_read_guid(&expected);
> +
> +    /* Setting to 'auto' generates a random GUID */
> +    response = qmp("{ 'execute': 'qom-set', 'arguments': { "
> +                   "'path': '/machine/peripheral/testvgid', "
> +                   "'property': 'guid', 'value': 'auto' } }");
> +
> +    g_assert(qdict_haskey(response, "return"));
> +    QDECREF(response);
> +
> +    vmgenid_read_guid(&measured);
> +    g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) != 
> 0);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    int ret;
> +    gchar *cmd;
> +
> +    g_test_init(&argc, &argv, NULL);
> +
> +    cmd = g_strdup_printf("-machine accel=tcg -device vmgenid,id=testvgid,"
> +                          "guid=%s", VGID_GUID);
> +    qtest_start(cmd);
> +    qtest_add_func("/vmgenid/vmgenid", vmgenid_test);
> +    qtest_add_func("/vmgenid/vmgenid/set-guid", vmgenid_set_guid_test);
this is probably no necessary as you've dropped dynamic set in v6,
so if this test passes it's a bug in vmgenid and you should forbid
setting property after device is realized.
See how qdev_prop_set_after_realize() is used.

> +    qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
> +                   vmgenid_set_guid_auto_test);
this should be replaced with this first test but with 'auto' value

maybe add query-vm-generation-id test

> +    ret = g_test_run();
> +
> +    qtest_end();
> +
> +    return ret;
> +}


Reply via email to