From b3db211f3c80bb996a704d665fe275619f728bd4 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Fri, 30 Sep 2016 15:45:27 +0100 Subject: qapi: rename *qmp-*-visitor* to *qobject-*-visitor* The QMP visitors have no direct dependency on QMP. It is valid to use them anywhere that one has a QObject. Rename them to better reflect their functionality as a generic QObject to QAPI converter. This is the first of three parts: rename the files. The next two parts will rename C identifiers. The split is necessary to make git rename detection work. Reviewed-by: Kevin Wolf Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange Reviewed-by: Markus Armbruster [Split into file and identifier rename, two comments touched up] Signed-off-by: Markus Armbruster --- tests/.gitignore | 6 +- tests/Makefile.include | 20 +- tests/check-qnull.c | 4 +- tests/test-qmp-commands.c | 2 +- tests/test-qmp-input-strict.c | 381 --------------- tests/test-qmp-input-visitor.c | 911 ------------------------------------ tests/test-qmp-output-visitor.c | 876 ---------------------------------- tests/test-qobject-input-strict.c | 381 +++++++++++++++ tests/test-qobject-input-visitor.c | 911 ++++++++++++++++++++++++++++++++++++ tests/test-qobject-output-visitor.c | 876 ++++++++++++++++++++++++++++++++++ tests/test-string-input-visitor.c | 2 +- tests/test-string-output-visitor.c | 2 +- tests/test-visitor-serialization.c | 4 +- 13 files changed, 2188 insertions(+), 2188 deletions(-) delete mode 100644 tests/test-qmp-input-strict.c delete mode 100644 tests/test-qmp-input-visitor.c delete mode 100644 tests/test-qmp-output-visitor.c create mode 100644 tests/test-qobject-input-strict.c create mode 100644 tests/test-qobject-input-visitor.c create mode 100644 tests/test-qobject-output-visitor.c (limited to 'tests') diff --git a/tests/.gitignore b/tests/.gitignore index 9f3d2ee..4aec8bc 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -59,11 +59,11 @@ test-qht-par test-qmp-commands test-qmp-commands.h test-qmp-event -test-qmp-input-strict -test-qmp-input-visitor +test-qobject-input-strict +test-qobject-input-visitor test-qmp-introspect.[ch] test-qmp-marshal.c -test-qmp-output-visitor +test-qobject-output-visitor test-rcu-list test-replication test-rfifolock diff --git a/tests/Makefile.include b/tests/Makefile.include index 73931d1..cd058ef 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -22,13 +22,13 @@ check-unit-y += tests/check-qnull$(EXESUF) gcov-files-check-qnull-y = qobject/qnull.c check-unit-y += tests/check-qjson$(EXESUF) gcov-files-check-qjson-y = qobject/qjson.c -check-unit-y += tests/test-qmp-output-visitor$(EXESUF) -gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c +check-unit-y += tests/test-qobject-output-visitor$(EXESUF) +gcov-files-test-qobject-output-visitor-y = qapi/qobject-output-visitor.c check-unit-y += tests/test-clone-visitor$(EXESUF) gcov-files-test-clone-visitor-y = qapi/qapi-clone-visitor.c -check-unit-y += tests/test-qmp-input-visitor$(EXESUF) -gcov-files-test-qmp-input-visitor-y = qapi/qmp-input-visitor.c -check-unit-y += tests/test-qmp-input-strict$(EXESUF) +check-unit-y += tests/test-qobject-input-visitor$(EXESUF) +gcov-files-test-qobject-input-visitor-y = qapi/qobject-input-visitor.c +check-unit-y += tests/test-qobject-input-strict$(EXESUF) check-unit-y += tests/test-qmp-commands$(EXESUF) gcov-files-test-qmp-commands-y = qapi/qmp-dispatch.c check-unit-y += tests/test-string-input-visitor$(EXESUF) @@ -452,9 +452,9 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \ tests/check-qlist.o tests/check-qfloat.o tests/check-qnull.o \ tests/check-qjson.o \ tests/test-coroutine.o tests/test-string-output-visitor.o \ - tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \ + tests/test-string-input-visitor.o tests/test-qobject-output-visitor.o \ tests/test-clone-visitor.o \ - tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \ + tests/test-qobject-input-visitor.o tests/test-qobject-input-strict.o \ tests/test-qmp-commands.o tests/test-visitor-serialization.o \ tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \ tests/test-opts-visitor.o tests/test-qmp-event.o \ @@ -558,10 +558,10 @@ $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-int tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) -tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-qapi-obj-y) +tests/test-qobject-output-visitor$(EXESUF): tests/test-qobject-output-visitor.o $(test-qapi-obj-y) tests/test-clone-visitor$(EXESUF): tests/test-clone-visitor.o $(test-qapi-obj-y) -tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y) -tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y) +tests/test-qobject-input-visitor$(EXESUF): tests/test-qobject-input-visitor.o $(test-qapi-obj-y) +tests/test-qobject-input-strict$(EXESUF): tests/test-qobject-input-strict.o $(test-qapi-obj-y) tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y) tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) diff --git a/tests/check-qnull.c b/tests/check-qnull.c index dc906b1..7a1cd94 100644 --- a/tests/check-qnull.c +++ b/tests/check-qnull.c @@ -10,8 +10,8 @@ #include "qapi/qmp/qobject.h" #include "qemu-common.h" -#include "qapi/qmp-input-visitor.h" -#include "qapi/qmp-output-visitor.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qobject-output-visitor.h" #include "qapi/error.h" /* diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c index 81cbe54..e582758 100644 --- a/tests/test-qmp-commands.c +++ b/tests/test-qmp-commands.c @@ -4,7 +4,7 @@ #include "test-qmp-commands.h" #include "qapi/qmp/dispatch.h" #include "qemu/module.h" -#include "qapi/qmp-input-visitor.h" +#include "qapi/qobject-input-visitor.h" #include "tests/test-qapi-types.h" #include "tests/test-qapi-visit.h" diff --git a/tests/test-qmp-input-strict.c b/tests/test-qmp-input-strict.c deleted file mode 100644 index d87f8b8..0000000 --- a/tests/test-qmp-input-strict.c +++ /dev/null @@ -1,381 +0,0 @@ -/* - * QMP Input Visitor unit-tests (strict mode). - * - * Copyright (C) 2011-2012, 2015 Red Hat Inc. - * - * Authors: - * Luiz Capitulino - * Paolo Bonzini - * - * 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 "qemu/osdep.h" - -#include "qemu-common.h" -#include "qapi/error.h" -#include "qapi/qmp-input-visitor.h" -#include "test-qapi-types.h" -#include "test-qapi-visit.h" -#include "qapi/qmp/types.h" -#include "qapi/qmp/qjson.h" -#include "test-qmp-introspect.h" -#include "qmp-introspect.h" -#include "qapi-visit.h" - -typedef struct TestInputVisitorData { - QObject *obj; - Visitor *qiv; -} TestInputVisitorData; - -static void validate_teardown(TestInputVisitorData *data, - const void *unused) -{ - qobject_decref(data->obj); - data->obj = NULL; - - if (data->qiv) { - visit_free(data->qiv); - data->qiv = NULL; - } -} - -/* The various test_init functions are provided instead of a test setup - function so that the JSON string used by the tests are kept in the test - functions (and not in main()). */ -static Visitor *validate_test_init_internal(TestInputVisitorData *data, - const char *json_string, - va_list *ap) -{ - validate_teardown(data, NULL); - - data->obj = qobject_from_jsonv(json_string, ap); - g_assert(data->obj); - - data->qiv = qmp_input_visitor_new(data->obj, true); - g_assert(data->qiv); - return data->qiv; -} - -static GCC_FMT_ATTR(2, 3) -Visitor *validate_test_init(TestInputVisitorData *data, - const char *json_string, ...) -{ - Visitor *v; - va_list ap; - - va_start(ap, json_string); - v = validate_test_init_internal(data, json_string, &ap); - va_end(ap); - return v; -} - -/* similar to validate_test_init(), but does not expect a string - * literal/format json_string argument and so can be used for - * programatically generated strings (and we can't pass in programatically - * generated strings via %s format parameters since qobject_from_jsonv() - * will wrap those in double-quotes and treat the entire object as a - * string) - */ -static Visitor *validate_test_init_raw(TestInputVisitorData *data, - const char *json_string) -{ - return validate_test_init_internal(data, json_string, NULL); -} - - -static void test_validate_struct(TestInputVisitorData *data, - const void *unused) -{ - TestStruct *p = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); - - visit_type_TestStruct(v, NULL, &p, &error_abort); - g_free(p->string); - g_free(p); -} - -static void test_validate_struct_nested(TestInputVisitorData *data, - const void *unused) -{ - UserDefTwo *udp = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'string0': 'string0', " - "'dict1': { 'string1': 'string1', " - "'dict2': { 'userdef': { 'integer': 42, " - "'string': 'string' }, 'string': 'string2'}}}"); - - visit_type_UserDefTwo(v, NULL, &udp, &error_abort); - qapi_free_UserDefTwo(udp); -} - -static void test_validate_list(TestInputVisitorData *data, - const void *unused) -{ - UserDefOneList *head = NULL; - Visitor *v; - - v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); - - visit_type_UserDefOneList(v, NULL, &head, &error_abort); - qapi_free_UserDefOneList(head); -} - -static void test_validate_union_native_list(TestInputVisitorData *data, - const void *unused) -{ - UserDefNativeListUnion *tmp = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }"); - - visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort); - qapi_free_UserDefNativeListUnion(tmp); -} - -static void test_validate_union_flat(TestInputVisitorData *data, - const void *unused) -{ - UserDefFlatUnion *tmp = NULL; - Visitor *v; - - v = validate_test_init(data, - "{ 'enum1': 'value1', " - "'integer': 41, " - "'string': 'str', " - "'boolean': true }"); - - visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); - qapi_free_UserDefFlatUnion(tmp); -} - -static void test_validate_alternate(TestInputVisitorData *data, - const void *unused) -{ - UserDefAlternate *tmp = NULL; - Visitor *v; - - v = validate_test_init(data, "42"); - - visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); - qapi_free_UserDefAlternate(tmp); -} - -static void test_validate_fail_struct(TestInputVisitorData *data, - const void *unused) -{ - TestStruct *p = NULL; - Error *err = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); - - visit_type_TestStruct(v, NULL, &p, &err); - error_free_or_abort(&err); - g_assert(!p); -} - -static void test_validate_fail_struct_nested(TestInputVisitorData *data, - const void *unused) -{ - UserDefTwo *udp = NULL; - Error *err = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); - - visit_type_UserDefTwo(v, NULL, &udp, &err); - error_free_or_abort(&err); - g_assert(!udp); -} - -static void test_validate_fail_struct_missing(TestInputVisitorData *data, - const void *unused) -{ - Error *err = NULL; - Visitor *v; - QObject *any; - GenericAlternate *alt; - bool present; - int en; - int64_t i64; - uint32_t u32; - int8_t i8; - char *str; - double dbl; - - v = validate_test_init(data, "{}"); - visit_start_struct(v, NULL, NULL, 0, &error_abort); - visit_start_struct(v, "struct", NULL, 0, &err); - error_free_or_abort(&err); - visit_start_list(v, "list", NULL, 0, &err); - error_free_or_abort(&err); - visit_start_alternate(v, "alternate", &alt, sizeof(*alt), false, &err); - error_free_or_abort(&err); - visit_optional(v, "optional", &present); - g_assert(!present); - visit_type_enum(v, "enum", &en, EnumOne_lookup, &err); - error_free_or_abort(&err); - visit_type_int(v, "i64", &i64, &err); - error_free_or_abort(&err); - visit_type_uint32(v, "u32", &u32, &err); - error_free_or_abort(&err); - visit_type_int8(v, "i8", &i8, &err); - error_free_or_abort(&err); - visit_type_str(v, "i8", &str, &err); - error_free_or_abort(&err); - visit_type_number(v, "dbl", &dbl, &err); - error_free_or_abort(&err); - visit_type_any(v, "any", &any, &err); - error_free_or_abort(&err); - visit_type_null(v, "null", &err); - error_free_or_abort(&err); - visit_end_struct(v, NULL); -} - -static void test_validate_fail_list(TestInputVisitorData *data, - const void *unused) -{ - UserDefOneList *head = NULL; - Error *err = NULL; - Visitor *v; - - v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); - - visit_type_UserDefOneList(v, NULL, &head, &err); - error_free_or_abort(&err); - g_assert(!head); -} - -static void test_validate_fail_union_native_list(TestInputVisitorData *data, - const void *unused) -{ - UserDefNativeListUnion *tmp = NULL; - Error *err = NULL; - Visitor *v; - - v = validate_test_init(data, - "{ 'type': 'integer', 'data' : [ 'string' ] }"); - - visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err); - error_free_or_abort(&err); - g_assert(!tmp); -} - -static void test_validate_fail_union_flat(TestInputVisitorData *data, - const void *unused) -{ - UserDefFlatUnion *tmp = NULL; - Error *err = NULL; - Visitor *v; - - v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); - - visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); - error_free_or_abort(&err); - g_assert(!tmp); -} - -static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, - const void *unused) -{ - UserDefFlatUnion2 *tmp = NULL; - Error *err = NULL; - Visitor *v; - - /* test situation where discriminator field ('enum1' here) is missing */ - v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); - - visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); - error_free_or_abort(&err); - g_assert(!tmp); -} - -static void test_validate_fail_alternate(TestInputVisitorData *data, - const void *unused) -{ - UserDefAlternate *tmp; - Visitor *v; - Error *err = NULL; - - v = validate_test_init(data, "3.14"); - - visit_type_UserDefAlternate(v, NULL, &tmp, &err); - error_free_or_abort(&err); - g_assert(!tmp); -} - -static void do_test_validate_qmp_introspect(TestInputVisitorData *data, - const char *schema_json) -{ - SchemaInfoList *schema = NULL; - Visitor *v; - - v = validate_test_init_raw(data, schema_json); - - visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); - g_assert(schema); - - qapi_free_SchemaInfoList(schema); -} - -static void test_validate_qmp_introspect(TestInputVisitorData *data, - const void *unused) -{ - do_test_validate_qmp_introspect(data, test_qmp_schema_json); - do_test_validate_qmp_introspect(data, qmp_schema_json); -} - -static void validate_test_add(const char *testpath, - TestInputVisitorData *data, - void (*test_func)(TestInputVisitorData *data, const void *user_data)) -{ - g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, - validate_teardown); -} - -int main(int argc, char **argv) -{ - TestInputVisitorData testdata; - - g_test_init(&argc, &argv, NULL); - - validate_test_add("/visitor/input-strict/pass/struct", - &testdata, test_validate_struct); - validate_test_add("/visitor/input-strict/pass/struct-nested", - &testdata, test_validate_struct_nested); - validate_test_add("/visitor/input-strict/pass/list", - &testdata, test_validate_list); - validate_test_add("/visitor/input-strict/pass/union-flat", - &testdata, test_validate_union_flat); - validate_test_add("/visitor/input-strict/pass/alternate", - &testdata, test_validate_alternate); - validate_test_add("/visitor/input-strict/pass/union-native-list", - &testdata, test_validate_union_native_list); - validate_test_add("/visitor/input-strict/fail/struct", - &testdata, test_validate_fail_struct); - validate_test_add("/visitor/input-strict/fail/struct-nested", - &testdata, test_validate_fail_struct_nested); - validate_test_add("/visitor/input-strict/fail/struct-missing", - &testdata, test_validate_fail_struct_missing); - validate_test_add("/visitor/input-strict/fail/list", - &testdata, test_validate_fail_list); - validate_test_add("/visitor/input-strict/fail/union-flat", - &testdata, test_validate_fail_union_flat); - validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator", - &testdata, test_validate_fail_union_flat_no_discrim); - validate_test_add("/visitor/input-strict/fail/alternate", - &testdata, test_validate_fail_alternate); - validate_test_add("/visitor/input-strict/fail/union-native-list", - &testdata, test_validate_fail_union_native_list); - validate_test_add("/visitor/input-strict/pass/qmp-introspect", - &testdata, test_validate_qmp_introspect); - - g_test_run(); - - return 0; -} diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c deleted file mode 100644 index f583dce..0000000 --- a/tests/test-qmp-input-visitor.c +++ /dev/null @@ -1,911 +0,0 @@ -/* - * QMP Input Visitor unit-tests. - * - * Copyright (C) 2011-2016 Red Hat Inc. - * - * Authors: - * Luiz Capitulino - * - * 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 "qemu/osdep.h" - -#include "qemu-common.h" -#include "qapi/error.h" -#include "qapi/qmp-input-visitor.h" -#include "test-qapi-types.h" -#include "test-qapi-visit.h" -#include "qapi/qmp/types.h" -#include "qapi/qmp/qjson.h" - -typedef struct TestInputVisitorData { - QObject *obj; - Visitor *qiv; -} TestInputVisitorData; - -static void visitor_input_teardown(TestInputVisitorData *data, - const void *unused) -{ - qobject_decref(data->obj); - data->obj = NULL; - - if (data->qiv) { - visit_free(data->qiv); - data->qiv = NULL; - } -} - -/* The various test_init functions are provided instead of a test setup - function so that the JSON string used by the tests are kept in the test - functions (and not in main()). */ -static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data, - const char *json_string, - va_list *ap) -{ - visitor_input_teardown(data, NULL); - - data->obj = qobject_from_jsonv(json_string, ap); - g_assert(data->obj); - - data->qiv = qmp_input_visitor_new(data->obj, false); - g_assert(data->qiv); - return data->qiv; -} - -static GCC_FMT_ATTR(2, 3) -Visitor *visitor_input_test_init(TestInputVisitorData *data, - const char *json_string, ...) -{ - Visitor *v; - va_list ap; - - va_start(ap, json_string); - v = visitor_input_test_init_internal(data, json_string, &ap); - va_end(ap); - return v; -} - -/* similar to visitor_input_test_init(), but does not expect a string - * literal/format json_string argument and so can be used for - * programatically generated strings (and we can't pass in programatically - * generated strings via %s format parameters since qobject_from_jsonv() - * will wrap those in double-quotes and treat the entire object as a - * string) - */ -static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data, - const char *json_string) -{ - return visitor_input_test_init_internal(data, json_string, NULL); -} - -static void test_visitor_in_int(TestInputVisitorData *data, - const void *unused) -{ - int64_t res = 0, value = -42; - Visitor *v; - - v = visitor_input_test_init(data, "%" PRId64, value); - - visit_type_int(v, NULL, &res, &error_abort); - g_assert_cmpint(res, ==, value); -} - -static void test_visitor_in_int_overflow(TestInputVisitorData *data, - const void *unused) -{ - int64_t res = 0; - Error *err = NULL; - Visitor *v; - - /* this will overflow a Qint/int64, so should be deserialized into - * a QFloat/double field instead, leading to an error if we pass it - * to visit_type_int. confirm this. - */ - v = visitor_input_test_init(data, "%f", DBL_MAX); - - visit_type_int(v, NULL, &res, &err); - error_free_or_abort(&err); -} - -static void test_visitor_in_bool(TestInputVisitorData *data, - const void *unused) -{ - bool res = false; - Visitor *v; - - v = visitor_input_test_init(data, "true"); - - visit_type_bool(v, NULL, &res, &error_abort); - g_assert_cmpint(res, ==, true); -} - -static void test_visitor_in_number(TestInputVisitorData *data, - const void *unused) -{ - double res = 0, value = 3.14; - Visitor *v; - - v = visitor_input_test_init(data, "%f", value); - - visit_type_number(v, NULL, &res, &error_abort); - g_assert_cmpfloat(res, ==, value); -} - -static void test_visitor_in_string(TestInputVisitorData *data, - const void *unused) -{ - char *res = NULL, *value = (char *) "Q E M U"; - Visitor *v; - - v = visitor_input_test_init(data, "%s", value); - - visit_type_str(v, NULL, &res, &error_abort); - g_assert_cmpstr(res, ==, value); - - g_free(res); -} - -static void test_visitor_in_enum(TestInputVisitorData *data, - const void *unused) -{ - Visitor *v; - EnumOne i; - - for (i = 0; EnumOne_lookup[i]; i++) { - EnumOne res = -1; - - v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]); - - visit_type_EnumOne(v, NULL, &res, &error_abort); - g_assert_cmpint(i, ==, res); - } -} - - -static void test_visitor_in_struct(TestInputVisitorData *data, - const void *unused) -{ - TestStruct *p = NULL; - Visitor *v; - - v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); - - visit_type_TestStruct(v, NULL, &p, &error_abort); - g_assert_cmpint(p->integer, ==, -42); - g_assert(p->boolean == true); - g_assert_cmpstr(p->string, ==, "foo"); - - g_free(p->string); - g_free(p); -} - -static void test_visitor_in_struct_nested(TestInputVisitorData *data, - const void *unused) -{ - UserDefTwo *udp = NULL; - Visitor *v; - - v = visitor_input_test_init(data, "{ 'string0': 'string0', " - "'dict1': { 'string1': 'string1', " - "'dict2': { 'userdef': { 'integer': 42, " - "'string': 'string' }, 'string': 'string2'}}}"); - - visit_type_UserDefTwo(v, NULL, &udp, &error_abort); - - g_assert_cmpstr(udp->string0, ==, "string0"); - g_assert_cmpstr(udp->dict1->string1, ==, "string1"); - g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42); - g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string"); - g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2"); - g_assert(udp->dict1->has_dict3 == false); - - qapi_free_UserDefTwo(udp); -} - -static void test_visitor_in_list(TestInputVisitorData *data, - const void *unused) -{ - UserDefOneList *item, *head = NULL; - Visitor *v; - int i; - - v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); - - visit_type_UserDefOneList(v, NULL, &head, &error_abort); - g_assert(head != NULL); - - for (i = 0, item = head; item; item = item->next, i++) { - char string[12]; - - snprintf(string, sizeof(string), "string%d", i); - g_assert_cmpstr(item->value->string, ==, string); - g_assert_cmpint(item->value->integer, ==, 42 + i); - } - - qapi_free_UserDefOneList(head); - head = NULL; - - /* An empty list is valid */ - v = visitor_input_test_init(data, "[]"); - visit_type_UserDefOneList(v, NULL, &head, &error_abort); - g_assert(!head); -} - -static void test_visitor_in_any(TestInputVisitorData *data, - const void *unused) -{ - QObject *res = NULL; - Visitor *v; - QInt *qint; - QBool *qbool; - QString *qstring; - QDict *qdict; - QObject *qobj; - - v = visitor_input_test_init(data, "-42"); - visit_type_any(v, NULL, &res, &error_abort); - qint = qobject_to_qint(res); - g_assert(qint); - g_assert_cmpint(qint_get_int(qint), ==, -42); - qobject_decref(res); - - v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); - visit_type_any(v, NULL, &res, &error_abort); - qdict = qobject_to_qdict(res); - g_assert(qdict && qdict_size(qdict) == 3); - qobj = qdict_get(qdict, "integer"); - g_assert(qobj); - qint = qobject_to_qint(qobj); - g_assert(qint); - g_assert_cmpint(qint_get_int(qint), ==, -42); - qobj = qdict_get(qdict, "boolean"); - g_assert(qobj); - qbool = qobject_to_qbool(qobj); - g_assert(qbool); - g_assert(qbool_get_bool(qbool) == true); - qobj = qdict_get(qdict, "string"); - g_assert(qobj); - qstring = qobject_to_qstring(qobj); - g_assert(qstring); - g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); - qobject_decref(res); -} - -static void test_visitor_in_null(TestInputVisitorData *data, - const void *unused) -{ - Visitor *v; - Error *err = NULL; - char *tmp; - - /* - * FIXME: Since QAPI doesn't know the 'null' type yet, we can't - * test visit_type_null() by reading into a QAPI struct then - * checking that it was populated correctly. The best we can do - * for now is ensure that we consumed null from the input, proven - * by the fact that we can't re-read the key; and that we detect - * when input is not null. - */ - - v = visitor_input_test_init(data, "{ 'a': null, 'b': '' }"); - visit_start_struct(v, NULL, NULL, 0, &error_abort); - visit_type_null(v, "a", &error_abort); - visit_type_str(v, "a", &tmp, &err); - g_assert(!tmp); - error_free_or_abort(&err); - visit_type_null(v, "b", &err); - error_free_or_abort(&err); - visit_check_struct(v, &error_abort); - visit_end_struct(v, NULL); -} - -static void test_visitor_in_union_flat(TestInputVisitorData *data, - const void *unused) -{ - Visitor *v; - UserDefFlatUnion *tmp; - UserDefUnionBase *base; - - v = visitor_input_test_init(data, - "{ 'enum1': 'value1', " - "'integer': 41, " - "'string': 'str', " - "'boolean': true }"); - - visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); - g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1); - g_assert_cmpstr(tmp->string, ==, "str"); - g_assert_cmpint(tmp->integer, ==, 41); - g_assert_cmpint(tmp->u.value1.boolean, ==, true); - - base = qapi_UserDefFlatUnion_base(tmp); - g_assert(&base->enum1 == &tmp->enum1); - - qapi_free_UserDefFlatUnion(tmp); -} - -static void test_visitor_in_alternate(TestInputVisitorData *data, - const void *unused) -{ - Visitor *v; - Error *err = NULL; - UserDefAlternate *tmp; - WrapAlternate *wrap; - - v = visitor_input_test_init(data, "42"); - visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); - g_assert_cmpint(tmp->type, ==, QTYPE_QINT); - g_assert_cmpint(tmp->u.i, ==, 42); - qapi_free_UserDefAlternate(tmp); - - v = visitor_input_test_init(data, "'string'"); - visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); - g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING); - g_assert_cmpstr(tmp->u.s, ==, "string"); - qapi_free_UserDefAlternate(tmp); - - v = visitor_input_test_init(data, "{'integer':1, 'string':'str', " - "'enum1':'value1', 'boolean':true}"); - visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); - g_assert_cmpint(tmp->type, ==, QTYPE_QDICT); - g_assert_cmpint(tmp->u.udfu.integer, ==, 1); - g_assert_cmpstr(tmp->u.udfu.string, ==, "str"); - g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1); - g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true); - g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false); - qapi_free_UserDefAlternate(tmp); - - v = visitor_input_test_init(data, "false"); - visit_type_UserDefAlternate(v, NULL, &tmp, &err); - error_free_or_abort(&err); - qapi_free_UserDefAlternate(tmp); - - v = visitor_input_test_init(data, "{ 'alt': 42 }"); - visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); - g_assert_cmpint(wrap->alt->type, ==, QTYPE_QINT); - g_assert_cmpint(wrap->alt->u.i, ==, 42); - qapi_free_WrapAlternate(wrap); - - v = visitor_input_test_init(data, "{ 'alt': 'string' }"); - visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); - g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING); - g_assert_cmpstr(wrap->alt->u.s, ==, "string"); - qapi_free_WrapAlternate(wrap); - - v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', " - "'enum1':'value1', 'boolean':true} }"); - visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); - g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT); - g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1); - g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str"); - g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1); - g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true); - g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false); - qapi_free_WrapAlternate(wrap); -} - -static void test_visitor_in_alternate_number(TestInputVisitorData *data, - const void *unused) -{ - Visitor *v; - Error *err = NULL; - AltStrBool *asb; - AltStrNum *asn; - AltNumStr *ans; - AltStrInt *asi; - AltIntNum *ain; - AltNumInt *ani; - - /* Parsing an int */ - - v = visitor_input_test_init(data, "42"); - visit_type_AltStrBool(v, NULL, &asb, &err); - error_free_or_abort(&err); - qapi_free_AltStrBool(asb); - - v = visitor_input_test_init(data, "42"); - visit_type_AltStrNum(v, NULL, &asn, &error_abort); - g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(asn->u.n, ==, 42); - qapi_free_AltStrNum(asn); - - v = visitor_input_test_init(data, "42"); - visit_type_AltNumStr(v, NULL, &ans, &error_abort); - g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(ans->u.n, ==, 42); - qapi_free_AltNumStr(ans); - - v = visitor_input_test_init(data, "42"); - visit_type_AltStrInt(v, NULL, &asi, &error_abort); - g_assert_cmpint(asi->type, ==, QTYPE_QINT); - g_assert_cmpint(asi->u.i, ==, 42); - qapi_free_AltStrInt(asi); - - v = visitor_input_test_init(data, "42"); - visit_type_AltIntNum(v, NULL, &ain, &error_abort); - g_assert_cmpint(ain->type, ==, QTYPE_QINT); - g_assert_cmpint(ain->u.i, ==, 42); - qapi_free_AltIntNum(ain); - - v = visitor_input_test_init(data, "42"); - visit_type_AltNumInt(v, NULL, &ani, &error_abort); - g_assert_cmpint(ani->type, ==, QTYPE_QINT); - g_assert_cmpint(ani->u.i, ==, 42); - qapi_free_AltNumInt(ani); - - /* Parsing a double */ - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltStrBool(v, NULL, &asb, &err); - error_free_or_abort(&err); - qapi_free_AltStrBool(asb); - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltStrNum(v, NULL, &asn, &error_abort); - g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(asn->u.n, ==, 42.5); - qapi_free_AltStrNum(asn); - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltNumStr(v, NULL, &ans, &error_abort); - g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(ans->u.n, ==, 42.5); - qapi_free_AltNumStr(ans); - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltStrInt(v, NULL, &asi, &err); - error_free_or_abort(&err); - qapi_free_AltStrInt(asi); - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltIntNum(v, NULL, &ain, &error_abort); - g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(ain->u.n, ==, 42.5); - qapi_free_AltIntNum(ain); - - v = visitor_input_test_init(data, "42.5"); - visit_type_AltNumInt(v, NULL, &ani, &error_abort); - g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT); - g_assert_cmpfloat(ani->u.n, ==, 42.5); - qapi_free_AltNumInt(ani); -} - -static void test_native_list_integer_helper(TestInputVisitorData *data, - const void *unused, - UserDefNativeListUnionKind kind) -{ - UserDefNativeListUnion *cvalue = NULL; - Visitor *v; - GString *gstr_list = g_string_new(""); - GString *gstr_union = g_string_new(""); - int i; - - for (i = 0; i < 32; i++) { - g_string_append_printf(gstr_list, "%d", i); - if (i != 31) { - g_string_append(gstr_list, ", "); - } - } - g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }", - UserDefNativeListUnionKind_lookup[kind], - gstr_list->str); - v = visitor_input_test_init_raw(data, gstr_union->str); - - visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); - g_assert(cvalue != NULL); - g_assert_cmpint(cvalue->type, ==, kind); - - switch (kind) { - case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { - intList *elem = NULL; - for (i = 0, elem = cvalue->u.integer.data; - elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { - int8List *elem = NULL; - for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { - int16List *elem = NULL; - for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { - int32List *elem = NULL; - for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { - int64List *elem = NULL; - for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { - uint8List *elem = NULL; - for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { - uint16List *elem = NULL; - for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { - uint32List *elem = NULL; - for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { - uint64List *elem = NULL; - for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, i); - } - break; - } - default: - g_assert_not_reached(); - } - - g_string_free(gstr_union, true); - g_string_free(gstr_list, true); - qapi_free_UserDefNativeListUnion(cvalue); -} - -static void test_visitor_in_native_list_int(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); -} - -static void test_visitor_in_native_list_int8(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_S8); -} - -static void test_visitor_in_native_list_int16(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_S16); -} - -static void test_visitor_in_native_list_int32(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_S32); -} - -static void test_visitor_in_native_list_int64(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_S64); -} - -static void test_visitor_in_native_list_uint8(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_U8); -} - -static void test_visitor_in_native_list_uint16(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_U16); -} - -static void test_visitor_in_native_list_uint32(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_U32); -} - -static void test_visitor_in_native_list_uint64(TestInputVisitorData *data, - const void *unused) -{ - test_native_list_integer_helper(data, unused, - USER_DEF_NATIVE_LIST_UNION_KIND_U64); -} - -static void test_visitor_in_native_list_bool(TestInputVisitorData *data, - const void *unused) -{ - UserDefNativeListUnion *cvalue = NULL; - boolList *elem = NULL; - Visitor *v; - GString *gstr_list = g_string_new(""); - GString *gstr_union = g_string_new(""); - int i; - - for (i = 0; i < 32; i++) { - g_string_append_printf(gstr_list, "%s", - (i % 3 == 0) ? "true" : "false"); - if (i != 31) { - g_string_append(gstr_list, ", "); - } - } - g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }", - gstr_list->str); - v = visitor_input_test_init_raw(data, gstr_union->str); - - visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); - g_assert(cvalue != NULL); - g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); - - for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) { - g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); - } - - g_string_free(gstr_union, true); - g_string_free(gstr_list, true); - qapi_free_UserDefNativeListUnion(cvalue); -} - -static void test_visitor_in_native_list_string(TestInputVisitorData *data, - const void *unused) -{ - UserDefNativeListUnion *cvalue = NULL; - strList *elem = NULL; - Visitor *v; - GString *gstr_list = g_string_new(""); - GString *gstr_union = g_string_new(""); - int i; - - for (i = 0; i < 32; i++) { - g_string_append_printf(gstr_list, "'%d'", i); - if (i != 31) { - g_string_append(gstr_list, ", "); - } - } - g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }", - gstr_list->str); - v = visitor_input_test_init_raw(data, gstr_union->str); - - visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); - g_assert(cvalue != NULL); - g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); - - for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) { - gchar str[8]; - sprintf(str, "%d", i); - g_assert_cmpstr(elem->value, ==, str); - } - - g_string_free(gstr_union, true); - g_string_free(gstr_list, true); - qapi_free_UserDefNativeListUnion(cvalue); -} - -#define DOUBLE_STR_MAX 16 - -static void test_visitor_in_native_list_number(TestInputVisitorData *data, - const void *unused) -{ - UserDefNativeListUnion *cvalue = NULL; - numberList *elem = NULL; - Visitor *v; - GString *gstr_list = g_string_new(""); - GString *gstr_union = g_string_new(""); - int i; - - for (i = 0; i < 32; i++) { - g_string_append_printf(gstr_list, "%f", (double)i / 3); - if (i != 31) { - g_string_append(gstr_list, ", "); - } - } - g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }", - gstr_list->str); - v = visitor_input_test_init_raw(data, gstr_union->str); - - visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); - g_assert(cvalue != NULL); - g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); - - for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) { - GString *double_expected = g_string_new(""); - GString *double_actual = g_string_new(""); - - g_string_printf(double_expected, "%.6f", (double)i / 3); - g_string_printf(double_actual, "%.6f", elem->value); - g_assert_cmpstr(double_expected->str, ==, double_actual->str); - - g_string_free(double_expected, true); - g_string_free(double_actual, true); - } - - g_string_free(gstr_union, true); - g_string_free(gstr_list, true); - qapi_free_UserDefNativeListUnion(cvalue); -} - -static void input_visitor_test_add(const char *testpath, - TestInputVisitorData *data, - void (*test_func)(TestInputVisitorData *data, const void *user_data)) -{ - g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, - visitor_input_teardown); -} - -static void test_visitor_in_errors(TestInputVisitorData *data, - const void *unused) -{ - TestStruct *p = NULL; - Error *err = NULL; - Visitor *v; - strList *q = NULL; - UserDefTwo *r = NULL; - WrapAlternate *s = NULL; - - v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', " - "'string': -42 }"); - - visit_type_TestStruct(v, NULL, &p, &err); - error_free_or_abort(&err); - g_assert(!p); - - v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]"); - visit_type_strList(v, NULL, &q, &err); - error_free_or_abort(&err); - assert(!q); - - v = visitor_input_test_init(data, "{ 'str':'hi' }"); - visit_type_UserDefTwo(v, NULL, &r, &err); - error_free_or_abort(&err); - assert(!r); - - v = visitor_input_test_init(data, "{ }"); - visit_type_WrapAlternate(v, NULL, &s, &err); - error_free_or_abort(&err); - assert(!s); -} - -static void test_visitor_in_wrong_type(TestInputVisitorData *data, - const void *unused) -{ - TestStruct *p = NULL; - Visitor *v; - strList *q = NULL; - int64_t i; - Error *err = NULL; - - /* Make sure arrays and structs cannot be confused */ - - v = visitor_input_test_init(data, "[]"); - visit_type_TestStruct(v, NULL, &p, &err); - error_free_or_abort(&err); - g_assert(!p); - - v = visitor_input_test_init(data, "{}"); - visit_type_strList(v, NULL, &q, &err); - error_free_or_abort(&err); - assert(!q); - - /* Make sure primitives and struct cannot be confused */ - - v = visitor_input_test_init(data, "1"); - visit_type_TestStruct(v, NULL, &p, &err); - error_free_or_abort(&err); - g_assert(!p); - - v = visitor_input_test_init(data, "{}"); - visit_type_int(v, NULL, &i, &err); - error_free_or_abort(&err); - - /* Make sure primitives and arrays cannot be confused */ - - v = visitor_input_test_init(data, "1"); - visit_type_strList(v, NULL, &q, &err); - error_free_or_abort(&err); - assert(!q); - - v = visitor_input_test_init(data, "[]"); - visit_type_int(v, NULL, &i, &err); - error_free_or_abort(&err); -} - -int main(int argc, char **argv) -{ - TestInputVisitorData in_visitor_data; - - g_test_init(&argc, &argv, NULL); - - input_visitor_test_add("/visitor/input/int", - &in_visitor_data, test_visitor_in_int); - input_visitor_test_add("/visitor/input/int_overflow", - &in_visitor_data, test_visitor_in_int_overflow); - input_visitor_test_add("/visitor/input/bool", - &in_visitor_data, test_visitor_in_bool); - input_visitor_test_add("/visitor/input/number", - &in_visitor_data, test_visitor_in_number); - input_visitor_test_add("/visitor/input/string", - &in_visitor_data, test_visitor_in_string); - input_visitor_test_add("/visitor/input/enum", - &in_visitor_data, test_visitor_in_enum); - input_visitor_test_add("/visitor/input/struct", - &in_visitor_data, test_visitor_in_struct); - input_visitor_test_add("/visitor/input/struct-nested", - &in_visitor_data, test_visitor_in_struct_nested); - input_visitor_test_add("/visitor/input/list", - &in_visitor_data, test_visitor_in_list); - input_visitor_test_add("/visitor/input/any", - &in_visitor_data, test_visitor_in_any); - input_visitor_test_add("/visitor/input/null", - &in_visitor_data, test_visitor_in_null); - input_visitor_test_add("/visitor/input/union-flat", - &in_visitor_data, test_visitor_in_union_flat); - input_visitor_test_add("/visitor/input/alternate", - &in_visitor_data, test_visitor_in_alternate); - input_visitor_test_add("/visitor/input/errors", - &in_visitor_data, test_visitor_in_errors); - input_visitor_test_add("/visitor/input/wrong-type", - &in_visitor_data, test_visitor_in_wrong_type); - input_visitor_test_add("/visitor/input/alternate-number", - &in_visitor_data, test_visitor_in_alternate_number); - input_visitor_test_add("/visitor/input/native_list/int", - &in_visitor_data, - test_visitor_in_native_list_int); - input_visitor_test_add("/visitor/input/native_list/int8", - &in_visitor_data, - test_visitor_in_native_list_int8); - input_visitor_test_add("/visitor/input/native_list/int16", - &in_visitor_data, - test_visitor_in_native_list_int16); - input_visitor_test_add("/visitor/input/native_list/int32", - &in_visitor_data, - test_visitor_in_native_list_int32); - input_visitor_test_add("/visitor/input/native_list/int64", - &in_visitor_data, - test_visitor_in_native_list_int64); - input_visitor_test_add("/visitor/input/native_list/uint8", - &in_visitor_data, - test_visitor_in_native_list_uint8); - input_visitor_test_add("/visitor/input/native_list/uint16", - &in_visitor_data, - test_visitor_in_native_list_uint16); - input_visitor_test_add("/visitor/input/native_list/uint32", - &in_visitor_data, - test_visitor_in_native_list_uint32); - input_visitor_test_add("/visitor/input/native_list/uint64", - &in_visitor_data, - test_visitor_in_native_list_uint64); - input_visitor_test_add("/visitor/input/native_list/bool", - &in_visitor_data, test_visitor_in_native_list_bool); - input_visitor_test_add("/visitor/input/native_list/str", - &in_visitor_data, - test_visitor_in_native_list_string); - input_visitor_test_add("/visitor/input/native_list/number", - &in_visitor_data, - test_visitor_in_native_list_number); - - g_test_run(); - - return 0; -} diff --git a/tests/test-qmp-output-visitor.c b/tests/test-qmp-output-visitor.c deleted file mode 100644 index 513d71f..0000000 --- a/tests/test-qmp-output-visitor.c +++ /dev/null @@ -1,876 +0,0 @@ -/* - * QMP Output Visitor unit-tests. - * - * Copyright (C) 2011-2016 Red Hat Inc. - * - * Authors: - * Luiz Capitulino - * - * 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 "qemu/osdep.h" - -#include "qemu-common.h" -#include "qapi/error.h" -#include "qapi/qmp-output-visitor.h" -#include "test-qapi-types.h" -#include "test-qapi-visit.h" -#include "qapi/qmp/types.h" -#include "qapi/qmp/qjson.h" - -typedef struct TestOutputVisitorData { - Visitor *ov; - QObject *obj; -} TestOutputVisitorData; - -static void visitor_output_setup(TestOutputVisitorData *data, - const void *unused) -{ - data->ov = qmp_output_visitor_new(&data->obj); - g_assert(data->ov); -} - -static void visitor_output_teardown(TestOutputVisitorData *data, - const void *unused) -{ - visit_free(data->ov); - data->ov = NULL; - qobject_decref(data->obj); - data->obj = NULL; -} - -static QObject *visitor_get(TestOutputVisitorData *data) -{ - visit_complete(data->ov, &data->obj); - g_assert(data->obj); - return data->obj; -} - -static void visitor_reset(TestOutputVisitorData *data) -{ - visitor_output_teardown(data, NULL); - visitor_output_setup(data, NULL); -} - -static void test_visitor_out_int(TestOutputVisitorData *data, - const void *unused) -{ - int64_t value = -42; - QObject *obj; - - visit_type_int(data->ov, NULL, &value, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QINT); - g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); -} - -static void test_visitor_out_bool(TestOutputVisitorData *data, - const void *unused) -{ - bool value = true; - QObject *obj; - - visit_type_bool(data->ov, NULL, &value, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QBOOL); - g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value); -} - -static void test_visitor_out_number(TestOutputVisitorData *data, - const void *unused) -{ - double value = 3.14; - QObject *obj; - - visit_type_number(data->ov, NULL, &value, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QFLOAT); - g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value); -} - -static void test_visitor_out_string(TestOutputVisitorData *data, - const void *unused) -{ - char *string = (char *) "Q E M U"; - QObject *obj; - - visit_type_str(data->ov, NULL, &string, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QSTRING); - g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); -} - -static void test_visitor_out_no_string(TestOutputVisitorData *data, - const void *unused) -{ - char *string = NULL; - QObject *obj; - - /* A null string should return "" */ - visit_type_str(data->ov, NULL, &string, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QSTRING); - g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); -} - -static void test_visitor_out_enum(TestOutputVisitorData *data, - const void *unused) -{ - QObject *obj; - EnumOne i; - - for (i = 0; i < ENUM_ONE__MAX; i++) { - visit_type_EnumOne(data->ov, "unused", &i, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QSTRING); - g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, - EnumOne_lookup[i]); - visitor_reset(data); - } -} - -static void test_visitor_out_enum_errors(TestOutputVisitorData *data, - const void *unused) -{ - EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 }; - Error *err; - - for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { - err = NULL; - visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err); - g_assert(err); - error_free(err); - visitor_reset(data); - } -} - - -static void test_visitor_out_struct(TestOutputVisitorData *data, - const void *unused) -{ - TestStruct test_struct = { .integer = 42, - .boolean = false, - .string = (char *) "foo"}; - TestStruct *p = &test_struct; - QObject *obj; - QDict *qdict; - - visit_type_TestStruct(data->ov, NULL, &p, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QDICT); - - qdict = qobject_to_qdict(obj); - g_assert_cmpint(qdict_size(qdict), ==, 3); - g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); - g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false); - g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); -} - -static void test_visitor_out_struct_nested(TestOutputVisitorData *data, - const void *unused) -{ - int64_t value = 42; - UserDefTwo *ud2; - QObject *obj; - QDict *qdict, *dict1, *dict2, *dict3, *userdef; - const char *string = "user def string"; - const char *strings[] = { "forty two", "forty three", "forty four", - "forty five" }; - - ud2 = g_malloc0(sizeof(*ud2)); - ud2->string0 = g_strdup(strings[0]); - - ud2->dict1 = g_malloc0(sizeof(*ud2->dict1)); - ud2->dict1->string1 = g_strdup(strings[1]); - - ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2)); - ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1); - ud2->dict1->dict2->userdef->string = g_strdup(string); - ud2->dict1->dict2->userdef->integer = value; - ud2->dict1->dict2->string = g_strdup(strings[2]); - - ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3)); - ud2->dict1->has_dict3 = true; - ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1); - ud2->dict1->dict3->userdef->string = g_strdup(string); - ud2->dict1->dict3->userdef->integer = value; - ud2->dict1->dict3->string = g_strdup(strings[3]); - - visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QDICT); - - qdict = qobject_to_qdict(obj); - g_assert_cmpint(qdict_size(qdict), ==, 2); - g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]); - - dict1 = qdict_get_qdict(qdict, "dict1"); - g_assert_cmpint(qdict_size(dict1), ==, 3); - g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]); - - dict2 = qdict_get_qdict(dict1, "dict2"); - g_assert_cmpint(qdict_size(dict2), ==, 2); - g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]); - userdef = qdict_get_qdict(dict2, "userdef"); - g_assert_cmpint(qdict_size(userdef), ==, 2); - g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); - g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); - - dict3 = qdict_get_qdict(dict1, "dict3"); - g_assert_cmpint(qdict_size(dict3), ==, 2); - g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]); - userdef = qdict_get_qdict(dict3, "userdef"); - g_assert_cmpint(qdict_size(userdef), ==, 2); - g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); - g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); - - qapi_free_UserDefTwo(ud2); -} - -static void test_visitor_out_struct_errors(TestOutputVisitorData *data, - const void *unused) -{ - EnumOne bad_values[] = { ENUM_ONE__MAX, -1 }; - UserDefOne u = {0}; - UserDefOne *pu = &u; - Error *err; - int i; - - for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { - err = NULL; - u.has_enum1 = true; - u.enum1 = bad_values[i]; - visit_type_UserDefOne(data->ov, "unused", &pu, &err); - g_assert(err); - error_free(err); - visitor_reset(data); - } -} - - -static void test_visitor_out_list(TestOutputVisitorData *data, - const void *unused) -{ - const char *value_str = "list value"; - TestStructList *p, *head = NULL; - const int max_items = 10; - bool value_bool = true; - int value_int = 10; - QListEntry *entry; - QObject *obj; - QList *qlist; - int i; - - /* Build the list in reverse order... */ - for (i = 0; i < max_items; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - p->value->integer = value_int + (max_items - i - 1); - p->value->boolean = value_bool; - p->value->string = g_strdup(value_str); - - p->next = head; - head = p; - } - - visit_type_TestStructList(data->ov, NULL, &head, &error_abort); - - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QLIST); - - qlist = qobject_to_qlist(obj); - g_assert(!qlist_empty(qlist)); - - /* ...and ensure that the visitor sees it in order */ - i = 0; - QLIST_FOREACH_ENTRY(qlist, entry) { - QDict *qdict; - - g_assert(qobject_type(entry->value) == QTYPE_QDICT); - qdict = qobject_to_qdict(entry->value); - g_assert_cmpint(qdict_size(qdict), ==, 3); - g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i); - g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool); - g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str); - i++; - } - g_assert_cmpint(i, ==, max_items); - - qapi_free_TestStructList(head); -} - -static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, - const void *unused) -{ - UserDefTwoList *p, *head = NULL; - const char string[] = "foo bar"; - int i, max_count = 1024; - - for (i = 0; i < max_count; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - - p->value->string0 = g_strdup(string); - p->value->dict1 = g_new0(UserDefTwoDict, 1); - p->value->dict1->string1 = g_strdup(string); - p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); - p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1); - p->value->dict1->dict2->userdef->string = g_strdup(string); - p->value->dict1->dict2->userdef->integer = 42; - p->value->dict1->dict2->string = g_strdup(string); - p->value->dict1->has_dict3 = false; - - p->next = head; - head = p; - } - - qapi_free_UserDefTwoList(head); -} - -static void test_visitor_out_any(TestOutputVisitorData *data, - const void *unused) -{ - QObject *qobj; - QInt *qint; - QBool *qbool; - QString *qstring; - QDict *qdict; - QObject *obj; - - qobj = QOBJECT(qint_from_int(-42)); - visit_type_any(data->ov, NULL, &qobj, &error_abort); - obj = visitor_get(data); - g_assert(qobject_type(obj) == QTYPE_QINT); - g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42); - qobject_decref(qobj); - - visitor_reset(data); - qdict = qdict_new(); - qdict_put(qdict, "integer", qint_from_int(-42)); - qdict_put(qdict, "boolean", qbool_from_bool(true)); - qdict_put(qdict, "string", qstring_from_str("foo")); - qobj = QOBJECT(qdict); - visit_type_any(data->ov, NULL, &qobj, &error_abort); - qobject_decref(qobj); - obj = visitor_get(data); - qdict = qobject_to_qdict(obj); - g_assert(qdict); - qobj = qdict_get(qdict, "integer"); - g_assert(qobj); - qint = qobject_to_qint(qobj); - g_assert(qint); - g_assert_cmpint(qint_get_int(qint), ==, -42); - qobj = qdict_get(qdict, "boolean"); - g_assert(qobj); - qbool = qobject_to_qbool(qobj); - g_assert(qbool); - g_assert(qbool_get_bool(qbool) == true); - qobj = qdict_get(qdict, "string"); - g_assert(qobj); - qstring = qobject_to_qstring(qobj); - g_assert(qstring); - g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); -} - -static void test_visitor_out_union_flat(TestOutputVisitorData *data, - const void *unused) -{ - QObject *arg; - QDict *qdict; - - UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion)); - tmp->enum1 = ENUM_ONE_VALUE1; - tmp->string = g_strdup("str"); - tmp->integer = 41; - tmp->u.value1.boolean = true; - - visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort); - arg = visitor_get(data); - - g_assert(qobject_type(arg) == QTYPE_QDICT); - qdict = qobject_to_qdict(arg); - - g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1"); - g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str"); - g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); - g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); - - qapi_free_UserDefFlatUnion(tmp); -} - -static void test_visitor_out_alternate(TestOutputVisitorData *data, - const void *unused) -{ - QObject *arg; - UserDefAlternate *tmp; - QDict *qdict; - - tmp = g_new0(UserDefAlternate, 1); - tmp->type = QTYPE_QINT; - tmp->u.i = 42; - - visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = visitor_get(data); - - g_assert(qobject_type(arg) == QTYPE_QINT); - g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42); - - qapi_free_UserDefAlternate(tmp); - - visitor_reset(data); - tmp = g_new0(UserDefAlternate, 1); - tmp->type = QTYPE_QSTRING; - tmp->u.s = g_strdup("hello"); - - visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = visitor_get(data); - - g_assert(qobject_type(arg) == QTYPE_QSTRING); - g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello"); - - qapi_free_UserDefAlternate(tmp); - - visitor_reset(data); - tmp = g_new0(UserDefAlternate, 1); - tmp->type = QTYPE_QDICT; - tmp->u.udfu.integer = 1; - tmp->u.udfu.string = g_strdup("str"); - tmp->u.udfu.enum1 = ENUM_ONE_VALUE1; - tmp->u.udfu.u.value1.boolean = true; - - visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); - arg = visitor_get(data); - - g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT); - qdict = qobject_to_qdict(arg); - g_assert_cmpint(qdict_size(qdict), ==, 4); - g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1); - g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str"); - g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1"); - g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); - - qapi_free_UserDefAlternate(tmp); -} - -static void test_visitor_out_null(TestOutputVisitorData *data, - const void *unused) -{ - QObject *arg; - QDict *qdict; - QObject *nil; - - visit_start_struct(data->ov, NULL, NULL, 0, &error_abort); - visit_type_null(data->ov, "a", &error_abort); - visit_check_struct(data->ov, &error_abort); - visit_end_struct(data->ov, NULL); - arg = visitor_get(data); - g_assert(qobject_type(arg) == QTYPE_QDICT); - qdict = qobject_to_qdict(arg); - g_assert_cmpint(qdict_size(qdict), ==, 1); - nil = qdict_get(qdict, "a"); - g_assert(nil); - g_assert(qobject_type(nil) == QTYPE_QNULL); -} - -static void init_native_list(UserDefNativeListUnion *cvalue) -{ - int i; - switch (cvalue->type) { - case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { - intList **list = &cvalue->u.integer.data; - for (i = 0; i < 32; i++) { - *list = g_new0(intList, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { - int8List **list = &cvalue->u.s8.data; - for (i = 0; i < 32; i++) { - *list = g_new0(int8List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { - int16List **list = &cvalue->u.s16.data; - for (i = 0; i < 32; i++) { - *list = g_new0(int16List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { - int32List **list = &cvalue->u.s32.data; - for (i = 0; i < 32; i++) { - *list = g_new0(int32List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { - int64List **list = &cvalue->u.s64.data; - for (i = 0; i < 32; i++) { - *list = g_new0(int64List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { - uint8List **list = &cvalue->u.u8.data; - for (i = 0; i < 32; i++) { - *list = g_new0(uint8List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { - uint16List **list = &cvalue->u.u16.data; - for (i = 0; i < 32; i++) { - *list = g_new0(uint16List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { - uint32List **list = &cvalue->u.u32.data; - for (i = 0; i < 32; i++) { - *list = g_new0(uint32List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { - uint64List **list = &cvalue->u.u64.data; - for (i = 0; i < 32; i++) { - *list = g_new0(uint64List, 1); - (*list)->value = i; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: { - boolList **list = &cvalue->u.boolean.data; - for (i = 0; i < 32; i++) { - *list = g_new0(boolList, 1); - (*list)->value = (i % 3 == 0); - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: { - strList **list = &cvalue->u.string.data; - for (i = 0; i < 32; i++) { - *list = g_new0(strList, 1); - (*list)->value = g_strdup_printf("%d", i); - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: { - numberList **list = &cvalue->u.number.data; - for (i = 0; i < 32; i++) { - *list = g_new0(numberList, 1); - (*list)->value = (double)i / 3; - (*list)->next = NULL; - list = &(*list)->next; - } - break; - } - default: - g_assert_not_reached(); - } -} - -static void check_native_list(QObject *qobj, - UserDefNativeListUnionKind kind) -{ - QDict *qdict; - QList *qlist; - int i; - - g_assert(qobj); - g_assert(qobject_type(qobj) == QTYPE_QDICT); - qdict = qobject_to_qdict(qobj); - g_assert(qdict); - g_assert(qdict_haskey(qdict, "data")); - qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data"))); - - switch (kind) { - case USER_DEF_NATIVE_LIST_UNION_KIND_S8: - case USER_DEF_NATIVE_LIST_UNION_KIND_S16: - case USER_DEF_NATIVE_LIST_UNION_KIND_S32: - case USER_DEF_NATIVE_LIST_UNION_KIND_S64: - case USER_DEF_NATIVE_LIST_UNION_KIND_U8: - case USER_DEF_NATIVE_LIST_UNION_KIND_U16: - case USER_DEF_NATIVE_LIST_UNION_KIND_U32: - case USER_DEF_NATIVE_LIST_UNION_KIND_U64: - /* all integer elements in JSON arrays get stored into QInts when - * we convert to QObjects, so we can check them all in the same - * fashion, so simply fall through here - */ - case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: - for (i = 0; i < 32; i++) { - QObject *tmp; - QInt *qvalue; - tmp = qlist_peek(qlist); - g_assert(tmp); - qvalue = qobject_to_qint(tmp); - g_assert_cmpint(qint_get_int(qvalue), ==, i); - qobject_decref(qlist_pop(qlist)); - } - break; - case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: - for (i = 0; i < 32; i++) { - QObject *tmp; - QBool *qvalue; - tmp = qlist_peek(qlist); - g_assert(tmp); - qvalue = qobject_to_qbool(tmp); - g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0); - qobject_decref(qlist_pop(qlist)); - } - break; - case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: - for (i = 0; i < 32; i++) { - QObject *tmp; - QString *qvalue; - gchar str[8]; - tmp = qlist_peek(qlist); - g_assert(tmp); - qvalue = qobject_to_qstring(tmp); - sprintf(str, "%d", i); - g_assert_cmpstr(qstring_get_str(qvalue), ==, str); - qobject_decref(qlist_pop(qlist)); - } - break; - case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: - for (i = 0; i < 32; i++) { - QObject *tmp; - QFloat *qvalue; - GString *double_expected = g_string_new(""); - GString *double_actual = g_string_new(""); - - tmp = qlist_peek(qlist); - g_assert(tmp); - qvalue = qobject_to_qfloat(tmp); - g_string_printf(double_expected, "%.6f", (double)i / 3); - g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue)); - g_assert_cmpstr(double_actual->str, ==, double_expected->str); - - qobject_decref(qlist_pop(qlist)); - g_string_free(double_expected, true); - g_string_free(double_actual, true); - } - break; - default: - g_assert_not_reached(); - } - QDECREF(qlist); -} - -static void test_native_list(TestOutputVisitorData *data, - const void *unused, - UserDefNativeListUnionKind kind) -{ - UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1); - QObject *obj; - - cvalue->type = kind; - init_native_list(cvalue); - - visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort); - - obj = visitor_get(data); - check_native_list(obj, cvalue->type); - qapi_free_UserDefNativeListUnion(cvalue); -} - -static void test_visitor_out_native_list_int(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); -} - -static void test_visitor_out_native_list_int8(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8); -} - -static void test_visitor_out_native_list_int16(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16); -} - -static void test_visitor_out_native_list_int32(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32); -} - -static void test_visitor_out_native_list_int64(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64); -} - -static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8); -} - -static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16); -} - -static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32); -} - -static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64); -} - -static void test_visitor_out_native_list_bool(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); -} - -static void test_visitor_out_native_list_str(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); -} - -static void test_visitor_out_native_list_number(TestOutputVisitorData *data, - const void *unused) -{ - test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); -} - -static void output_visitor_test_add(const char *testpath, - TestOutputVisitorData *data, - void (*test_func)(TestOutputVisitorData *data, const void *user_data)) -{ - g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup, - test_func, visitor_output_teardown); -} - -int main(int argc, char **argv) -{ - TestOutputVisitorData out_visitor_data; - - g_test_init(&argc, &argv, NULL); - - output_visitor_test_add("/visitor/output/int", - &out_visitor_data, test_visitor_out_int); - output_visitor_test_add("/visitor/output/bool", - &out_visitor_data, test_visitor_out_bool); - output_visitor_test_add("/visitor/output/number", - &out_visitor_data, test_visitor_out_number); - output_visitor_test_add("/visitor/output/string", - &out_visitor_data, test_visitor_out_string); - output_visitor_test_add("/visitor/output/no-string", - &out_visitor_data, test_visitor_out_no_string); - output_visitor_test_add("/visitor/output/enum", - &out_visitor_data, test_visitor_out_enum); - output_visitor_test_add("/visitor/output/enum-errors", - &out_visitor_data, test_visitor_out_enum_errors); - output_visitor_test_add("/visitor/output/struct", - &out_visitor_data, test_visitor_out_struct); - output_visitor_test_add("/visitor/output/struct-nested", - &out_visitor_data, test_visitor_out_struct_nested); - output_visitor_test_add("/visitor/output/struct-errors", - &out_visitor_data, test_visitor_out_struct_errors); - output_visitor_test_add("/visitor/output/list", - &out_visitor_data, test_visitor_out_list); - output_visitor_test_add("/visitor/output/any", - &out_visitor_data, test_visitor_out_any); - output_visitor_test_add("/visitor/output/list-qapi-free", - &out_visitor_data, test_visitor_out_list_qapi_free); - output_visitor_test_add("/visitor/output/union-flat", - &out_visitor_data, test_visitor_out_union_flat); - output_visitor_test_add("/visitor/output/alternate", - &out_visitor_data, test_visitor_out_alternate); - output_visitor_test_add("/visitor/output/null", - &out_visitor_data, test_visitor_out_null); - output_visitor_test_add("/visitor/output/native_list/int", - &out_visitor_data, - test_visitor_out_native_list_int); - output_visitor_test_add("/visitor/output/native_list/int8", - &out_visitor_data, - test_visitor_out_native_list_int8); - output_visitor_test_add("/visitor/output/native_list/int16", - &out_visitor_data, - test_visitor_out_native_list_int16); - output_visitor_test_add("/visitor/output/native_list/int32", - &out_visitor_data, - test_visitor_out_native_list_int32); - output_visitor_test_add("/visitor/output/native_list/int64", - &out_visitor_data, - test_visitor_out_native_list_int64); - output_visitor_test_add("/visitor/output/native_list/uint8", - &out_visitor_data, - test_visitor_out_native_list_uint8); - output_visitor_test_add("/visitor/output/native_list/uint16", - &out_visitor_data, - test_visitor_out_native_list_uint16); - output_visitor_test_add("/visitor/output/native_list/uint32", - &out_visitor_data, - test_visitor_out_native_list_uint32); - output_visitor_test_add("/visitor/output/native_list/uint64", - &out_visitor_data, - test_visitor_out_native_list_uint64); - output_visitor_test_add("/visitor/output/native_list/bool", - &out_visitor_data, - test_visitor_out_native_list_bool); - output_visitor_test_add("/visitor/output/native_list/string", - &out_visitor_data, - test_visitor_out_native_list_str); - output_visitor_test_add("/visitor/output/native_list/number", - &out_visitor_data, - test_visitor_out_native_list_number); - - g_test_run(); - - return 0; -} diff --git a/tests/test-qobject-input-strict.c b/tests/test-qobject-input-strict.c new file mode 100644 index 0000000..c6aac20 --- /dev/null +++ b/tests/test-qobject-input-strict.c @@ -0,0 +1,381 @@ +/* + * QObject Input Visitor unit-tests (strict mode). + * + * Copyright (C) 2011-2012, 2015 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * Paolo Bonzini + * + * 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 "qemu/osdep.h" + +#include "qemu-common.h" +#include "qapi/error.h" +#include "qapi/qobject-input-visitor.h" +#include "test-qapi-types.h" +#include "test-qapi-visit.h" +#include "qapi/qmp/types.h" +#include "qapi/qmp/qjson.h" +#include "test-qmp-introspect.h" +#include "qmp-introspect.h" +#include "qapi-visit.h" + +typedef struct TestInputVisitorData { + QObject *obj; + Visitor *qiv; +} TestInputVisitorData; + +static void validate_teardown(TestInputVisitorData *data, + const void *unused) +{ + qobject_decref(data->obj); + data->obj = NULL; + + if (data->qiv) { + visit_free(data->qiv); + data->qiv = NULL; + } +} + +/* The various test_init functions are provided instead of a test setup + function so that the JSON string used by the tests are kept in the test + functions (and not in main()). */ +static Visitor *validate_test_init_internal(TestInputVisitorData *data, + const char *json_string, + va_list *ap) +{ + validate_teardown(data, NULL); + + data->obj = qobject_from_jsonv(json_string, ap); + g_assert(data->obj); + + data->qiv = qmp_input_visitor_new(data->obj, true); + g_assert(data->qiv); + return data->qiv; +} + +static GCC_FMT_ATTR(2, 3) +Visitor *validate_test_init(TestInputVisitorData *data, + const char *json_string, ...) +{ + Visitor *v; + va_list ap; + + va_start(ap, json_string); + v = validate_test_init_internal(data, json_string, &ap); + va_end(ap); + return v; +} + +/* similar to validate_test_init(), but does not expect a string + * literal/format json_string argument and so can be used for + * programatically generated strings (and we can't pass in programatically + * generated strings via %s format parameters since qobject_from_jsonv() + * will wrap those in double-quotes and treat the entire object as a + * string) + */ +static Visitor *validate_test_init_raw(TestInputVisitorData *data, + const char *json_string) +{ + return validate_test_init_internal(data, json_string, NULL); +} + + +static void test_validate_struct(TestInputVisitorData *data, + const void *unused) +{ + TestStruct *p = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); + + visit_type_TestStruct(v, NULL, &p, &error_abort); + g_free(p->string); + g_free(p); +} + +static void test_validate_struct_nested(TestInputVisitorData *data, + const void *unused) +{ + UserDefTwo *udp = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'string0': 'string0', " + "'dict1': { 'string1': 'string1', " + "'dict2': { 'userdef': { 'integer': 42, " + "'string': 'string' }, 'string': 'string2'}}}"); + + visit_type_UserDefTwo(v, NULL, &udp, &error_abort); + qapi_free_UserDefTwo(udp); +} + +static void test_validate_list(TestInputVisitorData *data, + const void *unused) +{ + UserDefOneList *head = NULL; + Visitor *v; + + v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); + + visit_type_UserDefOneList(v, NULL, &head, &error_abort); + qapi_free_UserDefOneList(head); +} + +static void test_validate_union_native_list(TestInputVisitorData *data, + const void *unused) +{ + UserDefNativeListUnion *tmp = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }"); + + visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort); + qapi_free_UserDefNativeListUnion(tmp); +} + +static void test_validate_union_flat(TestInputVisitorData *data, + const void *unused) +{ + UserDefFlatUnion *tmp = NULL; + Visitor *v; + + v = validate_test_init(data, + "{ 'enum1': 'value1', " + "'integer': 41, " + "'string': 'str', " + "'boolean': true }"); + + visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); + qapi_free_UserDefFlatUnion(tmp); +} + +static void test_validate_alternate(TestInputVisitorData *data, + const void *unused) +{ + UserDefAlternate *tmp = NULL; + Visitor *v; + + v = validate_test_init(data, "42"); + + visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); + qapi_free_UserDefAlternate(tmp); +} + +static void test_validate_fail_struct(TestInputVisitorData *data, + const void *unused) +{ + TestStruct *p = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); + + visit_type_TestStruct(v, NULL, &p, &err); + error_free_or_abort(&err); + g_assert(!p); +} + +static void test_validate_fail_struct_nested(TestInputVisitorData *data, + const void *unused) +{ + UserDefTwo *udp = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); + + visit_type_UserDefTwo(v, NULL, &udp, &err); + error_free_or_abort(&err); + g_assert(!udp); +} + +static void test_validate_fail_struct_missing(TestInputVisitorData *data, + const void *unused) +{ + Error *err = NULL; + Visitor *v; + QObject *any; + GenericAlternate *alt; + bool present; + int en; + int64_t i64; + uint32_t u32; + int8_t i8; + char *str; + double dbl; + + v = validate_test_init(data, "{}"); + visit_start_struct(v, NULL, NULL, 0, &error_abort); + visit_start_struct(v, "struct", NULL, 0, &err); + error_free_or_abort(&err); + visit_start_list(v, "list", NULL, 0, &err); + error_free_or_abort(&err); + visit_start_alternate(v, "alternate", &alt, sizeof(*alt), false, &err); + error_free_or_abort(&err); + visit_optional(v, "optional", &present); + g_assert(!present); + visit_type_enum(v, "enum", &en, EnumOne_lookup, &err); + error_free_or_abort(&err); + visit_type_int(v, "i64", &i64, &err); + error_free_or_abort(&err); + visit_type_uint32(v, "u32", &u32, &err); + error_free_or_abort(&err); + visit_type_int8(v, "i8", &i8, &err); + error_free_or_abort(&err); + visit_type_str(v, "i8", &str, &err); + error_free_or_abort(&err); + visit_type_number(v, "dbl", &dbl, &err); + error_free_or_abort(&err); + visit_type_any(v, "any", &any, &err); + error_free_or_abort(&err); + visit_type_null(v, "null", &err); + error_free_or_abort(&err); + visit_end_struct(v, NULL); +} + +static void test_validate_fail_list(TestInputVisitorData *data, + const void *unused) +{ + UserDefOneList *head = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); + + visit_type_UserDefOneList(v, NULL, &head, &err); + error_free_or_abort(&err); + g_assert(!head); +} + +static void test_validate_fail_union_native_list(TestInputVisitorData *data, + const void *unused) +{ + UserDefNativeListUnion *tmp = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init(data, + "{ 'type': 'integer', 'data' : [ 'string' ] }"); + + visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err); + error_free_or_abort(&err); + g_assert(!tmp); +} + +static void test_validate_fail_union_flat(TestInputVisitorData *data, + const void *unused) +{ + UserDefFlatUnion *tmp = NULL; + Error *err = NULL; + Visitor *v; + + v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); + + visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); + error_free_or_abort(&err); + g_assert(!tmp); +} + +static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, + const void *unused) +{ + UserDefFlatUnion2 *tmp = NULL; + Error *err = NULL; + Visitor *v; + + /* test situation where discriminator field ('enum1' here) is missing */ + v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); + + visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); + error_free_or_abort(&err); + g_assert(!tmp); +} + +static void test_validate_fail_alternate(TestInputVisitorData *data, + const void *unused) +{ + UserDefAlternate *tmp; + Visitor *v; + Error *err = NULL; + + v = validate_test_init(data, "3.14"); + + visit_type_UserDefAlternate(v, NULL, &tmp, &err); + error_free_or_abort(&err); + g_assert(!tmp); +} + +static void do_test_validate_qmp_introspect(TestInputVisitorData *data, + const char *schema_json) +{ + SchemaInfoList *schema = NULL; + Visitor *v; + + v = validate_test_init_raw(data, schema_json); + + visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); + g_assert(schema); + + qapi_free_SchemaInfoList(schema); +} + +static void test_validate_qmp_introspect(TestInputVisitorData *data, + const void *unused) +{ + do_test_validate_qmp_introspect(data, test_qmp_schema_json); + do_test_validate_qmp_introspect(data, qmp_schema_json); +} + +static void validate_test_add(const char *testpath, + TestInputVisitorData *data, + void (*test_func)(TestInputVisitorData *data, const void *user_data)) +{ + g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, + validate_teardown); +} + +int main(int argc, char **argv) +{ + TestInputVisitorData testdata; + + g_test_init(&argc, &argv, NULL); + + validate_test_add("/visitor/input-strict/pass/struct", + &testdata, test_validate_struct); + validate_test_add("/visitor/input-strict/pass/struct-nested", + &testdata, test_validate_struct_nested); + validate_test_add("/visitor/input-strict/pass/list", + &testdata, test_validate_list); + validate_test_add("/visitor/input-strict/pass/union-flat", + &testdata, test_validate_union_flat); + validate_test_add("/visitor/input-strict/pass/alternate", + &testdata, test_validate_alternate); + validate_test_add("/visitor/input-strict/pass/union-native-list", + &testdata, test_validate_union_native_list); + validate_test_add("/visitor/input-strict/fail/struct", + &testdata, test_validate_fail_struct); + validate_test_add("/visitor/input-strict/fail/struct-nested", + &testdata, test_validate_fail_struct_nested); + validate_test_add("/visitor/input-strict/fail/struct-missing", + &testdata, test_validate_fail_struct_missing); + validate_test_add("/visitor/input-strict/fail/list", + &testdata, test_validate_fail_list); + validate_test_add("/visitor/input-strict/fail/union-flat", + &testdata, test_validate_fail_union_flat); + validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator", + &testdata, test_validate_fail_union_flat_no_discrim); + validate_test_add("/visitor/input-strict/fail/alternate", + &testdata, test_validate_fail_alternate); + validate_test_add("/visitor/input-strict/fail/union-native-list", + &testdata, test_validate_fail_union_native_list); + validate_test_add("/visitor/input-strict/pass/qmp-introspect", + &testdata, test_validate_qmp_introspect); + + g_test_run(); + + return 0; +} diff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c new file mode 100644 index 0000000..ced457f --- /dev/null +++ b/tests/test-qobject-input-visitor.c @@ -0,0 +1,911 @@ +/* + * QObject Input Visitor unit-tests. + * + * Copyright (C) 2011-2016 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * 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 "qemu/osdep.h" + +#include "qemu-common.h" +#include "qapi/error.h" +#include "qapi/qobject-input-visitor.h" +#include "test-qapi-types.h" +#include "test-qapi-visit.h" +#include "qapi/qmp/types.h" +#include "qapi/qmp/qjson.h" + +typedef struct TestInputVisitorData { + QObject *obj; + Visitor *qiv; +} TestInputVisitorData; + +static void visitor_input_teardown(TestInputVisitorData *data, + const void *unused) +{ + qobject_decref(data->obj); + data->obj = NULL; + + if (data->qiv) { + visit_free(data->qiv); + data->qiv = NULL; + } +} + +/* The various test_init functions are provided instead of a test setup + function so that the JSON string used by the tests are kept in the test + functions (and not in main()). */ +static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data, + const char *json_string, + va_list *ap) +{ + visitor_input_teardown(data, NULL); + + data->obj = qobject_from_jsonv(json_string, ap); + g_assert(data->obj); + + data->qiv = qmp_input_visitor_new(data->obj, false); + g_assert(data->qiv); + return data->qiv; +} + +static GCC_FMT_ATTR(2, 3) +Visitor *visitor_input_test_init(TestInputVisitorData *data, + const char *json_string, ...) +{ + Visitor *v; + va_list ap; + + va_start(ap, json_string); + v = visitor_input_test_init_internal(data, json_string, &ap); + va_end(ap); + return v; +} + +/* similar to visitor_input_test_init(), but does not expect a string + * literal/format json_string argument and so can be used for + * programatically generated strings (and we can't pass in programatically + * generated strings via %s format parameters since qobject_from_jsonv() + * will wrap those in double-quotes and treat the entire object as a + * string) + */ +static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data, + const char *json_string) +{ + return visitor_input_test_init_internal(data, json_string, NULL); +} + +static void test_visitor_in_int(TestInputVisitorData *data, + const void *unused) +{ + int64_t res = 0, value = -42; + Visitor *v; + + v = visitor_input_test_init(data, "%" PRId64, value); + + visit_type_int(v, NULL, &res, &error_abort); + g_assert_cmpint(res, ==, value); +} + +static void test_visitor_in_int_overflow(TestInputVisitorData *data, + const void *unused) +{ + int64_t res = 0; + Error *err = NULL; + Visitor *v; + + /* this will overflow a Qint/int64, so should be deserialized into + * a QFloat/double field instead, leading to an error if we pass it + * to visit_type_int. confirm this. + */ + v = visitor_input_test_init(data, "%f", DBL_MAX); + + visit_type_int(v, NULL, &res, &err); + error_free_or_abort(&err); +} + +static void test_visitor_in_bool(TestInputVisitorData *data, + const void *unused) +{ + bool res = false; + Visitor *v; + + v = visitor_input_test_init(data, "true"); + + visit_type_bool(v, NULL, &res, &error_abort); + g_assert_cmpint(res, ==, true); +} + +static void test_visitor_in_number(TestInputVisitorData *data, + const void *unused) +{ + double res = 0, value = 3.14; + Visitor *v; + + v = visitor_input_test_init(data, "%f", value); + + visit_type_number(v, NULL, &res, &error_abort); + g_assert_cmpfloat(res, ==, value); +} + +static void test_visitor_in_string(TestInputVisitorData *data, + const void *unused) +{ + char *res = NULL, *value = (char *) "Q E M U"; + Visitor *v; + + v = visitor_input_test_init(data, "%s", value); + + visit_type_str(v, NULL, &res, &error_abort); + g_assert_cmpstr(res, ==, value); + + g_free(res); +} + +static void test_visitor_in_enum(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + EnumOne i; + + for (i = 0; EnumOne_lookup[i]; i++) { + EnumOne res = -1; + + v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]); + + visit_type_EnumOne(v, NULL, &res, &error_abort); + g_assert_cmpint(i, ==, res); + } +} + + +static void test_visitor_in_struct(TestInputVisitorData *data, + const void *unused) +{ + TestStruct *p = NULL; + Visitor *v; + + v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); + + visit_type_TestStruct(v, NULL, &p, &error_abort); + g_assert_cmpint(p->integer, ==, -42); + g_assert(p->boolean == true); + g_assert_cmpstr(p->string, ==, "foo"); + + g_free(p->string); + g_free(p); +} + +static void test_visitor_in_struct_nested(TestInputVisitorData *data, + const void *unused) +{ + UserDefTwo *udp = NULL; + Visitor *v; + + v = visitor_input_test_init(data, "{ 'string0': 'string0', " + "'dict1': { 'string1': 'string1', " + "'dict2': { 'userdef': { 'integer': 42, " + "'string': 'string' }, 'string': 'string2'}}}"); + + visit_type_UserDefTwo(v, NULL, &udp, &error_abort); + + g_assert_cmpstr(udp->string0, ==, "string0"); + g_assert_cmpstr(udp->dict1->string1, ==, "string1"); + g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42); + g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string"); + g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2"); + g_assert(udp->dict1->has_dict3 == false); + + qapi_free_UserDefTwo(udp); +} + +static void test_visitor_in_list(TestInputVisitorData *data, + const void *unused) +{ + UserDefOneList *item, *head = NULL; + Visitor *v; + int i; + + v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); + + visit_type_UserDefOneList(v, NULL, &head, &error_abort); + g_assert(head != NULL); + + for (i = 0, item = head; item; item = item->next, i++) { + char string[12]; + + snprintf(string, sizeof(string), "string%d", i); + g_assert_cmpstr(item->value->string, ==, string); + g_assert_cmpint(item->value->integer, ==, 42 + i); + } + + qapi_free_UserDefOneList(head); + head = NULL; + + /* An empty list is valid */ + v = visitor_input_test_init(data, "[]"); + visit_type_UserDefOneList(v, NULL, &head, &error_abort); + g_assert(!head); +} + +static void test_visitor_in_any(TestInputVisitorData *data, + const void *unused) +{ + QObject *res = NULL; + Visitor *v; + QInt *qint; + QBool *qbool; + QString *qstring; + QDict *qdict; + QObject *qobj; + + v = visitor_input_test_init(data, "-42"); + visit_type_any(v, NULL, &res, &error_abort); + qint = qobject_to_qint(res); + g_assert(qint); + g_assert_cmpint(qint_get_int(qint), ==, -42); + qobject_decref(res); + + v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); + visit_type_any(v, NULL, &res, &error_abort); + qdict = qobject_to_qdict(res); + g_assert(qdict && qdict_size(qdict) == 3); + qobj = qdict_get(qdict, "integer"); + g_assert(qobj); + qint = qobject_to_qint(qobj); + g_assert(qint); + g_assert_cmpint(qint_get_int(qint), ==, -42); + qobj = qdict_get(qdict, "boolean"); + g_assert(qobj); + qbool = qobject_to_qbool(qobj); + g_assert(qbool); + g_assert(qbool_get_bool(qbool) == true); + qobj = qdict_get(qdict, "string"); + g_assert(qobj); + qstring = qobject_to_qstring(qobj); + g_assert(qstring); + g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); + qobject_decref(res); +} + +static void test_visitor_in_null(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + Error *err = NULL; + char *tmp; + + /* + * FIXME: Since QAPI doesn't know the 'null' type yet, we can't + * test visit_type_null() by reading into a QAPI struct then + * checking that it was populated correctly. The best we can do + * for now is ensure that we consumed null from the input, proven + * by the fact that we can't re-read the key; and that we detect + * when input is not null. + */ + + v = visitor_input_test_init(data, "{ 'a': null, 'b': '' }"); + visit_start_struct(v, NULL, NULL, 0, &error_abort); + visit_type_null(v, "a", &error_abort); + visit_type_str(v, "a", &tmp, &err); + g_assert(!tmp); + error_free_or_abort(&err); + visit_type_null(v, "b", &err); + error_free_or_abort(&err); + visit_check_struct(v, &error_abort); + visit_end_struct(v, NULL); +} + +static void test_visitor_in_union_flat(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + UserDefFlatUnion *tmp; + UserDefUnionBase *base; + + v = visitor_input_test_init(data, + "{ 'enum1': 'value1', " + "'integer': 41, " + "'string': 'str', " + "'boolean': true }"); + + visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); + g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1); + g_assert_cmpstr(tmp->string, ==, "str"); + g_assert_cmpint(tmp->integer, ==, 41); + g_assert_cmpint(tmp->u.value1.boolean, ==, true); + + base = qapi_UserDefFlatUnion_base(tmp); + g_assert(&base->enum1 == &tmp->enum1); + + qapi_free_UserDefFlatUnion(tmp); +} + +static void test_visitor_in_alternate(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + Error *err = NULL; + UserDefAlternate *tmp; + WrapAlternate *wrap; + + v = visitor_input_test_init(data, "42"); + visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); + g_assert_cmpint(tmp->type, ==, QTYPE_QINT); + g_assert_cmpint(tmp->u.i, ==, 42); + qapi_free_UserDefAlternate(tmp); + + v = visitor_input_test_init(data, "'string'"); + visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); + g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING); + g_assert_cmpstr(tmp->u.s, ==, "string"); + qapi_free_UserDefAlternate(tmp); + + v = visitor_input_test_init(data, "{'integer':1, 'string':'str', " + "'enum1':'value1', 'boolean':true}"); + visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); + g_assert_cmpint(tmp->type, ==, QTYPE_QDICT); + g_assert_cmpint(tmp->u.udfu.integer, ==, 1); + g_assert_cmpstr(tmp->u.udfu.string, ==, "str"); + g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1); + g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true); + g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false); + qapi_free_UserDefAlternate(tmp); + + v = visitor_input_test_init(data, "false"); + visit_type_UserDefAlternate(v, NULL, &tmp, &err); + error_free_or_abort(&err); + qapi_free_UserDefAlternate(tmp); + + v = visitor_input_test_init(data, "{ 'alt': 42 }"); + visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); + g_assert_cmpint(wrap->alt->type, ==, QTYPE_QINT); + g_assert_cmpint(wrap->alt->u.i, ==, 42); + qapi_free_WrapAlternate(wrap); + + v = visitor_input_test_init(data, "{ 'alt': 'string' }"); + visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); + g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING); + g_assert_cmpstr(wrap->alt->u.s, ==, "string"); + qapi_free_WrapAlternate(wrap); + + v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', " + "'enum1':'value1', 'boolean':true} }"); + visit_type_WrapAlternate(v, NULL, &wrap, &error_abort); + g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT); + g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1); + g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str"); + g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1); + g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true); + g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false); + qapi_free_WrapAlternate(wrap); +} + +static void test_visitor_in_alternate_number(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + Error *err = NULL; + AltStrBool *asb; + AltStrNum *asn; + AltNumStr *ans; + AltStrInt *asi; + AltIntNum *ain; + AltNumInt *ani; + + /* Parsing an int */ + + v = visitor_input_test_init(data, "42"); + visit_type_AltStrBool(v, NULL, &asb, &err); + error_free_or_abort(&err); + qapi_free_AltStrBool(asb); + + v = visitor_input_test_init(data, "42"); + visit_type_AltStrNum(v, NULL, &asn, &error_abort); + g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(asn->u.n, ==, 42); + qapi_free_AltStrNum(asn); + + v = visitor_input_test_init(data, "42"); + visit_type_AltNumStr(v, NULL, &ans, &error_abort); + g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(ans->u.n, ==, 42); + qapi_free_AltNumStr(ans); + + v = visitor_input_test_init(data, "42"); + visit_type_AltStrInt(v, NULL, &asi, &error_abort); + g_assert_cmpint(asi->type, ==, QTYPE_QINT); + g_assert_cmpint(asi->u.i, ==, 42); + qapi_free_AltStrInt(asi); + + v = visitor_input_test_init(data, "42"); + visit_type_AltIntNum(v, NULL, &ain, &error_abort); + g_assert_cmpint(ain->type, ==, QTYPE_QINT); + g_assert_cmpint(ain->u.i, ==, 42); + qapi_free_AltIntNum(ain); + + v = visitor_input_test_init(data, "42"); + visit_type_AltNumInt(v, NULL, &ani, &error_abort); + g_assert_cmpint(ani->type, ==, QTYPE_QINT); + g_assert_cmpint(ani->u.i, ==, 42); + qapi_free_AltNumInt(ani); + + /* Parsing a double */ + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltStrBool(v, NULL, &asb, &err); + error_free_or_abort(&err); + qapi_free_AltStrBool(asb); + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltStrNum(v, NULL, &asn, &error_abort); + g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(asn->u.n, ==, 42.5); + qapi_free_AltStrNum(asn); + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltNumStr(v, NULL, &ans, &error_abort); + g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(ans->u.n, ==, 42.5); + qapi_free_AltNumStr(ans); + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltStrInt(v, NULL, &asi, &err); + error_free_or_abort(&err); + qapi_free_AltStrInt(asi); + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltIntNum(v, NULL, &ain, &error_abort); + g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(ain->u.n, ==, 42.5); + qapi_free_AltIntNum(ain); + + v = visitor_input_test_init(data, "42.5"); + visit_type_AltNumInt(v, NULL, &ani, &error_abort); + g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT); + g_assert_cmpfloat(ani->u.n, ==, 42.5); + qapi_free_AltNumInt(ani); +} + +static void test_native_list_integer_helper(TestInputVisitorData *data, + const void *unused, + UserDefNativeListUnionKind kind) +{ + UserDefNativeListUnion *cvalue = NULL; + Visitor *v; + GString *gstr_list = g_string_new(""); + GString *gstr_union = g_string_new(""); + int i; + + for (i = 0; i < 32; i++) { + g_string_append_printf(gstr_list, "%d", i); + if (i != 31) { + g_string_append(gstr_list, ", "); + } + } + g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }", + UserDefNativeListUnionKind_lookup[kind], + gstr_list->str); + v = visitor_input_test_init_raw(data, gstr_union->str); + + visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); + g_assert(cvalue != NULL); + g_assert_cmpint(cvalue->type, ==, kind); + + switch (kind) { + case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { + intList *elem = NULL; + for (i = 0, elem = cvalue->u.integer.data; + elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { + int8List *elem = NULL; + for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { + int16List *elem = NULL; + for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { + int32List *elem = NULL; + for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { + int64List *elem = NULL; + for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { + uint8List *elem = NULL; + for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { + uint16List *elem = NULL; + for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { + uint32List *elem = NULL; + for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { + uint64List *elem = NULL; + for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, i); + } + break; + } + default: + g_assert_not_reached(); + } + + g_string_free(gstr_union, true); + g_string_free(gstr_list, true); + qapi_free_UserDefNativeListUnion(cvalue); +} + +static void test_visitor_in_native_list_int(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); +} + +static void test_visitor_in_native_list_int8(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_S8); +} + +static void test_visitor_in_native_list_int16(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_S16); +} + +static void test_visitor_in_native_list_int32(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_S32); +} + +static void test_visitor_in_native_list_int64(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_S64); +} + +static void test_visitor_in_native_list_uint8(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_U8); +} + +static void test_visitor_in_native_list_uint16(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_U16); +} + +static void test_visitor_in_native_list_uint32(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_U32); +} + +static void test_visitor_in_native_list_uint64(TestInputVisitorData *data, + const void *unused) +{ + test_native_list_integer_helper(data, unused, + USER_DEF_NATIVE_LIST_UNION_KIND_U64); +} + +static void test_visitor_in_native_list_bool(TestInputVisitorData *data, + const void *unused) +{ + UserDefNativeListUnion *cvalue = NULL; + boolList *elem = NULL; + Visitor *v; + GString *gstr_list = g_string_new(""); + GString *gstr_union = g_string_new(""); + int i; + + for (i = 0; i < 32; i++) { + g_string_append_printf(gstr_list, "%s", + (i % 3 == 0) ? "true" : "false"); + if (i != 31) { + g_string_append(gstr_list, ", "); + } + } + g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }", + gstr_list->str); + v = visitor_input_test_init_raw(data, gstr_union->str); + + visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); + g_assert(cvalue != NULL); + g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); + + for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) { + g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); + } + + g_string_free(gstr_union, true); + g_string_free(gstr_list, true); + qapi_free_UserDefNativeListUnion(cvalue); +} + +static void test_visitor_in_native_list_string(TestInputVisitorData *data, + const void *unused) +{ + UserDefNativeListUnion *cvalue = NULL; + strList *elem = NULL; + Visitor *v; + GString *gstr_list = g_string_new(""); + GString *gstr_union = g_string_new(""); + int i; + + for (i = 0; i < 32; i++) { + g_string_append_printf(gstr_list, "'%d'", i); + if (i != 31) { + g_string_append(gstr_list, ", "); + } + } + g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }", + gstr_list->str); + v = visitor_input_test_init_raw(data, gstr_union->str); + + visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); + g_assert(cvalue != NULL); + g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); + + for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) { + gchar str[8]; + sprintf(str, "%d", i); + g_assert_cmpstr(elem->value, ==, str); + } + + g_string_free(gstr_union, true); + g_string_free(gstr_list, true); + qapi_free_UserDefNativeListUnion(cvalue); +} + +#define DOUBLE_STR_MAX 16 + +static void test_visitor_in_native_list_number(TestInputVisitorData *data, + const void *unused) +{ + UserDefNativeListUnion *cvalue = NULL; + numberList *elem = NULL; + Visitor *v; + GString *gstr_list = g_string_new(""); + GString *gstr_union = g_string_new(""); + int i; + + for (i = 0; i < 32; i++) { + g_string_append_printf(gstr_list, "%f", (double)i / 3); + if (i != 31) { + g_string_append(gstr_list, ", "); + } + } + g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }", + gstr_list->str); + v = visitor_input_test_init_raw(data, gstr_union->str); + + visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort); + g_assert(cvalue != NULL); + g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); + + for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) { + GString *double_expected = g_string_new(""); + GString *double_actual = g_string_new(""); + + g_string_printf(double_expected, "%.6f", (double)i / 3); + g_string_printf(double_actual, "%.6f", elem->value); + g_assert_cmpstr(double_expected->str, ==, double_actual->str); + + g_string_free(double_expected, true); + g_string_free(double_actual, true); + } + + g_string_free(gstr_union, true); + g_string_free(gstr_list, true); + qapi_free_UserDefNativeListUnion(cvalue); +} + +static void input_visitor_test_add(const char *testpath, + TestInputVisitorData *data, + void (*test_func)(TestInputVisitorData *data, const void *user_data)) +{ + g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, + visitor_input_teardown); +} + +static void test_visitor_in_errors(TestInputVisitorData *data, + const void *unused) +{ + TestStruct *p = NULL; + Error *err = NULL; + Visitor *v; + strList *q = NULL; + UserDefTwo *r = NULL; + WrapAlternate *s = NULL; + + v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', " + "'string': -42 }"); + + visit_type_TestStruct(v, NULL, &p, &err); + error_free_or_abort(&err); + g_assert(!p); + + v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]"); + visit_type_strList(v, NULL, &q, &err); + error_free_or_abort(&err); + assert(!q); + + v = visitor_input_test_init(data, "{ 'str':'hi' }"); + visit_type_UserDefTwo(v, NULL, &r, &err); + error_free_or_abort(&err); + assert(!r); + + v = visitor_input_test_init(data, "{ }"); + visit_type_WrapAlternate(v, NULL, &s, &err); + error_free_or_abort(&err); + assert(!s); +} + +static void test_visitor_in_wrong_type(TestInputVisitorData *data, + const void *unused) +{ + TestStruct *p = NULL; + Visitor *v; + strList *q = NULL; + int64_t i; + Error *err = NULL; + + /* Make sure arrays and structs cannot be confused */ + + v = visitor_input_test_init(data, "[]"); + visit_type_TestStruct(v, NULL, &p, &err); + error_free_or_abort(&err); + g_assert(!p); + + v = visitor_input_test_init(data, "{}"); + visit_type_strList(v, NULL, &q, &err); + error_free_or_abort(&err); + assert(!q); + + /* Make sure primitives and struct cannot be confused */ + + v = visitor_input_test_init(data, "1"); + visit_type_TestStruct(v, NULL, &p, &err); + error_free_or_abort(&err); + g_assert(!p); + + v = visitor_input_test_init(data, "{}"); + visit_type_int(v, NULL, &i, &err); + error_free_or_abort(&err); + + /* Make sure primitives and arrays cannot be confused */ + + v = visitor_input_test_init(data, "1"); + visit_type_strList(v, NULL, &q, &err); + error_free_or_abort(&err); + assert(!q); + + v = visitor_input_test_init(data, "[]"); + visit_type_int(v, NULL, &i, &err); + error_free_or_abort(&err); +} + +int main(int argc, char **argv) +{ + TestInputVisitorData in_visitor_data; + + g_test_init(&argc, &argv, NULL); + + input_visitor_test_add("/visitor/input/int", + &in_visitor_data, test_visitor_in_int); + input_visitor_test_add("/visitor/input/int_overflow", + &in_visitor_data, test_visitor_in_int_overflow); + input_visitor_test_add("/visitor/input/bool", + &in_visitor_data, test_visitor_in_bool); + input_visitor_test_add("/visitor/input/number", + &in_visitor_data, test_visitor_in_number); + input_visitor_test_add("/visitor/input/string", + &in_visitor_data, test_visitor_in_string); + input_visitor_test_add("/visitor/input/enum", + &in_visitor_data, test_visitor_in_enum); + input_visitor_test_add("/visitor/input/struct", + &in_visitor_data, test_visitor_in_struct); + input_visitor_test_add("/visitor/input/struct-nested", + &in_visitor_data, test_visitor_in_struct_nested); + input_visitor_test_add("/visitor/input/list", + &in_visitor_data, test_visitor_in_list); + input_visitor_test_add("/visitor/input/any", + &in_visitor_data, test_visitor_in_any); + input_visitor_test_add("/visitor/input/null", + &in_visitor_data, test_visitor_in_null); + input_visitor_test_add("/visitor/input/union-flat", + &in_visitor_data, test_visitor_in_union_flat); + input_visitor_test_add("/visitor/input/alternate", + &in_visitor_data, test_visitor_in_alternate); + input_visitor_test_add("/visitor/input/errors", + &in_visitor_data, test_visitor_in_errors); + input_visitor_test_add("/visitor/input/wrong-type", + &in_visitor_data, test_visitor_in_wrong_type); + input_visitor_test_add("/visitor/input/alternate-number", + &in_visitor_data, test_visitor_in_alternate_number); + input_visitor_test_add("/visitor/input/native_list/int", + &in_visitor_data, + test_visitor_in_native_list_int); + input_visitor_test_add("/visitor/input/native_list/int8", + &in_visitor_data, + test_visitor_in_native_list_int8); + input_visitor_test_add("/visitor/input/native_list/int16", + &in_visitor_data, + test_visitor_in_native_list_int16); + input_visitor_test_add("/visitor/input/native_list/int32", + &in_visitor_data, + test_visitor_in_native_list_int32); + input_visitor_test_add("/visitor/input/native_list/int64", + &in_visitor_data, + test_visitor_in_native_list_int64); + input_visitor_test_add("/visitor/input/native_list/uint8", + &in_visitor_data, + test_visitor_in_native_list_uint8); + input_visitor_test_add("/visitor/input/native_list/uint16", + &in_visitor_data, + test_visitor_in_native_list_uint16); + input_visitor_test_add("/visitor/input/native_list/uint32", + &in_visitor_data, + test_visitor_in_native_list_uint32); + input_visitor_test_add("/visitor/input/native_list/uint64", + &in_visitor_data, + test_visitor_in_native_list_uint64); + input_visitor_test_add("/visitor/input/native_list/bool", + &in_visitor_data, test_visitor_in_native_list_bool); + input_visitor_test_add("/visitor/input/native_list/str", + &in_visitor_data, + test_visitor_in_native_list_string); + input_visitor_test_add("/visitor/input/native_list/number", + &in_visitor_data, + test_visitor_in_native_list_number); + + g_test_run(); + + return 0; +} diff --git a/tests/test-qobject-output-visitor.c b/tests/test-qobject-output-visitor.c new file mode 100644 index 0000000..f10d695 --- /dev/null +++ b/tests/test-qobject-output-visitor.c @@ -0,0 +1,876 @@ +/* + * QObject Output Visitor unit-tests. + * + * Copyright (C) 2011-2016 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * 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 "qemu/osdep.h" + +#include "qemu-common.h" +#include "qapi/error.h" +#include "qapi/qobject-output-visitor.h" +#include "test-qapi-types.h" +#include "test-qapi-visit.h" +#include "qapi/qmp/types.h" +#include "qapi/qmp/qjson.h" + +typedef struct TestOutputVisitorData { + Visitor *ov; + QObject *obj; +} TestOutputVisitorData; + +static void visitor_output_setup(TestOutputVisitorData *data, + const void *unused) +{ + data->ov = qmp_output_visitor_new(&data->obj); + g_assert(data->ov); +} + +static void visitor_output_teardown(TestOutputVisitorData *data, + const void *unused) +{ + visit_free(data->ov); + data->ov = NULL; + qobject_decref(data->obj); + data->obj = NULL; +} + +static QObject *visitor_get(TestOutputVisitorData *data) +{ + visit_complete(data->ov, &data->obj); + g_assert(data->obj); + return data->obj; +} + +static void visitor_reset(TestOutputVisitorData *data) +{ + visitor_output_teardown(data, NULL); + visitor_output_setup(data, NULL); +} + +static void test_visitor_out_int(TestOutputVisitorData *data, + const void *unused) +{ + int64_t value = -42; + QObject *obj; + + visit_type_int(data->ov, NULL, &value, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QINT); + g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); +} + +static void test_visitor_out_bool(TestOutputVisitorData *data, + const void *unused) +{ + bool value = true; + QObject *obj; + + visit_type_bool(data->ov, NULL, &value, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QBOOL); + g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value); +} + +static void test_visitor_out_number(TestOutputVisitorData *data, + const void *unused) +{ + double value = 3.14; + QObject *obj; + + visit_type_number(data->ov, NULL, &value, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QFLOAT); + g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value); +} + +static void test_visitor_out_string(TestOutputVisitorData *data, + const void *unused) +{ + char *string = (char *) "Q E M U"; + QObject *obj; + + visit_type_str(data->ov, NULL, &string, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QSTRING); + g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); +} + +static void test_visitor_out_no_string(TestOutputVisitorData *data, + const void *unused) +{ + char *string = NULL; + QObject *obj; + + /* A null string should return "" */ + visit_type_str(data->ov, NULL, &string, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QSTRING); + g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, ""); +} + +static void test_visitor_out_enum(TestOutputVisitorData *data, + const void *unused) +{ + QObject *obj; + EnumOne i; + + for (i = 0; i < ENUM_ONE__MAX; i++) { + visit_type_EnumOne(data->ov, "unused", &i, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QSTRING); + g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, + EnumOne_lookup[i]); + visitor_reset(data); + } +} + +static void test_visitor_out_enum_errors(TestOutputVisitorData *data, + const void *unused) +{ + EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 }; + Error *err; + + for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { + err = NULL; + visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err); + g_assert(err); + error_free(err); + visitor_reset(data); + } +} + + +static void test_visitor_out_struct(TestOutputVisitorData *data, + const void *unused) +{ + TestStruct test_struct = { .integer = 42, + .boolean = false, + .string = (char *) "foo"}; + TestStruct *p = &test_struct; + QObject *obj; + QDict *qdict; + + visit_type_TestStruct(data->ov, NULL, &p, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QDICT); + + qdict = qobject_to_qdict(obj); + g_assert_cmpint(qdict_size(qdict), ==, 3); + g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); + g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false); + g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); +} + +static void test_visitor_out_struct_nested(TestOutputVisitorData *data, + const void *unused) +{ + int64_t value = 42; + UserDefTwo *ud2; + QObject *obj; + QDict *qdict, *dict1, *dict2, *dict3, *userdef; + const char *string = "user def string"; + const char *strings[] = { "forty two", "forty three", "forty four", + "forty five" }; + + ud2 = g_malloc0(sizeof(*ud2)); + ud2->string0 = g_strdup(strings[0]); + + ud2->dict1 = g_malloc0(sizeof(*ud2->dict1)); + ud2->dict1->string1 = g_strdup(strings[1]); + + ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2)); + ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1); + ud2->dict1->dict2->userdef->string = g_strdup(string); + ud2->dict1->dict2->userdef->integer = value; + ud2->dict1->dict2->string = g_strdup(strings[2]); + + ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3)); + ud2->dict1->has_dict3 = true; + ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1); + ud2->dict1->dict3->userdef->string = g_strdup(string); + ud2->dict1->dict3->userdef->integer = value; + ud2->dict1->dict3->string = g_strdup(strings[3]); + + visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QDICT); + + qdict = qobject_to_qdict(obj); + g_assert_cmpint(qdict_size(qdict), ==, 2); + g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]); + + dict1 = qdict_get_qdict(qdict, "dict1"); + g_assert_cmpint(qdict_size(dict1), ==, 3); + g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]); + + dict2 = qdict_get_qdict(dict1, "dict2"); + g_assert_cmpint(qdict_size(dict2), ==, 2); + g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]); + userdef = qdict_get_qdict(dict2, "userdef"); + g_assert_cmpint(qdict_size(userdef), ==, 2); + g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); + g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); + + dict3 = qdict_get_qdict(dict1, "dict3"); + g_assert_cmpint(qdict_size(dict3), ==, 2); + g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]); + userdef = qdict_get_qdict(dict3, "userdef"); + g_assert_cmpint(qdict_size(userdef), ==, 2); + g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value); + g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string); + + qapi_free_UserDefTwo(ud2); +} + +static void test_visitor_out_struct_errors(TestOutputVisitorData *data, + const void *unused) +{ + EnumOne bad_values[] = { ENUM_ONE__MAX, -1 }; + UserDefOne u = {0}; + UserDefOne *pu = &u; + Error *err; + int i; + + for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { + err = NULL; + u.has_enum1 = true; + u.enum1 = bad_values[i]; + visit_type_UserDefOne(data->ov, "unused", &pu, &err); + g_assert(err); + error_free(err); + visitor_reset(data); + } +} + + +static void test_visitor_out_list(TestOutputVisitorData *data, + const void *unused) +{ + const char *value_str = "list value"; + TestStructList *p, *head = NULL; + const int max_items = 10; + bool value_bool = true; + int value_int = 10; + QListEntry *entry; + QObject *obj; + QList *qlist; + int i; + + /* Build the list in reverse order... */ + for (i = 0; i < max_items; i++) { + p = g_malloc0(sizeof(*p)); + p->value = g_malloc0(sizeof(*p->value)); + p->value->integer = value_int + (max_items - i - 1); + p->value->boolean = value_bool; + p->value->string = g_strdup(value_str); + + p->next = head; + head = p; + } + + visit_type_TestStructList(data->ov, NULL, &head, &error_abort); + + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QLIST); + + qlist = qobject_to_qlist(obj); + g_assert(!qlist_empty(qlist)); + + /* ...and ensure that the visitor sees it in order */ + i = 0; + QLIST_FOREACH_ENTRY(qlist, entry) { + QDict *qdict; + + g_assert(qobject_type(entry->value) == QTYPE_QDICT); + qdict = qobject_to_qdict(entry->value); + g_assert_cmpint(qdict_size(qdict), ==, 3); + g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i); + g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool); + g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str); + i++; + } + g_assert_cmpint(i, ==, max_items); + + qapi_free_TestStructList(head); +} + +static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, + const void *unused) +{ + UserDefTwoList *p, *head = NULL; + const char string[] = "foo bar"; + int i, max_count = 1024; + + for (i = 0; i < max_count; i++) { + p = g_malloc0(sizeof(*p)); + p->value = g_malloc0(sizeof(*p->value)); + + p->value->string0 = g_strdup(string); + p->value->dict1 = g_new0(UserDefTwoDict, 1); + p->value->dict1->string1 = g_strdup(string); + p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); + p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1); + p->value->dict1->dict2->userdef->string = g_strdup(string); + p->value->dict1->dict2->userdef->integer = 42; + p->value->dict1->dict2->string = g_strdup(string); + p->value->dict1->has_dict3 = false; + + p->next = head; + head = p; + } + + qapi_free_UserDefTwoList(head); +} + +static void test_visitor_out_any(TestOutputVisitorData *data, + const void *unused) +{ + QObject *qobj; + QInt *qint; + QBool *qbool; + QString *qstring; + QDict *qdict; + QObject *obj; + + qobj = QOBJECT(qint_from_int(-42)); + visit_type_any(data->ov, NULL, &qobj, &error_abort); + obj = visitor_get(data); + g_assert(qobject_type(obj) == QTYPE_QINT); + g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42); + qobject_decref(qobj); + + visitor_reset(data); + qdict = qdict_new(); + qdict_put(qdict, "integer", qint_from_int(-42)); + qdict_put(qdict, "boolean", qbool_from_bool(true)); + qdict_put(qdict, "string", qstring_from_str("foo")); + qobj = QOBJECT(qdict); + visit_type_any(data->ov, NULL, &qobj, &error_abort); + qobject_decref(qobj); + obj = visitor_get(data); + qdict = qobject_to_qdict(obj); + g_assert(qdict); + qobj = qdict_get(qdict, "integer"); + g_assert(qobj); + qint = qobject_to_qint(qobj); + g_assert(qint); + g_assert_cmpint(qint_get_int(qint), ==, -42); + qobj = qdict_get(qdict, "boolean"); + g_assert(qobj); + qbool = qobject_to_qbool(qobj); + g_assert(qbool); + g_assert(qbool_get_bool(qbool) == true); + qobj = qdict_get(qdict, "string"); + g_assert(qobj); + qstring = qobject_to_qstring(qobj); + g_assert(qstring); + g_assert_cmpstr(qstring_get_str(qstring), ==, "foo"); +} + +static void test_visitor_out_union_flat(TestOutputVisitorData *data, + const void *unused) +{ + QObject *arg; + QDict *qdict; + + UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion)); + tmp->enum1 = ENUM_ONE_VALUE1; + tmp->string = g_strdup("str"); + tmp->integer = 41; + tmp->u.value1.boolean = true; + + visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort); + arg = visitor_get(data); + + g_assert(qobject_type(arg) == QTYPE_QDICT); + qdict = qobject_to_qdict(arg); + + g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1"); + g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str"); + g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); + g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); + + qapi_free_UserDefFlatUnion(tmp); +} + +static void test_visitor_out_alternate(TestOutputVisitorData *data, + const void *unused) +{ + QObject *arg; + UserDefAlternate *tmp; + QDict *qdict; + + tmp = g_new0(UserDefAlternate, 1); + tmp->type = QTYPE_QINT; + tmp->u.i = 42; + + visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); + arg = visitor_get(data); + + g_assert(qobject_type(arg) == QTYPE_QINT); + g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42); + + qapi_free_UserDefAlternate(tmp); + + visitor_reset(data); + tmp = g_new0(UserDefAlternate, 1); + tmp->type = QTYPE_QSTRING; + tmp->u.s = g_strdup("hello"); + + visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); + arg = visitor_get(data); + + g_assert(qobject_type(arg) == QTYPE_QSTRING); + g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello"); + + qapi_free_UserDefAlternate(tmp); + + visitor_reset(data); + tmp = g_new0(UserDefAlternate, 1); + tmp->type = QTYPE_QDICT; + tmp->u.udfu.integer = 1; + tmp->u.udfu.string = g_strdup("str"); + tmp->u.udfu.enum1 = ENUM_ONE_VALUE1; + tmp->u.udfu.u.value1.boolean = true; + + visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort); + arg = visitor_get(data); + + g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT); + qdict = qobject_to_qdict(arg); + g_assert_cmpint(qdict_size(qdict), ==, 4); + g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1); + g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str"); + g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1"); + g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true); + + qapi_free_UserDefAlternate(tmp); +} + +static void test_visitor_out_null(TestOutputVisitorData *data, + const void *unused) +{ + QObject *arg; + QDict *qdict; + QObject *nil; + + visit_start_struct(data->ov, NULL, NULL, 0, &error_abort); + visit_type_null(data->ov, "a", &error_abort); + visit_check_struct(data->ov, &error_abort); + visit_end_struct(data->ov, NULL); + arg = visitor_get(data); + g_assert(qobject_type(arg) == QTYPE_QDICT); + qdict = qobject_to_qdict(arg); + g_assert_cmpint(qdict_size(qdict), ==, 1); + nil = qdict_get(qdict, "a"); + g_assert(nil); + g_assert(qobject_type(nil) == QTYPE_QNULL); +} + +static void init_native_list(UserDefNativeListUnion *cvalue) +{ + int i; + switch (cvalue->type) { + case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { + intList **list = &cvalue->u.integer.data; + for (i = 0; i < 32; i++) { + *list = g_new0(intList, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { + int8List **list = &cvalue->u.s8.data; + for (i = 0; i < 32; i++) { + *list = g_new0(int8List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { + int16List **list = &cvalue->u.s16.data; + for (i = 0; i < 32; i++) { + *list = g_new0(int16List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { + int32List **list = &cvalue->u.s32.data; + for (i = 0; i < 32; i++) { + *list = g_new0(int32List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { + int64List **list = &cvalue->u.s64.data; + for (i = 0; i < 32; i++) { + *list = g_new0(int64List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { + uint8List **list = &cvalue->u.u8.data; + for (i = 0; i < 32; i++) { + *list = g_new0(uint8List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { + uint16List **list = &cvalue->u.u16.data; + for (i = 0; i < 32; i++) { + *list = g_new0(uint16List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { + uint32List **list = &cvalue->u.u32.data; + for (i = 0; i < 32; i++) { + *list = g_new0(uint32List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { + uint64List **list = &cvalue->u.u64.data; + for (i = 0; i < 32; i++) { + *list = g_new0(uint64List, 1); + (*list)->value = i; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: { + boolList **list = &cvalue->u.boolean.data; + for (i = 0; i < 32; i++) { + *list = g_new0(boolList, 1); + (*list)->value = (i % 3 == 0); + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: { + strList **list = &cvalue->u.string.data; + for (i = 0; i < 32; i++) { + *list = g_new0(strList, 1); + (*list)->value = g_strdup_printf("%d", i); + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: { + numberList **list = &cvalue->u.number.data; + for (i = 0; i < 32; i++) { + *list = g_new0(numberList, 1); + (*list)->value = (double)i / 3; + (*list)->next = NULL; + list = &(*list)->next; + } + break; + } + default: + g_assert_not_reached(); + } +} + +static void check_native_list(QObject *qobj, + UserDefNativeListUnionKind kind) +{ + QDict *qdict; + QList *qlist; + int i; + + g_assert(qobj); + g_assert(qobject_type(qobj) == QTYPE_QDICT); + qdict = qobject_to_qdict(qobj); + g_assert(qdict); + g_assert(qdict_haskey(qdict, "data")); + qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data"))); + + switch (kind) { + case USER_DEF_NATIVE_LIST_UNION_KIND_S8: + case USER_DEF_NATIVE_LIST_UNION_KIND_S16: + case USER_DEF_NATIVE_LIST_UNION_KIND_S32: + case USER_DEF_NATIVE_LIST_UNION_KIND_S64: + case USER_DEF_NATIVE_LIST_UNION_KIND_U8: + case USER_DEF_NATIVE_LIST_UNION_KIND_U16: + case USER_DEF_NATIVE_LIST_UNION_KIND_U32: + case USER_DEF_NATIVE_LIST_UNION_KIND_U64: + /* all integer elements in JSON arrays get stored into QInts when + * we convert to QObjects, so we can check them all in the same + * fashion, so simply fall through here + */ + case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: + for (i = 0; i < 32; i++) { + QObject *tmp; + QInt *qvalue; + tmp = qlist_peek(qlist); + g_assert(tmp); + qvalue = qobject_to_qint(tmp); + g_assert_cmpint(qint_get_int(qvalue), ==, i); + qobject_decref(qlist_pop(qlist)); + } + break; + case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: + for (i = 0; i < 32; i++) { + QObject *tmp; + QBool *qvalue; + tmp = qlist_peek(qlist); + g_assert(tmp); + qvalue = qobject_to_qbool(tmp); + g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0); + qobject_decref(qlist_pop(qlist)); + } + break; + case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: + for (i = 0; i < 32; i++) { + QObject *tmp; + QString *qvalue; + gchar str[8]; + tmp = qlist_peek(qlist); + g_assert(tmp); + qvalue = qobject_to_qstring(tmp); + sprintf(str, "%d", i); + g_assert_cmpstr(qstring_get_str(qvalue), ==, str); + qobject_decref(qlist_pop(qlist)); + } + break; + case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: + for (i = 0; i < 32; i++) { + QObject *tmp; + QFloat *qvalue; + GString *double_expected = g_string_new(""); + GString *double_actual = g_string_new(""); + + tmp = qlist_peek(qlist); + g_assert(tmp); + qvalue = qobject_to_qfloat(tmp); + g_string_printf(double_expected, "%.6f", (double)i / 3); + g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue)); + g_assert_cmpstr(double_actual->str, ==, double_expected->str); + + qobject_decref(qlist_pop(qlist)); + g_string_free(double_expected, true); + g_string_free(double_actual, true); + } + break; + default: + g_assert_not_reached(); + } + QDECREF(qlist); +} + +static void test_native_list(TestOutputVisitorData *data, + const void *unused, + UserDefNativeListUnionKind kind) +{ + UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1); + QObject *obj; + + cvalue->type = kind; + init_native_list(cvalue); + + visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort); + + obj = visitor_get(data); + check_native_list(obj, cvalue->type); + qapi_free_UserDefNativeListUnion(cvalue); +} + +static void test_visitor_out_native_list_int(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); +} + +static void test_visitor_out_native_list_int8(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8); +} + +static void test_visitor_out_native_list_int16(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16); +} + +static void test_visitor_out_native_list_int32(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32); +} + +static void test_visitor_out_native_list_int64(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64); +} + +static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8); +} + +static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16); +} + +static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32); +} + +static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64); +} + +static void test_visitor_out_native_list_bool(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); +} + +static void test_visitor_out_native_list_str(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); +} + +static void test_visitor_out_native_list_number(TestOutputVisitorData *data, + const void *unused) +{ + test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); +} + +static void output_visitor_test_add(const char *testpath, + TestOutputVisitorData *data, + void (*test_func)(TestOutputVisitorData *data, const void *user_data)) +{ + g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup, + test_func, visitor_output_teardown); +} + +int main(int argc, char **argv) +{ + TestOutputVisitorData out_visitor_data; + + g_test_init(&argc, &argv, NULL); + + output_visitor_test_add("/visitor/output/int", + &out_visitor_data, test_visitor_out_int); + output_visitor_test_add("/visitor/output/bool", + &out_visitor_data, test_visitor_out_bool); + output_visitor_test_add("/visitor/output/number", + &out_visitor_data, test_visitor_out_number); + output_visitor_test_add("/visitor/output/string", + &out_visitor_data, test_visitor_out_string); + output_visitor_test_add("/visitor/output/no-string", + &out_visitor_data, test_visitor_out_no_string); + output_visitor_test_add("/visitor/output/enum", + &out_visitor_data, test_visitor_out_enum); + output_visitor_test_add("/visitor/output/enum-errors", + &out_visitor_data, test_visitor_out_enum_errors); + output_visitor_test_add("/visitor/output/struct", + &out_visitor_data, test_visitor_out_struct); + output_visitor_test_add("/visitor/output/struct-nested", + &out_visitor_data, test_visitor_out_struct_nested); + output_visitor_test_add("/visitor/output/struct-errors", + &out_visitor_data, test_visitor_out_struct_errors); + output_visitor_test_add("/visitor/output/list", + &out_visitor_data, test_visitor_out_list); + output_visitor_test_add("/visitor/output/any", + &out_visitor_data, test_visitor_out_any); + output_visitor_test_add("/visitor/output/list-qapi-free", + &out_visitor_data, test_visitor_out_list_qapi_free); + output_visitor_test_add("/visitor/output/union-flat", + &out_visitor_data, test_visitor_out_union_flat); + output_visitor_test_add("/visitor/output/alternate", + &out_visitor_data, test_visitor_out_alternate); + output_visitor_test_add("/visitor/output/null", + &out_visitor_data, test_visitor_out_null); + output_visitor_test_add("/visitor/output/native_list/int", + &out_visitor_data, + test_visitor_out_native_list_int); + output_visitor_test_add("/visitor/output/native_list/int8", + &out_visitor_data, + test_visitor_out_native_list_int8); + output_visitor_test_add("/visitor/output/native_list/int16", + &out_visitor_data, + test_visitor_out_native_list_int16); + output_visitor_test_add("/visitor/output/native_list/int32", + &out_visitor_data, + test_visitor_out_native_list_int32); + output_visitor_test_add("/visitor/output/native_list/int64", + &out_visitor_data, + test_visitor_out_native_list_int64); + output_visitor_test_add("/visitor/output/native_list/uint8", + &out_visitor_data, + test_visitor_out_native_list_uint8); + output_visitor_test_add("/visitor/output/native_list/uint16", + &out_visitor_data, + test_visitor_out_native_list_uint16); + output_visitor_test_add("/visitor/output/native_list/uint32", + &out_visitor_data, + test_visitor_out_native_list_uint32); + output_visitor_test_add("/visitor/output/native_list/uint64", + &out_visitor_data, + test_visitor_out_native_list_uint64); + output_visitor_test_add("/visitor/output/native_list/bool", + &out_visitor_data, + test_visitor_out_native_list_bool); + output_visitor_test_add("/visitor/output/native_list/string", + &out_visitor_data, + test_visitor_out_native_list_str); + output_visitor_test_add("/visitor/output/native_list/number", + &out_visitor_data, + test_visitor_out_native_list_number); + + g_test_run(); + + return 0; +} diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-visitor.c index a679fbc..7f10e25 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -4,7 +4,7 @@ * Copyright (C) 2012 Red Hat Inc. * * Authors: - * Paolo Bonzini (based on test-qmp-input-visitor) + * Paolo Bonzini (based on test-qobject-input-visitor) * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c index 444844a..e736db3 100644 --- a/tests/test-string-output-visitor.c +++ b/tests/test-string-output-visitor.c @@ -4,7 +4,7 @@ * Copyright (C) 2012 Red Hat Inc. * * Authors: - * Paolo Bonzini (based on test-qmp-output-visitor) + * Paolo Bonzini (based on test-qobject-output-visitor) * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index dba4670..7e5805f 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -20,8 +20,8 @@ #include "qapi/error.h" #include "qapi/qmp/types.h" #include "qapi/qmp/qjson.h" -#include "qapi/qmp-input-visitor.h" -#include "qapi/qmp-output-visitor.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qobject-output-visitor.h" #include "qapi/string-input-visitor.h" #include "qapi/string-output-visitor.h" #include "qapi-types.h" -- cgit v1.1