3f66f764ee
By using &error_abort, we can avoid a local err variable in situations where we expect success. It also has the nice effect that if the test breaks, the error message from error_abort tends to be nicer than that of g_assert(). This patch has an additional bonus of fixing several call sites that were passing &err to two different functions without checking it in between. In general that is unsafe practice; because if the first function sets an error, the second function could abort() if it tries to set a different error. We got away with it because we were asserting that err was NULL through the entire chain, but switching to &error_abort avoids the questionable practice up front. Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <1446791754-23823-7-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
1189 lines
33 KiB
C
1189 lines
33 KiB
C
/*
|
|
* Unit-tests for visitor-based serialization
|
|
*
|
|
* Copyright (C) 2014-2015 Red Hat, Inc.
|
|
* Copyright IBM, Corp. 2012
|
|
*
|
|
* Authors:
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
* See the COPYING file in the top-level directory.
|
|
*/
|
|
|
|
#include <glib.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <float.h>
|
|
|
|
#include "qemu-common.h"
|
|
#include "test-qapi-types.h"
|
|
#include "test-qapi-visit.h"
|
|
#include "qapi/qmp/types.h"
|
|
#include "qapi/qmp-input-visitor.h"
|
|
#include "qapi/qmp-output-visitor.h"
|
|
#include "qapi/string-input-visitor.h"
|
|
#include "qapi/string-output-visitor.h"
|
|
#include "qapi-types.h"
|
|
#include "qapi-visit.h"
|
|
#include "qapi/dealloc-visitor.h"
|
|
|
|
enum PrimitiveTypeKind {
|
|
PTYPE_STRING = 0,
|
|
PTYPE_BOOLEAN,
|
|
PTYPE_NUMBER,
|
|
PTYPE_INTEGER,
|
|
PTYPE_U8,
|
|
PTYPE_U16,
|
|
PTYPE_U32,
|
|
PTYPE_U64,
|
|
PTYPE_S8,
|
|
PTYPE_S16,
|
|
PTYPE_S32,
|
|
PTYPE_S64,
|
|
PTYPE_EOL,
|
|
};
|
|
|
|
typedef struct PrimitiveType {
|
|
union {
|
|
const char *string;
|
|
bool boolean;
|
|
double number;
|
|
int64_t integer;
|
|
uint8_t u8;
|
|
uint16_t u16;
|
|
uint32_t u32;
|
|
uint64_t u64;
|
|
int8_t s8;
|
|
int16_t s16;
|
|
int32_t s32;
|
|
int64_t s64;
|
|
intmax_t max;
|
|
} value;
|
|
enum PrimitiveTypeKind type;
|
|
const char *description;
|
|
} PrimitiveType;
|
|
|
|
typedef struct PrimitiveList {
|
|
union {
|
|
strList *strings;
|
|
boolList *booleans;
|
|
numberList *numbers;
|
|
intList *integers;
|
|
int8List *s8_integers;
|
|
int16List *s16_integers;
|
|
int32List *s32_integers;
|
|
int64List *s64_integers;
|
|
uint8List *u8_integers;
|
|
uint16List *u16_integers;
|
|
uint32List *u32_integers;
|
|
uint64List *u64_integers;
|
|
} value;
|
|
enum PrimitiveTypeKind type;
|
|
const char *description;
|
|
} PrimitiveList;
|
|
|
|
/* test helpers */
|
|
|
|
typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp);
|
|
|
|
static void dealloc_helper(void *native_in, VisitorFunc visit, Error **errp)
|
|
{
|
|
QapiDeallocVisitor *qdv = qapi_dealloc_visitor_new();
|
|
|
|
visit(qapi_dealloc_get_visitor(qdv), &native_in, errp);
|
|
|
|
qapi_dealloc_visitor_cleanup(qdv);
|
|
}
|
|
|
|
static void visit_primitive_type(Visitor *v, void **native, Error **errp)
|
|
{
|
|
PrimitiveType *pt = *native;
|
|
switch(pt->type) {
|
|
case PTYPE_STRING:
|
|
visit_type_str(v, (char **)&pt->value.string, NULL, errp);
|
|
break;
|
|
case PTYPE_BOOLEAN:
|
|
visit_type_bool(v, &pt->value.boolean, NULL, errp);
|
|
break;
|
|
case PTYPE_NUMBER:
|
|
visit_type_number(v, &pt->value.number, NULL, errp);
|
|
break;
|
|
case PTYPE_INTEGER:
|
|
visit_type_int(v, &pt->value.integer, NULL, errp);
|
|
break;
|
|
case PTYPE_U8:
|
|
visit_type_uint8(v, &pt->value.u8, NULL, errp);
|
|
break;
|
|
case PTYPE_U16:
|
|
visit_type_uint16(v, &pt->value.u16, NULL, errp);
|
|
break;
|
|
case PTYPE_U32:
|
|
visit_type_uint32(v, &pt->value.u32, NULL, errp);
|
|
break;
|
|
case PTYPE_U64:
|
|
visit_type_uint64(v, &pt->value.u64, NULL, errp);
|
|
break;
|
|
case PTYPE_S8:
|
|
visit_type_int8(v, &pt->value.s8, NULL, errp);
|
|
break;
|
|
case PTYPE_S16:
|
|
visit_type_int16(v, &pt->value.s16, NULL, errp);
|
|
break;
|
|
case PTYPE_S32:
|
|
visit_type_int32(v, &pt->value.s32, NULL, errp);
|
|
break;
|
|
case PTYPE_S64:
|
|
visit_type_int64(v, &pt->value.s64, NULL, errp);
|
|
break;
|
|
case PTYPE_EOL:
|
|
g_assert_not_reached();
|
|
}
|
|
}
|
|
|
|
static void visit_primitive_list(Visitor *v, void **native, Error **errp)
|
|
{
|
|
PrimitiveList *pl = *native;
|
|
switch (pl->type) {
|
|
case PTYPE_STRING:
|
|
visit_type_strList(v, &pl->value.strings, NULL, errp);
|
|
break;
|
|
case PTYPE_BOOLEAN:
|
|
visit_type_boolList(v, &pl->value.booleans, NULL, errp);
|
|
break;
|
|
case PTYPE_NUMBER:
|
|
visit_type_numberList(v, &pl->value.numbers, NULL, errp);
|
|
break;
|
|
case PTYPE_INTEGER:
|
|
visit_type_intList(v, &pl->value.integers, NULL, errp);
|
|
break;
|
|
case PTYPE_S8:
|
|
visit_type_int8List(v, &pl->value.s8_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_S16:
|
|
visit_type_int16List(v, &pl->value.s16_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_S32:
|
|
visit_type_int32List(v, &pl->value.s32_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_S64:
|
|
visit_type_int64List(v, &pl->value.s64_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_U8:
|
|
visit_type_uint8List(v, &pl->value.u8_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_U16:
|
|
visit_type_uint16List(v, &pl->value.u16_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_U32:
|
|
visit_type_uint32List(v, &pl->value.u32_integers, NULL, errp);
|
|
break;
|
|
case PTYPE_U64:
|
|
visit_type_uint64List(v, &pl->value.u64_integers, NULL, errp);
|
|
break;
|
|
default:
|
|
g_assert_not_reached();
|
|
}
|
|
}
|
|
|
|
|
|
static TestStruct *struct_create(void)
|
|
{
|
|
TestStruct *ts = g_malloc0(sizeof(*ts));
|
|
ts->integer = -42;
|
|
ts->boolean = true;
|
|
ts->string = strdup("test string");
|
|
return ts;
|
|
}
|
|
|
|
static void struct_compare(TestStruct *ts1, TestStruct *ts2)
|
|
{
|
|
g_assert(ts1);
|
|
g_assert(ts2);
|
|
g_assert_cmpint(ts1->integer, ==, ts2->integer);
|
|
g_assert(ts1->boolean == ts2->boolean);
|
|
g_assert_cmpstr(ts1->string, ==, ts2->string);
|
|
}
|
|
|
|
static void struct_cleanup(TestStruct *ts)
|
|
{
|
|
g_free(ts->string);
|
|
g_free(ts);
|
|
}
|
|
|
|
static void visit_struct(Visitor *v, void **native, Error **errp)
|
|
{
|
|
visit_type_TestStruct(v, (TestStruct **)native, NULL, errp);
|
|
}
|
|
|
|
static UserDefTwo *nested_struct_create(void)
|
|
{
|
|
UserDefTwo *udnp = g_malloc0(sizeof(*udnp));
|
|
udnp->string0 = strdup("test_string0");
|
|
udnp->dict1 = g_malloc0(sizeof(*udnp->dict1));
|
|
udnp->dict1->string1 = strdup("test_string1");
|
|
udnp->dict1->dict2 = g_malloc0(sizeof(*udnp->dict1->dict2));
|
|
udnp->dict1->dict2->userdef = g_new0(UserDefOne, 1);
|
|
udnp->dict1->dict2->userdef->integer = 42;
|
|
udnp->dict1->dict2->userdef->string = strdup("test_string");
|
|
udnp->dict1->dict2->string = strdup("test_string2");
|
|
udnp->dict1->dict3 = g_malloc0(sizeof(*udnp->dict1->dict3));
|
|
udnp->dict1->has_dict3 = true;
|
|
udnp->dict1->dict3->userdef = g_new0(UserDefOne, 1);
|
|
udnp->dict1->dict3->userdef->integer = 43;
|
|
udnp->dict1->dict3->userdef->string = strdup("test_string");
|
|
udnp->dict1->dict3->string = strdup("test_string3");
|
|
return udnp;
|
|
}
|
|
|
|
static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
|
|
{
|
|
g_assert(udnp1);
|
|
g_assert(udnp2);
|
|
g_assert_cmpstr(udnp1->string0, ==, udnp2->string0);
|
|
g_assert_cmpstr(udnp1->dict1->string1, ==, udnp2->dict1->string1);
|
|
g_assert_cmpint(udnp1->dict1->dict2->userdef->integer, ==,
|
|
udnp2->dict1->dict2->userdef->integer);
|
|
g_assert_cmpstr(udnp1->dict1->dict2->userdef->string, ==,
|
|
udnp2->dict1->dict2->userdef->string);
|
|
g_assert_cmpstr(udnp1->dict1->dict2->string, ==,
|
|
udnp2->dict1->dict2->string);
|
|
g_assert(udnp1->dict1->has_dict3 == udnp2->dict1->has_dict3);
|
|
g_assert_cmpint(udnp1->dict1->dict3->userdef->integer, ==,
|
|
udnp2->dict1->dict3->userdef->integer);
|
|
g_assert_cmpstr(udnp1->dict1->dict3->userdef->string, ==,
|
|
udnp2->dict1->dict3->userdef->string);
|
|
g_assert_cmpstr(udnp1->dict1->dict3->string, ==,
|
|
udnp2->dict1->dict3->string);
|
|
}
|
|
|
|
static void nested_struct_cleanup(UserDefTwo *udnp)
|
|
{
|
|
qapi_free_UserDefTwo(udnp);
|
|
}
|
|
|
|
static void visit_nested_struct(Visitor *v, void **native, Error **errp)
|
|
{
|
|
visit_type_UserDefTwo(v, (UserDefTwo **)native, NULL, errp);
|
|
}
|
|
|
|
static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)
|
|
{
|
|
visit_type_UserDefTwoList(v, (UserDefTwoList **)native, NULL, errp);
|
|
}
|
|
|
|
/* test cases */
|
|
|
|
typedef enum VisitorCapabilities {
|
|
VCAP_PRIMITIVES = 1,
|
|
VCAP_STRUCTURES = 2,
|
|
VCAP_LISTS = 4,
|
|
VCAP_PRIMITIVE_LISTS = 8,
|
|
} VisitorCapabilities;
|
|
|
|
typedef struct SerializeOps {
|
|
void (*serialize)(void *native_in, void **datap,
|
|
VisitorFunc visit, Error **errp);
|
|
void (*deserialize)(void **native_out, void *datap,
|
|
VisitorFunc visit, Error **errp);
|
|
void (*cleanup)(void *datap);
|
|
const char *type;
|
|
VisitorCapabilities caps;
|
|
} SerializeOps;
|
|
|
|
typedef struct TestArgs {
|
|
const SerializeOps *ops;
|
|
void *test_data;
|
|
} TestArgs;
|
|
|
|
static void test_primitives(gconstpointer opaque)
|
|
{
|
|
TestArgs *args = (TestArgs *) opaque;
|
|
const SerializeOps *ops = args->ops;
|
|
PrimitiveType *pt = args->test_data;
|
|
PrimitiveType *pt_copy = g_malloc0(sizeof(*pt_copy));
|
|
void *serialize_data;
|
|
|
|
pt_copy->type = pt->type;
|
|
ops->serialize(pt, &serialize_data, visit_primitive_type, &error_abort);
|
|
ops->deserialize((void **)&pt_copy, serialize_data, visit_primitive_type,
|
|
&error_abort);
|
|
|
|
g_assert(pt_copy != NULL);
|
|
if (pt->type == PTYPE_STRING) {
|
|
g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
|
|
g_free((char *)pt_copy->value.string);
|
|
} else if (pt->type == PTYPE_NUMBER) {
|
|
GString *double_expected = g_string_new("");
|
|
GString *double_actual = g_string_new("");
|
|
/* we serialize with %f for our reference visitors, so rather than fuzzy
|
|
* floating math to test "equality", just compare the formatted values
|
|
*/
|
|
g_string_printf(double_expected, "%.6f", pt->value.number);
|
|
g_string_printf(double_actual, "%.6f", pt_copy->value.number);
|
|
g_assert_cmpstr(double_actual->str, ==, double_expected->str);
|
|
|
|
g_string_free(double_expected, true);
|
|
g_string_free(double_actual, true);
|
|
} else if (pt->type == PTYPE_BOOLEAN) {
|
|
g_assert_cmpint(!!pt->value.max, ==, !!pt->value.max);
|
|
} else {
|
|
g_assert_cmpint(pt->value.max, ==, pt_copy->value.max);
|
|
}
|
|
|
|
ops->cleanup(serialize_data);
|
|
g_free(args);
|
|
g_free(pt_copy);
|
|
}
|
|
|
|
static void test_primitive_lists(gconstpointer opaque)
|
|
{
|
|
TestArgs *args = (TestArgs *) opaque;
|
|
const SerializeOps *ops = args->ops;
|
|
PrimitiveType *pt = args->test_data;
|
|
PrimitiveList pl = { .value = { NULL } };
|
|
PrimitiveList pl_copy = { .value = { NULL } };
|
|
PrimitiveList *pl_copy_ptr = &pl_copy;
|
|
void *serialize_data;
|
|
void *cur_head = NULL;
|
|
int i;
|
|
|
|
pl.type = pl_copy.type = pt->type;
|
|
|
|
/* build up our list of primitive types */
|
|
for (i = 0; i < 32; i++) {
|
|
switch (pl.type) {
|
|
case PTYPE_STRING: {
|
|
strList *tmp = g_new0(strList, 1);
|
|
tmp->value = g_strdup(pt->value.string);
|
|
if (pl.value.strings == NULL) {
|
|
pl.value.strings = tmp;
|
|
} else {
|
|
tmp->next = pl.value.strings;
|
|
pl.value.strings = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_INTEGER: {
|
|
intList *tmp = g_new0(intList, 1);
|
|
tmp->value = pt->value.integer;
|
|
if (pl.value.integers == NULL) {
|
|
pl.value.integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.integers;
|
|
pl.value.integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_S8: {
|
|
int8List *tmp = g_new0(int8List, 1);
|
|
tmp->value = pt->value.s8;
|
|
if (pl.value.s8_integers == NULL) {
|
|
pl.value.s8_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.s8_integers;
|
|
pl.value.s8_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_S16: {
|
|
int16List *tmp = g_new0(int16List, 1);
|
|
tmp->value = pt->value.s16;
|
|
if (pl.value.s16_integers == NULL) {
|
|
pl.value.s16_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.s16_integers;
|
|
pl.value.s16_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_S32: {
|
|
int32List *tmp = g_new0(int32List, 1);
|
|
tmp->value = pt->value.s32;
|
|
if (pl.value.s32_integers == NULL) {
|
|
pl.value.s32_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.s32_integers;
|
|
pl.value.s32_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_S64: {
|
|
int64List *tmp = g_new0(int64List, 1);
|
|
tmp->value = pt->value.s64;
|
|
if (pl.value.s64_integers == NULL) {
|
|
pl.value.s64_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.s64_integers;
|
|
pl.value.s64_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_U8: {
|
|
uint8List *tmp = g_new0(uint8List, 1);
|
|
tmp->value = pt->value.u8;
|
|
if (pl.value.u8_integers == NULL) {
|
|
pl.value.u8_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.u8_integers;
|
|
pl.value.u8_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_U16: {
|
|
uint16List *tmp = g_new0(uint16List, 1);
|
|
tmp->value = pt->value.u16;
|
|
if (pl.value.u16_integers == NULL) {
|
|
pl.value.u16_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.u16_integers;
|
|
pl.value.u16_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_U32: {
|
|
uint32List *tmp = g_new0(uint32List, 1);
|
|
tmp->value = pt->value.u32;
|
|
if (pl.value.u32_integers == NULL) {
|
|
pl.value.u32_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.u32_integers;
|
|
pl.value.u32_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_U64: {
|
|
uint64List *tmp = g_new0(uint64List, 1);
|
|
tmp->value = pt->value.u64;
|
|
if (pl.value.u64_integers == NULL) {
|
|
pl.value.u64_integers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.u64_integers;
|
|
pl.value.u64_integers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_NUMBER: {
|
|
numberList *tmp = g_new0(numberList, 1);
|
|
tmp->value = pt->value.number;
|
|
if (pl.value.numbers == NULL) {
|
|
pl.value.numbers = tmp;
|
|
} else {
|
|
tmp->next = pl.value.numbers;
|
|
pl.value.numbers = tmp;
|
|
}
|
|
break;
|
|
}
|
|
case PTYPE_BOOLEAN: {
|
|
boolList *tmp = g_new0(boolList, 1);
|
|
tmp->value = pt->value.boolean;
|
|
if (pl.value.booleans == NULL) {
|
|
pl.value.booleans = tmp;
|
|
} else {
|
|
tmp->next = pl.value.booleans;
|
|
pl.value.booleans = tmp;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
g_assert_not_reached();
|
|
}
|
|
}
|
|
|
|
ops->serialize((void **)&pl, &serialize_data, visit_primitive_list,
|
|
&error_abort);
|
|
ops->deserialize((void **)&pl_copy_ptr, serialize_data,
|
|
visit_primitive_list, &error_abort);
|
|
|
|
i = 0;
|
|
|
|
/* compare our deserialized list of primitives to the original */
|
|
do {
|
|
switch (pl_copy.type) {
|
|
case PTYPE_STRING: {
|
|
strList *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.strings;
|
|
}
|
|
g_assert_cmpstr(pt->value.string, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_INTEGER: {
|
|
intList *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.integers;
|
|
}
|
|
g_assert_cmpint(pt->value.integer, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_S8: {
|
|
int8List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.s8_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.s8, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_S16: {
|
|
int16List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.s16_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.s16, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_S32: {
|
|
int32List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.s32_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.s32, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_S64: {
|
|
int64List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.s64_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.s64, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_U8: {
|
|
uint8List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.u8_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.u8, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_U16: {
|
|
uint16List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.u16_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.u16, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_U32: {
|
|
uint32List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.u32_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.u32, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_U64: {
|
|
uint64List *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.u64_integers;
|
|
}
|
|
g_assert_cmpint(pt->value.u64, ==, ptr->value);
|
|
break;
|
|
}
|
|
case PTYPE_NUMBER: {
|
|
numberList *ptr;
|
|
GString *double_expected = g_string_new("");
|
|
GString *double_actual = g_string_new("");
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.numbers;
|
|
}
|
|
/* we serialize with %f for our reference visitors, so rather than
|
|
* fuzzy floating math to test "equality", just compare the
|
|
* formatted values
|
|
*/
|
|
g_string_printf(double_expected, "%.6f", pt->value.number);
|
|
g_string_printf(double_actual, "%.6f", ptr->value);
|
|
g_assert_cmpstr(double_actual->str, ==, double_expected->str);
|
|
g_string_free(double_expected, true);
|
|
g_string_free(double_actual, true);
|
|
break;
|
|
}
|
|
case PTYPE_BOOLEAN: {
|
|
boolList *ptr;
|
|
if (cur_head) {
|
|
ptr = cur_head;
|
|
cur_head = ptr->next;
|
|
} else {
|
|
cur_head = ptr = pl_copy.value.booleans;
|
|
}
|
|
g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
|
|
break;
|
|
}
|
|
default:
|
|
g_assert_not_reached();
|
|
}
|
|
i++;
|
|
} while (cur_head);
|
|
|
|
g_assert_cmpint(i, ==, 33);
|
|
|
|
ops->cleanup(serialize_data);
|
|
dealloc_helper(&pl, visit_primitive_list, &error_abort);
|
|
dealloc_helper(&pl_copy, visit_primitive_list, &error_abort);
|
|
g_free(args);
|
|
}
|
|
|
|
static void test_struct(gconstpointer opaque)
|
|
{
|
|
TestArgs *args = (TestArgs *) opaque;
|
|
const SerializeOps *ops = args->ops;
|
|
TestStruct *ts = struct_create();
|
|
TestStruct *ts_copy = NULL;
|
|
void *serialize_data;
|
|
|
|
ops->serialize(ts, &serialize_data, visit_struct, &error_abort);
|
|
ops->deserialize((void **)&ts_copy, serialize_data, visit_struct,
|
|
&error_abort);
|
|
|
|
struct_compare(ts, ts_copy);
|
|
|
|
struct_cleanup(ts);
|
|
struct_cleanup(ts_copy);
|
|
|
|
ops->cleanup(serialize_data);
|
|
g_free(args);
|
|
}
|
|
|
|
static void test_nested_struct(gconstpointer opaque)
|
|
{
|
|
TestArgs *args = (TestArgs *) opaque;
|
|
const SerializeOps *ops = args->ops;
|
|
UserDefTwo *udnp = nested_struct_create();
|
|
UserDefTwo *udnp_copy = NULL;
|
|
void *serialize_data;
|
|
|
|
ops->serialize(udnp, &serialize_data, visit_nested_struct, &error_abort);
|
|
ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_struct,
|
|
&error_abort);
|
|
|
|
nested_struct_compare(udnp, udnp_copy);
|
|
|
|
nested_struct_cleanup(udnp);
|
|
nested_struct_cleanup(udnp_copy);
|
|
|
|
ops->cleanup(serialize_data);
|
|
g_free(args);
|
|
}
|
|
|
|
static void test_nested_struct_list(gconstpointer opaque)
|
|
{
|
|
TestArgs *args = (TestArgs *) opaque;
|
|
const SerializeOps *ops = args->ops;
|
|
UserDefTwoList *listp = NULL, *tmp, *tmp_copy, *listp_copy = NULL;
|
|
void *serialize_data;
|
|
int i = 0;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
tmp = g_new0(UserDefTwoList, 1);
|
|
tmp->value = nested_struct_create();
|
|
tmp->next = listp;
|
|
listp = tmp;
|
|
}
|
|
|
|
ops->serialize(listp, &serialize_data, visit_nested_struct_list,
|
|
&error_abort);
|
|
ops->deserialize((void **)&listp_copy, serialize_data,
|
|
visit_nested_struct_list, &error_abort);
|
|
|
|
tmp = listp;
|
|
tmp_copy = listp_copy;
|
|
while (listp_copy) {
|
|
g_assert(listp);
|
|
nested_struct_compare(listp->value, listp_copy->value);
|
|
listp = listp->next;
|
|
listp_copy = listp_copy->next;
|
|
}
|
|
|
|
qapi_free_UserDefTwoList(tmp);
|
|
qapi_free_UserDefTwoList(tmp_copy);
|
|
|
|
ops->cleanup(serialize_data);
|
|
g_free(args);
|
|
}
|
|
|
|
static PrimitiveType pt_values[] = {
|
|
/* string tests */
|
|
{
|
|
.description = "string_empty",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "",
|
|
},
|
|
{
|
|
.description = "string_whitespace",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "a b c\td",
|
|
},
|
|
{
|
|
.description = "string_newlines",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "a\nb\n",
|
|
},
|
|
{
|
|
.description = "string_commas",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "a,b, c,d",
|
|
},
|
|
{
|
|
.description = "string_single_quoted",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "'a b',cd",
|
|
},
|
|
{
|
|
.description = "string_double_quoted",
|
|
.type = PTYPE_STRING,
|
|
.value.string = "\"a b\",cd",
|
|
},
|
|
/* boolean tests */
|
|
{
|
|
.description = "boolean_true1",
|
|
.type = PTYPE_BOOLEAN,
|
|
.value.boolean = true,
|
|
},
|
|
{
|
|
.description = "boolean_true2",
|
|
.type = PTYPE_BOOLEAN,
|
|
.value.boolean = 8,
|
|
},
|
|
{
|
|
.description = "boolean_true3",
|
|
.type = PTYPE_BOOLEAN,
|
|
.value.boolean = -1,
|
|
},
|
|
{
|
|
.description = "boolean_false1",
|
|
.type = PTYPE_BOOLEAN,
|
|
.value.boolean = false,
|
|
},
|
|
{
|
|
.description = "boolean_false2",
|
|
.type = PTYPE_BOOLEAN,
|
|
.value.boolean = 0,
|
|
},
|
|
/* number tests (double) */
|
|
/* note: we format these to %.6f before comparing, since that's how
|
|
* we serialize them and it doesn't make sense to check precision
|
|
* beyond that.
|
|
*/
|
|
{
|
|
.description = "number_sanity1",
|
|
.type = PTYPE_NUMBER,
|
|
.value.number = -1,
|
|
},
|
|
{
|
|
.description = "number_sanity2",
|
|
.type = PTYPE_NUMBER,
|
|
.value.number = 3.14159265,
|
|
},
|
|
{
|
|
.description = "number_min",
|
|
.type = PTYPE_NUMBER,
|
|
.value.number = DBL_MIN,
|
|
},
|
|
{
|
|
.description = "number_max",
|
|
.type = PTYPE_NUMBER,
|
|
.value.number = DBL_MAX,
|
|
},
|
|
/* integer tests (int64) */
|
|
{
|
|
.description = "integer_sanity1",
|
|
.type = PTYPE_INTEGER,
|
|
.value.integer = -1,
|
|
},
|
|
{
|
|
.description = "integer_sanity2",
|
|
.type = PTYPE_INTEGER,
|
|
.value.integer = INT64_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "integer_min",
|
|
.type = PTYPE_INTEGER,
|
|
.value.integer = INT64_MIN,
|
|
},
|
|
{
|
|
.description = "integer_max",
|
|
.type = PTYPE_INTEGER,
|
|
.value.integer = INT64_MAX,
|
|
},
|
|
/* uint8 tests */
|
|
{
|
|
.description = "uint8_sanity1",
|
|
.type = PTYPE_U8,
|
|
.value.u8 = 1,
|
|
},
|
|
{
|
|
.description = "uint8_sanity2",
|
|
.type = PTYPE_U8,
|
|
.value.u8 = UINT8_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "uint8_min",
|
|
.type = PTYPE_U8,
|
|
.value.u8 = 0,
|
|
},
|
|
{
|
|
.description = "uint8_max",
|
|
.type = PTYPE_U8,
|
|
.value.u8 = UINT8_MAX,
|
|
},
|
|
/* uint16 tests */
|
|
{
|
|
.description = "uint16_sanity1",
|
|
.type = PTYPE_U16,
|
|
.value.u16 = 1,
|
|
},
|
|
{
|
|
.description = "uint16_sanity2",
|
|
.type = PTYPE_U16,
|
|
.value.u16 = UINT16_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "uint16_min",
|
|
.type = PTYPE_U16,
|
|
.value.u16 = 0,
|
|
},
|
|
{
|
|
.description = "uint16_max",
|
|
.type = PTYPE_U16,
|
|
.value.u16 = UINT16_MAX,
|
|
},
|
|
/* uint32 tests */
|
|
{
|
|
.description = "uint32_sanity1",
|
|
.type = PTYPE_U32,
|
|
.value.u32 = 1,
|
|
},
|
|
{
|
|
.description = "uint32_sanity2",
|
|
.type = PTYPE_U32,
|
|
.value.u32 = UINT32_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "uint32_min",
|
|
.type = PTYPE_U32,
|
|
.value.u32 = 0,
|
|
},
|
|
{
|
|
.description = "uint32_max",
|
|
.type = PTYPE_U32,
|
|
.value.u32 = UINT32_MAX,
|
|
},
|
|
/* uint64 tests */
|
|
{
|
|
.description = "uint64_sanity1",
|
|
.type = PTYPE_U64,
|
|
.value.u64 = 1,
|
|
},
|
|
{
|
|
.description = "uint64_sanity2",
|
|
.type = PTYPE_U64,
|
|
.value.u64 = UINT64_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "uint64_min",
|
|
.type = PTYPE_U64,
|
|
.value.u64 = 0,
|
|
},
|
|
{
|
|
.description = "uint64_max",
|
|
.type = PTYPE_U64,
|
|
.value.u64 = UINT64_MAX,
|
|
},
|
|
/* int8 tests */
|
|
{
|
|
.description = "int8_sanity1",
|
|
.type = PTYPE_S8,
|
|
.value.s8 = -1,
|
|
},
|
|
{
|
|
.description = "int8_sanity2",
|
|
.type = PTYPE_S8,
|
|
.value.s8 = INT8_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "int8_min",
|
|
.type = PTYPE_S8,
|
|
.value.s8 = INT8_MIN,
|
|
},
|
|
{
|
|
.description = "int8_max",
|
|
.type = PTYPE_S8,
|
|
.value.s8 = INT8_MAX,
|
|
},
|
|
/* int16 tests */
|
|
{
|
|
.description = "int16_sanity1",
|
|
.type = PTYPE_S16,
|
|
.value.s16 = -1,
|
|
},
|
|
{
|
|
.description = "int16_sanity2",
|
|
.type = PTYPE_S16,
|
|
.value.s16 = INT16_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "int16_min",
|
|
.type = PTYPE_S16,
|
|
.value.s16 = INT16_MIN,
|
|
},
|
|
{
|
|
.description = "int16_max",
|
|
.type = PTYPE_S16,
|
|
.value.s16 = INT16_MAX,
|
|
},
|
|
/* int32 tests */
|
|
{
|
|
.description = "int32_sanity1",
|
|
.type = PTYPE_S32,
|
|
.value.s32 = -1,
|
|
},
|
|
{
|
|
.description = "int32_sanity2",
|
|
.type = PTYPE_S32,
|
|
.value.s32 = INT32_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "int32_min",
|
|
.type = PTYPE_S32,
|
|
.value.s32 = INT32_MIN,
|
|
},
|
|
{
|
|
.description = "int32_max",
|
|
.type = PTYPE_S32,
|
|
.value.s32 = INT32_MAX,
|
|
},
|
|
/* int64 tests */
|
|
{
|
|
.description = "int64_sanity1",
|
|
.type = PTYPE_S64,
|
|
.value.s64 = -1,
|
|
},
|
|
{
|
|
.description = "int64_sanity2",
|
|
.type = PTYPE_S64,
|
|
.value.s64 = INT64_MAX / 2 + 1,
|
|
},
|
|
{
|
|
.description = "int64_min",
|
|
.type = PTYPE_S64,
|
|
.value.s64 = INT64_MIN,
|
|
},
|
|
{
|
|
.description = "int64_max",
|
|
.type = PTYPE_S64,
|
|
.value.s64 = INT64_MAX,
|
|
},
|
|
{ .type = PTYPE_EOL }
|
|
};
|
|
|
|
/* visitor-specific op implementations */
|
|
|
|
typedef struct QmpSerializeData {
|
|
QmpOutputVisitor *qov;
|
|
QmpInputVisitor *qiv;
|
|
} QmpSerializeData;
|
|
|
|
static void qmp_serialize(void *native_in, void **datap,
|
|
VisitorFunc visit, Error **errp)
|
|
{
|
|
QmpSerializeData *d = g_malloc0(sizeof(*d));
|
|
|
|
d->qov = qmp_output_visitor_new();
|
|
visit(qmp_output_get_visitor(d->qov), &native_in, errp);
|
|
*datap = d;
|
|
}
|
|
|
|
static void qmp_deserialize(void **native_out, void *datap,
|
|
VisitorFunc visit, Error **errp)
|
|
{
|
|
QmpSerializeData *d = datap;
|
|
QString *output_json;
|
|
QObject *obj_orig, *obj;
|
|
|
|
obj_orig = qmp_output_get_qobject(d->qov);
|
|
output_json = qobject_to_json(obj_orig);
|
|
obj = qobject_from_json(qstring_get_str(output_json));
|
|
|
|
QDECREF(output_json);
|
|
d->qiv = qmp_input_visitor_new(obj);
|
|
qobject_decref(obj_orig);
|
|
qobject_decref(obj);
|
|
visit(qmp_input_get_visitor(d->qiv), native_out, errp);
|
|
}
|
|
|
|
static void qmp_cleanup(void *datap)
|
|
{
|
|
QmpSerializeData *d = datap;
|
|
qmp_output_visitor_cleanup(d->qov);
|
|
qmp_input_visitor_cleanup(d->qiv);
|
|
|
|
g_free(d);
|
|
}
|
|
|
|
typedef struct StringSerializeData {
|
|
char *string;
|
|
StringOutputVisitor *sov;
|
|
StringInputVisitor *siv;
|
|
} StringSerializeData;
|
|
|
|
static void string_serialize(void *native_in, void **datap,
|
|
VisitorFunc visit, Error **errp)
|
|
{
|
|
StringSerializeData *d = g_malloc0(sizeof(*d));
|
|
|
|
d->sov = string_output_visitor_new(false);
|
|
visit(string_output_get_visitor(d->sov), &native_in, errp);
|
|
*datap = d;
|
|
}
|
|
|
|
static void string_deserialize(void **native_out, void *datap,
|
|
VisitorFunc visit, Error **errp)
|
|
{
|
|
StringSerializeData *d = datap;
|
|
|
|
d->string = string_output_get_string(d->sov);
|
|
d->siv = string_input_visitor_new(d->string);
|
|
visit(string_input_get_visitor(d->siv), native_out, errp);
|
|
}
|
|
|
|
static void string_cleanup(void *datap)
|
|
{
|
|
StringSerializeData *d = datap;
|
|
|
|
string_output_visitor_cleanup(d->sov);
|
|
string_input_visitor_cleanup(d->siv);
|
|
g_free(d->string);
|
|
g_free(d);
|
|
}
|
|
|
|
/* visitor registration, test harness */
|
|
|
|
/* note: to function interchangeably as a serialization mechanism your
|
|
* visitor test implementation should pass the test cases for all visitor
|
|
* capabilities: primitives, structures, and lists
|
|
*/
|
|
static const SerializeOps visitors[] = {
|
|
{
|
|
.type = "QMP",
|
|
.serialize = qmp_serialize,
|
|
.deserialize = qmp_deserialize,
|
|
.cleanup = qmp_cleanup,
|
|
.caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
|
|
VCAP_PRIMITIVE_LISTS
|
|
},
|
|
{
|
|
.type = "String",
|
|
.serialize = string_serialize,
|
|
.deserialize = string_deserialize,
|
|
.cleanup = string_cleanup,
|
|
.caps = VCAP_PRIMITIVES
|
|
},
|
|
{ NULL }
|
|
};
|
|
|
|
static void add_visitor_type(const SerializeOps *ops)
|
|
{
|
|
char testname_prefix[128];
|
|
char testname[128];
|
|
TestArgs *args;
|
|
int i = 0;
|
|
|
|
sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
|
|
|
|
if (ops->caps & VCAP_PRIMITIVES) {
|
|
while (pt_values[i].type != PTYPE_EOL) {
|
|
sprintf(testname, "%s/primitives/%s", testname_prefix,
|
|
pt_values[i].description);
|
|
args = g_malloc0(sizeof(*args));
|
|
args->ops = ops;
|
|
args->test_data = &pt_values[i];
|
|
g_test_add_data_func(testname, args, test_primitives);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
if (ops->caps & VCAP_STRUCTURES) {
|
|
sprintf(testname, "%s/struct", testname_prefix);
|
|
args = g_malloc0(sizeof(*args));
|
|
args->ops = ops;
|
|
args->test_data = NULL;
|
|
g_test_add_data_func(testname, args, test_struct);
|
|
|
|
sprintf(testname, "%s/nested_struct", testname_prefix);
|
|
args = g_malloc0(sizeof(*args));
|
|
args->ops = ops;
|
|
args->test_data = NULL;
|
|
g_test_add_data_func(testname, args, test_nested_struct);
|
|
}
|
|
|
|
if (ops->caps & VCAP_LISTS) {
|
|
sprintf(testname, "%s/nested_struct_list", testname_prefix);
|
|
args = g_malloc0(sizeof(*args));
|
|
args->ops = ops;
|
|
args->test_data = NULL;
|
|
g_test_add_data_func(testname, args, test_nested_struct_list);
|
|
}
|
|
|
|
if (ops->caps & VCAP_PRIMITIVE_LISTS) {
|
|
i = 0;
|
|
while (pt_values[i].type != PTYPE_EOL) {
|
|
sprintf(testname, "%s/primitive_list/%s", testname_prefix,
|
|
pt_values[i].description);
|
|
args = g_malloc0(sizeof(*args));
|
|
args->ops = ops;
|
|
args->test_data = &pt_values[i];
|
|
g_test_add_data_func(testname, args, test_primitive_lists);
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int i = 0;
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
while (visitors[i].type != NULL) {
|
|
add_visitor_type(&visitors[i]);
|
|
i++;
|
|
}
|
|
|
|
g_test_run();
|
|
|
|
return 0;
|
|
}
|