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