2016-04-28 23:45:21 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2017-11-14 19:01:23 +01:00
|
|
|
#include "qapi/qmp/qnull.h"
|
2016-09-30 16:45:27 +02:00
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
#include "qapi/qobject-output-visitor.h"
|
2016-04-28 23:45:23 +02:00
|
|
|
#include "qapi/error.h"
|
2016-04-28 23:45:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Public Interface test-cases
|
|
|
|
*
|
|
|
|
* (with some violations to access 'private' data)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void qnull_ref_test(void)
|
|
|
|
{
|
|
|
|
QObject *obj;
|
|
|
|
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(qnull_.base.refcnt == 1);
|
|
|
|
obj = QOBJECT(qnull());
|
2016-04-28 23:45:21 +02:00
|
|
|
g_assert(obj);
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(obj == QOBJECT(&qnull_));
|
|
|
|
g_assert(qnull_.base.refcnt == 2);
|
2016-04-28 23:45:21 +02:00
|
|
|
g_assert(qobject_type(obj) == QTYPE_QNULL);
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(obj);
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(qnull_.base.refcnt == 1);
|
2016-04-28 23:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qnull_visit_test(void)
|
|
|
|
{
|
|
|
|
QObject *obj;
|
qmp-input-visitor: Favor new visit_free() function
Now that we have a polymorphic visit_free(), we no longer need
qmp_input_visitor_cleanup(); which in turn means we no longer
need to return a subtype from qmp_input_visitor_new() nor a
public upcast function.
Generated code changes to qmp-marshal.c look like:
|@@ -52,11 +52,10 @@ void qmp_marshal_add_fd(QDict *args, QOb
| {
| Error *err = NULL;
| AddfdInfo *retval;
|- QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
| Visitor *v;
| q_obj_add_fd_arg arg = {0};
|
|- v = qmp_input_get_visitor(qiv);
|+ v = qmp_input_visitor_new(QOBJECT(args), true);
| visit_start_struct(v, NULL, NULL, 0, &err);
| if (err) {
| goto out;
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1465490926-28625-8-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-06-09 18:48:38 +02:00
|
|
|
Visitor *v;
|
2017-06-26 18:22:59 +02:00
|
|
|
QNull *null;
|
2016-04-28 23:45:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(qnull_.base.refcnt == 1);
|
|
|
|
obj = QOBJECT(qnull());
|
2017-03-03 13:32:39 +01:00
|
|
|
v = qobject_input_visitor_new(obj);
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(obj);
|
2017-06-26 18:22:59 +02:00
|
|
|
visit_type_null(v, NULL, &null, &error_abort);
|
|
|
|
g_assert(obj == QOBJECT(&qnull_));
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(null);
|
qmp-input-visitor: Favor new visit_free() function
Now that we have a polymorphic visit_free(), we no longer need
qmp_input_visitor_cleanup(); which in turn means we no longer
need to return a subtype from qmp_input_visitor_new() nor a
public upcast function.
Generated code changes to qmp-marshal.c look like:
|@@ -52,11 +52,10 @@ void qmp_marshal_add_fd(QDict *args, QOb
| {
| Error *err = NULL;
| AddfdInfo *retval;
|- QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
| Visitor *v;
| q_obj_add_fd_arg arg = {0};
|
|- v = qmp_input_get_visitor(qiv);
|+ v = qmp_input_visitor_new(QOBJECT(args), true);
| visit_start_struct(v, NULL, NULL, 0, &err);
| if (err) {
| goto out;
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1465490926-28625-8-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-06-09 18:48:38 +02:00
|
|
|
visit_free(v);
|
2016-04-28 23:45:23 +02:00
|
|
|
|
2017-06-26 18:22:59 +02:00
|
|
|
null = NULL;
|
2016-09-30 16:45:28 +02:00
|
|
|
v = qobject_output_visitor_new(&obj);
|
2017-06-26 18:22:59 +02:00
|
|
|
visit_type_null(v, NULL, &null, &error_abort);
|
qapi: Add new visit_complete() function
Making each output visitor provide its own output collection
function was the only remaining reason for exposing visitor
sub-types to the rest of the code base. Add a polymorphic
visit_complete() function which is a no-op for input visitors,
and which populates an opaque pointer for output visitors. For
maximum type-safety, also add a parameter to the output visitor
constructors with a type-correct version of the output pointer,
and assert that the two uses match.
This approach was considered superior to either passing the
output parameter only during construction (action at a distance
during visit_free() feels awkward) or only during visit_complete()
(defeating type safety makes it easier to use incorrectly).
Most callers were function-local, and therefore a mechanical
conversion; the testsuite was a bit trickier, but the previous
cleanup patch minimized the churn here.
The visit_complete() function may be called at most once; doing
so lets us use transfer semantics rather than duplication or
ref-count semantics to get the just-built output back to the
caller, even though it means our behavior is not idempotent.
Generated code is simplified as follows for events:
|@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP
| QDict *qmp;
| Error *err = NULL;
| QMPEventFuncEmit emit;
|- QmpOutputVisitor *qov;
|+ QObject *obj;
| Visitor *v;
| q_obj_ACPI_DEVICE_OST_arg param = {
| info
|@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP
|
| qmp = qmp_event_build_dict("ACPI_DEVICE_OST");
|
|- qov = qmp_output_visitor_new();
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(&obj);
|
| visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err);
| if (err) {
|@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP
| goto out;
| }
|
|- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
|+ visit_complete(v, &obj);
|+ qdict_put_obj(qmp, "data", obj);
| emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);
and for commands:
| {
| Error *err = NULL;
|- QmpOutputVisitor *qov = qmp_output_visitor_new();
| Visitor *v;
|
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(ret_out);
| visit_type_AddfdInfo(v, "unused", &ret_in, &err);
|- if (err) {
|- goto out;
|+ if (!err) {
|+ visit_complete(v, ret_out);
| }
|- *ret_out = qmp_output_get_qobject(qov);
|-
|-out:
| error_propagate(errp, err);
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1465490926-28625-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-06-09 18:48:43 +02:00
|
|
|
visit_complete(v, &obj);
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(obj == QOBJECT(&qnull_));
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(null);
|
|
|
|
qobject_unref(obj);
|
qapi: Add new visit_complete() function
Making each output visitor provide its own output collection
function was the only remaining reason for exposing visitor
sub-types to the rest of the code base. Add a polymorphic
visit_complete() function which is a no-op for input visitors,
and which populates an opaque pointer for output visitors. For
maximum type-safety, also add a parameter to the output visitor
constructors with a type-correct version of the output pointer,
and assert that the two uses match.
This approach was considered superior to either passing the
output parameter only during construction (action at a distance
during visit_free() feels awkward) or only during visit_complete()
(defeating type safety makes it easier to use incorrectly).
Most callers were function-local, and therefore a mechanical
conversion; the testsuite was a bit trickier, but the previous
cleanup patch minimized the churn here.
The visit_complete() function may be called at most once; doing
so lets us use transfer semantics rather than duplication or
ref-count semantics to get the just-built output back to the
caller, even though it means our behavior is not idempotent.
Generated code is simplified as follows for events:
|@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP
| QDict *qmp;
| Error *err = NULL;
| QMPEventFuncEmit emit;
|- QmpOutputVisitor *qov;
|+ QObject *obj;
| Visitor *v;
| q_obj_ACPI_DEVICE_OST_arg param = {
| info
|@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP
|
| qmp = qmp_event_build_dict("ACPI_DEVICE_OST");
|
|- qov = qmp_output_visitor_new();
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(&obj);
|
| visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err);
| if (err) {
|@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP
| goto out;
| }
|
|- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
|+ visit_complete(v, &obj);
|+ qdict_put_obj(qmp, "data", obj);
| emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err);
and for commands:
| {
| Error *err = NULL;
|- QmpOutputVisitor *qov = qmp_output_visitor_new();
| Visitor *v;
|
|- v = qmp_output_get_visitor(qov);
|+ v = qmp_output_visitor_new(ret_out);
| visit_type_AddfdInfo(v, "unused", &ret_in, &err);
|- if (err) {
|- goto out;
|+ if (!err) {
|+ visit_complete(v, ret_out);
| }
|- *ret_out = qmp_output_get_qobject(qov);
|-
|-out:
| error_propagate(errp, err);
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1465490926-28625-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-06-09 18:48:43 +02:00
|
|
|
visit_free(v);
|
2016-04-28 23:45:23 +02:00
|
|
|
|
2017-06-26 13:52:24 +02:00
|
|
|
g_assert(qnull_.base.refcnt == 1);
|
2016-04-28 23:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|