Blame SOURCES/kvm-qobject-Move-block-specific-qdict-code-to-block-qdic.patch

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