qemu/tests/check-qnull.c
Eric Blake 3df016f185 qmp: Support explicit null during visits
Implement the new type_null() callback for the qmp input and
output visitors. While we don't yet have a use for this in QAPI
input (the generator will need some tweaks first), some
potential usages have already been discussed on the list.
Meanwhile, the output visitor could already output explicit null
via type_any, but this gives us finer control.

At any rate, it's easy to test that we can round-trip an explicit
null through manual use of visit_type_null() wrapped by a virtual
visit_start_struct() walk, even if we can't do the visit in a
QAPI type.  Repurpose the test_visitor_out_empty test,
particularly since a future patch will tighten semantics to
forbid use of qmp_output_get_qobject() without at least one
intervening visit_type_*.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1461879932-9020-16-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-05-12 09:47:54 +02:00

76 lines
1.8 KiB
C

/*
* QNull unit-tests.
*
* Copyright (C) 2016 Red Hat Inc.
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*/
#include "qemu/osdep.h"
#include <glib.h>
#include "qapi/qmp/qobject.h"
#include "qemu-common.h"
#include "qapi/qmp-input-visitor.h"
#include "qapi/qmp-output-visitor.h"
#include "qapi/error.h"
/*
* Public Interface test-cases
*
* (with some violations to access 'private' data)
*/
static void qnull_ref_test(void)
{
QObject *obj;
g_assert(qnull_.refcnt == 1);
obj = qnull();
g_assert(obj);
g_assert(obj == &qnull_);
g_assert(qnull_.refcnt == 2);
g_assert(qobject_type(obj) == QTYPE_QNULL);
qobject_decref(obj);
g_assert(qnull_.refcnt == 1);
}
static void qnull_visit_test(void)
{
QObject *obj;
QmpOutputVisitor *qov;
QmpInputVisitor *qiv;
/*
* Most tests of interactions between QObject and visitors are in
* test-qmp-*-visitor; but these tests live here because they
* depend on layering violations to check qnull_ refcnt.
*/
g_assert(qnull_.refcnt == 1);
obj = qnull();
qiv = qmp_input_visitor_new(obj, true);
qobject_decref(obj);
visit_type_null(qmp_input_get_visitor(qiv), NULL, &error_abort);
qmp_input_visitor_cleanup(qiv);
qov = qmp_output_visitor_new();
visit_type_null(qmp_output_get_visitor(qov), NULL, &error_abort);
obj = qmp_output_get_qobject(qov);
g_assert(obj == &qnull_);
qobject_decref(obj);
qmp_output_visitor_cleanup(qov);
g_assert(qnull_.refcnt == 1);
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/public/qnull_ref", qnull_ref_test);
g_test_add_func("/public/qnull_visit", qnull_visit_test);
return g_test_run();
}