2011-06-30 14:08:29 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2012-12-12 17:53:50 +00:00
|
|
|
#include "virjson.h"
|
2011-06-30 14:08:29 +00:00
|
|
|
#include "testutils.h"
|
|
|
|
|
2017-06-26 16:02:35 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2011-06-30 14:08:29 +00:00
|
|
|
struct testInfo {
|
2018-05-31 17:20:13 +00:00
|
|
|
const char *name;
|
2011-06-30 14:08:29 +00:00
|
|
|
const char *doc;
|
2013-04-26 14:59:02 +00:00
|
|
|
const char *expect;
|
2011-06-30 14:08:29 +00:00
|
|
|
bool pass;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-06-01 11:06:30 +00:00
|
|
|
static int
|
|
|
|
testJSONFromFile(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) injson = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *infile = NULL;
|
|
|
|
g_autofree char *indata = NULL;
|
|
|
|
g_autofree char *outfile = NULL;
|
|
|
|
g_autofree char *actual = NULL;
|
2018-06-01 11:06:30 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
infile = g_strdup_printf("%s/virjsondata/parse-%s-in.json",
|
|
|
|
abs_srcdir, info->name);
|
|
|
|
outfile = g_strdup_printf("%s/virjsondata/parse-%s-out.json",
|
|
|
|
abs_srcdir, info->name);
|
2018-06-01 11:06:30 +00:00
|
|
|
|
|
|
|
if (virTestLoadFile(infile, &indata) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
injson = virJSONValueFromString(indata);
|
|
|
|
|
|
|
|
if (!injson) {
|
|
|
|
if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to parse %s", info->doc);
|
2018-06-01 11:06:30 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("As expected, failed to parse %s", info->doc);
|
2018-06-01 11:06:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Unexpected success while parsing %s", info->doc);
|
2018-06-01 11:06:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(actual = virJSONValueToString(injson, false)))
|
|
|
|
return -1;
|
|
|
|
|
2018-06-01 11:10:11 +00:00
|
|
|
if (virTestCompareToFile(actual, outfile) < 0)
|
2018-06-01 11:06:30 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-30 14:08:29 +00:00
|
|
|
static int
|
|
|
|
testJSONFromString(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) json = NULL;
|
2017-07-03 13:37:21 +00:00
|
|
|
const char *expectstr = info->expect ? info->expect : info->doc;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *formatted = NULL;
|
2011-06-30 14:08:29 +00:00
|
|
|
|
|
|
|
json = virJSONValueFromString(info->doc);
|
|
|
|
|
2017-07-03 13:02:38 +00:00
|
|
|
if (!json) {
|
|
|
|
if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to parse %s", info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2011-06-30 14:08:29 +00:00
|
|
|
} else {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("As expected, failed to parse %s", info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2011-06-30 14:08:29 +00:00
|
|
|
}
|
2019-04-02 14:06:52 +00:00
|
|
|
} else {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Unexpected success while parsing %s", info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2019-04-02 14:06:52 +00:00
|
|
|
}
|
2017-07-03 13:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Parsed %s", info->doc);
|
2017-07-03 13:02:38 +00:00
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
if (!(formatted = virJSONValueToString(json, false))) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to format json data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 13:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ(expectstr, formatted)) {
|
|
|
|
virTestDifference(stderr, expectstr, formatted);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 13:37:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2011-06-30 14:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-26 14:59:02 +00:00
|
|
|
static int
|
|
|
|
testJSONAddRemove(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) json = NULL;
|
|
|
|
g_autoptr(virJSONValue) name = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *infile = NULL;
|
|
|
|
g_autofree char *indata = NULL;
|
|
|
|
g_autofree char *outfile = NULL;
|
|
|
|
g_autofree char *actual = NULL;
|
2013-04-26 14:59:02 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
infile = g_strdup_printf("%s/virjsondata/add-remove-%s-in.json",
|
|
|
|
abs_srcdir, info->name);
|
|
|
|
outfile = g_strdup_printf("%s/virjsondata/add-remove-%s-out.json",
|
|
|
|
abs_srcdir, info->name);
|
2018-06-01 07:05:23 +00:00
|
|
|
|
|
|
|
if (virTestLoadFile(infile, &indata) < 0)
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2018-06-01 07:05:23 +00:00
|
|
|
|
|
|
|
json = virJSONValueFromString(indata);
|
2013-08-01 19:54:51 +00:00
|
|
|
if (!json) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Fail to parse %s", info->name);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-08-01 19:54:51 +00:00
|
|
|
}
|
2013-04-26 14:59:02 +00:00
|
|
|
|
|
|
|
switch (virJSONValueObjectRemoveKey(json, "name", &name)) {
|
|
|
|
case 1:
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("should not remove from non-object %s",
|
2018-06-01 07:09:46 +00:00
|
|
|
info->name);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
if (!info->pass)
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2015-04-23 17:38:00 +00:00
|
|
|
else
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Fail to recognize non-object %s", info->name);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
default:
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("unexpected result when removing from %s",
|
2018-06-01 07:09:46 +00:00
|
|
|
info->name);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
|
|
|
if (STRNEQ_NULLABLE(virJSONValueGetString(name), "sample")) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("unexpected value after removing name: %s",
|
2015-05-12 12:40:34 +00:00
|
|
|
NULLSTR(virJSONValueGetString(name)));
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectRemoveKey(json, "name", NULL)) {
|
2015-04-23 17:38:00 +00:00
|
|
|
VIR_TEST_VERBOSE("%s",
|
2019-05-03 08:45:58 +00:00
|
|
|
"unexpected success when removing missing key");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
|
|
|
if (virJSONValueObjectAppendString(json, "newname", "foo") < 0) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("%s", "unexpected failure adding new key");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
2018-06-01 07:05:23 +00:00
|
|
|
if (!(actual = virJSONValueToString(json, false))) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("%s", "failed to stringize result");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
2018-06-01 07:05:23 +00:00
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
if (virTestCompareToFile(actual, outfile) < 0)
|
|
|
|
return -1;
|
2013-04-26 14:59:02 +00:00
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2013-04-26 14:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-19 23:13:03 +00:00
|
|
|
static int
|
|
|
|
testJSONLookup(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) json = NULL;
|
2015-06-19 23:13:03 +00:00
|
|
|
virJSONValuePtr value = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *result = NULL;
|
2015-06-19 23:13:03 +00:00
|
|
|
int rc;
|
|
|
|
int number;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
json = virJSONValueFromString(info->doc);
|
|
|
|
if (!json) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Fail to parse %s", info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value = virJSONValueObjectGetObject(json, "a");
|
|
|
|
if (value) {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have failed",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
} else {
|
|
|
|
result = virJSONValueToString(value, false);
|
|
|
|
if (STRNEQ_NULLABLE(result, "{}")) {
|
|
|
|
VIR_TEST_VERBOSE("lookup for 'a' in '%s' found '%s' but "
|
2019-05-03 08:45:58 +00:00
|
|
|
"should have found '{}'",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc, NULLSTR(result));
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
VIR_FREE(result);
|
|
|
|
}
|
|
|
|
} else if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have succeeded",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
number = 2;
|
|
|
|
rc = virJSONValueObjectGetNumberInt(json, "b", &number);
|
|
|
|
if (rc == 0) {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have failed",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
} else if (number != 1) {
|
|
|
|
VIR_TEST_VERBOSE("lookup for 'b' in '%s' found %d but "
|
2019-05-03 08:45:58 +00:00
|
|
|
"should have found 1",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc, number);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
} else if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have succeeded",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
str = virJSONValueObjectGetString(json, "c");
|
|
|
|
if (str) {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have failed",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
} else if (STRNEQ(str, "str")) {
|
|
|
|
VIR_TEST_VERBOSE("lookup for 'c' in '%s' found '%s' but "
|
2019-05-03 08:45:58 +00:00
|
|
|
"should have found 'str'", info->doc, str);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
} else if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have succeeded",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value = virJSONValueObjectGetArray(json, "d");
|
|
|
|
if (value) {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have failed",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
} else {
|
|
|
|
result = virJSONValueToString(value, false);
|
|
|
|
if (STRNEQ_NULLABLE(result, "[]")) {
|
|
|
|
VIR_TEST_VERBOSE("lookup for 'd' in '%s' found '%s' but "
|
2019-05-03 08:45:58 +00:00
|
|
|
"should have found '[]'",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc, NULLSTR(result));
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
VIR_FREE(result);
|
|
|
|
}
|
|
|
|
} else if (info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have succeeded",
|
2015-06-19 23:13:03 +00:00
|
|
|
info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2015-06-19 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-21 10:23:41 +00:00
|
|
|
static int
|
|
|
|
testJSONCopy(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) json = NULL;
|
|
|
|
g_autoptr(virJSONValue) jsonCopy = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *result = NULL;
|
|
|
|
g_autofree char *resultCopy = NULL;
|
2014-08-21 10:23:41 +00:00
|
|
|
|
|
|
|
json = virJSONValueFromString(info->doc);
|
|
|
|
if (!json) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to parse %s", info->doc);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonCopy = virJSONValueCopy(json);
|
|
|
|
if (!jsonCopy) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to copy JSON data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = virJSONValueToString(json, false);
|
|
|
|
if (!result) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to format original JSON data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultCopy = virJSONValueToString(json, false);
|
|
|
|
if (!resultCopy) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to format copied JSON data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ(result, resultCopy)) {
|
|
|
|
if (virTestGetVerbose())
|
2016-05-26 15:01:51 +00:00
|
|
|
virTestDifference(stderr, result, resultCopy);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(result);
|
|
|
|
VIR_FREE(resultCopy);
|
|
|
|
|
|
|
|
result = virJSONValueToString(json, true);
|
|
|
|
if (!result) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to format original JSON data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultCopy = virJSONValueToString(json, true);
|
|
|
|
if (!resultCopy) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("Failed to format copied JSON data");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ(result, resultCopy)) {
|
|
|
|
if (virTestGetVerbose())
|
2016-05-26 15:01:51 +00:00
|
|
|
virTestDifference(stderr, result, resultCopy);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2014-08-21 10:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-26 16:02:35 +00:00
|
|
|
static int
|
|
|
|
testJSONDeflatten(const void *data)
|
|
|
|
{
|
|
|
|
const struct testInfo *info = data;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) injson = NULL;
|
|
|
|
g_autoptr(virJSONValue) deflattened = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *infile = NULL;
|
|
|
|
g_autofree char *indata = NULL;
|
|
|
|
g_autofree char *outfile = NULL;
|
|
|
|
g_autofree char *actual = NULL;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
infile = g_strdup_printf("%s/virjsondata/deflatten-%s-in.json",
|
|
|
|
abs_srcdir, info->name);
|
|
|
|
outfile = g_strdup_printf("%s/virjsondata/deflatten-%s-out.json",
|
|
|
|
abs_srcdir, info->name);
|
2017-06-26 16:02:35 +00:00
|
|
|
|
|
|
|
if (virTestLoadFile(infile, &indata) < 0)
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
|
|
|
if (!(injson = virJSONValueFromString(indata)))
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
|
|
|
if ((deflattened = virJSONValueObjectDeflatten(injson))) {
|
|
|
|
if (!info->pass) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("%s: deflattening should have failed", info->name);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!info->pass)
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(actual = virJSONValueToString(deflattened, true)))
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
|
|
|
if (virTestCompareToFile(actual, outfile) < 0)
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-06-26 16:02:35 +00:00
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testJSONEscapeObj(const void *data G_GNUC_UNUSED)
|
2017-07-03 14:39:21 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) json = NULL;
|
|
|
|
g_autoptr(virJSONValue) nestjson = NULL;
|
|
|
|
g_autoptr(virJSONValue) parsejson = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *neststr = NULL;
|
|
|
|
g_autofree char *result = NULL;
|
2017-07-03 14:39:21 +00:00
|
|
|
const char *parsednestedstr;
|
|
|
|
|
|
|
|
if (virJSONValueObjectCreate(&nestjson,
|
|
|
|
"s:stringkey", "stringvalue",
|
|
|
|
"i:numberkey", 1234,
|
|
|
|
"b:booleankey", false, NULL) < 0) {
|
|
|
|
VIR_TEST_VERBOSE("failed to create nested json object");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(neststr = virJSONValueToString(nestjson, false))) {
|
|
|
|
VIR_TEST_VERBOSE("failed to format nested json object");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectCreate(&json, "s:test", neststr, NULL) < 0) {
|
|
|
|
VIR_TEST_VERBOSE("Failed to create json object");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(result = virJSONValueToString(json, false))) {
|
|
|
|
VIR_TEST_VERBOSE("Failed to format json object");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(parsejson = virJSONValueFromString(result))) {
|
|
|
|
VIR_TEST_VERBOSE("Failed to parse JSON with nested JSON in string");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(parsednestedstr = virJSONValueObjectGetString(parsejson, "test"))) {
|
|
|
|
VIR_TEST_VERBOSE("Failed to retrieve string containing nested json");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
2017-06-26 16:02:35 +00:00
|
|
|
|
2017-07-03 14:39:21 +00:00
|
|
|
if (STRNEQ(parsednestedstr, neststr)) {
|
|
|
|
virTestDifference(stderr, neststr, parsednestedstr);
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2017-07-03 14:39:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2017-06-26 16:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 10:18:50 +00:00
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testJSONObjectFormatSteal(const void *opaque G_GNUC_UNUSED)
|
2018-03-30 10:18:50 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) a1 = NULL;
|
|
|
|
g_autoptr(virJSONValue) a2 = NULL;
|
|
|
|
g_autoptr(virJSONValue) t1 = NULL;
|
|
|
|
g_autoptr(virJSONValue) t2 = NULL;
|
2018-03-30 10:18:50 +00:00
|
|
|
|
|
|
|
if (!(a1 = virJSONValueNewString("test")) ||
|
|
|
|
!(a2 = virJSONValueNewString("test"))) {
|
|
|
|
VIR_TEST_VERBOSE("Failed to create json object");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectCreate(&t1, "a:t", &a1, "s:f", NULL, NULL) != -1) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("virJSONValueObjectCreate(t1) should have failed");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2018-03-30 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a1) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("appended object a1 was not consumed");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2018-03-30 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectCreate(&t2, "s:f", NULL, "a:t", &a1, NULL) != -1) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("virJSONValueObjectCreate(t2) should have failed");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2018-03-30 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!a2) {
|
2019-05-03 08:45:58 +00:00
|
|
|
VIR_TEST_VERBOSE("appended object a2 was consumed");
|
2019-04-02 14:21:57 +00:00
|
|
|
return -1;
|
2018-03-30 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:21:57 +00:00
|
|
|
return 0;
|
2018-03-30 10:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-30 14:08:29 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_FULL(name, cmd, doc, expect, pass) \
|
|
|
|
do { \
|
2018-05-31 17:20:13 +00:00
|
|
|
struct testInfo info = { name, doc, expect, pass }; \
|
2017-11-03 12:09:47 +00:00
|
|
|
if (virTestRun(name, testJSON ## cmd, &info) < 0) \
|
|
|
|
ret = -1; \
|
2011-06-30 14:08:29 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
/**
|
|
|
|
* DO_TEST_PARSE:
|
|
|
|
* @name: test name
|
|
|
|
* @doc: source JSON string
|
|
|
|
* @expect: expected output JSON formatted from parsed @doc
|
|
|
|
*
|
|
|
|
* Parses @doc and formats it back. If @expect is NULL the result has to be
|
|
|
|
* identical to @doc.
|
|
|
|
*/
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_PARSE(name, doc, expect) \
|
2017-07-03 13:37:21 +00:00
|
|
|
DO_TEST_FULL(name, FromString, doc, expect, true)
|
2011-06-30 14:08:29 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_PARSE_FAIL(name, doc) \
|
2013-10-14 13:37:06 +00:00
|
|
|
DO_TEST_FULL(name, FromString, doc, NULL, false)
|
|
|
|
|
2018-06-01 11:10:11 +00:00
|
|
|
#define DO_TEST_PARSE_FILE(name) \
|
|
|
|
DO_TEST_FULL(name, FromFile, NULL, NULL, true)
|
|
|
|
|
|
|
|
|
|
|
|
DO_TEST_PARSE_FILE("Simple");
|
|
|
|
DO_TEST_PARSE_FILE("NotSoSimple");
|
|
|
|
DO_TEST_PARSE_FILE("Harder");
|
|
|
|
DO_TEST_PARSE_FILE("VeryHard");
|
2011-06-30 14:08:29 +00:00
|
|
|
|
2018-06-01 07:05:23 +00:00
|
|
|
DO_TEST_FULL("success", AddRemove, NULL, NULL, true);
|
|
|
|
DO_TEST_FULL("failure", AddRemove, NULL, NULL, false);
|
2013-04-26 14:59:02 +00:00
|
|
|
|
2014-08-21 10:23:41 +00:00
|
|
|
DO_TEST_FULL("copy and free", Copy,
|
|
|
|
"{\"return\": [{\"name\": \"quit\"}, {\"name\": \"eject\"},"
|
|
|
|
"{\"name\": \"change\"}, {\"name\": \"screendump\"},"
|
|
|
|
"{\"name\": \"stop\"}, {\"name\": \"cont\"}, {\"name\": "
|
|
|
|
"\"system_reset\"}, {\"name\": \"system_powerdown\"}, "
|
|
|
|
"{\"name\": \"device_add\"}, {\"name\": \"device_del\"}, "
|
|
|
|
"{\"name\": \"cpu\"}, {\"name\": \"memsave\"}, {\"name\": "
|
|
|
|
"\"pmemsave\"}, {\"name\": \"migrate\"}, {\"name\": "
|
|
|
|
"\"migrate_cancel\"}, {\"name\": \"migrate_set_speed\"},"
|
|
|
|
"{\"name\": \"client_migrate_info\"}, {\"name\": "
|
|
|
|
"\"migrate_set_downtime\"}, {\"name\": \"netdev_add\"}, "
|
|
|
|
"{\"name\": \"netdev_del\"}, {\"name\": \"block_resize\"},"
|
|
|
|
"{\"name\": \"balloon\"}, {\"name\": \"set_link\"}, {\"name\":"
|
|
|
|
"\"getfd\"}, {\"name\": \"closefd\"}, {\"name\": \"block_passwd\"},"
|
|
|
|
"{\"name\": \"set_password\"}, {\"name\": \"expire_password\"},"
|
|
|
|
"{\"name\": \"qmp_capabilities\"}, {\"name\": "
|
|
|
|
"\"human-monitor-command\"}, {\"name\": \"query-version\"},"
|
|
|
|
"{\"name\": \"query-commands\"}, {\"name\": \"query-chardev\"},"
|
|
|
|
"{\"name\": \"query-block\"}, {\"name\": \"query-blockstats\"}, "
|
|
|
|
"{\"name\": \"query-cpus\"}, {\"name\": \"query-pci\"}, {\"name\":"
|
|
|
|
"\"query-kvm\"}, {\"name\": \"query-status\"}, {\"name\": "
|
|
|
|
"\"query-mice\"}, {\"name\": \"query-vnc\"}, {\"name\": "
|
|
|
|
"\"query-spice\"}, {\"name\": \"query-name\"}, {\"name\": "
|
|
|
|
"\"query-uuid\"}, {\"name\": \"query-migrate\"}, {\"name\": "
|
|
|
|
"\"query-balloon\"}], \"id\": \"libvirt-2\"}", NULL, true);
|
|
|
|
|
2013-10-14 13:37:06 +00:00
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
DO_TEST_PARSE("almost nothing", "[]", NULL);
|
2013-10-14 13:37:06 +00:00
|
|
|
DO_TEST_PARSE_FAIL("nothing", "");
|
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
DO_TEST_PARSE("number without garbage", "[ 234545 ]", "[234545]");
|
2013-10-14 13:37:06 +00:00
|
|
|
DO_TEST_PARSE_FAIL("number with garbage", "[ 2345b45 ]");
|
|
|
|
|
2018-01-11 13:41:58 +00:00
|
|
|
DO_TEST_PARSE("float without garbage", "[ 1.024e19 ]", "[1.024e19]");
|
2013-10-14 13:37:06 +00:00
|
|
|
DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]");
|
|
|
|
|
2018-08-22 13:12:47 +00:00
|
|
|
DO_TEST_PARSE("unsigned minus one", "[ 18446744073709551615 ]", "[18446744073709551615]");
|
|
|
|
DO_TEST_PARSE("another big number", "[ 9223372036854775808 ]", "[9223372036854775808]");
|
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
DO_TEST_PARSE("string", "[ \"The meaning of life\" ]",
|
|
|
|
"[\"The meaning of life\"]");
|
2013-11-04 13:50:11 +00:00
|
|
|
DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]");
|
|
|
|
|
2017-07-03 13:37:21 +00:00
|
|
|
DO_TEST_PARSE("integer", "1", NULL);
|
|
|
|
DO_TEST_PARSE("boolean", "true", NULL);
|
|
|
|
DO_TEST_PARSE("null", "null", NULL);
|
2017-07-03 14:39:21 +00:00
|
|
|
|
|
|
|
DO_TEST_PARSE("escaping symbols", "[\"\\\"\\t\\n\\\\\"]", NULL);
|
|
|
|
DO_TEST_PARSE("escaped strings", "[\"{\\\"blurb\\\":\\\"test\\\"}\"]", NULL);
|
|
|
|
|
2015-06-19 23:28:22 +00:00
|
|
|
DO_TEST_PARSE_FAIL("incomplete keyword", "tr");
|
2015-06-22 20:18:32 +00:00
|
|
|
DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]");
|
2015-06-19 23:28:22 +00:00
|
|
|
DO_TEST_PARSE_FAIL("unknown keyword", "huh");
|
2015-06-22 20:18:33 +00:00
|
|
|
DO_TEST_PARSE_FAIL("comments", "[ /* nope */\n1 // not this either\n]");
|
2015-06-22 20:18:34 +00:00
|
|
|
DO_TEST_PARSE_FAIL("trailing garbage", "[] []");
|
|
|
|
DO_TEST_PARSE_FAIL("list without array", "1, 1");
|
2015-06-22 21:01:15 +00:00
|
|
|
DO_TEST_PARSE_FAIL("parser abuse", "1] [2");
|
2015-06-22 21:14:04 +00:00
|
|
|
DO_TEST_PARSE_FAIL("invalid UTF-8", "\"\x80\"");
|
2013-10-14 13:37:06 +00:00
|
|
|
|
|
|
|
DO_TEST_PARSE_FAIL("object with numeric keys", "{ 1:1, 2:1, 3:2 }");
|
2013-11-04 13:50:11 +00:00
|
|
|
DO_TEST_PARSE_FAIL("unterminated object", "{ \"1\":1, \"2\":1, \"3\":2");
|
|
|
|
DO_TEST_PARSE_FAIL("unterminated array of objects",
|
|
|
|
"[ {\"name\": \"John\"}, {\"name\": \"Paul\"}, ");
|
2013-10-14 13:37:06 +00:00
|
|
|
DO_TEST_PARSE_FAIL("array of an object with an array as a key",
|
|
|
|
"[ {[\"key1\", \"key2\"]: \"value\"} ]");
|
2013-11-04 13:50:11 +00:00
|
|
|
DO_TEST_PARSE_FAIL("object with unterminated key", "{ \"key:7 }");
|
2015-06-22 21:14:04 +00:00
|
|
|
DO_TEST_PARSE_FAIL("duplicate key", "{ \"a\": 1, \"a\": 1 }");
|
2013-10-14 13:37:06 +00:00
|
|
|
|
2015-06-19 23:13:03 +00:00
|
|
|
DO_TEST_FULL("lookup on array", Lookup,
|
|
|
|
"[ 1 ]", NULL, false);
|
|
|
|
DO_TEST_FULL("lookup on string", Lookup,
|
|
|
|
"\"str\"", NULL, false);
|
|
|
|
DO_TEST_FULL("lookup on integer", Lookup,
|
|
|
|
"1", NULL, false);
|
|
|
|
DO_TEST_FULL("lookup with missing key", Lookup,
|
|
|
|
"{ }", NULL, false);
|
|
|
|
DO_TEST_FULL("lookup with wrong type", Lookup,
|
|
|
|
"{ \"a\": 1, \"b\": \"str\", \"c\": [], \"d\": {} }",
|
|
|
|
NULL, false);
|
|
|
|
DO_TEST_FULL("lookup with correct type", Lookup,
|
|
|
|
"{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
|
|
|
|
NULL, true);
|
2017-07-03 14:39:21 +00:00
|
|
|
DO_TEST_FULL("create object with nested json in attribute", EscapeObj,
|
|
|
|
NULL, NULL, true);
|
2018-03-30 10:18:50 +00:00
|
|
|
DO_TEST_FULL("stealing of attributes while creating objects",
|
|
|
|
ObjectFormatSteal, NULL, NULL, true);
|
2015-06-19 23:13:03 +00:00
|
|
|
|
2017-06-26 16:02:35 +00:00
|
|
|
#define DO_TEST_DEFLATTEN(name, pass) \
|
2018-06-01 11:00:12 +00:00
|
|
|
DO_TEST_FULL(name, Deflatten, NULL, NULL, pass)
|
2017-06-26 16:02:35 +00:00
|
|
|
|
|
|
|
DO_TEST_DEFLATTEN("unflattened", true);
|
|
|
|
DO_TEST_DEFLATTEN("basic-file", true);
|
2017-06-27 11:48:56 +00:00
|
|
|
DO_TEST_DEFLATTEN("basic-generic", true);
|
2017-06-26 16:02:35 +00:00
|
|
|
DO_TEST_DEFLATTEN("deep-file", true);
|
2017-06-27 11:48:56 +00:00
|
|
|
DO_TEST_DEFLATTEN("deep-generic", true);
|
2017-06-26 16:02:35 +00:00
|
|
|
DO_TEST_DEFLATTEN("nested", true);
|
2017-06-27 11:48:56 +00:00
|
|
|
DO_TEST_DEFLATTEN("double-key", false);
|
2017-06-26 16:02:35 +00:00
|
|
|
DO_TEST_DEFLATTEN("concat", true);
|
2017-06-27 11:48:56 +00:00
|
|
|
DO_TEST_DEFLATTEN("concat-double-key", false);
|
2017-06-26 17:42:06 +00:00
|
|
|
DO_TEST_DEFLATTEN("qemu-sheepdog", true);
|
2017-06-26 16:02:35 +00:00
|
|
|
|
2011-06-30 14:08:29 +00:00
|
|
|
return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|