2009-11-11 17:39:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
2012-01-10 20:10:49 +01:00
|
|
|
#include <glib.h>
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/qstring.h"
|
|
|
|
#include "qapi/qmp/qint.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/qmp/qlist.h"
|
|
|
|
#include "qapi/qmp/qfloat.h"
|
|
|
|
#include "qapi/qmp/qbool.h"
|
|
|
|
#include "qapi/qmp/qjson.h"
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void escaped_string(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
const char *decoded;
|
2009-11-11 20:16:03 +01:00
|
|
|
int skip;
|
2009-11-11 17:39:47 +01:00
|
|
|
} test_cases[] = {
|
2010-05-19 22:08:37 +02:00
|
|
|
{ "\"\\b\"", "\b" },
|
|
|
|
{ "\"\\f\"", "\f" },
|
|
|
|
{ "\"\\n\"", "\n" },
|
|
|
|
{ "\"\\r\"", "\r" },
|
|
|
|
{ "\"\\t\"", "\t" },
|
2011-08-26 19:52:11 +02:00
|
|
|
{ "\"/\"", "/" },
|
|
|
|
{ "\"\\/\"", "/", .skip = 1 },
|
2010-05-19 22:08:37 +02:00
|
|
|
{ "\"\\\\\"", "\\" },
|
2009-11-11 17:39:47 +01:00
|
|
|
{ "\"\\\"\"", "\"" },
|
|
|
|
{ "\"hello world \\\"embedded string\\\"\"",
|
|
|
|
"hello world \"embedded string\"" },
|
|
|
|
{ "\"hello world\\nwith new line\"", "hello world\nwith new line" },
|
2009-11-11 20:16:03 +01:00
|
|
|
{ "\"single byte utf-8 \\u0020\"", "single byte utf-8 ", .skip = 1 },
|
2009-11-11 17:39:47 +01:00
|
|
|
{ "\"double byte utf-8 \\u00A2\"", "double byte utf-8 \xc2\xa2" },
|
|
|
|
{ "\"triple byte utf-8 \\u20AC\"", "triple byte utf-8 \xe2\x82\xac" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QSTRING);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
str = qobject_to_qstring(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
if (test_cases[i].skip == 0) {
|
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
|
2009-11-11 20:16:03 +01:00
|
|
|
qobject_decref(obj);
|
|
|
|
}
|
|
|
|
|
2009-11-11 17:39:47 +01:00
|
|
|
QDECREF(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_string(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
const char *decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{ "\"hello world\"", "hello world" },
|
|
|
|
{ "\"the quick brown fox jumped over the fence\"",
|
|
|
|
"the quick brown fox jumped over the fence" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QSTRING);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
str = qobject_to_qstring(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
|
|
|
qobject_decref(obj);
|
|
|
|
|
2009-11-11 17:39:47 +01:00
|
|
|
QDECREF(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void single_quote_string(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
const char *decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{ "'hello world'", "hello world" },
|
|
|
|
{ "'the quick brown fox \\' jumped over the fence'",
|
|
|
|
"the quick brown fox ' jumped over the fence" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QSTRING);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
str = qobject_to_qstring(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void vararg_string(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{ "hello world" },
|
|
|
|
{ "the quick brown fox jumped over the fence" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].decoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%s", test_cases[i].decoded);
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QSTRING);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
str = qobject_to_qstring(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_number(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
int64_t decoded;
|
2009-11-11 20:16:03 +01:00
|
|
|
int skip;
|
2009-11-11 17:39:47 +01:00
|
|
|
} test_cases[] = {
|
|
|
|
{ "0", 0 },
|
|
|
|
{ "1234", 1234 },
|
|
|
|
{ "1", 1 },
|
|
|
|
{ "-32", -32 },
|
2009-11-11 20:16:03 +01:00
|
|
|
{ "-0", 0, .skip = 1 },
|
2009-11-11 17:39:47 +01:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QInt *qint;
|
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QINT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qint = qobject_to_qint(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qint_get_int(qint) == test_cases[i].decoded);
|
2009-11-11 20:16:03 +01:00
|
|
|
if (test_cases[i].skip == 0) {
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
|
|
|
}
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void float_number(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
double decoded;
|
2009-11-11 20:16:03 +01:00
|
|
|
int skip;
|
2009-11-11 17:39:47 +01:00
|
|
|
} test_cases[] = {
|
|
|
|
{ "32.43", 32.43 },
|
|
|
|
{ "0.222", 0.222 },
|
|
|
|
{ "-32.12313", -32.12313 },
|
2009-11-11 20:16:03 +01:00
|
|
|
{ "-32.20e-10", -32.20e-10, .skip = 1 },
|
2009-11-11 17:39:47 +01:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
|
|
|
QFloat *qfloat;
|
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QFLOAT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qfloat = qobject_to_qfloat(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
if (test_cases[i].skip == 0) {
|
|
|
|
QString *str;
|
|
|
|
|
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
|
|
|
}
|
|
|
|
|
2009-11-11 17:39:47 +01:00
|
|
|
QDECREF(qfloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void vararg_number(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
QObject *obj;
|
|
|
|
QInt *qint;
|
|
|
|
QFloat *qfloat;
|
|
|
|
int value = 0x2342;
|
|
|
|
int64_t value64 = 0x2342342343LL;
|
|
|
|
double valuef = 2.323423423;
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%d", value);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QINT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qint = qobject_to_qint(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qint_get_int(qint) == value);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qint);
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%" PRId64, value64);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QINT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qint = qobject_to_qint(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qint_get_int(qint) == value64);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qint);
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%f", valuef);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QFLOAT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qfloat = qobject_to_qfloat(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qfloat_get_double(qfloat) == valuef);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qfloat);
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void keyword_literal(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
QObject *obj;
|
|
|
|
QBool *qbool;
|
2009-11-11 20:16:03 +01:00
|
|
|
QString *str;
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
obj = qobject_from_json("true");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QBOOL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qbool = qobject_to_qbool(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qbool_get_int(qbool) != 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), "true") == 0);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
|
|
|
|
2009-11-11 17:39:47 +01:00
|
|
|
QDECREF(qbool);
|
|
|
|
|
|
|
|
obj = qobject_from_json("false");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QBOOL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qbool = qobject_to_qbool(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qbool_get_int(qbool) == 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(strcmp(qstring_get_str(str), "false") == 0);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
|
|
|
|
2009-11-11 17:39:47 +01:00
|
|
|
QDECREF(qbool);
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%i", false);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QBOOL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qbool = qobject_to_qbool(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qbool_get_int(qbool) == 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qbool);
|
|
|
|
|
|
|
|
obj = qobject_from_jsonf("%i", true);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QBOOL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qbool = qobject_to_qbool(obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(qbool_get_int(qbool) != 0);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
QDECREF(qbool);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct LiteralQDictEntry LiteralQDictEntry;
|
|
|
|
typedef struct LiteralQObject LiteralQObject;
|
|
|
|
|
|
|
|
struct LiteralQObject
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
union {
|
|
|
|
int64_t qint;
|
|
|
|
const char *qstr;
|
|
|
|
LiteralQDictEntry *qdict;
|
|
|
|
LiteralQObject *qlist;
|
|
|
|
} value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LiteralQDictEntry
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
LiteralQObject value;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define QLIT_QINT(val) (LiteralQObject){.type = QTYPE_QINT, .value.qint = (val)}
|
|
|
|
#define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
|
|
|
|
#define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
|
|
|
|
#define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
|
|
|
|
|
|
|
|
typedef struct QListCompareHelper
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
LiteralQObject *objs;
|
|
|
|
int result;
|
|
|
|
} QListCompareHelper;
|
|
|
|
|
|
|
|
static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
|
|
|
|
|
|
|
|
static void compare_helper(QObject *obj, void *opaque)
|
|
|
|
{
|
|
|
|
QListCompareHelper *helper = opaque;
|
|
|
|
|
|
|
|
if (helper->result == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (helper->objs[helper->index].type == QTYPE_NONE) {
|
|
|
|
helper->result = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
helper->result = compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
|
|
|
|
{
|
|
|
|
if (lhs->type != qobject_type(rhs)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lhs->type) {
|
|
|
|
case QTYPE_QINT:
|
|
|
|
return lhs->value.qint == qint_get_int(qobject_to_qint(rhs));
|
|
|
|
case QTYPE_QSTRING:
|
|
|
|
return (strcmp(lhs->value.qstr, qstring_get_str(qobject_to_qstring(rhs))) == 0);
|
|
|
|
case QTYPE_QDICT: {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; lhs->value.qdict[i].key; i++) {
|
|
|
|
QObject *obj = qdict_get(qobject_to_qdict(rhs), lhs->value.qdict[i].key);
|
|
|
|
|
|
|
|
if (!compare_litqobj_to_qobj(&lhs->value.qdict[i].value, obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case QTYPE_QLIST: {
|
|
|
|
QListCompareHelper helper;
|
|
|
|
|
|
|
|
helper.index = 0;
|
|
|
|
helper.objs = lhs->value.qlist;
|
|
|
|
helper.result = 1;
|
|
|
|
|
|
|
|
qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
|
|
|
|
|
|
|
|
return helper.result;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_dict(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
LiteralQObject decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{
|
2009-11-11 20:16:03 +01:00
|
|
|
.encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
|
2009-11-11 17:39:47 +01:00
|
|
|
.decoded = QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{ "foo", QLIT_QINT(42) },
|
|
|
|
{ "bar", QLIT_QSTR("hello world") },
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
}, {
|
|
|
|
.encoded = "{}",
|
|
|
|
.decoded = QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
}, {
|
2009-11-11 20:16:03 +01:00
|
|
|
.encoded = "{\"foo\": 43}",
|
2009-11-11 17:39:47 +01:00
|
|
|
.decoded = QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{ "foo", QLIT_QINT(43) },
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
2009-11-11 20:16:03 +01:00
|
|
|
QString *str;
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QDICT);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
|
|
|
qobject_decref(obj);
|
|
|
|
|
|
|
|
obj = qobject_from_json(qstring_get_str(str));
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QDICT);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
qobject_decref(obj);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
2009-11-11 17:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:44 +02:00
|
|
|
/*
|
|
|
|
* this generates json of the form:
|
|
|
|
* a(0,m) = [0, 1, ..., m-1]
|
|
|
|
* a(n,m) = {
|
|
|
|
* 'key0': a(0,m),
|
|
|
|
* 'key1': a(1,m),
|
|
|
|
* ...
|
|
|
|
* 'key(n-1)': a(n-1,m)
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static void gen_test_json(GString *gstr, int nest_level_max,
|
|
|
|
int elem_count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_assert(gstr);
|
|
|
|
if (nest_level_max == 0) {
|
|
|
|
g_string_append(gstr, "[");
|
|
|
|
for (i = 0; i < elem_count; i++) {
|
|
|
|
g_string_append_printf(gstr, "%d", i);
|
|
|
|
if (i < elem_count - 1) {
|
|
|
|
g_string_append_printf(gstr, ", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_string_append(gstr, "]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append(gstr, "{");
|
|
|
|
for (i = 0; i < nest_level_max; i++) {
|
|
|
|
g_string_append_printf(gstr, "'key%d': ", i);
|
|
|
|
gen_test_json(gstr, i, elem_count);
|
|
|
|
if (i < nest_level_max - 1) {
|
|
|
|
g_string_append(gstr, ",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_string_append(gstr, "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void large_dict(void)
|
|
|
|
{
|
|
|
|
GString *gstr = g_string_new("");
|
|
|
|
QObject *obj;
|
|
|
|
|
|
|
|
gen_test_json(gstr, 10, 100);
|
|
|
|
obj = qobject_from_json(gstr->str);
|
|
|
|
g_assert(obj != NULL);
|
|
|
|
|
|
|
|
qobject_decref(obj);
|
|
|
|
g_string_free(gstr, true);
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_list(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
LiteralQObject decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{
|
|
|
|
.encoded = "[43,42]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(43),
|
|
|
|
QLIT_QINT(42),
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.encoded = "[43]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(43),
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.encoded = "[]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.encoded = "[{}]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{},
|
|
|
|
})),
|
|
|
|
{},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
2009-11-11 20:16:03 +01:00
|
|
|
QString *str;
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QLIST);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
|
|
|
qobject_decref(obj);
|
|
|
|
|
|
|
|
obj = qobject_from_json(qstring_get_str(str));
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QLIST);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
qobject_decref(obj);
|
2009-11-11 20:16:03 +01:00
|
|
|
QDECREF(str);
|
2009-11-11 17:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_whitespace(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct {
|
|
|
|
const char *encoded;
|
|
|
|
LiteralQObject decoded;
|
|
|
|
} test_cases[] = {
|
|
|
|
{
|
|
|
|
.encoded = " [ 43 , 42 ]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(43),
|
|
|
|
QLIT_QINT(42),
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(43),
|
|
|
|
QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{ "h", QLIT_QSTR("b") },
|
|
|
|
{ }})),
|
|
|
|
QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
{ }})),
|
|
|
|
QLIT_QINT(42),
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
|
|
|
|
.decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(43),
|
|
|
|
QLIT_QDICT(((LiteralQDictEntry[]){
|
|
|
|
{ "h", QLIT_QSTR("b") },
|
|
|
|
{ "a", QLIT_QINT(32) },
|
|
|
|
{ }})),
|
|
|
|
QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
{ }})),
|
|
|
|
QLIT_QINT(42),
|
|
|
|
{ }
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; test_cases[i].encoded; i++) {
|
|
|
|
QObject *obj;
|
2009-11-11 20:16:03 +01:00
|
|
|
QString *str;
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
obj = qobject_from_json(test_cases[i].encoded);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QLIST);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2009-11-11 20:16:03 +01:00
|
|
|
str = qobject_to_json(obj);
|
2009-11-11 17:39:47 +01:00
|
|
|
qobject_decref(obj);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
|
|
|
obj = qobject_from_json(qstring_get_str(str));
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
|
|
|
g_assert(qobject_type(obj) == QTYPE_QLIST);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
2009-11-11 20:16:03 +01:00
|
|
|
|
|
|
|
qobject_decref(obj);
|
|
|
|
QDECREF(str);
|
2009-11-11 17:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void simple_varargs(void)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
|
|
|
QObject *embedded_obj;
|
|
|
|
QObject *obj;
|
|
|
|
LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(1),
|
|
|
|
QLIT_QINT(2),
|
|
|
|
QLIT_QLIST(((LiteralQObject[]){
|
|
|
|
QLIT_QINT(32),
|
|
|
|
QLIT_QINT(42),
|
|
|
|
{}})),
|
|
|
|
{}}));
|
|
|
|
|
|
|
|
embedded_obj = qobject_from_json("[32, 42]");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(embedded_obj != NULL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj != NULL);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
|
2009-11-11 17:39:47 +01:00
|
|
|
|
|
|
|
qobject_decref(obj);
|
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void empty_input(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
2010-10-16 19:42:43 +02:00
|
|
|
const char *empty = "";
|
|
|
|
|
|
|
|
QObject *obj = qobject_from_json(empty);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_string(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("\"abc");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_sq_string(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("'abc");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_escape(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("\"abc\\\"");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_array(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("[32");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_array_comma(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("[32,");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void invalid_array_comma(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("[32,}");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_dict(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("{'abc':32");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_dict_comma(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("{'abc':32,");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void invalid_dict_comma(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("{'abc':32,}");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
static void unterminated_literal(void)
|
2010-05-24 09:39:51 +02:00
|
|
|
{
|
|
|
|
QObject *obj = qobject_from_json("nul");
|
2012-01-10 20:10:49 +01:00
|
|
|
g_assert(obj == NULL);
|
2010-05-24 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
int main(int argc, char **argv)
|
2009-11-11 17:39:47 +01:00
|
|
|
{
|
2012-01-10 20:10:49 +01:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_test_add_func("/literals/string/simple", simple_string);
|
|
|
|
g_test_add_func("/literals/string/escaped", escaped_string);
|
|
|
|
g_test_add_func("/literals/string/single_quote", single_quote_string);
|
|
|
|
g_test_add_func("/literals/string/vararg", vararg_string);
|
|
|
|
|
|
|
|
g_test_add_func("/literals/number/simple", simple_number);
|
|
|
|
g_test_add_func("/literals/number/float", float_number);
|
|
|
|
g_test_add_func("/literals/number/vararg", vararg_number);
|
|
|
|
|
|
|
|
g_test_add_func("/literals/keyword", keyword_literal);
|
|
|
|
|
|
|
|
g_test_add_func("/dicts/simple_dict", simple_dict);
|
2012-08-15 20:45:44 +02:00
|
|
|
g_test_add_func("/dicts/large_dict", large_dict);
|
2012-01-10 20:10:49 +01:00
|
|
|
g_test_add_func("/lists/simple_list", simple_list);
|
|
|
|
|
|
|
|
g_test_add_func("/whitespace/simple_whitespace", simple_whitespace);
|
|
|
|
|
|
|
|
g_test_add_func("/varargs/simple_varargs", simple_varargs);
|
|
|
|
|
|
|
|
g_test_add_func("/errors/empty_input", empty_input);
|
|
|
|
g_test_add_func("/errors/unterminated/string", unterminated_string);
|
|
|
|
g_test_add_func("/errors/unterminated/escape", unterminated_escape);
|
|
|
|
g_test_add_func("/errors/unterminated/sq_string", unterminated_sq_string);
|
|
|
|
g_test_add_func("/errors/unterminated/array", unterminated_array);
|
|
|
|
g_test_add_func("/errors/unterminated/array_comma", unterminated_array_comma);
|
|
|
|
g_test_add_func("/errors/unterminated/dict", unterminated_dict);
|
|
|
|
g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
|
|
|
|
g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
|
|
|
|
g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
|
|
|
|
g_test_add_func("/errors/unterminated/literal", unterminated_literal);
|
2010-05-24 09:39:51 +02:00
|
|
|
|
2012-01-10 20:10:49 +01:00
|
|
|
return g_test_run();
|
2009-11-11 17:39:47 +01:00
|
|
|
}
|