|
|
357786 |
From 4b3bbc27a6554471969347a1f0ddd70cd80e2b0b Mon Sep 17 00:00:00 2001
|
|
|
357786 |
From: Markus Armbruster <armbru@redhat.com>
|
|
|
357786 |
Date: Mon, 18 Jun 2018 08:43:16 +0200
|
|
|
357786 |
Subject: [PATCH 09/54] qobject: Move block-specific qdict code to
|
|
|
357786 |
block-qdict.c
|
|
|
357786 |
|
|
|
357786 |
RH-Author: Markus Armbruster <armbru@redhat.com>
|
|
|
357786 |
Message-id: <20180618084330.30009-10-armbru@redhat.com>
|
|
|
357786 |
Patchwork-id: 80722
|
|
|
357786 |
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 09/23] qobject: Move block-specific qdict code to block-qdict.c
|
|
|
357786 |
Bugzilla: 1557995
|
|
|
357786 |
RH-Acked-by: Max Reitz <mreitz@redhat.com>
|
|
|
357786 |
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
|
|
|
357786 |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
357786 |
|
|
|
357786 |
Pure code motion, except for two brace placements and a comment
|
|
|
357786 |
tweaked to appease checkpatch.
|
|
|
357786 |
|
|
|
357786 |
Signed-off-by: Markus Armbruster <armbru@redhat.com>
|
|
|
357786 |
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
357786 |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
357786 |
(cherry picked from commit 0bcc8e5bd8d6fd6e5cb6462054f7cfa45b282f9a)
|
|
|
357786 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
357786 |
---
|
|
|
357786 |
MAINTAINERS | 2 +
|
|
|
357786 |
qobject/Makefile.objs | 1 +
|
|
|
357786 |
qobject/block-qdict.c | 640 ++++++++++++++++++++++++++++++++++++++++++++
|
|
|
357786 |
qobject/qdict.c | 629 --------------------------------------------
|
|
|
357786 |
tests/Makefile.include | 4 +
|
|
|
357786 |
tests/check-block-qdict.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
357786 |
tests/check-qdict.c | 642 ---------------------------------------------
|
|
|
357786 |
7 files changed, 1302 insertions(+), 1271 deletions(-)
|
|
|
357786 |
create mode 100644 qobject/block-qdict.c
|
|
|
357786 |
create mode 100644 tests/check-block-qdict.c
|
|
|
357786 |
|
|
|
357786 |
diff --git a/MAINTAINERS b/MAINTAINERS
|
|
|
357786 |
index 24b7016..ad5edc8 100644
|
|
|
357786 |
--- a/MAINTAINERS
|
|
|
357786 |
+++ b/MAINTAINERS
|
|
|
357786 |
@@ -1339,6 +1339,8 @@ F: qemu-img*
|
|
|
357786 |
F: qemu-io*
|
|
|
357786 |
F: tests/qemu-iotests/
|
|
|
357786 |
F: util/qemu-progress.c
|
|
|
357786 |
+F: qobject/block-qdict.c
|
|
|
357786 |
+F: test/check-block-qdict.c
|
|
|
357786 |
T: git git://repo.or.cz/qemu/kevin.git block
|
|
|
357786 |
|
|
|
357786 |
Block I/O path
|
|
|
357786 |
diff --git a/qobject/Makefile.objs b/qobject/Makefile.objs
|
|
|
357786 |
index 002d258..7b12c9c 100644
|
|
|
357786 |
--- a/qobject/Makefile.objs
|
|
|
357786 |
+++ b/qobject/Makefile.objs
|
|
|
357786 |
@@ -1,2 +1,3 @@
|
|
|
357786 |
util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
|
|
|
357786 |
util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
|
|
|
357786 |
+util-obj-y += block-qdict.o
|
|
|
357786 |
diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c
|
|
|
357786 |
new file mode 100644
|
|
|
357786 |
index 0000000..fb92010
|
|
|
357786 |
--- /dev/null
|
|
|
357786 |
+++ b/qobject/block-qdict.c
|
|
|
357786 |
@@ -0,0 +1,640 @@
|
|
|
357786 |
+/*
|
|
|
357786 |
+ * Special QDict functions used by the block layer
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Copyright (c) 2013-2018 Red Hat, Inc.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
357786 |
+ * See the COPYING.LIB file in the top-level directory.
|
|
|
357786 |
+ */
|
|
|
357786 |
+
|
|
|
357786 |
+#include "qemu/osdep.h"
|
|
|
357786 |
+#include "block/qdict.h"
|
|
|
357786 |
+#include "qapi/qmp/qlist.h"
|
|
|
357786 |
+#include "qemu/cutils.h"
|
|
|
357786 |
+#include "qapi/error.h"
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
|
|
|
357786 |
+ * value of 'key' in 'src' is copied there (and the refcount increased
|
|
|
357786 |
+ * accordingly).
|
|
|
357786 |
+ */
|
|
|
357786 |
+void qdict_copy_default(QDict *dst, QDict *src, const char *key)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QObject *val;
|
|
|
357786 |
+
|
|
|
357786 |
+ if (qdict_haskey(dst, key)) {
|
|
|
357786 |
+ return;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ val = qdict_get(src, key);
|
|
|
357786 |
+ if (val) {
|
|
|
357786 |
+ qdict_put_obj(dst, key, qobject_ref(val));
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
|
|
|
357786 |
+ * new QString initialised by 'val' is put there.
|
|
|
357786 |
+ */
|
|
|
357786 |
+void qdict_set_default_str(QDict *dst, const char *key, const char *val)
|
|
|
357786 |
+{
|
|
|
357786 |
+ if (qdict_haskey(dst, key)) {
|
|
|
357786 |
+ return;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_str(dst, key, val);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_flatten_qdict(QDict *qdict, QDict *target,
|
|
|
357786 |
+ const char *prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QObject *value;
|
|
|
357786 |
+ const QListEntry *entry;
|
|
|
357786 |
+ char *new_key;
|
|
|
357786 |
+ int i;
|
|
|
357786 |
+
|
|
|
357786 |
+ /* This function is never called with prefix == NULL, i.e., it is always
|
|
|
357786 |
+ * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
|
|
|
357786 |
+ * need to remove list entries during the iteration (the whole list will be
|
|
|
357786 |
+ * deleted eventually anyway from qdict_flatten_qdict()). */
|
|
|
357786 |
+ assert(prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+ entry = qlist_first(qlist);
|
|
|
357786 |
+
|
|
|
357786 |
+ for (i = 0; entry; entry = qlist_next(entry), i++) {
|
|
|
357786 |
+ value = qlist_entry_obj(entry);
|
|
|
357786 |
+ new_key = g_strdup_printf("%s.%i", prefix, i);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (qobject_type(value) == QTYPE_QDICT) {
|
|
|
357786 |
+ qdict_flatten_qdict(qobject_to(QDict, value), target, new_key);
|
|
|
357786 |
+ } else if (qobject_type(value) == QTYPE_QLIST) {
|
|
|
357786 |
+ qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ /* All other types are moved to the target unchanged. */
|
|
|
357786 |
+ qdict_put_obj(target, new_key, qobject_ref(value));
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ g_free(new_key);
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QObject *value;
|
|
|
357786 |
+ const QDictEntry *entry, *next;
|
|
|
357786 |
+ char *new_key;
|
|
|
357786 |
+ bool delete;
|
|
|
357786 |
+
|
|
|
357786 |
+ entry = qdict_first(qdict);
|
|
|
357786 |
+
|
|
|
357786 |
+ while (entry != NULL) {
|
|
|
357786 |
+
|
|
|
357786 |
+ next = qdict_next(qdict, entry);
|
|
|
357786 |
+ value = qdict_entry_value(entry);
|
|
|
357786 |
+ new_key = NULL;
|
|
|
357786 |
+ delete = false;
|
|
|
357786 |
+
|
|
|
357786 |
+ if (prefix) {
|
|
|
357786 |
+ new_key = g_strdup_printf("%s.%s", prefix, entry->key);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ if (qobject_type(value) == QTYPE_QDICT) {
|
|
|
357786 |
+ /* Entries of QDicts are processed recursively, the QDict object
|
|
|
357786 |
+ * itself disappears. */
|
|
|
357786 |
+ qdict_flatten_qdict(qobject_to(QDict, value), target,
|
|
|
357786 |
+ new_key ? new_key : entry->key);
|
|
|
357786 |
+ delete = true;
|
|
|
357786 |
+ } else if (qobject_type(value) == QTYPE_QLIST) {
|
|
|
357786 |
+ qdict_flatten_qlist(qobject_to(QList, value), target,
|
|
|
357786 |
+ new_key ? new_key : entry->key);
|
|
|
357786 |
+ delete = true;
|
|
|
357786 |
+ } else if (prefix) {
|
|
|
357786 |
+ /* All other objects are moved to the target unchanged. */
|
|
|
357786 |
+ qdict_put_obj(target, new_key, qobject_ref(value));
|
|
|
357786 |
+ delete = true;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ g_free(new_key);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (delete) {
|
|
|
357786 |
+ qdict_del(qdict, entry->key);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Restart loop after modifying the iterated QDict */
|
|
|
357786 |
+ entry = qdict_first(qdict);
|
|
|
357786 |
+ continue;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ entry = next;
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_flatten(): For each nested QDict with key x, all fields with key y
|
|
|
357786 |
+ * are moved to this QDict and their key is renamed to "x.y". For each nested
|
|
|
357786 |
+ * QList with key x, the field at index y is moved to this QDict with the key
|
|
|
357786 |
+ * "x.y" (i.e., the reverse of what qdict_array_split() does).
|
|
|
357786 |
+ * This operation is applied recursively for nested QDicts and QLists.
|
|
|
357786 |
+ */
|
|
|
357786 |
+void qdict_flatten(QDict *qdict)
|
|
|
357786 |
+{
|
|
|
357786 |
+ qdict_flatten_qdict(qdict, qdict, NULL);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/* extract all the src QDict entries starting by start into dst */
|
|
|
357786 |
+void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
|
|
|
357786 |
+
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *entry, *next;
|
|
|
357786 |
+ const char *p;
|
|
|
357786 |
+
|
|
|
357786 |
+ *dst = qdict_new();
|
|
|
357786 |
+ entry = qdict_first(src);
|
|
|
357786 |
+
|
|
|
357786 |
+ while (entry != NULL) {
|
|
|
357786 |
+ next = qdict_next(src, entry);
|
|
|
357786 |
+ if (strstart(entry->key, start, &p)) {
|
|
|
357786 |
+ qdict_put_obj(*dst, p, qobject_ref(entry->value));
|
|
|
357786 |
+ qdict_del(src, entry->key);
|
|
|
357786 |
+ }
|
|
|
357786 |
+ entry = next;
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static int qdict_count_prefixed_entries(const QDict *src, const char *start)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *entry;
|
|
|
357786 |
+ int count = 0;
|
|
|
357786 |
+
|
|
|
357786 |
+ for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
|
|
|
357786 |
+ if (strstart(entry->key, start, NULL)) {
|
|
|
357786 |
+ if (count == INT_MAX) {
|
|
|
357786 |
+ return -ERANGE;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ count++;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ return count;
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_array_split(): This function moves array-like elements of a QDict into
|
|
|
357786 |
+ * a new QList. Every entry in the original QDict with a key "%u" or one
|
|
|
357786 |
+ * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
|
|
|
357786 |
+ * incrementally counting up, will be moved to a new QDict at index %u in the
|
|
|
357786 |
+ * output QList with the key prefix removed, if that prefix is "%u.". If the
|
|
|
357786 |
+ * whole key is just "%u", the whole QObject will be moved unchanged without
|
|
|
357786 |
+ * creating a new QDict. The function terminates when there is no entry in the
|
|
|
357786 |
+ * QDict with a prefix directly (incrementally) following the last one; it also
|
|
|
357786 |
+ * returns if there are both entries with "%u" and "%u." for the same index %u.
|
|
|
357786 |
+ * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
|
|
|
357786 |
+ * (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
|
|
|
357786 |
+ * => [{"a": 42, "b": 23}, {"x": 0}, 66]
|
|
|
357786 |
+ * and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
|
|
|
357786 |
+ */
|
|
|
357786 |
+void qdict_array_split(QDict *src, QList **dst)
|
|
|
357786 |
+{
|
|
|
357786 |
+ unsigned i;
|
|
|
357786 |
+
|
|
|
357786 |
+ *dst = qlist_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ for (i = 0; i < UINT_MAX; i++) {
|
|
|
357786 |
+ QObject *subqobj;
|
|
|
357786 |
+ bool is_subqdict;
|
|
|
357786 |
+ QDict *subqdict;
|
|
|
357786 |
+ char indexstr[32], prefix[32];
|
|
|
357786 |
+ size_t snprintf_ret;
|
|
|
357786 |
+
|
|
|
357786 |
+ snprintf_ret = snprintf(indexstr, 32, "%u", i);
|
|
|
357786 |
+ assert(snprintf_ret < 32);
|
|
|
357786 |
+
|
|
|
357786 |
+ subqobj = qdict_get(src, indexstr);
|
|
|
357786 |
+
|
|
|
357786 |
+ snprintf_ret = snprintf(prefix, 32, "%u.", i);
|
|
|
357786 |
+ assert(snprintf_ret < 32);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Overflow is the same as positive non-zero results */
|
|
|
357786 |
+ is_subqdict = qdict_count_prefixed_entries(src, prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+ /*
|
|
|
357786 |
+ * There may be either a single subordinate object (named
|
|
|
357786 |
+ * "%u") or multiple objects (each with a key prefixed "%u."),
|
|
|
357786 |
+ * but not both.
|
|
|
357786 |
+ */
|
|
|
357786 |
+ if (!subqobj == !is_subqdict) {
|
|
|
357786 |
+ break;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ if (is_subqdict) {
|
|
|
357786 |
+ qdict_extract_subqdict(src, &subqdict, prefix);
|
|
|
357786 |
+ assert(qdict_size(subqdict) > 0);
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ qobject_ref(subqobj);
|
|
|
357786 |
+ qdict_del(src, indexstr);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_split_flat_key:
|
|
|
357786 |
+ * @key: the key string to split
|
|
|
357786 |
+ * @prefix: non-NULL pointer to hold extracted prefix
|
|
|
357786 |
+ * @suffix: non-NULL pointer to remaining suffix
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Given a flattened key such as 'foo.0.bar', split it into two parts
|
|
|
357786 |
+ * at the first '.' separator. Allows double dot ('..') to escape the
|
|
|
357786 |
+ * normal separator.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * e.g.
|
|
|
357786 |
+ * 'foo.0.bar' -> prefix='foo' and suffix='0.bar'
|
|
|
357786 |
+ * 'foo..0.bar' -> prefix='foo.0' and suffix='bar'
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * The '..' sequence will be unescaped in the returned 'prefix'
|
|
|
357786 |
+ * string. The 'suffix' string will be left in escaped format, so it
|
|
|
357786 |
+ * can be fed back into the qdict_split_flat_key() key as the input
|
|
|
357786 |
+ * later.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * The caller is responsible for freeing the string returned in @prefix
|
|
|
357786 |
+ * using g_free().
|
|
|
357786 |
+ */
|
|
|
357786 |
+static void qdict_split_flat_key(const char *key, char **prefix,
|
|
|
357786 |
+ const char **suffix)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const char *separator;
|
|
|
357786 |
+ size_t i, j;
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Find first '.' separator, but if there is a pair '..'
|
|
|
357786 |
+ * that acts as an escape, so skip over '..' */
|
|
|
357786 |
+ separator = NULL;
|
|
|
357786 |
+ do {
|
|
|
357786 |
+ if (separator) {
|
|
|
357786 |
+ separator += 2;
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ separator = key;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ separator = strchr(separator, '.');
|
|
|
357786 |
+ } while (separator && separator[1] == '.');
|
|
|
357786 |
+
|
|
|
357786 |
+ if (separator) {
|
|
|
357786 |
+ *prefix = g_strndup(key, separator - key);
|
|
|
357786 |
+ *suffix = separator + 1;
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ *prefix = g_strdup(key);
|
|
|
357786 |
+ *suffix = NULL;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Unescape the '..' sequence into '.' */
|
|
|
357786 |
+ for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
|
|
|
357786 |
+ if ((*prefix)[i] == '.') {
|
|
|
357786 |
+ assert((*prefix)[i + 1] == '.');
|
|
|
357786 |
+ i++;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ (*prefix)[j] = (*prefix)[i];
|
|
|
357786 |
+ }
|
|
|
357786 |
+ (*prefix)[j] = '\0';
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_is_list:
|
|
|
357786 |
+ * @maybe_list: dict to check if keys represent list elements.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Determine whether all keys in @maybe_list are valid list elements.
|
|
|
357786 |
+ * If @maybe_list is non-zero in length and all the keys look like
|
|
|
357786 |
+ * valid list indexes, this will return 1. If @maybe_list is zero
|
|
|
357786 |
+ * length or all keys are non-numeric then it will return 0 to indicate
|
|
|
357786 |
+ * it is a normal qdict. If there is a mix of numeric and non-numeric
|
|
|
357786 |
+ * keys, or the list indexes are non-contiguous, an error is reported.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Returns: 1 if a valid list, 0 if a dict, -1 on error
|
|
|
357786 |
+ */
|
|
|
357786 |
+static int qdict_is_list(QDict *maybe_list, Error **errp)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *ent;
|
|
|
357786 |
+ ssize_t len = 0;
|
|
|
357786 |
+ ssize_t max = -1;
|
|
|
357786 |
+ int is_list = -1;
|
|
|
357786 |
+ int64_t val;
|
|
|
357786 |
+
|
|
|
357786 |
+ for (ent = qdict_first(maybe_list); ent != NULL;
|
|
|
357786 |
+ ent = qdict_next(maybe_list, ent)) {
|
|
|
357786 |
+
|
|
|
357786 |
+ if (qemu_strtoi64(ent->key, NULL, 10, &val) == 0) {
|
|
|
357786 |
+ if (is_list == -1) {
|
|
|
357786 |
+ is_list = 1;
|
|
|
357786 |
+ } else if (!is_list) {
|
|
|
357786 |
+ error_setg(errp,
|
|
|
357786 |
+ "Cannot mix list and non-list keys");
|
|
|
357786 |
+ return -1;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ len++;
|
|
|
357786 |
+ if (val > max) {
|
|
|
357786 |
+ max = val;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ if (is_list == -1) {
|
|
|
357786 |
+ is_list = 0;
|
|
|
357786 |
+ } else if (is_list) {
|
|
|
357786 |
+ error_setg(errp,
|
|
|
357786 |
+ "Cannot mix list and non-list keys");
|
|
|
357786 |
+ return -1;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ }
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ if (is_list == -1) {
|
|
|
357786 |
+ assert(!qdict_size(maybe_list));
|
|
|
357786 |
+ is_list = 0;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* NB this isn't a perfect check - e.g. it won't catch
|
|
|
357786 |
+ * a list containing '1', '+1', '01', '3', but that
|
|
|
357786 |
+ * does not matter - we've still proved that the
|
|
|
357786 |
+ * input is a list. It is up the caller to do a
|
|
|
357786 |
+ * stricter check if desired */
|
|
|
357786 |
+ if (len != (max + 1)) {
|
|
|
357786 |
+ error_setg(errp, "List indices are not contiguous, "
|
|
|
357786 |
+ "saw %zd elements but %zd largest index",
|
|
|
357786 |
+ len, max);
|
|
|
357786 |
+ return -1;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ return is_list;
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_crumple:
|
|
|
357786 |
+ * @src: the original flat dictionary (only scalar values) to crumple
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Takes a flat dictionary whose keys use '.' separator to indicate
|
|
|
357786 |
+ * nesting, and values are scalars, and crumples it into a nested
|
|
|
357786 |
+ * structure.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * To include a literal '.' in a key name, it must be escaped as '..'
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * For example, an input of:
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
|
|
|
357786 |
+ * 'foo.1.bar': 'two', 'foo.1.wizz': '2' }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * will result in an output of:
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * 'foo': [
|
|
|
357786 |
+ * { 'bar': 'one', 'wizz': '1' },
|
|
|
357786 |
+ * { 'bar': 'two', 'wizz': '2' }
|
|
|
357786 |
+ * ],
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * The following scenarios in the input dict will result in an
|
|
|
357786 |
+ * error being returned:
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * - Any values in @src are non-scalar types
|
|
|
357786 |
+ * - If keys in @src imply that a particular level is both a
|
|
|
357786 |
+ * list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
|
|
|
357786 |
+ * - If keys in @src imply that a particular level is a list,
|
|
|
357786 |
+ * but the indices are non-contiguous. e.g. "foo.0.bar" and
|
|
|
357786 |
+ * "foo.2.bar" without any "foo.1.bar" present.
|
|
|
357786 |
+ * - If keys in @src represent list indexes, but are not in
|
|
|
357786 |
+ * the "%zu" format. e.g. "foo.+0.bar"
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Returns: either a QDict or QList for the nested data structure, or NULL
|
|
|
357786 |
+ * on error
|
|
|
357786 |
+ */
|
|
|
357786 |
+QObject *qdict_crumple(const QDict *src, Error **errp)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *ent;
|
|
|
357786 |
+ QDict *two_level, *multi_level = NULL;
|
|
|
357786 |
+ QObject *dst = NULL, *child;
|
|
|
357786 |
+ size_t i;
|
|
|
357786 |
+ char *prefix = NULL;
|
|
|
357786 |
+ const char *suffix = NULL;
|
|
|
357786 |
+ int is_list;
|
|
|
357786 |
+
|
|
|
357786 |
+ two_level = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Step 1: split our totally flat dict into a two level dict */
|
|
|
357786 |
+ for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
|
|
|
357786 |
+ if (qobject_type(ent->value) == QTYPE_QDICT ||
|
|
|
357786 |
+ qobject_type(ent->value) == QTYPE_QLIST) {
|
|
|
357786 |
+ error_setg(errp, "Value %s is not a scalar",
|
|
|
357786 |
+ ent->key);
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_split_flat_key(ent->key, &prefix, &suffix);
|
|
|
357786 |
+
|
|
|
357786 |
+ child = qdict_get(two_level, prefix);
|
|
|
357786 |
+ if (suffix) {
|
|
|
357786 |
+ QDict *child_dict = qobject_to(QDict, child);
|
|
|
357786 |
+ if (!child_dict) {
|
|
|
357786 |
+ if (child) {
|
|
|
357786 |
+ error_setg(errp, "Key %s prefix is already set as a scalar",
|
|
|
357786 |
+ prefix);
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ child_dict = qdict_new();
|
|
|
357786 |
+ qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ if (child) {
|
|
|
357786 |
+ error_setg(errp, "Key %s prefix is already set as a dict",
|
|
|
357786 |
+ prefix);
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ g_free(prefix);
|
|
|
357786 |
+ prefix = NULL;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Step 2: optionally process the two level dict recursively
|
|
|
357786 |
+ * into a multi-level dict */
|
|
|
357786 |
+ multi_level = qdict_new();
|
|
|
357786 |
+ for (ent = qdict_first(two_level); ent != NULL;
|
|
|
357786 |
+ ent = qdict_next(two_level, ent)) {
|
|
|
357786 |
+ QDict *dict = qobject_to(QDict, ent->value);
|
|
|
357786 |
+ if (dict) {
|
|
|
357786 |
+ child = qdict_crumple(dict, errp);
|
|
|
357786 |
+ if (!child) {
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_obj(multi_level, ent->key, child);
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
|
|
|
357786 |
+ }
|
|
|
357786 |
+ }
|
|
|
357786 |
+ qobject_unref(two_level);
|
|
|
357786 |
+ two_level = NULL;
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Step 3: detect if we need to turn our dict into list */
|
|
|
357786 |
+ is_list = qdict_is_list(multi_level, errp);
|
|
|
357786 |
+ if (is_list < 0) {
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ if (is_list) {
|
|
|
357786 |
+ dst = QOBJECT(qlist_new());
|
|
|
357786 |
+
|
|
|
357786 |
+ for (i = 0; i < qdict_size(multi_level); i++) {
|
|
|
357786 |
+ char *key = g_strdup_printf("%zu", i);
|
|
|
357786 |
+
|
|
|
357786 |
+ child = qdict_get(multi_level, key);
|
|
|
357786 |
+ g_free(key);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (!child) {
|
|
|
357786 |
+ error_setg(errp, "Missing list index %zu", i);
|
|
|
357786 |
+ goto error;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
|
|
|
357786 |
+ }
|
|
|
357786 |
+ qobject_unref(multi_level);
|
|
|
357786 |
+ multi_level = NULL;
|
|
|
357786 |
+ } else {
|
|
|
357786 |
+ dst = QOBJECT(multi_level);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ return dst;
|
|
|
357786 |
+
|
|
|
357786 |
+ error:
|
|
|
357786 |
+ g_free(prefix);
|
|
|
357786 |
+ qobject_unref(multi_level);
|
|
|
357786 |
+ qobject_unref(two_level);
|
|
|
357786 |
+ qobject_unref(dst);
|
|
|
357786 |
+ return NULL;
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_array_entries(): Returns the number of direct array entries if the
|
|
|
357786 |
+ * sub-QDict of src specified by the prefix in subqdict (or src itself for
|
|
|
357786 |
+ * prefix == "") is valid as an array, i.e. the length of the created list if
|
|
|
357786 |
+ * the sub-QDict would become empty after calling qdict_array_split() on it. If
|
|
|
357786 |
+ * the array is not valid, -EINVAL is returned.
|
|
|
357786 |
+ */
|
|
|
357786 |
+int qdict_array_entries(QDict *src, const char *subqdict)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *entry;
|
|
|
357786 |
+ unsigned i;
|
|
|
357786 |
+ unsigned entries = 0;
|
|
|
357786 |
+ size_t subqdict_len = strlen(subqdict);
|
|
|
357786 |
+
|
|
|
357786 |
+ assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
|
|
|
357786 |
+
|
|
|
357786 |
+ /* qdict_array_split() loops until UINT_MAX, but as we want to return
|
|
|
357786 |
+ * negative errors, we only have a signed return value here. Any additional
|
|
|
357786 |
+ * entries will lead to -EINVAL. */
|
|
|
357786 |
+ for (i = 0; i < INT_MAX; i++) {
|
|
|
357786 |
+ QObject *subqobj;
|
|
|
357786 |
+ int subqdict_entries;
|
|
|
357786 |
+ char *prefix = g_strdup_printf("%s%u.", subqdict, i);
|
|
|
357786 |
+
|
|
|
357786 |
+ subqdict_entries = qdict_count_prefixed_entries(src, prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Remove ending "." */
|
|
|
357786 |
+ prefix[strlen(prefix) - 1] = 0;
|
|
|
357786 |
+ subqobj = qdict_get(src, prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_free(prefix);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (subqdict_entries < 0) {
|
|
|
357786 |
+ return subqdict_entries;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* There may be either a single subordinate object (named "%u") or
|
|
|
357786 |
+ * multiple objects (each with a key prefixed "%u."), but not both. */
|
|
|
357786 |
+ if (subqobj && subqdict_entries) {
|
|
|
357786 |
+ return -EINVAL;
|
|
|
357786 |
+ } else if (!subqobj && !subqdict_entries) {
|
|
|
357786 |
+ break;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ entries += subqdict_entries ? subqdict_entries : 1;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Consider everything handled that isn't part of the given sub-QDict */
|
|
|
357786 |
+ for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
|
|
|
357786 |
+ if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
|
|
|
357786 |
+ entries++;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Anything left in the sub-QDict that wasn't handled? */
|
|
|
357786 |
+ if (qdict_size(src) != entries) {
|
|
|
357786 |
+ return -EINVAL;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ return i;
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
|
|
|
357786 |
+ * elements from src to dest.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * If an element from src has a key already present in dest, it will not be
|
|
|
357786 |
+ * moved unless overwrite is true.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * If overwrite is true, the conflicting values in dest will be discarded and
|
|
|
357786 |
+ * replaced by the corresponding values from src.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Therefore, with overwrite being true, the src QDict will always be empty when
|
|
|
357786 |
+ * this function returns. If overwrite is false, the src QDict will be empty
|
|
|
357786 |
+ * iff there were no conflicts.
|
|
|
357786 |
+ */
|
|
|
357786 |
+void qdict_join(QDict *dest, QDict *src, bool overwrite)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *entry, *next;
|
|
|
357786 |
+
|
|
|
357786 |
+ entry = qdict_first(src);
|
|
|
357786 |
+ while (entry) {
|
|
|
357786 |
+ next = qdict_next(src, entry);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (overwrite || !qdict_haskey(dest, entry->key)) {
|
|
|
357786 |
+ qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
|
|
|
357786 |
+ qdict_del(src, entry->key);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ entry = next;
|
|
|
357786 |
+ }
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+/**
|
|
|
357786 |
+ * qdict_rename_keys(): Rename keys in qdict according to the replacements
|
|
|
357786 |
+ * specified in the array renames. The array must be terminated by an entry
|
|
|
357786 |
+ * with from = NULL.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * The renames are performed individually in the order of the array, so entries
|
|
|
357786 |
+ * may be renamed multiple times and may or may not conflict depending on the
|
|
|
357786 |
+ * order of the renames array.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Returns true for success, false in error cases.
|
|
|
357786 |
+ */
|
|
|
357786 |
+bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QObject *qobj;
|
|
|
357786 |
+
|
|
|
357786 |
+ while (renames->from) {
|
|
|
357786 |
+ if (qdict_haskey(qdict, renames->from)) {
|
|
|
357786 |
+ if (qdict_haskey(qdict, renames->to)) {
|
|
|
357786 |
+ error_setg(errp, "'%s' and its alias '%s' can't be used at the "
|
|
|
357786 |
+ "same time", renames->to, renames->from);
|
|
|
357786 |
+ return false;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ qobj = qdict_get(qdict, renames->from);
|
|
|
357786 |
+ qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
|
|
|
357786 |
+ qdict_del(qdict, renames->from);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ renames++;
|
|
|
357786 |
+ }
|
|
|
357786 |
+ return true;
|
|
|
357786 |
+}
|
|
|
357786 |
diff --git a/qobject/qdict.c b/qobject/qdict.c
|
|
|
357786 |
index 0554c64..3d8c2f7 100644
|
|
|
357786 |
--- a/qobject/qdict.c
|
|
|
357786 |
+++ b/qobject/qdict.c
|
|
|
357786 |
@@ -11,17 +11,11 @@
|
|
|
357786 |
*/
|
|
|
357786 |
|
|
|
357786 |
#include "qemu/osdep.h"
|
|
|
357786 |
-#include "block/qdict.h"
|
|
|
357786 |
#include "qapi/qmp/qnum.h"
|
|
|
357786 |
#include "qapi/qmp/qdict.h"
|
|
|
357786 |
#include "qapi/qmp/qbool.h"
|
|
|
357786 |
-#include "qapi/qmp/qlist.h"
|
|
|
357786 |
#include "qapi/qmp/qnull.h"
|
|
|
357786 |
#include "qapi/qmp/qstring.h"
|
|
|
357786 |
-#include "qapi/error.h"
|
|
|
357786 |
-#include "qemu/queue.h"
|
|
|
357786 |
-#include "qemu-common.h"
|
|
|
357786 |
-#include "qemu/cutils.h"
|
|
|
357786 |
|
|
|
357786 |
/**
|
|
|
357786 |
* qdict_new(): Create a new QDict
|
|
|
357786 |
@@ -464,626 +458,3 @@ void qdict_destroy_obj(QObject *obj)
|
|
|
357786 |
|
|
|
357786 |
g_free(qdict);
|
|
|
357786 |
}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
|
|
|
357786 |
- * value of 'key' in 'src' is copied there (and the refcount increased
|
|
|
357786 |
- * accordingly).
|
|
|
357786 |
- */
|
|
|
357786 |
-void qdict_copy_default(QDict *dst, QDict *src, const char *key)
|
|
|
357786 |
-{
|
|
|
357786 |
- QObject *val;
|
|
|
357786 |
-
|
|
|
357786 |
- if (qdict_haskey(dst, key)) {
|
|
|
357786 |
- return;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- val = qdict_get(src, key);
|
|
|
357786 |
- if (val) {
|
|
|
357786 |
- qdict_put_obj(dst, key, qobject_ref(val));
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
|
|
|
357786 |
- * new QString initialised by 'val' is put there.
|
|
|
357786 |
- */
|
|
|
357786 |
-void qdict_set_default_str(QDict *dst, const char *key, const char *val)
|
|
|
357786 |
-{
|
|
|
357786 |
- if (qdict_haskey(dst, key)) {
|
|
|
357786 |
- return;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_str(dst, key, val);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_flatten_qdict(QDict *qdict, QDict *target,
|
|
|
357786 |
- const char *prefix);
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
|
|
|
357786 |
-{
|
|
|
357786 |
- QObject *value;
|
|
|
357786 |
- const QListEntry *entry;
|
|
|
357786 |
- char *new_key;
|
|
|
357786 |
- int i;
|
|
|
357786 |
-
|
|
|
357786 |
- /* This function is never called with prefix == NULL, i.e., it is always
|
|
|
357786 |
- * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
|
|
|
357786 |
- * need to remove list entries during the iteration (the whole list will be
|
|
|
357786 |
- * deleted eventually anyway from qdict_flatten_qdict()). */
|
|
|
357786 |
- assert(prefix);
|
|
|
357786 |
-
|
|
|
357786 |
- entry = qlist_first(qlist);
|
|
|
357786 |
-
|
|
|
357786 |
- for (i = 0; entry; entry = qlist_next(entry), i++) {
|
|
|
357786 |
- value = qlist_entry_obj(entry);
|
|
|
357786 |
- new_key = g_strdup_printf("%s.%i", prefix, i);
|
|
|
357786 |
-
|
|
|
357786 |
- if (qobject_type(value) == QTYPE_QDICT) {
|
|
|
357786 |
- qdict_flatten_qdict(qobject_to(QDict, value), target, new_key);
|
|
|
357786 |
- } else if (qobject_type(value) == QTYPE_QLIST) {
|
|
|
357786 |
- qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
|
|
|
357786 |
- } else {
|
|
|
357786 |
- /* All other types are moved to the target unchanged. */
|
|
|
357786 |
- qdict_put_obj(target, new_key, qobject_ref(value));
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- g_free(new_key);
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
|
|
|
357786 |
-{
|
|
|
357786 |
- QObject *value;
|
|
|
357786 |
- const QDictEntry *entry, *next;
|
|
|
357786 |
- char *new_key;
|
|
|
357786 |
- bool delete;
|
|
|
357786 |
-
|
|
|
357786 |
- entry = qdict_first(qdict);
|
|
|
357786 |
-
|
|
|
357786 |
- while (entry != NULL) {
|
|
|
357786 |
-
|
|
|
357786 |
- next = qdict_next(qdict, entry);
|
|
|
357786 |
- value = qdict_entry_value(entry);
|
|
|
357786 |
- new_key = NULL;
|
|
|
357786 |
- delete = false;
|
|
|
357786 |
-
|
|
|
357786 |
- if (prefix) {
|
|
|
357786 |
- new_key = g_strdup_printf("%s.%s", prefix, entry->key);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- if (qobject_type(value) == QTYPE_QDICT) {
|
|
|
357786 |
- /* Entries of QDicts are processed recursively, the QDict object
|
|
|
357786 |
- * itself disappears. */
|
|
|
357786 |
- qdict_flatten_qdict(qobject_to(QDict, value), target,
|
|
|
357786 |
- new_key ? new_key : entry->key);
|
|
|
357786 |
- delete = true;
|
|
|
357786 |
- } else if (qobject_type(value) == QTYPE_QLIST) {
|
|
|
357786 |
- qdict_flatten_qlist(qobject_to(QList, value), target,
|
|
|
357786 |
- new_key ? new_key : entry->key);
|
|
|
357786 |
- delete = true;
|
|
|
357786 |
- } else if (prefix) {
|
|
|
357786 |
- /* All other objects are moved to the target unchanged. */
|
|
|
357786 |
- qdict_put_obj(target, new_key, qobject_ref(value));
|
|
|
357786 |
- delete = true;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- g_free(new_key);
|
|
|
357786 |
-
|
|
|
357786 |
- if (delete) {
|
|
|
357786 |
- qdict_del(qdict, entry->key);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Restart loop after modifying the iterated QDict */
|
|
|
357786 |
- entry = qdict_first(qdict);
|
|
|
357786 |
- continue;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- entry = next;
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_flatten(): For each nested QDict with key x, all fields with key y
|
|
|
357786 |
- * are moved to this QDict and their key is renamed to "x.y". For each nested
|
|
|
357786 |
- * QList with key x, the field at index y is moved to this QDict with the key
|
|
|
357786 |
- * "x.y" (i.e., the reverse of what qdict_array_split() does).
|
|
|
357786 |
- * This operation is applied recursively for nested QDicts and QLists.
|
|
|
357786 |
- */
|
|
|
357786 |
-void qdict_flatten(QDict *qdict)
|
|
|
357786 |
-{
|
|
|
357786 |
- qdict_flatten_qdict(qdict, qdict, NULL);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/* extract all the src QDict entries starting by start into dst */
|
|
|
357786 |
-void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
|
|
|
357786 |
-
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *entry, *next;
|
|
|
357786 |
- const char *p;
|
|
|
357786 |
-
|
|
|
357786 |
- *dst = qdict_new();
|
|
|
357786 |
- entry = qdict_first(src);
|
|
|
357786 |
-
|
|
|
357786 |
- while (entry != NULL) {
|
|
|
357786 |
- next = qdict_next(src, entry);
|
|
|
357786 |
- if (strstart(entry->key, start, &p)) {
|
|
|
357786 |
- qdict_put_obj(*dst, p, qobject_ref(entry->value));
|
|
|
357786 |
- qdict_del(src, entry->key);
|
|
|
357786 |
- }
|
|
|
357786 |
- entry = next;
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static int qdict_count_prefixed_entries(const QDict *src, const char *start)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *entry;
|
|
|
357786 |
- int count = 0;
|
|
|
357786 |
-
|
|
|
357786 |
- for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
|
|
|
357786 |
- if (strstart(entry->key, start, NULL)) {
|
|
|
357786 |
- if (count == INT_MAX) {
|
|
|
357786 |
- return -ERANGE;
|
|
|
357786 |
- }
|
|
|
357786 |
- count++;
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- return count;
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_array_split(): This function moves array-like elements of a QDict into
|
|
|
357786 |
- * a new QList. Every entry in the original QDict with a key "%u" or one
|
|
|
357786 |
- * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
|
|
|
357786 |
- * incrementally counting up, will be moved to a new QDict at index %u in the
|
|
|
357786 |
- * output QList with the key prefix removed, if that prefix is "%u.". If the
|
|
|
357786 |
- * whole key is just "%u", the whole QObject will be moved unchanged without
|
|
|
357786 |
- * creating a new QDict. The function terminates when there is no entry in the
|
|
|
357786 |
- * QDict with a prefix directly (incrementally) following the last one; it also
|
|
|
357786 |
- * returns if there are both entries with "%u" and "%u." for the same index %u.
|
|
|
357786 |
- * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
|
|
|
357786 |
- * (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
|
|
|
357786 |
- * => [{"a": 42, "b": 23}, {"x": 0}, 66]
|
|
|
357786 |
- * and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
|
|
|
357786 |
- */
|
|
|
357786 |
-void qdict_array_split(QDict *src, QList **dst)
|
|
|
357786 |
-{
|
|
|
357786 |
- unsigned i;
|
|
|
357786 |
-
|
|
|
357786 |
- *dst = qlist_new();
|
|
|
357786 |
-
|
|
|
357786 |
- for (i = 0; i < UINT_MAX; i++) {
|
|
|
357786 |
- QObject *subqobj;
|
|
|
357786 |
- bool is_subqdict;
|
|
|
357786 |
- QDict *subqdict;
|
|
|
357786 |
- char indexstr[32], prefix[32];
|
|
|
357786 |
- size_t snprintf_ret;
|
|
|
357786 |
-
|
|
|
357786 |
- snprintf_ret = snprintf(indexstr, 32, "%u", i);
|
|
|
357786 |
- assert(snprintf_ret < 32);
|
|
|
357786 |
-
|
|
|
357786 |
- subqobj = qdict_get(src, indexstr);
|
|
|
357786 |
-
|
|
|
357786 |
- snprintf_ret = snprintf(prefix, 32, "%u.", i);
|
|
|
357786 |
- assert(snprintf_ret < 32);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Overflow is the same as positive non-zero results */
|
|
|
357786 |
- is_subqdict = qdict_count_prefixed_entries(src, prefix);
|
|
|
357786 |
-
|
|
|
357786 |
- // There may be either a single subordinate object (named "%u") or
|
|
|
357786 |
- // multiple objects (each with a key prefixed "%u."), but not both.
|
|
|
357786 |
- if (!subqobj == !is_subqdict) {
|
|
|
357786 |
- break;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- if (is_subqdict) {
|
|
|
357786 |
- qdict_extract_subqdict(src, &subqdict, prefix);
|
|
|
357786 |
- assert(qdict_size(subqdict) > 0);
|
|
|
357786 |
- } else {
|
|
|
357786 |
- qobject_ref(subqobj);
|
|
|
357786 |
- qdict_del(src, indexstr);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_split_flat_key:
|
|
|
357786 |
- * @key: the key string to split
|
|
|
357786 |
- * @prefix: non-NULL pointer to hold extracted prefix
|
|
|
357786 |
- * @suffix: non-NULL pointer to remaining suffix
|
|
|
357786 |
- *
|
|
|
357786 |
- * Given a flattened key such as 'foo.0.bar', split it into two parts
|
|
|
357786 |
- * at the first '.' separator. Allows double dot ('..') to escape the
|
|
|
357786 |
- * normal separator.
|
|
|
357786 |
- *
|
|
|
357786 |
- * e.g.
|
|
|
357786 |
- * 'foo.0.bar' -> prefix='foo' and suffix='0.bar'
|
|
|
357786 |
- * 'foo..0.bar' -> prefix='foo.0' and suffix='bar'
|
|
|
357786 |
- *
|
|
|
357786 |
- * The '..' sequence will be unescaped in the returned 'prefix'
|
|
|
357786 |
- * string. The 'suffix' string will be left in escaped format, so it
|
|
|
357786 |
- * can be fed back into the qdict_split_flat_key() key as the input
|
|
|
357786 |
- * later.
|
|
|
357786 |
- *
|
|
|
357786 |
- * The caller is responsible for freeing the string returned in @prefix
|
|
|
357786 |
- * using g_free().
|
|
|
357786 |
- */
|
|
|
357786 |
-static void qdict_split_flat_key(const char *key, char **prefix,
|
|
|
357786 |
- const char **suffix)
|
|
|
357786 |
-{
|
|
|
357786 |
- const char *separator;
|
|
|
357786 |
- size_t i, j;
|
|
|
357786 |
-
|
|
|
357786 |
- /* Find first '.' separator, but if there is a pair '..'
|
|
|
357786 |
- * that acts as an escape, so skip over '..' */
|
|
|
357786 |
- separator = NULL;
|
|
|
357786 |
- do {
|
|
|
357786 |
- if (separator) {
|
|
|
357786 |
- separator += 2;
|
|
|
357786 |
- } else {
|
|
|
357786 |
- separator = key;
|
|
|
357786 |
- }
|
|
|
357786 |
- separator = strchr(separator, '.');
|
|
|
357786 |
- } while (separator && separator[1] == '.');
|
|
|
357786 |
-
|
|
|
357786 |
- if (separator) {
|
|
|
357786 |
- *prefix = g_strndup(key, separator - key);
|
|
|
357786 |
- *suffix = separator + 1;
|
|
|
357786 |
- } else {
|
|
|
357786 |
- *prefix = g_strdup(key);
|
|
|
357786 |
- *suffix = NULL;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Unescape the '..' sequence into '.' */
|
|
|
357786 |
- for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
|
|
|
357786 |
- if ((*prefix)[i] == '.') {
|
|
|
357786 |
- assert((*prefix)[i + 1] == '.');
|
|
|
357786 |
- i++;
|
|
|
357786 |
- }
|
|
|
357786 |
- (*prefix)[j] = (*prefix)[i];
|
|
|
357786 |
- }
|
|
|
357786 |
- (*prefix)[j] = '\0';
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_is_list:
|
|
|
357786 |
- * @maybe_list: dict to check if keys represent list elements.
|
|
|
357786 |
- *
|
|
|
357786 |
- * Determine whether all keys in @maybe_list are valid list elements.
|
|
|
357786 |
- * If @maybe_list is non-zero in length and all the keys look like
|
|
|
357786 |
- * valid list indexes, this will return 1. If @maybe_list is zero
|
|
|
357786 |
- * length or all keys are non-numeric then it will return 0 to indicate
|
|
|
357786 |
- * it is a normal qdict. If there is a mix of numeric and non-numeric
|
|
|
357786 |
- * keys, or the list indexes are non-contiguous, an error is reported.
|
|
|
357786 |
- *
|
|
|
357786 |
- * Returns: 1 if a valid list, 0 if a dict, -1 on error
|
|
|
357786 |
- */
|
|
|
357786 |
-static int qdict_is_list(QDict *maybe_list, Error **errp)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *ent;
|
|
|
357786 |
- ssize_t len = 0;
|
|
|
357786 |
- ssize_t max = -1;
|
|
|
357786 |
- int is_list = -1;
|
|
|
357786 |
- int64_t val;
|
|
|
357786 |
-
|
|
|
357786 |
- for (ent = qdict_first(maybe_list); ent != NULL;
|
|
|
357786 |
- ent = qdict_next(maybe_list, ent)) {
|
|
|
357786 |
-
|
|
|
357786 |
- if (qemu_strtoi64(ent->key, NULL, 10, &val) == 0) {
|
|
|
357786 |
- if (is_list == -1) {
|
|
|
357786 |
- is_list = 1;
|
|
|
357786 |
- } else if (!is_list) {
|
|
|
357786 |
- error_setg(errp,
|
|
|
357786 |
- "Cannot mix list and non-list keys");
|
|
|
357786 |
- return -1;
|
|
|
357786 |
- }
|
|
|
357786 |
- len++;
|
|
|
357786 |
- if (val > max) {
|
|
|
357786 |
- max = val;
|
|
|
357786 |
- }
|
|
|
357786 |
- } else {
|
|
|
357786 |
- if (is_list == -1) {
|
|
|
357786 |
- is_list = 0;
|
|
|
357786 |
- } else if (is_list) {
|
|
|
357786 |
- error_setg(errp,
|
|
|
357786 |
- "Cannot mix list and non-list keys");
|
|
|
357786 |
- return -1;
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- if (is_list == -1) {
|
|
|
357786 |
- assert(!qdict_size(maybe_list));
|
|
|
357786 |
- is_list = 0;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* NB this isn't a perfect check - e.g. it won't catch
|
|
|
357786 |
- * a list containing '1', '+1', '01', '3', but that
|
|
|
357786 |
- * does not matter - we've still proved that the
|
|
|
357786 |
- * input is a list. It is up the caller to do a
|
|
|
357786 |
- * stricter check if desired */
|
|
|
357786 |
- if (len != (max + 1)) {
|
|
|
357786 |
- error_setg(errp, "List indices are not contiguous, "
|
|
|
357786 |
- "saw %zd elements but %zd largest index",
|
|
|
357786 |
- len, max);
|
|
|
357786 |
- return -1;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- return is_list;
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_crumple:
|
|
|
357786 |
- * @src: the original flat dictionary (only scalar values) to crumple
|
|
|
357786 |
- *
|
|
|
357786 |
- * Takes a flat dictionary whose keys use '.' separator to indicate
|
|
|
357786 |
- * nesting, and values are scalars, and crumples it into a nested
|
|
|
357786 |
- * structure.
|
|
|
357786 |
- *
|
|
|
357786 |
- * To include a literal '.' in a key name, it must be escaped as '..'
|
|
|
357786 |
- *
|
|
|
357786 |
- * For example, an input of:
|
|
|
357786 |
- *
|
|
|
357786 |
- * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
|
|
|
357786 |
- * 'foo.1.bar': 'two', 'foo.1.wizz': '2' }
|
|
|
357786 |
- *
|
|
|
357786 |
- * will result in an output of:
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * 'foo': [
|
|
|
357786 |
- * { 'bar': 'one', 'wizz': '1' },
|
|
|
357786 |
- * { 'bar': 'two', 'wizz': '2' }
|
|
|
357786 |
- * ],
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * The following scenarios in the input dict will result in an
|
|
|
357786 |
- * error being returned:
|
|
|
357786 |
- *
|
|
|
357786 |
- * - Any values in @src are non-scalar types
|
|
|
357786 |
- * - If keys in @src imply that a particular level is both a
|
|
|
357786 |
- * list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
|
|
|
357786 |
- * - If keys in @src imply that a particular level is a list,
|
|
|
357786 |
- * but the indices are non-contiguous. e.g. "foo.0.bar" and
|
|
|
357786 |
- * "foo.2.bar" without any "foo.1.bar" present.
|
|
|
357786 |
- * - If keys in @src represent list indexes, but are not in
|
|
|
357786 |
- * the "%zu" format. e.g. "foo.+0.bar"
|
|
|
357786 |
- *
|
|
|
357786 |
- * Returns: either a QDict or QList for the nested data structure, or NULL
|
|
|
357786 |
- * on error
|
|
|
357786 |
- */
|
|
|
357786 |
-QObject *qdict_crumple(const QDict *src, Error **errp)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *ent;
|
|
|
357786 |
- QDict *two_level, *multi_level = NULL;
|
|
|
357786 |
- QObject *dst = NULL, *child;
|
|
|
357786 |
- size_t i;
|
|
|
357786 |
- char *prefix = NULL;
|
|
|
357786 |
- const char *suffix = NULL;
|
|
|
357786 |
- int is_list;
|
|
|
357786 |
-
|
|
|
357786 |
- two_level = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- /* Step 1: split our totally flat dict into a two level dict */
|
|
|
357786 |
- for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
|
|
|
357786 |
- if (qobject_type(ent->value) == QTYPE_QDICT ||
|
|
|
357786 |
- qobject_type(ent->value) == QTYPE_QLIST) {
|
|
|
357786 |
- error_setg(errp, "Value %s is not a scalar",
|
|
|
357786 |
- ent->key);
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_split_flat_key(ent->key, &prefix, &suffix);
|
|
|
357786 |
-
|
|
|
357786 |
- child = qdict_get(two_level, prefix);
|
|
|
357786 |
- if (suffix) {
|
|
|
357786 |
- QDict *child_dict = qobject_to(QDict, child);
|
|
|
357786 |
- if (!child_dict) {
|
|
|
357786 |
- if (child) {
|
|
|
357786 |
- error_setg(errp, "Key %s prefix is already set as a scalar",
|
|
|
357786 |
- prefix);
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- child_dict = qdict_new();
|
|
|
357786 |
- qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
|
|
|
357786 |
- } else {
|
|
|
357786 |
- if (child) {
|
|
|
357786 |
- error_setg(errp, "Key %s prefix is already set as a dict",
|
|
|
357786 |
- prefix);
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
- qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- g_free(prefix);
|
|
|
357786 |
- prefix = NULL;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Step 2: optionally process the two level dict recursively
|
|
|
357786 |
- * into a multi-level dict */
|
|
|
357786 |
- multi_level = qdict_new();
|
|
|
357786 |
- for (ent = qdict_first(two_level); ent != NULL;
|
|
|
357786 |
- ent = qdict_next(two_level, ent)) {
|
|
|
357786 |
- QDict *dict = qobject_to(QDict, ent->value);
|
|
|
357786 |
- if (dict) {
|
|
|
357786 |
- child = qdict_crumple(dict, errp);
|
|
|
357786 |
- if (!child) {
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_obj(multi_level, ent->key, child);
|
|
|
357786 |
- } else {
|
|
|
357786 |
- qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
- qobject_unref(two_level);
|
|
|
357786 |
- two_level = NULL;
|
|
|
357786 |
-
|
|
|
357786 |
- /* Step 3: detect if we need to turn our dict into list */
|
|
|
357786 |
- is_list = qdict_is_list(multi_level, errp);
|
|
|
357786 |
- if (is_list < 0) {
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- if (is_list) {
|
|
|
357786 |
- dst = QOBJECT(qlist_new());
|
|
|
357786 |
-
|
|
|
357786 |
- for (i = 0; i < qdict_size(multi_level); i++) {
|
|
|
357786 |
- char *key = g_strdup_printf("%zu", i);
|
|
|
357786 |
-
|
|
|
357786 |
- child = qdict_get(multi_level, key);
|
|
|
357786 |
- g_free(key);
|
|
|
357786 |
-
|
|
|
357786 |
- if (!child) {
|
|
|
357786 |
- error_setg(errp, "Missing list index %zu", i);
|
|
|
357786 |
- goto error;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
|
|
|
357786 |
- }
|
|
|
357786 |
- qobject_unref(multi_level);
|
|
|
357786 |
- multi_level = NULL;
|
|
|
357786 |
- } else {
|
|
|
357786 |
- dst = QOBJECT(multi_level);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- return dst;
|
|
|
357786 |
-
|
|
|
357786 |
- error:
|
|
|
357786 |
- g_free(prefix);
|
|
|
357786 |
- qobject_unref(multi_level);
|
|
|
357786 |
- qobject_unref(two_level);
|
|
|
357786 |
- qobject_unref(dst);
|
|
|
357786 |
- return NULL;
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_array_entries(): Returns the number of direct array entries if the
|
|
|
357786 |
- * sub-QDict of src specified by the prefix in subqdict (or src itself for
|
|
|
357786 |
- * prefix == "") is valid as an array, i.e. the length of the created list if
|
|
|
357786 |
- * the sub-QDict would become empty after calling qdict_array_split() on it. If
|
|
|
357786 |
- * the array is not valid, -EINVAL is returned.
|
|
|
357786 |
- */
|
|
|
357786 |
-int qdict_array_entries(QDict *src, const char *subqdict)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *entry;
|
|
|
357786 |
- unsigned i;
|
|
|
357786 |
- unsigned entries = 0;
|
|
|
357786 |
- size_t subqdict_len = strlen(subqdict);
|
|
|
357786 |
-
|
|
|
357786 |
- assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
|
|
|
357786 |
-
|
|
|
357786 |
- /* qdict_array_split() loops until UINT_MAX, but as we want to return
|
|
|
357786 |
- * negative errors, we only have a signed return value here. Any additional
|
|
|
357786 |
- * entries will lead to -EINVAL. */
|
|
|
357786 |
- for (i = 0; i < INT_MAX; i++) {
|
|
|
357786 |
- QObject *subqobj;
|
|
|
357786 |
- int subqdict_entries;
|
|
|
357786 |
- char *prefix = g_strdup_printf("%s%u.", subqdict, i);
|
|
|
357786 |
-
|
|
|
357786 |
- subqdict_entries = qdict_count_prefixed_entries(src, prefix);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Remove ending "." */
|
|
|
357786 |
- prefix[strlen(prefix) - 1] = 0;
|
|
|
357786 |
- subqobj = qdict_get(src, prefix);
|
|
|
357786 |
-
|
|
|
357786 |
- g_free(prefix);
|
|
|
357786 |
-
|
|
|
357786 |
- if (subqdict_entries < 0) {
|
|
|
357786 |
- return subqdict_entries;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* There may be either a single subordinate object (named "%u") or
|
|
|
357786 |
- * multiple objects (each with a key prefixed "%u."), but not both. */
|
|
|
357786 |
- if (subqobj && subqdict_entries) {
|
|
|
357786 |
- return -EINVAL;
|
|
|
357786 |
- } else if (!subqobj && !subqdict_entries) {
|
|
|
357786 |
- break;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- entries += subqdict_entries ? subqdict_entries : 1;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Consider everything handled that isn't part of the given sub-QDict */
|
|
|
357786 |
- for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
|
|
|
357786 |
- if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
|
|
|
357786 |
- entries++;
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Anything left in the sub-QDict that wasn't handled? */
|
|
|
357786 |
- if (qdict_size(src) != entries) {
|
|
|
357786 |
- return -EINVAL;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- return i;
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
|
|
|
357786 |
- * elements from src to dest.
|
|
|
357786 |
- *
|
|
|
357786 |
- * If an element from src has a key already present in dest, it will not be
|
|
|
357786 |
- * moved unless overwrite is true.
|
|
|
357786 |
- *
|
|
|
357786 |
- * If overwrite is true, the conflicting values in dest will be discarded and
|
|
|
357786 |
- * replaced by the corresponding values from src.
|
|
|
357786 |
- *
|
|
|
357786 |
- * Therefore, with overwrite being true, the src QDict will always be empty when
|
|
|
357786 |
- * this function returns. If overwrite is false, the src QDict will be empty
|
|
|
357786 |
- * iff there were no conflicts.
|
|
|
357786 |
- */
|
|
|
357786 |
-void qdict_join(QDict *dest, QDict *src, bool overwrite)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *entry, *next;
|
|
|
357786 |
-
|
|
|
357786 |
- entry = qdict_first(src);
|
|
|
357786 |
- while (entry) {
|
|
|
357786 |
- next = qdict_next(src, entry);
|
|
|
357786 |
-
|
|
|
357786 |
- if (overwrite || !qdict_haskey(dest, entry->key)) {
|
|
|
357786 |
- qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
|
|
|
357786 |
- qdict_del(src, entry->key);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- entry = next;
|
|
|
357786 |
- }
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-/**
|
|
|
357786 |
- * qdict_rename_keys(): Rename keys in qdict according to the replacements
|
|
|
357786 |
- * specified in the array renames. The array must be terminated by an entry
|
|
|
357786 |
- * with from = NULL.
|
|
|
357786 |
- *
|
|
|
357786 |
- * The renames are performed individually in the order of the array, so entries
|
|
|
357786 |
- * may be renamed multiple times and may or may not conflict depending on the
|
|
|
357786 |
- * order of the renames array.
|
|
|
357786 |
- *
|
|
|
357786 |
- * Returns true for success, false in error cases.
|
|
|
357786 |
- */
|
|
|
357786 |
-bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
|
|
|
357786 |
-{
|
|
|
357786 |
- QObject *qobj;
|
|
|
357786 |
-
|
|
|
357786 |
- while (renames->from) {
|
|
|
357786 |
- if (qdict_haskey(qdict, renames->from)) {
|
|
|
357786 |
- if (qdict_haskey(qdict, renames->to)) {
|
|
|
357786 |
- error_setg(errp, "'%s' and its alias '%s' can't be used at the "
|
|
|
357786 |
- "same time", renames->to, renames->from);
|
|
|
357786 |
- return false;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- qobj = qdict_get(qdict, renames->from);
|
|
|
357786 |
- qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
|
|
|
357786 |
- qdict_del(qdict, renames->from);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- renames++;
|
|
|
357786 |
- }
|
|
|
357786 |
- return true;
|
|
|
357786 |
-}
|
|
|
357786 |
diff --git a/tests/Makefile.include b/tests/Makefile.include
|
|
|
357786 |
index 5cb1902..9dd63e5 100644
|
|
|
357786 |
--- a/tests/Makefile.include
|
|
|
357786 |
+++ b/tests/Makefile.include
|
|
|
357786 |
@@ -40,6 +40,8 @@ SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \
|
|
|
357786 |
|
|
|
357786 |
check-unit-y = tests/check-qdict$(EXESUF)
|
|
|
357786 |
gcov-files-check-qdict-y = qobject/qdict.c
|
|
|
357786 |
+check-unit-y = tests/check-block-qdict$(EXESUF)
|
|
|
357786 |
+gcov-files-check-block-qdict-y = qobject/block-qdict.c
|
|
|
357786 |
check-unit-y += tests/test-char$(EXESUF)
|
|
|
357786 |
gcov-files-check-qdict-y = chardev/char.c
|
|
|
357786 |
check-unit-y += tests/check-qnum$(EXESUF)
|
|
|
357786 |
@@ -559,6 +561,7 @@ GENERATED_FILES += tests/test-qapi-types.h tests/test-qapi-visit.h \
|
|
|
357786 |
test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
|
|
|
357786 |
tests/check-qlist.o tests/check-qnull.o tests/check-qobject.o \
|
|
|
357786 |
tests/check-qjson.o tests/check-qlit.o \
|
|
|
357786 |
+ tests/check-block-qtest.o \
|
|
|
357786 |
tests/test-coroutine.o tests/test-string-output-visitor.o \
|
|
|
357786 |
tests/test-string-input-visitor.o tests/test-qobject-output-visitor.o \
|
|
|
357786 |
tests/test-clone-visitor.o \
|
|
|
357786 |
@@ -589,6 +592,7 @@ test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o
|
|
|
357786 |
tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
|
|
|
357786 |
tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
|
|
|
357786 |
tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
|
|
|
357786 |
+tests/check-block-qdict$(EXESUF): tests/check-block-qdict.o $(test-util-obj-y)
|
|
|
357786 |
tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
|
|
|
357786 |
tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
|
|
|
357786 |
tests/check-qobject$(EXESUF): tests/check-qobject.o $(test-util-obj-y)
|
|
|
357786 |
diff --git a/tests/check-block-qdict.c b/tests/check-block-qdict.c
|
|
|
357786 |
new file mode 100644
|
|
|
357786 |
index 0000000..5b9f4d5
|
|
|
357786 |
--- /dev/null
|
|
|
357786 |
+++ b/tests/check-block-qdict.c
|
|
|
357786 |
@@ -0,0 +1,655 @@
|
|
|
357786 |
+/*
|
|
|
357786 |
+ * Unit-tests for Block layer QDict extras
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * Copyright (c) 2013-2018 Red Hat, Inc.
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
357786 |
+ * See the COPYING.LIB file in the top-level directory.
|
|
|
357786 |
+ */
|
|
|
357786 |
+
|
|
|
357786 |
+#include "qemu/osdep.h"
|
|
|
357786 |
+#include "block/qdict.h"
|
|
|
357786 |
+#include "qapi/qmp/qlist.h"
|
|
|
357786 |
+#include "qapi/qmp/qnum.h"
|
|
|
357786 |
+#include "qapi/error.h"
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_defaults_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *dict, *copy;
|
|
|
357786 |
+
|
|
|
357786 |
+ dict = qdict_new();
|
|
|
357786 |
+ copy = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_set_default_str(dict, "foo", "abc");
|
|
|
357786 |
+ qdict_set_default_str(dict, "foo", "def");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "abc");
|
|
|
357786 |
+ qdict_set_default_str(dict, "bar", "ghi");
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_copy_default(copy, dict, "foo");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "foo"), ==, "abc");
|
|
|
357786 |
+ qdict_set_default_str(copy, "bar", "xyz");
|
|
|
357786 |
+ qdict_copy_default(copy, dict, "bar");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "bar"), ==, "xyz");
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(copy);
|
|
|
357786 |
+ qobject_unref(dict);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_flatten_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QList *list1 = qlist_new();
|
|
|
357786 |
+ QList *list2 = qlist_new();
|
|
|
357786 |
+ QDict *dict1 = qdict_new();
|
|
|
357786 |
+ QDict *dict2 = qdict_new();
|
|
|
357786 |
+ QDict *dict3 = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ /*
|
|
|
357786 |
+ * Test the flattening of
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "e": [
|
|
|
357786 |
+ * 42,
|
|
|
357786 |
+ * [
|
|
|
357786 |
+ * 23,
|
|
|
357786 |
+ * 66,
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "a": 0,
|
|
|
357786 |
+ * "b": 1
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ * ]
|
|
|
357786 |
+ * ],
|
|
|
357786 |
+ * "f": {
|
|
|
357786 |
+ * "c": 2,
|
|
|
357786 |
+ * "d": 3,
|
|
|
357786 |
+ * },
|
|
|
357786 |
+ * "g": 4
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * to
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "e.0": 42,
|
|
|
357786 |
+ * "e.1.0": 23,
|
|
|
357786 |
+ * "e.1.1": 66,
|
|
|
357786 |
+ * "e.1.2.a": 0,
|
|
|
357786 |
+ * "e.1.2.b": 1,
|
|
|
357786 |
+ * "f.c": 2,
|
|
|
357786 |
+ * "f.d": 3,
|
|
|
357786 |
+ * "g": 4
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ */
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict1, "a", 0);
|
|
|
357786 |
+ qdict_put_int(dict1, "b", 1);
|
|
|
357786 |
+
|
|
|
357786 |
+ qlist_append_int(list1, 23);
|
|
|
357786 |
+ qlist_append_int(list1, 66);
|
|
|
357786 |
+ qlist_append(list1, dict1);
|
|
|
357786 |
+ qlist_append_int(list2, 42);
|
|
|
357786 |
+ qlist_append(list2, list1);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict2, "c", 2);
|
|
|
357786 |
+ qdict_put_int(dict2, "d", 3);
|
|
|
357786 |
+ qdict_put(dict3, "e", list2);
|
|
|
357786 |
+ qdict_put(dict3, "f", dict2);
|
|
|
357786 |
+ qdict_put_int(dict3, "g", 4);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_flatten(dict3);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "e.0") == 42);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "e.1.0") == 23);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "e.1.1") == 66);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "e.1.2.a") == 0);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "e.1.2.b") == 1);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "f.c") == 2);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "f.d") == 3);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict3, "g") == 4);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict3) == 8);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict3);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_array_split_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *test_dict = qdict_new();
|
|
|
357786 |
+ QDict *dict1, *dict2;
|
|
|
357786 |
+ QNum *int1;
|
|
|
357786 |
+ QList *test_list;
|
|
|
357786 |
+
|
|
|
357786 |
+ /*
|
|
|
357786 |
+ * Test the split of
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "1.x": 0,
|
|
|
357786 |
+ * "4.y": 1,
|
|
|
357786 |
+ * "0.a": 42,
|
|
|
357786 |
+ * "o.o": 7,
|
|
|
357786 |
+ * "0.b": 23,
|
|
|
357786 |
+ * "2": 66
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * to
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * [
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "a": 42,
|
|
|
357786 |
+ * "b": 23
|
|
|
357786 |
+ * },
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "x": 0
|
|
|
357786 |
+ * },
|
|
|
357786 |
+ * 66
|
|
|
357786 |
+ * ]
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * and
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "4.y": 1,
|
|
|
357786 |
+ * "o.o": 7
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * (remaining in the old QDict)
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * This example is given in the comment of qdict_array_split().
|
|
|
357786 |
+ */
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(test_dict, "1.x", 0);
|
|
|
357786 |
+ qdict_put_int(test_dict, "4.y", 1);
|
|
|
357786 |
+ qdict_put_int(test_dict, "0.a", 42);
|
|
|
357786 |
+ qdict_put_int(test_dict, "o.o", 7);
|
|
|
357786 |
+ qdict_put_int(test_dict, "0.b", 23);
|
|
|
357786 |
+ qdict_put_int(test_dict, "2", 66);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_array_split(test_dict, &test_list);
|
|
|
357786 |
+
|
|
|
357786 |
+ dict1 = qobject_to(QDict, qlist_pop(test_list));
|
|
|
357786 |
+ dict2 = qobject_to(QDict, qlist_pop(test_list));
|
|
|
357786 |
+ int1 = qobject_to(QNum, qlist_pop(test_list));
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(dict1);
|
|
|
357786 |
+ g_assert(dict2);
|
|
|
357786 |
+ g_assert(int1);
|
|
|
357786 |
+ g_assert(qlist_empty(test_list));
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(test_list);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "a") == 42);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "b") == 23);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict1);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict2, "x") == 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict2) == 1);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict2);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpint(qnum_get_int(int1), ==, 66);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(int1);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(test_dict, "4.y") == 1);
|
|
|
357786 |
+ g_assert(qdict_get_int(test_dict, "o.o") == 7);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(test_dict) == 2);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(test_dict);
|
|
|
357786 |
+
|
|
|
357786 |
+ /*
|
|
|
357786 |
+ * Test the split of
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "0": 42,
|
|
|
357786 |
+ * "1": 23,
|
|
|
357786 |
+ * "1.x": 84
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * to
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * [
|
|
|
357786 |
+ * 42
|
|
|
357786 |
+ * ]
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * and
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * {
|
|
|
357786 |
+ * "1": 23,
|
|
|
357786 |
+ * "1.x": 84
|
|
|
357786 |
+ * }
|
|
|
357786 |
+ *
|
|
|
357786 |
+ * That is, test whether splitting stops if there is both an entry with key
|
|
|
357786 |
+ * of "%u" and other entries with keys prefixed "%u." for the same index.
|
|
|
357786 |
+ */
|
|
|
357786 |
+
|
|
|
357786 |
+ test_dict = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(test_dict, "0", 42);
|
|
|
357786 |
+ qdict_put_int(test_dict, "1", 23);
|
|
|
357786 |
+ qdict_put_int(test_dict, "1.x", 84);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_array_split(test_dict, &test_list);
|
|
|
357786 |
+
|
|
|
357786 |
+ int1 = qobject_to(QNum, qlist_pop(test_list));
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(int1);
|
|
|
357786 |
+ g_assert(qlist_empty(test_list));
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(test_list);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpint(qnum_get_int(int1), ==, 42);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(int1);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(test_dict, "1") == 23);
|
|
|
357786 |
+ g_assert(qdict_get_int(test_dict, "1.x") == 84);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(test_dict) == 2);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(test_dict);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_array_entries_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *dict = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict, "bar", 0);
|
|
|
357786 |
+ qdict_put_int(dict, "baz.0", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict, "foo.1", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
|
|
|
357786 |
+ qdict_put_int(dict, "foo.0", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 2);
|
|
|
357786 |
+ qdict_put_int(dict, "foo.bar", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
|
|
|
357786 |
+ qdict_del(dict, "foo.bar");
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict, "foo.2.a", 0);
|
|
|
357786 |
+ qdict_put_int(dict, "foo.2.b", 0);
|
|
|
357786 |
+ qdict_put_int(dict, "foo.2.c", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 3);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict);
|
|
|
357786 |
+
|
|
|
357786 |
+ dict = qdict_new();
|
|
|
357786 |
+ qdict_put_int(dict, "1", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
+ qdict_put_int(dict, "0", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, ""), ==, 2);
|
|
|
357786 |
+ qdict_put_int(dict, "bar", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
+ qdict_del(dict, "bar");
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_int(dict, "2.a", 0);
|
|
|
357786 |
+ qdict_put_int(dict, "2.b", 0);
|
|
|
357786 |
+ qdict_put_int(dict, "2.c", 0);
|
|
|
357786 |
+ g_assert_cmpint(qdict_array_entries(dict, ""), ==, 3);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_join_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *dict1, *dict2;
|
|
|
357786 |
+ bool overwrite = false;
|
|
|
357786 |
+ int i;
|
|
|
357786 |
+
|
|
|
357786 |
+ dict1 = qdict_new();
|
|
|
357786 |
+ dict2 = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Test everything once without overwrite and once with */
|
|
|
357786 |
+ do {
|
|
|
357786 |
+ /* Test empty dicts */
|
|
|
357786 |
+ qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict1) == 0);
|
|
|
357786 |
+ g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* First iteration: Test movement */
|
|
|
357786 |
+ /* Second iteration: Test empty source and non-empty destination */
|
|
|
357786 |
+ qdict_put_int(dict2, "foo", 42);
|
|
|
357786 |
+
|
|
|
357786 |
+ for (i = 0; i < 2; i++) {
|
|
|
357786 |
+ qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict1) == 1);
|
|
|
357786 |
+ g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "foo") == 42);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Test non-empty source and destination without conflict */
|
|
|
357786 |
+ qdict_put_int(dict2, "bar", 23);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
+ g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "foo") == 42);
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "bar") == 23);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Test conflict */
|
|
|
357786 |
+ qdict_put_int(dict2, "foo", 84);
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
+ g_assert(qdict_size(dict2) == !overwrite);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "foo") == (overwrite ? 84 : 42));
|
|
|
357786 |
+ g_assert(qdict_get_int(dict1, "bar") == 23);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (!overwrite) {
|
|
|
357786 |
+ g_assert(qdict_get_int(dict2, "foo") == 84);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Check the references */
|
|
|
357786 |
+ g_assert(qdict_get(dict1, "foo")->base.refcnt == 1);
|
|
|
357786 |
+ g_assert(qdict_get(dict1, "bar")->base.refcnt == 1);
|
|
|
357786 |
+
|
|
|
357786 |
+ if (!overwrite) {
|
|
|
357786 |
+ g_assert(qdict_get(dict2, "foo")->base.refcnt == 1);
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Clean up */
|
|
|
357786 |
+ qdict_del(dict1, "foo");
|
|
|
357786 |
+ qdict_del(dict1, "bar");
|
|
|
357786 |
+
|
|
|
357786 |
+ if (!overwrite) {
|
|
|
357786 |
+ qdict_del(dict2, "foo");
|
|
|
357786 |
+ }
|
|
|
357786 |
+ } while (overwrite ^= true);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict1);
|
|
|
357786 |
+ qobject_unref(dict2);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_crumple_test_recursive(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *src, *dst, *rule, *vnc, *acl, *listen;
|
|
|
357786 |
+ QList *rules;
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ qdict_put_str(src, "vnc.listen.addr", "127.0.0.1");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.listen.port", "5901");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.rules.0.match", "fred");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.rules.0.policy", "allow");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.rules.1.match", "bob");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.rules.1.policy", "deny");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.default", "deny");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl..name", "acl0");
|
|
|
357786 |
+ qdict_put_str(src, "vnc.acl.rule..name", "acl0");
|
|
|
357786 |
+
|
|
|
357786 |
+ dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
|
|
|
357786 |
+ g_assert(dst);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(dst), ==, 1);
|
|
|
357786 |
+
|
|
|
357786 |
+ vnc = qdict_get_qdict(dst, "vnc");
|
|
|
357786 |
+ g_assert(vnc);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(vnc), ==, 3);
|
|
|
357786 |
+
|
|
|
357786 |
+ listen = qdict_get_qdict(vnc, "listen");
|
|
|
357786 |
+ g_assert(listen);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(listen), ==, 2);
|
|
|
357786 |
+ g_assert_cmpstr("127.0.0.1", ==, qdict_get_str(listen, "addr"));
|
|
|
357786 |
+ g_assert_cmpstr("5901", ==, qdict_get_str(listen, "port"));
|
|
|
357786 |
+
|
|
|
357786 |
+ acl = qdict_get_qdict(vnc, "acl");
|
|
|
357786 |
+ g_assert(acl);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(acl), ==, 3);
|
|
|
357786 |
+
|
|
|
357786 |
+ rules = qdict_get_qlist(acl, "rules");
|
|
|
357786 |
+ g_assert(rules);
|
|
|
357786 |
+ g_assert_cmpint(qlist_size(rules), ==, 2);
|
|
|
357786 |
+
|
|
|
357786 |
+ rule = qobject_to(QDict, qlist_pop(rules));
|
|
|
357786 |
+ g_assert(rule);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(rule), ==, 2);
|
|
|
357786 |
+ g_assert_cmpstr("fred", ==, qdict_get_str(rule, "match"));
|
|
|
357786 |
+ g_assert_cmpstr("allow", ==, qdict_get_str(rule, "policy"));
|
|
|
357786 |
+ qobject_unref(rule);
|
|
|
357786 |
+
|
|
|
357786 |
+ rule = qobject_to(QDict, qlist_pop(rules));
|
|
|
357786 |
+ g_assert(rule);
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(rule), ==, 2);
|
|
|
357786 |
+ g_assert_cmpstr("bob", ==, qdict_get_str(rule, "match"));
|
|
|
357786 |
+ g_assert_cmpstr("deny", ==, qdict_get_str(rule, "policy"));
|
|
|
357786 |
+ qobject_unref(rule);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* With recursive crumpling, we should see all names unescaped */
|
|
|
357786 |
+ g_assert_cmpstr("acl0", ==, qdict_get_str(vnc, "acl.name"));
|
|
|
357786 |
+ g_assert_cmpstr("acl0", ==, qdict_get_str(acl, "rule.name"));
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+ qobject_unref(dst);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_crumple_test_empty(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *src, *dst;
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpint(qdict_size(dst), ==, 0);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+ qobject_unref(dst);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static int qdict_count_entries(QDict *dict)
|
|
|
357786 |
+{
|
|
|
357786 |
+ const QDictEntry *e;
|
|
|
357786 |
+ int count = 0;
|
|
|
357786 |
+
|
|
|
357786 |
+ for (e = qdict_first(dict); e; e = qdict_next(dict, e)) {
|
|
|
357786 |
+ count++;
|
|
|
357786 |
+ }
|
|
|
357786 |
+
|
|
|
357786 |
+ return count;
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_rename_keys_test(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *dict = qdict_new();
|
|
|
357786 |
+ QDict *copy;
|
|
|
357786 |
+ QDictRenames *renames;
|
|
|
357786 |
+ Error *local_err = NULL;
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_put_str(dict, "abc", "foo");
|
|
|
357786 |
+ qdict_put_str(dict, "abcdef", "bar");
|
|
|
357786 |
+ qdict_put_int(dict, "number", 42);
|
|
|
357786 |
+ qdict_put_bool(dict, "flag", true);
|
|
|
357786 |
+ qdict_put_null(dict, "nothing");
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Empty rename list */
|
|
|
357786 |
+ renames = (QDictRenames[]) {
|
|
|
357786 |
+ { NULL, "this can be anything" }
|
|
|
357786 |
+ };
|
|
|
357786 |
+ copy = qdict_clone_shallow(dict);
|
|
|
357786 |
+ qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
|
|
|
357786 |
+ g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
|
|
|
357786 |
+ g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(copy);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Simple rename of all entries */
|
|
|
357786 |
+ renames = (QDictRenames[]) {
|
|
|
357786 |
+ { "abc", "str1" },
|
|
|
357786 |
+ { "abcdef", "str2" },
|
|
|
357786 |
+ { "number", "int" },
|
|
|
357786 |
+ { "flag", "bool" },
|
|
|
357786 |
+ { "nothing", "null" },
|
|
|
357786 |
+ { NULL , NULL }
|
|
|
357786 |
+ };
|
|
|
357786 |
+ copy = qdict_clone_shallow(dict);
|
|
|
357786 |
+ qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "abc"));
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "abcdef"));
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "number"));
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "flag"));
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "nothing"));
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "str1"), ==, "foo");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "str2"), ==, "bar");
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_int(copy, "int"), ==, 42);
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_bool(copy, "bool"), ==, true);
|
|
|
357786 |
+ g_assert(qobject_type(qdict_get(copy, "null")) == QTYPE_QNULL);
|
|
|
357786 |
+ g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(copy);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Renames are processed top to bottom */
|
|
|
357786 |
+ renames = (QDictRenames[]) {
|
|
|
357786 |
+ { "abc", "tmp" },
|
|
|
357786 |
+ { "abcdef", "abc" },
|
|
|
357786 |
+ { "number", "abcdef" },
|
|
|
357786 |
+ { "flag", "number" },
|
|
|
357786 |
+ { "nothing", "flag" },
|
|
|
357786 |
+ { "tmp", "nothing" },
|
|
|
357786 |
+ { NULL , NULL }
|
|
|
357786 |
+ };
|
|
|
357786 |
+ copy = qdict_clone_shallow(dict);
|
|
|
357786 |
+ qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "nothing"), ==, "foo");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "bar");
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_int(copy, "abcdef"), ==, 42);
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_bool(copy, "number"), ==, true);
|
|
|
357786 |
+ g_assert(qobject_type(qdict_get(copy, "flag")) == QTYPE_QNULL);
|
|
|
357786 |
+ g_assert(!qdict_haskey(copy, "tmp"));
|
|
|
357786 |
+ g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(copy);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Conflicting rename */
|
|
|
357786 |
+ renames = (QDictRenames[]) {
|
|
|
357786 |
+ { "abcdef", "abc" },
|
|
|
357786 |
+ { NULL , NULL }
|
|
|
357786 |
+ };
|
|
|
357786 |
+ copy = qdict_clone_shallow(dict);
|
|
|
357786 |
+ qdict_rename_keys(copy, renames, &local_err);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(local_err != NULL);
|
|
|
357786 |
+ error_free(local_err);
|
|
|
357786 |
+ local_err = NULL;
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
|
|
|
357786 |
+ g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
|
|
|
357786 |
+ g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
|
|
|
357786 |
+ g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
|
|
|
357786 |
+ g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(copy);
|
|
|
357786 |
+
|
|
|
357786 |
+ /* Renames in an empty dict */
|
|
|
357786 |
+ renames = (QDictRenames[]) {
|
|
|
357786 |
+ { "abcdef", "abc" },
|
|
|
357786 |
+ { NULL , NULL }
|
|
|
357786 |
+ };
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict);
|
|
|
357786 |
+ dict = qdict_new();
|
|
|
357786 |
+
|
|
|
357786 |
+ qdict_rename_keys(dict, renames, &error_abort);
|
|
|
357786 |
+ g_assert(qdict_first(dict) == NULL);
|
|
|
357786 |
+
|
|
|
357786 |
+ qobject_unref(dict);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+static void qdict_crumple_test_bad_inputs(void)
|
|
|
357786 |
+{
|
|
|
357786 |
+ QDict *src;
|
|
|
357786 |
+ Error *error = NULL;
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ /* rule.0 can't be both a string and a dict */
|
|
|
357786 |
+ qdict_put_str(src, "rule.0", "fred");
|
|
|
357786 |
+ qdict_put_str(src, "rule.0.policy", "allow");
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
+ g_assert(error != NULL);
|
|
|
357786 |
+ error_free(error);
|
|
|
357786 |
+ error = NULL;
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ /* rule can't be both a list and a dict */
|
|
|
357786 |
+ qdict_put_str(src, "rule.0", "fred");
|
|
|
357786 |
+ qdict_put_str(src, "rule.a", "allow");
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
+ g_assert(error != NULL);
|
|
|
357786 |
+ error_free(error);
|
|
|
357786 |
+ error = NULL;
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ /* The input should be flat, ie no dicts or lists */
|
|
|
357786 |
+ qdict_put(src, "rule.a", qdict_new());
|
|
|
357786 |
+ qdict_put_str(src, "rule.b", "allow");
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
+ g_assert(error != NULL);
|
|
|
357786 |
+ error_free(error);
|
|
|
357786 |
+ error = NULL;
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ /* List indexes must not have gaps */
|
|
|
357786 |
+ qdict_put_str(src, "rule.0", "deny");
|
|
|
357786 |
+ qdict_put_str(src, "rule.3", "allow");
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
+ g_assert(error != NULL);
|
|
|
357786 |
+ error_free(error);
|
|
|
357786 |
+ error = NULL;
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+
|
|
|
357786 |
+ src = qdict_new();
|
|
|
357786 |
+ /* List indexes must be in %zu format */
|
|
|
357786 |
+ qdict_put_str(src, "rule.0", "deny");
|
|
|
357786 |
+ qdict_put_str(src, "rule.+1", "allow");
|
|
|
357786 |
+
|
|
|
357786 |
+ g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
+ g_assert(error != NULL);
|
|
|
357786 |
+ error_free(error);
|
|
|
357786 |
+ error = NULL;
|
|
|
357786 |
+ qobject_unref(src);
|
|
|
357786 |
+}
|
|
|
357786 |
+
|
|
|
357786 |
+int main(int argc, char **argv)
|
|
|
357786 |
+{
|
|
|
357786 |
+ g_test_init(&argc, &argv, NULL);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_test_add_func("/public/defaults", qdict_defaults_test);
|
|
|
357786 |
+ g_test_add_func("/public/flatten", qdict_flatten_test);
|
|
|
357786 |
+ g_test_add_func("/public/array_split", qdict_array_split_test);
|
|
|
357786 |
+ g_test_add_func("/public/array_entries", qdict_array_entries_test);
|
|
|
357786 |
+ g_test_add_func("/public/join", qdict_join_test);
|
|
|
357786 |
+ g_test_add_func("/public/crumple/recursive",
|
|
|
357786 |
+ qdict_crumple_test_recursive);
|
|
|
357786 |
+ g_test_add_func("/public/crumple/empty",
|
|
|
357786 |
+ qdict_crumple_test_empty);
|
|
|
357786 |
+ g_test_add_func("/public/crumple/bad_inputs",
|
|
|
357786 |
+ qdict_crumple_test_bad_inputs);
|
|
|
357786 |
+
|
|
|
357786 |
+ g_test_add_func("/public/rename_keys", qdict_rename_keys_test);
|
|
|
357786 |
+
|
|
|
357786 |
+ return g_test_run();
|
|
|
357786 |
+}
|
|
|
357786 |
diff --git a/tests/check-qdict.c b/tests/check-qdict.c
|
|
|
357786 |
index 93e2112..86e9fe7 100644
|
|
|
357786 |
--- a/tests/check-qdict.c
|
|
|
357786 |
+++ b/tests/check-qdict.c
|
|
|
357786 |
@@ -11,13 +11,7 @@
|
|
|
357786 |
*/
|
|
|
357786 |
|
|
|
357786 |
#include "qemu/osdep.h"
|
|
|
357786 |
-#include "block/qdict.h"
|
|
|
357786 |
#include "qapi/qmp/qdict.h"
|
|
|
357786 |
-#include "qapi/qmp/qlist.h"
|
|
|
357786 |
-#include "qapi/qmp/qnum.h"
|
|
|
357786 |
-#include "qapi/qmp/qstring.h"
|
|
|
357786 |
-#include "qapi/error.h"
|
|
|
357786 |
-#include "qemu-common.h"
|
|
|
357786 |
|
|
|
357786 |
/*
|
|
|
357786 |
* Public Interface test-cases
|
|
|
357786 |
@@ -157,28 +151,6 @@ static void qdict_get_try_str_test(void)
|
|
|
357786 |
qobject_unref(tests_dict);
|
|
|
357786 |
}
|
|
|
357786 |
|
|
|
357786 |
-static void qdict_defaults_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *dict, *copy;
|
|
|
357786 |
-
|
|
|
357786 |
- dict = qdict_new();
|
|
|
357786 |
- copy = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_set_default_str(dict, "foo", "abc");
|
|
|
357786 |
- qdict_set_default_str(dict, "foo", "def");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "abc");
|
|
|
357786 |
- qdict_set_default_str(dict, "bar", "ghi");
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_copy_default(copy, dict, "foo");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "foo"), ==, "abc");
|
|
|
357786 |
- qdict_set_default_str(copy, "bar", "xyz");
|
|
|
357786 |
- qdict_copy_default(copy, dict, "bar");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "bar"), ==, "xyz");
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(copy);
|
|
|
357786 |
- qobject_unref(dict);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
static void qdict_haskey_not_test(void)
|
|
|
357786 |
{
|
|
|
357786 |
QDict *tests_dict = qdict_new();
|
|
|
357786 |
@@ -254,606 +226,6 @@ static void qdict_iterapi_test(void)
|
|
|
357786 |
qobject_unref(tests_dict);
|
|
|
357786 |
}
|
|
|
357786 |
|
|
|
357786 |
-static void qdict_flatten_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QList *list1 = qlist_new();
|
|
|
357786 |
- QList *list2 = qlist_new();
|
|
|
357786 |
- QDict *dict1 = qdict_new();
|
|
|
357786 |
- QDict *dict2 = qdict_new();
|
|
|
357786 |
- QDict *dict3 = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- /*
|
|
|
357786 |
- * Test the flattening of
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "e": [
|
|
|
357786 |
- * 42,
|
|
|
357786 |
- * [
|
|
|
357786 |
- * 23,
|
|
|
357786 |
- * 66,
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "a": 0,
|
|
|
357786 |
- * "b": 1
|
|
|
357786 |
- * }
|
|
|
357786 |
- * ]
|
|
|
357786 |
- * ],
|
|
|
357786 |
- * "f": {
|
|
|
357786 |
- * "c": 2,
|
|
|
357786 |
- * "d": 3,
|
|
|
357786 |
- * },
|
|
|
357786 |
- * "g": 4
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * to
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "e.0": 42,
|
|
|
357786 |
- * "e.1.0": 23,
|
|
|
357786 |
- * "e.1.1": 66,
|
|
|
357786 |
- * "e.1.2.a": 0,
|
|
|
357786 |
- * "e.1.2.b": 1,
|
|
|
357786 |
- * "f.c": 2,
|
|
|
357786 |
- * "f.d": 3,
|
|
|
357786 |
- * "g": 4
|
|
|
357786 |
- * }
|
|
|
357786 |
- */
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict1, "a", 0);
|
|
|
357786 |
- qdict_put_int(dict1, "b", 1);
|
|
|
357786 |
-
|
|
|
357786 |
- qlist_append_int(list1, 23);
|
|
|
357786 |
- qlist_append_int(list1, 66);
|
|
|
357786 |
- qlist_append(list1, dict1);
|
|
|
357786 |
- qlist_append_int(list2, 42);
|
|
|
357786 |
- qlist_append(list2, list1);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict2, "c", 2);
|
|
|
357786 |
- qdict_put_int(dict2, "d", 3);
|
|
|
357786 |
- qdict_put(dict3, "e", list2);
|
|
|
357786 |
- qdict_put(dict3, "f", dict2);
|
|
|
357786 |
- qdict_put_int(dict3, "g", 4);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_flatten(dict3);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "e.0") == 42);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "e.1.0") == 23);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "e.1.1") == 66);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "e.1.2.a") == 0);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "e.1.2.b") == 1);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "f.c") == 2);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "f.d") == 3);
|
|
|
357786 |
- g_assert(qdict_get_int(dict3, "g") == 4);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict3) == 8);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict3);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_array_split_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *test_dict = qdict_new();
|
|
|
357786 |
- QDict *dict1, *dict2;
|
|
|
357786 |
- QNum *int1;
|
|
|
357786 |
- QList *test_list;
|
|
|
357786 |
-
|
|
|
357786 |
- /*
|
|
|
357786 |
- * Test the split of
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "1.x": 0,
|
|
|
357786 |
- * "4.y": 1,
|
|
|
357786 |
- * "0.a": 42,
|
|
|
357786 |
- * "o.o": 7,
|
|
|
357786 |
- * "0.b": 23,
|
|
|
357786 |
- * "2": 66
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * to
|
|
|
357786 |
- *
|
|
|
357786 |
- * [
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "a": 42,
|
|
|
357786 |
- * "b": 23
|
|
|
357786 |
- * },
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "x": 0
|
|
|
357786 |
- * },
|
|
|
357786 |
- * 66
|
|
|
357786 |
- * ]
|
|
|
357786 |
- *
|
|
|
357786 |
- * and
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "4.y": 1,
|
|
|
357786 |
- * "o.o": 7
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * (remaining in the old QDict)
|
|
|
357786 |
- *
|
|
|
357786 |
- * This example is given in the comment of qdict_array_split().
|
|
|
357786 |
- */
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(test_dict, "1.x", 0);
|
|
|
357786 |
- qdict_put_int(test_dict, "4.y", 1);
|
|
|
357786 |
- qdict_put_int(test_dict, "0.a", 42);
|
|
|
357786 |
- qdict_put_int(test_dict, "o.o", 7);
|
|
|
357786 |
- qdict_put_int(test_dict, "0.b", 23);
|
|
|
357786 |
- qdict_put_int(test_dict, "2", 66);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_array_split(test_dict, &test_list);
|
|
|
357786 |
-
|
|
|
357786 |
- dict1 = qobject_to(QDict, qlist_pop(test_list));
|
|
|
357786 |
- dict2 = qobject_to(QDict, qlist_pop(test_list));
|
|
|
357786 |
- int1 = qobject_to(QNum, qlist_pop(test_list));
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(dict1);
|
|
|
357786 |
- g_assert(dict2);
|
|
|
357786 |
- g_assert(int1);
|
|
|
357786 |
- g_assert(qlist_empty(test_list));
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(test_list);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "a") == 42);
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "b") == 23);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict1);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict2, "x") == 0);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict2) == 1);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict2);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpint(qnum_get_int(int1), ==, 66);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(int1);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(test_dict, "4.y") == 1);
|
|
|
357786 |
- g_assert(qdict_get_int(test_dict, "o.o") == 7);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(test_dict) == 2);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(test_dict);
|
|
|
357786 |
-
|
|
|
357786 |
- /*
|
|
|
357786 |
- * Test the split of
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "0": 42,
|
|
|
357786 |
- * "1": 23,
|
|
|
357786 |
- * "1.x": 84
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * to
|
|
|
357786 |
- *
|
|
|
357786 |
- * [
|
|
|
357786 |
- * 42
|
|
|
357786 |
- * ]
|
|
|
357786 |
- *
|
|
|
357786 |
- * and
|
|
|
357786 |
- *
|
|
|
357786 |
- * {
|
|
|
357786 |
- * "1": 23,
|
|
|
357786 |
- * "1.x": 84
|
|
|
357786 |
- * }
|
|
|
357786 |
- *
|
|
|
357786 |
- * That is, test whether splitting stops if there is both an entry with key
|
|
|
357786 |
- * of "%u" and other entries with keys prefixed "%u." for the same index.
|
|
|
357786 |
- */
|
|
|
357786 |
-
|
|
|
357786 |
- test_dict = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(test_dict, "0", 42);
|
|
|
357786 |
- qdict_put_int(test_dict, "1", 23);
|
|
|
357786 |
- qdict_put_int(test_dict, "1.x", 84);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_array_split(test_dict, &test_list);
|
|
|
357786 |
-
|
|
|
357786 |
- int1 = qobject_to(QNum, qlist_pop(test_list));
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(int1);
|
|
|
357786 |
- g_assert(qlist_empty(test_list));
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(test_list);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpint(qnum_get_int(int1), ==, 42);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(int1);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(test_dict, "1") == 23);
|
|
|
357786 |
- g_assert(qdict_get_int(test_dict, "1.x") == 84);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(test_dict) == 2);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(test_dict);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_array_entries_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *dict = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict, "bar", 0);
|
|
|
357786 |
- qdict_put_int(dict, "baz.0", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict, "foo.1", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
|
|
|
357786 |
- qdict_put_int(dict, "foo.0", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 2);
|
|
|
357786 |
- qdict_put_int(dict, "foo.bar", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL);
|
|
|
357786 |
- qdict_del(dict, "foo.bar");
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict, "foo.2.a", 0);
|
|
|
357786 |
- qdict_put_int(dict, "foo.2.b", 0);
|
|
|
357786 |
- qdict_put_int(dict, "foo.2.c", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 3);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict);
|
|
|
357786 |
-
|
|
|
357786 |
- dict = qdict_new();
|
|
|
357786 |
- qdict_put_int(dict, "1", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
- qdict_put_int(dict, "0", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, ""), ==, 2);
|
|
|
357786 |
- qdict_put_int(dict, "bar", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL);
|
|
|
357786 |
- qdict_del(dict, "bar");
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_int(dict, "2.a", 0);
|
|
|
357786 |
- qdict_put_int(dict, "2.b", 0);
|
|
|
357786 |
- qdict_put_int(dict, "2.c", 0);
|
|
|
357786 |
- g_assert_cmpint(qdict_array_entries(dict, ""), ==, 3);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_join_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *dict1, *dict2;
|
|
|
357786 |
- bool overwrite = false;
|
|
|
357786 |
- int i;
|
|
|
357786 |
-
|
|
|
357786 |
- dict1 = qdict_new();
|
|
|
357786 |
- dict2 = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- /* Test everything once without overwrite and once with */
|
|
|
357786 |
- do
|
|
|
357786 |
- {
|
|
|
357786 |
- /* Test empty dicts */
|
|
|
357786 |
- qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict1) == 0);
|
|
|
357786 |
- g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
-
|
|
|
357786 |
- /* First iteration: Test movement */
|
|
|
357786 |
- /* Second iteration: Test empty source and non-empty destination */
|
|
|
357786 |
- qdict_put_int(dict2, "foo", 42);
|
|
|
357786 |
-
|
|
|
357786 |
- for (i = 0; i < 2; i++) {
|
|
|
357786 |
- qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict1) == 1);
|
|
|
357786 |
- g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "foo") == 42);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Test non-empty source and destination without conflict */
|
|
|
357786 |
- qdict_put_int(dict2, "bar", 23);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
- g_assert(qdict_size(dict2) == 0);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "foo") == 42);
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "bar") == 23);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Test conflict */
|
|
|
357786 |
- qdict_put_int(dict2, "foo", 84);
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_join(dict1, dict2, overwrite);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_size(dict1) == 2);
|
|
|
357786 |
- g_assert(qdict_size(dict2) == !overwrite);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "foo") == (overwrite ? 84 : 42));
|
|
|
357786 |
- g_assert(qdict_get_int(dict1, "bar") == 23);
|
|
|
357786 |
-
|
|
|
357786 |
- if (!overwrite) {
|
|
|
357786 |
- g_assert(qdict_get_int(dict2, "foo") == 84);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Check the references */
|
|
|
357786 |
- g_assert(qdict_get(dict1, "foo")->base.refcnt == 1);
|
|
|
357786 |
- g_assert(qdict_get(dict1, "bar")->base.refcnt == 1);
|
|
|
357786 |
-
|
|
|
357786 |
- if (!overwrite) {
|
|
|
357786 |
- g_assert(qdict_get(dict2, "foo")->base.refcnt == 1);
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- /* Clean up */
|
|
|
357786 |
- qdict_del(dict1, "foo");
|
|
|
357786 |
- qdict_del(dict1, "bar");
|
|
|
357786 |
-
|
|
|
357786 |
- if (!overwrite) {
|
|
|
357786 |
- qdict_del(dict2, "foo");
|
|
|
357786 |
- }
|
|
|
357786 |
- }
|
|
|
357786 |
- while (overwrite ^= true);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict1);
|
|
|
357786 |
- qobject_unref(dict2);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_crumple_test_recursive(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *src, *dst, *rule, *vnc, *acl, *listen;
|
|
|
357786 |
- QList *rules;
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- qdict_put_str(src, "vnc.listen.addr", "127.0.0.1");
|
|
|
357786 |
- qdict_put_str(src, "vnc.listen.port", "5901");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.rules.0.match", "fred");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.rules.0.policy", "allow");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.rules.1.match", "bob");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.rules.1.policy", "deny");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.default", "deny");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl..name", "acl0");
|
|
|
357786 |
- qdict_put_str(src, "vnc.acl.rule..name", "acl0");
|
|
|
357786 |
-
|
|
|
357786 |
- dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
|
|
|
357786 |
- g_assert(dst);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(dst), ==, 1);
|
|
|
357786 |
-
|
|
|
357786 |
- vnc = qdict_get_qdict(dst, "vnc");
|
|
|
357786 |
- g_assert(vnc);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(vnc), ==, 3);
|
|
|
357786 |
-
|
|
|
357786 |
- listen = qdict_get_qdict(vnc, "listen");
|
|
|
357786 |
- g_assert(listen);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(listen), ==, 2);
|
|
|
357786 |
- g_assert_cmpstr("127.0.0.1", ==, qdict_get_str(listen, "addr"));
|
|
|
357786 |
- g_assert_cmpstr("5901", ==, qdict_get_str(listen, "port"));
|
|
|
357786 |
-
|
|
|
357786 |
- acl = qdict_get_qdict(vnc, "acl");
|
|
|
357786 |
- g_assert(acl);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(acl), ==, 3);
|
|
|
357786 |
-
|
|
|
357786 |
- rules = qdict_get_qlist(acl, "rules");
|
|
|
357786 |
- g_assert(rules);
|
|
|
357786 |
- g_assert_cmpint(qlist_size(rules), ==, 2);
|
|
|
357786 |
-
|
|
|
357786 |
- rule = qobject_to(QDict, qlist_pop(rules));
|
|
|
357786 |
- g_assert(rule);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(rule), ==, 2);
|
|
|
357786 |
- g_assert_cmpstr("fred", ==, qdict_get_str(rule, "match"));
|
|
|
357786 |
- g_assert_cmpstr("allow", ==, qdict_get_str(rule, "policy"));
|
|
|
357786 |
- qobject_unref(rule);
|
|
|
357786 |
-
|
|
|
357786 |
- rule = qobject_to(QDict, qlist_pop(rules));
|
|
|
357786 |
- g_assert(rule);
|
|
|
357786 |
- g_assert_cmpint(qdict_size(rule), ==, 2);
|
|
|
357786 |
- g_assert_cmpstr("bob", ==, qdict_get_str(rule, "match"));
|
|
|
357786 |
- g_assert_cmpstr("deny", ==, qdict_get_str(rule, "policy"));
|
|
|
357786 |
- qobject_unref(rule);
|
|
|
357786 |
-
|
|
|
357786 |
- /* With recursive crumpling, we should see all names unescaped */
|
|
|
357786 |
- g_assert_cmpstr("acl0", ==, qdict_get_str(vnc, "acl.name"));
|
|
|
357786 |
- g_assert_cmpstr("acl0", ==, qdict_get_str(acl, "rule.name"));
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
- qobject_unref(dst);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_crumple_test_empty(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *src, *dst;
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpint(qdict_size(dst), ==, 0);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
- qobject_unref(dst);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static int qdict_count_entries(QDict *dict)
|
|
|
357786 |
-{
|
|
|
357786 |
- const QDictEntry *e;
|
|
|
357786 |
- int count = 0;
|
|
|
357786 |
-
|
|
|
357786 |
- for (e = qdict_first(dict); e; e = qdict_next(dict, e)) {
|
|
|
357786 |
- count++;
|
|
|
357786 |
- }
|
|
|
357786 |
-
|
|
|
357786 |
- return count;
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_rename_keys_test(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *dict = qdict_new();
|
|
|
357786 |
- QDict *copy;
|
|
|
357786 |
- QDictRenames *renames;
|
|
|
357786 |
- Error *local_err = NULL;
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_put_str(dict, "abc", "foo");
|
|
|
357786 |
- qdict_put_str(dict, "abcdef", "bar");
|
|
|
357786 |
- qdict_put_int(dict, "number", 42);
|
|
|
357786 |
- qdict_put_bool(dict, "flag", true);
|
|
|
357786 |
- qdict_put_null(dict, "nothing");
|
|
|
357786 |
-
|
|
|
357786 |
- /* Empty rename list */
|
|
|
357786 |
- renames = (QDictRenames[]) {
|
|
|
357786 |
- { NULL, "this can be anything" }
|
|
|
357786 |
- };
|
|
|
357786 |
- copy = qdict_clone_shallow(dict);
|
|
|
357786 |
- qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
|
|
|
357786 |
- g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
|
|
|
357786 |
- g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
|
|
|
357786 |
- g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
|
|
|
357786 |
- g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(copy);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Simple rename of all entries */
|
|
|
357786 |
- renames = (QDictRenames[]) {
|
|
|
357786 |
- { "abc", "str1" },
|
|
|
357786 |
- { "abcdef", "str2" },
|
|
|
357786 |
- { "number", "int" },
|
|
|
357786 |
- { "flag", "bool" },
|
|
|
357786 |
- { "nothing", "null" },
|
|
|
357786 |
- { NULL , NULL }
|
|
|
357786 |
- };
|
|
|
357786 |
- copy = qdict_clone_shallow(dict);
|
|
|
357786 |
- qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "abc"));
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "abcdef"));
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "number"));
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "flag"));
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "nothing"));
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "str1"), ==, "foo");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "str2"), ==, "bar");
|
|
|
357786 |
- g_assert_cmpint(qdict_get_int(copy, "int"), ==, 42);
|
|
|
357786 |
- g_assert_cmpint(qdict_get_bool(copy, "bool"), ==, true);
|
|
|
357786 |
- g_assert(qobject_type(qdict_get(copy, "null")) == QTYPE_QNULL);
|
|
|
357786 |
- g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(copy);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Renames are processed top to bottom */
|
|
|
357786 |
- renames = (QDictRenames[]) {
|
|
|
357786 |
- { "abc", "tmp" },
|
|
|
357786 |
- { "abcdef", "abc" },
|
|
|
357786 |
- { "number", "abcdef" },
|
|
|
357786 |
- { "flag", "number" },
|
|
|
357786 |
- { "nothing", "flag" },
|
|
|
357786 |
- { "tmp", "nothing" },
|
|
|
357786 |
- { NULL , NULL }
|
|
|
357786 |
- };
|
|
|
357786 |
- copy = qdict_clone_shallow(dict);
|
|
|
357786 |
- qdict_rename_keys(copy, renames, &error_abort);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "nothing"), ==, "foo");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "bar");
|
|
|
357786 |
- g_assert_cmpint(qdict_get_int(copy, "abcdef"), ==, 42);
|
|
|
357786 |
- g_assert_cmpint(qdict_get_bool(copy, "number"), ==, true);
|
|
|
357786 |
- g_assert(qobject_type(qdict_get(copy, "flag")) == QTYPE_QNULL);
|
|
|
357786 |
- g_assert(!qdict_haskey(copy, "tmp"));
|
|
|
357786 |
- g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(copy);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Conflicting rename */
|
|
|
357786 |
- renames = (QDictRenames[]) {
|
|
|
357786 |
- { "abcdef", "abc" },
|
|
|
357786 |
- { NULL , NULL }
|
|
|
357786 |
- };
|
|
|
357786 |
- copy = qdict_clone_shallow(dict);
|
|
|
357786 |
- qdict_rename_keys(copy, renames, &local_err);
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(local_err != NULL);
|
|
|
357786 |
- error_free(local_err);
|
|
|
357786 |
- local_err = NULL;
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "abc"), ==, "foo");
|
|
|
357786 |
- g_assert_cmpstr(qdict_get_str(copy, "abcdef"), ==, "bar");
|
|
|
357786 |
- g_assert_cmpint(qdict_get_int(copy, "number"), ==, 42);
|
|
|
357786 |
- g_assert_cmpint(qdict_get_bool(copy, "flag"), ==, true);
|
|
|
357786 |
- g_assert(qobject_type(qdict_get(copy, "nothing")) == QTYPE_QNULL);
|
|
|
357786 |
- g_assert_cmpint(qdict_count_entries(copy), ==, 5);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(copy);
|
|
|
357786 |
-
|
|
|
357786 |
- /* Renames in an empty dict */
|
|
|
357786 |
- renames = (QDictRenames[]) {
|
|
|
357786 |
- { "abcdef", "abc" },
|
|
|
357786 |
- { NULL , NULL }
|
|
|
357786 |
- };
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict);
|
|
|
357786 |
- dict = qdict_new();
|
|
|
357786 |
-
|
|
|
357786 |
- qdict_rename_keys(dict, renames, &error_abort);
|
|
|
357786 |
- g_assert(qdict_first(dict) == NULL);
|
|
|
357786 |
-
|
|
|
357786 |
- qobject_unref(dict);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
-static void qdict_crumple_test_bad_inputs(void)
|
|
|
357786 |
-{
|
|
|
357786 |
- QDict *src;
|
|
|
357786 |
- Error *error = NULL;
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- /* rule.0 can't be both a string and a dict */
|
|
|
357786 |
- qdict_put_str(src, "rule.0", "fred");
|
|
|
357786 |
- qdict_put_str(src, "rule.0.policy", "allow");
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
- g_assert(error != NULL);
|
|
|
357786 |
- error_free(error);
|
|
|
357786 |
- error = NULL;
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- /* rule can't be both a list and a dict */
|
|
|
357786 |
- qdict_put_str(src, "rule.0", "fred");
|
|
|
357786 |
- qdict_put_str(src, "rule.a", "allow");
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
- g_assert(error != NULL);
|
|
|
357786 |
- error_free(error);
|
|
|
357786 |
- error = NULL;
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- /* The input should be flat, ie no dicts or lists */
|
|
|
357786 |
- qdict_put(src, "rule.a", qdict_new());
|
|
|
357786 |
- qdict_put_str(src, "rule.b", "allow");
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
- g_assert(error != NULL);
|
|
|
357786 |
- error_free(error);
|
|
|
357786 |
- error = NULL;
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- /* List indexes must not have gaps */
|
|
|
357786 |
- qdict_put_str(src, "rule.0", "deny");
|
|
|
357786 |
- qdict_put_str(src, "rule.3", "allow");
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
- g_assert(error != NULL);
|
|
|
357786 |
- error_free(error);
|
|
|
357786 |
- error = NULL;
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
-
|
|
|
357786 |
- src = qdict_new();
|
|
|
357786 |
- /* List indexes must be in %zu format */
|
|
|
357786 |
- qdict_put_str(src, "rule.0", "deny");
|
|
|
357786 |
- qdict_put_str(src, "rule.+1", "allow");
|
|
|
357786 |
-
|
|
|
357786 |
- g_assert(qdict_crumple(src, &error) == NULL);
|
|
|
357786 |
- g_assert(error != NULL);
|
|
|
357786 |
- error_free(error);
|
|
|
357786 |
- error = NULL;
|
|
|
357786 |
- qobject_unref(src);
|
|
|
357786 |
-}
|
|
|
357786 |
-
|
|
|
357786 |
/*
|
|
|
357786 |
* Errors test-cases
|
|
|
357786 |
*/
|
|
|
357786 |
@@ -987,29 +359,15 @@ int main(int argc, char **argv)
|
|
|
357786 |
g_test_add_func("/public/get_try_int", qdict_get_try_int_test);
|
|
|
357786 |
g_test_add_func("/public/get_str", qdict_get_str_test);
|
|
|
357786 |
g_test_add_func("/public/get_try_str", qdict_get_try_str_test);
|
|
|
357786 |
- g_test_add_func("/public/defaults", qdict_defaults_test);
|
|
|
357786 |
g_test_add_func("/public/haskey_not", qdict_haskey_not_test);
|
|
|
357786 |
g_test_add_func("/public/haskey", qdict_haskey_test);
|
|
|
357786 |
g_test_add_func("/public/del", qdict_del_test);
|
|
|
357786 |
g_test_add_func("/public/to_qdict", qobject_to_qdict_test);
|
|
|
357786 |
g_test_add_func("/public/iterapi", qdict_iterapi_test);
|
|
|
357786 |
- g_test_add_func("/public/flatten", qdict_flatten_test);
|
|
|
357786 |
- g_test_add_func("/public/array_split", qdict_array_split_test);
|
|
|
357786 |
- g_test_add_func("/public/array_entries", qdict_array_entries_test);
|
|
|
357786 |
- g_test_add_func("/public/join", qdict_join_test);
|
|
|
357786 |
|
|
|
357786 |
g_test_add_func("/errors/put_exists", qdict_put_exists_test);
|
|
|
357786 |
g_test_add_func("/errors/get_not_exists", qdict_get_not_exists_test);
|
|
|
357786 |
|
|
|
357786 |
- g_test_add_func("/public/crumple/recursive",
|
|
|
357786 |
- qdict_crumple_test_recursive);
|
|
|
357786 |
- g_test_add_func("/public/crumple/empty",
|
|
|
357786 |
- qdict_crumple_test_empty);
|
|
|
357786 |
- g_test_add_func("/public/crumple/bad_inputs",
|
|
|
357786 |
- qdict_crumple_test_bad_inputs);
|
|
|
357786 |
-
|
|
|
357786 |
- g_test_add_func("/public/rename_keys", qdict_rename_keys_test);
|
|
|
357786 |
-
|
|
|
357786 |
/* The Big one */
|
|
|
357786 |
if (g_test_slow()) {
|
|
|
357786 |
g_test_add_func("/stress/test", qdict_stress_test);
|
|
|
357786 |
--
|
|
|
357786 |
1.8.3.1
|
|
|
357786 |
|