aboutsummaryrefslogtreecommitdiff
path: root/tests/test-keyval.c
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2017-02-28 22:26:51 +0100
committerMarkus Armbruster <armbru@redhat.com>2017-03-07 16:07:46 +0100
commit9e3943f8837d0fda55044809798186a8453d582c (patch)
tree44a584c0bfb3839a7c350f9b40eb33866cd85aad /tests/test-keyval.c
parentcbd8acf38f37544b830086af840bfb1015ce10e0 (diff)
downloadqemu-9e3943f8837d0fda55044809798186a8453d582c.zip
qemu-9e3943f8837d0fda55044809798186a8453d582c.tar.gz
qemu-9e3943f8837d0fda55044809798186a8453d582c.tar.bz2
test-keyval: Cover use with qobject input visitor
Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Message-Id: <1488317230-26248-6-git-send-email-armbru@redhat.com>
Diffstat (limited to 'tests/test-keyval.c')
-rw-r--r--tests/test-keyval.c312
1 files changed, 312 insertions, 0 deletions
diff --git a/tests/test-keyval.c b/tests/test-keyval.c
index 27f6625..1c2aeea 100644
--- a/tests/test-keyval.c
+++ b/tests/test-keyval.c
@@ -12,6 +12,8 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
+#include "qapi/qobject-input-visitor.h"
+#include "qemu/cutils.h"
#include "qemu/option.h"
static void test_keyval_parse(void)
@@ -171,10 +173,320 @@ static void test_keyval_parse(void)
g_assert(!qdict);
}
+static void test_keyval_visit_bool(void)
+{
+ Error *err = NULL;
+ Visitor *v;
+ QDict *qdict;
+ bool b;
+
+ qdict = keyval_parse("bool1=on,bool2=off", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_bool(v, "bool1", &b, &error_abort);
+ g_assert(b);
+ visit_type_bool(v, "bool2", &b, &error_abort);
+ g_assert(!b);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ qdict = keyval_parse("bool1=offer", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_bool(v, "bool1", &b, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+}
+
+static void test_keyval_visit_number(void)
+{
+ Error *err = NULL;
+ Visitor *v;
+ QDict *qdict;
+ uint64_t u;
+
+ /* Lower limit zero */
+ qdict = keyval_parse("number1=0", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &error_abort);
+ g_assert_cmpuint(u, ==, 0);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Upper limit 2^64-1 */
+ qdict = keyval_parse("number1=18446744073709551615,number2=-1",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &error_abort);
+ g_assert_cmphex(u, ==, UINT64_MAX);
+ visit_type_uint64(v, "number2", &u, &error_abort);
+ g_assert_cmphex(u, ==, UINT64_MAX);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Above upper limit */
+ qdict = keyval_parse("number1=18446744073709551616",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Below lower limit */
+ qdict = keyval_parse("number1=-18446744073709551616",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Hex and octal */
+ qdict = keyval_parse("number1=0x2a,number2=052",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &error_abort);
+ g_assert_cmpuint(u, ==, 42);
+ visit_type_uint64(v, "number2", &u, &error_abort);
+ g_assert_cmpuint(u, ==, 42);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Trailing crap */
+ qdict = keyval_parse("number1=3.14,number2=08",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_uint64(v, "number1", &u, &err);
+ error_free_or_abort(&err);
+ visit_type_uint64(v, "number2", &u, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+}
+
+static void test_keyval_visit_size(void)
+{
+ Error *err = NULL;
+ Visitor *v;
+ QDict *qdict;
+ uint64_t sz;
+
+ /* Lower limit zero */
+ qdict = keyval_parse("sz1=0", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &error_abort);
+ g_assert_cmpuint(sz, ==, 0);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Note: precision is 53 bits since we're parsing with strtod() */
+
+ /* Around limit of precision: 2^53-1, 2^53, 2^53+1 */
+ qdict = keyval_parse("sz1=9007199254740991,"
+ "sz2=9007199254740992,"
+ "sz3=9007199254740993",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0x1fffffffffffff);
+ visit_type_size(v, "sz2", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0x20000000000000);
+ visit_type_size(v, "sz3", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0x20000000000000);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */
+ qdict = keyval_parse("sz1=9223372036854774784," /* 7ffffffffffffc00 */
+ "sz2=9223372036854775295", /* 7ffffffffffffdff */
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
+ visit_type_size(v, "sz2", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
+ qdict = keyval_parse("sz1=18446744073709549568," /* fffffffffffff800 */
+ "sz2=18446744073709550591", /* fffffffffffffbff */
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0xfffffffffffff800);
+ visit_type_size(v, "sz2", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 0xfffffffffffff800);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Beyond limits */
+ qdict = keyval_parse("sz1=-1,"
+ "sz2=18446744073709550592", /* fffffffffffffc00 */
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &err);
+ error_free_or_abort(&err);
+ visit_type_size(v, "sz2", &sz, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Suffixes */
+ qdict = keyval_parse("sz1=8b,sz2=1.5k,sz3=2M,sz4=0.1G,sz5=16777215T",
+ NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &error_abort);
+ g_assert_cmpuint(sz, ==, 8);
+ visit_type_size(v, "sz2", &sz, &error_abort);
+ g_assert_cmpuint(sz, ==, 1536);
+ visit_type_size(v, "sz3", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 2 * M_BYTE);
+ visit_type_size(v, "sz4", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, G_BYTE / 10);
+ visit_type_size(v, "sz5", &sz, &error_abort);
+ g_assert_cmphex(sz, ==, 16777215 * T_BYTE);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Beyond limit with suffix */
+ qdict = keyval_parse("sz1=16777216T", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ /* Trailing crap */
+ qdict = keyval_parse("sz1=16E,sz2=16Gi", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_type_size(v, "sz1", &sz, &err);
+ error_free_or_abort(&err);
+ visit_type_size(v, "sz2", &sz, &err);
+ error_free_or_abort(&err);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+}
+
+static void test_keyval_visit_dict(void)
+{
+ Error *err = NULL;
+ Visitor *v;
+ QDict *qdict;
+ int64_t i;
+
+ qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_start_struct(v, "a", NULL, 0, &error_abort);
+ visit_start_struct(v, "b", NULL, 0, &error_abort);
+ visit_type_int(v, "c", &i, &error_abort);
+ g_assert_cmpint(i, ==, 2);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_type_int(v, "d", &i, &error_abort);
+ g_assert_cmpint(i, ==, 3);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+
+ qdict = keyval_parse("a.b=", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_start_struct(v, "a", NULL, 0, &error_abort);
+ visit_type_int(v, "c", &i, &err); /* a.c missing */
+ error_free_or_abort(&err);
+ visit_check_struct(v, &err);
+ error_free_or_abort(&err); /* a.b unexpected */
+ visit_end_struct(v, NULL);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+}
+
+static void test_keyval_visit_optional(void)
+{
+ Visitor *v;
+ QDict *qdict;
+ bool present;
+ int64_t i;
+
+ qdict = keyval_parse("a.b=1", NULL, &error_abort);
+ v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+ QDECREF(qdict);
+ visit_start_struct(v, NULL, NULL, 0, &error_abort);
+ visit_optional(v, "b", &present);
+ g_assert(!present); /* b missing */
+ visit_optional(v, "a", &present);
+ g_assert(present); /* a present */
+ visit_start_struct(v, "a", NULL, 0, &error_abort);
+ visit_optional(v, "b", &present);
+ g_assert(present); /* a.b present */
+ visit_type_int(v, "b", &i, &error_abort);
+ g_assert_cmpint(i, ==, 1);
+ visit_optional(v, "a", &present);
+ g_assert(!present); /* a.a missing */
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_check_struct(v, &error_abort);
+ visit_end_struct(v, NULL);
+ visit_free(v);
+}
+
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/keyval/keyval_parse", test_keyval_parse);
+ g_test_add_func("/keyval/visit/bool", test_keyval_visit_bool);
+ g_test_add_func("/keyval/visit/number", test_keyval_visit_number);
+ g_test_add_func("/keyval/visit/size", test_keyval_visit_size);
+ g_test_add_func("/keyval/visit/dict", test_keyval_visit_dict);
+ g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
g_test_run();
return 0;
}