static void test_visitor_in_number(TestInputVisitorData *data, const void *unused) { double res = 0, value = 3.14; Error *err = NULL; Visitor *v; v = visitor_input_test_init(data, "3.14"); visit_type_number(v, NULL, &res, &err); g_assert(!err); 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"; Error *err = NULL; Visitor *v; v = visitor_input_test_init(data, value); visit_type_str(v, &res, NULL, &err); g_assert(!err); g_assert_cmpstr(res, ==, value); g_free(res); }
static void test_visitor_in_alternate(TestInputVisitorData *data, const void *unused) { Visitor *v; Error *err = NULL; UserDefAlternate *tmp; v = visitor_input_test_init(data, "42"); visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort); g_assert_cmpint(tmp->type, ==, USER_DEF_ALTERNATE_KIND_I); g_assert_cmpint(tmp->u.i, ==, 42); qapi_free_UserDefAlternate(tmp); v = visitor_input_test_init(data, "'string'"); visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort); g_assert_cmpint(tmp->type, ==, USER_DEF_ALTERNATE_KIND_S); g_assert_cmpstr(tmp->u.s, ==, "string"); qapi_free_UserDefAlternate(tmp); v = visitor_input_test_init(data, "false"); visit_type_UserDefAlternate(v, &tmp, NULL, &err); error_free_or_abort(&err); qapi_free_UserDefAlternate(tmp); }
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, &res, NULL, &error_abort); g_assert_cmpint(i, ==, res); } }
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, &res, NULL, &err); error_free_or_abort(&err); }
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, &p, NULL, &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); }
/* Try to crash the visitors */ static void test_visitor_in_fuzz(TestInputVisitorData *data, const void *unused) { int64_t ires; intList *ilres; bool bres; double nres; char *sres; EnumOne eres; Visitor *v; unsigned int i; char buf[10000]; for (i = 0; i < 100; i++) { unsigned int j; j = g_test_rand_int_range(0, sizeof(buf) - 1); buf[j] = '\0'; if (j != 0) { for (j--; j != 0; j--) { buf[j - 1] = (char)g_test_rand_int_range(0, 256); } } v = visitor_input_test_init(data, buf); visit_type_int(v, NULL, &ires, NULL); visitor_input_teardown(data, NULL); v = visitor_input_test_init(data, buf); visit_type_intList(v, NULL, &ilres, NULL); visitor_input_teardown(data, NULL); v = visitor_input_test_init(data, buf); visit_type_bool(v, NULL, &bres, NULL); visitor_input_teardown(data, NULL); v = visitor_input_test_init(data, buf); visit_type_number(v, NULL, &nres, NULL); visitor_input_teardown(data, NULL); v = visitor_input_test_init(data, buf); sres = NULL; visit_type_str(v, NULL, &sres, NULL); g_free(sres); visitor_input_teardown(data, NULL); v = visitor_input_test_init(data, buf); visit_type_EnumOne(v, NULL, &eres, NULL); visitor_input_teardown(data, NULL); } }
static void test_visitor_in_bool(TestInputVisitorData *data, const void *unused) { Error *err = NULL; bool res = false; Visitor *v; v = visitor_input_test_init(data, "true"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, true); visitor_input_teardown(data, unused); v = visitor_input_test_init(data, "yes"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, true); visitor_input_teardown(data, unused); v = visitor_input_test_init(data, "on"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, true); visitor_input_teardown(data, unused); v = visitor_input_test_init(data, "false"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, false); visitor_input_teardown(data, unused); v = visitor_input_test_init(data, "no"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, false); visitor_input_teardown(data, unused); v = visitor_input_test_init(data, "off"); visit_type_bool(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(res, ==, false); }
static void test_visitor_in_enum(TestInputVisitorData *data, const void *unused) { Error *err = NULL; Visitor *v; EnumOne i; for (i = 0; EnumOne_lookup[i]; i++) { EnumOne res = -1; v = visitor_input_test_init(data, EnumOne_lookup[i]); visit_type_EnumOne(v, &res, NULL, &err); g_assert(!err); g_assert_cmpint(i, ==, res); visitor_input_teardown(data, NULL); } data->siv = NULL; }
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, &udp, NULL, &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_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, &tmp, NULL, &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_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, &p, NULL, &err); error_free_or_abort(&err); g_assert(!p); v = visitor_input_test_init(data, "{}"); visit_type_strList(v, &q, NULL, &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, &p, NULL, &err); error_free_or_abort(&err); g_assert(!p); v = visitor_input_test_init(data, "{}"); visit_type_int(v, &i, NULL, &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, &q, NULL, &err); error_free_or_abort(&err); assert(!q); v = visitor_input_test_init(data, "[]"); visit_type_int(v, &i, NULL, &err); error_free_or_abort(&err); }
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, &asb, NULL, &err); error_free_or_abort(&err); qapi_free_AltStrBool(asb); /* FIXME: Order of alternate should not affect semantics; asn should * parse the same as ans */ v = visitor_input_test_init(data, "42"); visit_type_AltStrNum(v, &asn, NULL, &err); /* FIXME g_assert_cmpint(asn->type, == ALT_STR_NUM_KIND_N); */ /* FIXME g_assert_cmpfloat(asn->u.n, ==, 42); */ error_free_or_abort(&err); qapi_free_AltStrNum(asn); v = visitor_input_test_init(data, "42"); visit_type_AltNumStr(v, &ans, NULL, &error_abort); g_assert_cmpint(ans->type, ==, ALT_NUM_STR_KIND_N); g_assert_cmpfloat(ans->u.n, ==, 42); qapi_free_AltNumStr(ans); v = visitor_input_test_init(data, "42"); visit_type_AltStrInt(v, &asi, NULL, &error_abort); g_assert_cmpint(asi->type, ==, ALT_STR_INT_KIND_I); g_assert_cmpint(asi->u.i, ==, 42); qapi_free_AltStrInt(asi); v = visitor_input_test_init(data, "42"); visit_type_AltIntNum(v, &ain, NULL, &error_abort); g_assert_cmpint(ain->type, ==, ALT_INT_NUM_KIND_I); g_assert_cmpint(ain->u.i, ==, 42); qapi_free_AltIntNum(ain); v = visitor_input_test_init(data, "42"); visit_type_AltNumInt(v, &ani, NULL, &error_abort); g_assert_cmpint(ani->type, ==, ALT_NUM_INT_KIND_I); 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, &asb, NULL, &err); error_free_or_abort(&err); qapi_free_AltStrBool(asb); v = visitor_input_test_init(data, "42.5"); visit_type_AltStrNum(v, &asn, NULL, &error_abort); g_assert_cmpint(asn->type, ==, ALT_STR_NUM_KIND_N); g_assert_cmpfloat(asn->u.n, ==, 42.5); qapi_free_AltStrNum(asn); v = visitor_input_test_init(data, "42.5"); visit_type_AltNumStr(v, &ans, NULL, &error_abort); g_assert_cmpint(ans->type, ==, ALT_NUM_STR_KIND_N); g_assert_cmpfloat(ans->u.n, ==, 42.5); qapi_free_AltNumStr(ans); v = visitor_input_test_init(data, "42.5"); visit_type_AltStrInt(v, &asi, NULL, &err); error_free_or_abort(&err); qapi_free_AltStrInt(asi); v = visitor_input_test_init(data, "42.5"); visit_type_AltIntNum(v, &ain, NULL, &error_abort); g_assert_cmpint(ain->type, ==, ALT_INT_NUM_KIND_N); g_assert_cmpfloat(ain->u.n, ==, 42.5); qapi_free_AltIntNum(ain); v = visitor_input_test_init(data, "42.5"); visit_type_AltNumInt(v, &ani, NULL, &error_abort); g_assert_cmpint(ani->type, ==, ALT_NUM_INT_KIND_N); g_assert_cmpfloat(ani->u.n, ==, 42.5); qapi_free_AltNumInt(ani); }
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); }