aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/.gitignore1
-rw-r--r--tests/Makefile.include3
-rw-r--r--tests/test-keyval.c180
3 files changed, 184 insertions, 0 deletions
diff --git a/tests/.gitignore b/tests/.gitignore
index dc37519421..30b7740b4e 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -47,6 +47,7 @@ test-io-channel-file.txt
test-io-channel-socket
test-io-channel-tls
test-io-task
+test-keyval
test-logging
test-mul64
test-opts-visitor
diff --git a/tests/Makefile.include b/tests/Makefile.include
index b65c2b50ea..3c34295d08 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -94,6 +94,8 @@ check-unit-y += tests/check-qom-proplist$(EXESUF)
gcov-files-check-qom-proplist-y = qom/object.c
check-unit-y += tests/test-qemu-opts$(EXESUF)
gcov-files-test-qemu-opts-y = util/qemu-option.c
+check-unit-y += tests/test-keyval$(EXESUF)
+gcov-files-test-keyval-y = util/keyval.c
check-unit-y += tests/test-write-threshold$(EXESUF)
gcov-files-test-write-threshold-y = block/write-threshold.c
check-unit-y += tests/test-crypto-hash$(EXESUF)
@@ -720,6 +722,7 @@ tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o $(test-util-obj-y) \
$(chardev-obj-y)
tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y)
+tests/test-keyval$(EXESUF): tests/test-keyval.o $(test-util-obj-y)
tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y)
tests/test-filter-mirror$(EXESUF): tests/test-filter-mirror.o $(qtest-obj-y)
diff --git a/tests/test-keyval.c b/tests/test-keyval.c
new file mode 100644
index 0000000000..27f66258c9
--- /dev/null
+++ b/tests/test-keyval.c
@@ -0,0 +1,180 @@
+/*
+ * Unit tests for parsing of KEY=VALUE,... strings
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * Authors:
+ * Markus Armbruster <armbru@redhat.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 "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/option.h"
+
+static void test_keyval_parse(void)
+{
+ Error *err = NULL;
+ QDict *qdict, *sub_qdict;
+ char long_key[129];
+ char *params;
+
+ /* Nothing */
+ qdict = keyval_parse("", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 0);
+ QDECREF(qdict);
+
+ /* Empty key (qemu_opts_parse() accepts this) */
+ qdict = keyval_parse("=val", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Empty key fragment */
+ qdict = keyval_parse(".", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+ qdict = keyval_parse("key.", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Overlong key */
+ memset(long_key, 'a', 127);
+ long_key[127] = 'z';
+ long_key[128] = 0;
+ params = g_strdup_printf("k.%s=v", long_key);
+ qdict = keyval_parse(params + 2, NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Overlong key fragment */
+ qdict = keyval_parse(params, NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+ g_free(params);
+
+ /* Long key (qemu_opts_parse() accepts and truncates silently) */
+ params = g_strdup_printf("k.%s=v", long_key + 1);
+ qdict = keyval_parse(params + 2, NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(qdict, long_key + 1), ==, "v");
+ QDECREF(qdict);
+
+ /* Long key fragment */
+ qdict = keyval_parse(params, NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ sub_qdict = qdict_get_qdict(qdict, "k");
+ g_assert(sub_qdict);
+ g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(sub_qdict, long_key + 1), ==, "v");
+ QDECREF(qdict);
+ g_free(params);
+
+ /* Multiple keys, last one wins */
+ qdict = keyval_parse("a=1,b=2,,x,a=3", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 2);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "a"), ==, "3");
+ g_assert_cmpstr(qdict_get_try_str(qdict, "b"), ==, "2,x");
+ QDECREF(qdict);
+
+ /* Even when it doesn't in qemu_opts_parse() */
+ qdict = keyval_parse("id=foo,id=bar", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "bar");
+ QDECREF(qdict);
+
+ /* Dotted keys */
+ qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 2);
+ sub_qdict = qdict_get_qdict(qdict, "a");
+ g_assert(sub_qdict);
+ g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
+ sub_qdict = qdict_get_qdict(sub_qdict, "b");
+ g_assert(sub_qdict);
+ g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(sub_qdict, "c"), ==, "2");
+ g_assert_cmpstr(qdict_get_try_str(qdict, "d"), ==, "3");
+ QDECREF(qdict);
+
+ /* Inconsistent dotted keys */
+ qdict = keyval_parse("a.b=1,a=2", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+ qdict = keyval_parse("a.b=1,a.b.c=2", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Trailing comma is ignored */
+ qdict = keyval_parse("x=y,", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, "y");
+ QDECREF(qdict);
+
+ /* Except when it isn't */
+ qdict = keyval_parse(",", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Value containing ,id= not misinterpreted as qemu_opts_parse() does */
+ qdict = keyval_parse("x=,,id=bar", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, ",id=bar");
+ QDECREF(qdict);
+
+ /* Anti-social ID is left to caller (qemu_opts_parse() rejects it) */
+ qdict = keyval_parse("id=666", NULL, &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "666");
+ QDECREF(qdict);
+
+ /* Implied value not supported (unlike qemu_opts_parse()) */
+ qdict = keyval_parse("an,noaus,noaus=", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Implied value, key "no" (qemu_opts_parse(): negated empty key) */
+ qdict = keyval_parse("no", NULL, &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Implied key */
+ qdict = keyval_parse("an,aus=off,noaus=", "implied", &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 3);
+ g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "an");
+ g_assert_cmpstr(qdict_get_try_str(qdict, "aus"), ==, "off");
+ g_assert_cmpstr(qdict_get_try_str(qdict, "noaus"), ==, "");
+ QDECREF(qdict);
+
+ /* Implied dotted key */
+ qdict = keyval_parse("val", "eins.zwei", &error_abort);
+ g_assert_cmpuint(qdict_size(qdict), ==, 1);
+ sub_qdict = qdict_get_qdict(qdict, "eins");
+ g_assert(sub_qdict);
+ g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
+ g_assert_cmpstr(qdict_get_try_str(sub_qdict, "zwei"), ==, "val");
+ QDECREF(qdict);
+
+ /* Implied key with empty value (qemu_opts_parse() accepts this) */
+ qdict = keyval_parse(",", "implied", &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Likewise (qemu_opts_parse(): implied key with comma value) */
+ qdict = keyval_parse(",,,a=1", "implied", &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+
+ /* Empty key is not an implied key */
+ qdict = keyval_parse("=val", "implied", &err);
+ error_free_or_abort(&err);
+ g_assert(!qdict);
+}
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+ g_test_add_func("/keyval/keyval_parse", test_keyval_parse);
+ g_test_run();
+ return 0;
+}