]> git.proxmox.com Git - mirror_qemu.git/blame - tests/check-qnull.c
qmp: Support explicit null during visits
[mirror_qemu.git] / tests / check-qnull.c
CommitLineData
7d7a337e
EB
1/*
2 * QNull unit-tests.
3 *
4 * Copyright (C) 2016 Red Hat Inc.
5 *
6 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7 * See the COPYING.LIB file in the top-level directory.
8 */
9#include "qemu/osdep.h"
10#include <glib.h>
11
12#include "qapi/qmp/qobject.h"
13#include "qemu-common.h"
3df016f1 14#include "qapi/qmp-input-visitor.h"
7d7a337e 15#include "qapi/qmp-output-visitor.h"
3df016f1 16#include "qapi/error.h"
7d7a337e
EB
17
18/*
19 * Public Interface test-cases
20 *
21 * (with some violations to access 'private' data)
22 */
23
24static void qnull_ref_test(void)
25{
26 QObject *obj;
27
28 g_assert(qnull_.refcnt == 1);
29 obj = qnull();
30 g_assert(obj);
31 g_assert(obj == &qnull_);
32 g_assert(qnull_.refcnt == 2);
33 g_assert(qobject_type(obj) == QTYPE_QNULL);
34 qobject_decref(obj);
35 g_assert(qnull_.refcnt == 1);
36}
37
38static void qnull_visit_test(void)
39{
40 QObject *obj;
41 QmpOutputVisitor *qov;
3df016f1 42 QmpInputVisitor *qiv;
7d7a337e
EB
43
44 /*
45 * Most tests of interactions between QObject and visitors are in
46 * test-qmp-*-visitor; but these tests live here because they
47 * depend on layering violations to check qnull_ refcnt.
48 */
49
50 g_assert(qnull_.refcnt == 1);
3df016f1
EB
51 obj = qnull();
52 qiv = qmp_input_visitor_new(obj, true);
53 qobject_decref(obj);
54 visit_type_null(qmp_input_get_visitor(qiv), NULL, &error_abort);
55 qmp_input_visitor_cleanup(qiv);
56
7d7a337e 57 qov = qmp_output_visitor_new();
3df016f1 58 visit_type_null(qmp_output_get_visitor(qov), NULL, &error_abort);
7d7a337e
EB
59 obj = qmp_output_get_qobject(qov);
60 g_assert(obj == &qnull_);
61 qobject_decref(obj);
7d7a337e 62 qmp_output_visitor_cleanup(qov);
3df016f1 63
7d7a337e
EB
64 g_assert(qnull_.refcnt == 1);
65}
66
67int main(int argc, char **argv)
68{
69 g_test_init(&argc, &argv, NULL);
70
71 g_test_add_func("/public/qnull_ref", qnull_ref_test);
72 g_test_add_func("/public/qnull_visit", qnull_visit_test);
73
74 return g_test_run();
75}