* See the COPYING.LIB file in the top-level directory.
*
*/
+
#include "qemu/osdep.h"
+#include "qapi/error.h"
#include "qapi/qmp/types.h"
#include "qapi/qmp/qjson.h"
#include "qemu-common.h"
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
utf8_in = test_cases[i].utf8_in ?: test_cases[i].utf8_out;
json_out = test_cases[i].json_out ?: test_cases[i].json_in;
- obj = qobject_from_json(json_in);
+ obj = qobject_from_json(json_in, utf8_out ? &error_abort : NULL);
if (utf8_out) {
str = qobject_to_qstring(obj);
g_assert(str);
* FIXME Enable once these bugs have been fixed.
*/
if (0 && json_out != json_in) {
- obj = qobject_from_json(json_out);
+ obj = qobject_from_json(json_out, &error_abort);
str = qobject_to_qstring(obj);
g_assert(str);
g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
};
for (i = 0; test_cases[i].encoded; i++) {
- QInt *qint;
-
- qint = qobject_to_qint(qobject_from_json(test_cases[i].encoded));
- g_assert(qint);
- g_assert(qint_get_int(qint) == test_cases[i].decoded);
+ QNum *qnum;
+ int64_t val;
+
+ qnum = qobject_to_qnum(qobject_from_json(test_cases[i].encoded,
+ &error_abort));
+ g_assert(qnum);
+ g_assert(qnum_get_try_int(qnum, &val));
+ g_assert_cmpint(val, ==, test_cases[i].decoded);
if (test_cases[i].skip == 0) {
QString *str;
- str = qobject_to_json(QOBJECT(qint));
+ str = qobject_to_json(QOBJECT(qnum));
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
QDECREF(str);
}
- QDECREF(qint);
+ QDECREF(qnum);
}
}
+static void large_number(void)
+{
+ const char *maxu64 = "18446744073709551615"; /* 2^64-1 */
+ const char *gtu64 = "18446744073709551616"; /* 2^64 */
+ const char *lti64 = "-9223372036854775809"; /* -2^63 - 1 */
+ QNum *qnum;
+ QString *str;
+ uint64_t val;
+ int64_t ival;
+
+ qnum = qobject_to_qnum(qobject_from_json(maxu64, &error_abort));
+ g_assert(qnum);
+ g_assert_cmpuint(qnum_get_uint(qnum), ==, 18446744073709551615U);
+ g_assert(!qnum_get_try_int(qnum, &ival));
+
+ str = qobject_to_json(QOBJECT(qnum));
+ g_assert_cmpstr(qstring_get_str(str), ==, maxu64);
+ QDECREF(str);
+ QDECREF(qnum);
+
+ qnum = qobject_to_qnum(qobject_from_json(gtu64, &error_abort));
+ g_assert(qnum);
+ g_assert_cmpfloat(qnum_get_double(qnum), ==, 18446744073709552e3);
+ g_assert(!qnum_get_try_uint(qnum, &val));
+ g_assert(!qnum_get_try_int(qnum, &ival));
+
+ str = qobject_to_json(QOBJECT(qnum));
+ g_assert_cmpstr(qstring_get_str(str), ==, gtu64);
+ QDECREF(str);
+ QDECREF(qnum);
+
+ qnum = qobject_to_qnum(qobject_from_json(lti64, &error_abort));
+ g_assert(qnum);
+ g_assert_cmpfloat(qnum_get_double(qnum), ==, -92233720368547758e2);
+ g_assert(!qnum_get_try_uint(qnum, &val));
+ g_assert(!qnum_get_try_int(qnum, &ival));
+
+ str = qobject_to_json(QOBJECT(qnum));
+ g_assert_cmpstr(qstring_get_str(str), ==, "-9223372036854775808");
+ QDECREF(str);
+ QDECREF(qnum);
+}
+
static void float_number(void)
{
int i;
for (i = 0; test_cases[i].encoded; i++) {
QObject *obj;
- QFloat *qfloat;
+ QNum *qnum;
- obj = qobject_from_json(test_cases[i].encoded);
- qfloat = qobject_to_qfloat(obj);
- g_assert(qfloat);
- g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
+ qnum = qobject_to_qnum(obj);
+ g_assert(qnum);
+ g_assert(qnum_get_double(qnum) == test_cases[i].decoded);
if (test_cases[i].skip == 0) {
QString *str;
QDECREF(str);
}
- QDECREF(qfloat);
+ QDECREF(qnum);
}
}
static void vararg_number(void)
{
- QInt *qint;
- QFloat *qfloat;
+ QNum *qnum;
int value = 0x2342;
long long value_ll = 0x2342342343LL;
double valuef = 2.323423423;
+ int64_t val;
- qint = qobject_to_qint(qobject_from_jsonf("%d", value));
- g_assert(qint_get_int(qint) == value);
- QDECREF(qint);
+ qnum = qobject_to_qnum(qobject_from_jsonf("%d", value));
+ g_assert(qnum_get_try_int(qnum, &val));
+ g_assert_cmpint(val, ==, value);
+ QDECREF(qnum);
- qint = qobject_to_qint(qobject_from_jsonf("%lld", value_ll));
- g_assert(qint_get_int(qint) == value_ll);
- QDECREF(qint);
+ qnum = qobject_to_qnum(qobject_from_jsonf("%lld", value_ll));
+ g_assert(qnum_get_try_int(qnum, &val));
+ g_assert_cmpint(val, ==, value_ll);
+ QDECREF(qnum);
- qfloat = qobject_to_qfloat(qobject_from_jsonf("%f", valuef));
- g_assert(qfloat_get_double(qfloat) == valuef);
- QDECREF(qfloat);
+ qnum = qobject_to_qnum(qobject_from_jsonf("%f", valuef));
+ g_assert(qnum_get_double(qnum) == valuef);
+ QDECREF(qnum);
}
static void keyword_literal(void)
QObject *null;
QString *str;
- obj = qobject_from_json("true");
+ obj = qobject_from_json("true", &error_abort);
qbool = qobject_to_qbool(obj);
g_assert(qbool);
g_assert(qbool_get_bool(qbool) == true);
QDECREF(qbool);
- obj = qobject_from_json("false");
+ obj = qobject_from_json("false", &error_abort);
qbool = qobject_to_qbool(obj);
g_assert(qbool);
g_assert(qbool_get_bool(qbool) == false);
g_assert(qbool_get_bool(qbool) == true);
QDECREF(qbool);
- obj = qobject_from_json("null");
+ obj = qobject_from_json("null", &error_abort);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QNULL);
{
int type;
union {
- int64_t qint;
+ int64_t qnum;
const char *qstr;
LiteralQDictEntry *qdict;
LiteralQObject *qlist;
LiteralQObject value;
};
-#define QLIT_QINT(val) (LiteralQObject){.type = QTYPE_QINT, .value.qint = (val)}
+#define QLIT_QNUM(val) (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (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)}
static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
{
+ int64_t val;
+
if (!rhs || 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_QNUM:
+ g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
+ return lhs->value.qnum == val;
case QTYPE_QSTRING:
return (strcmp(lhs->value.qstr, qstring_get_str(qobject_to_qstring(rhs))) == 0);
case QTYPE_QDICT: {
{
.encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
.decoded = QLIT_QDICT(((LiteralQDictEntry[]){
- { "foo", QLIT_QINT(42) },
+ { "foo", QLIT_QNUM(42) },
{ "bar", QLIT_QSTR("hello world") },
{ }
})),
}, {
.encoded = "{\"foo\": 43}",
.decoded = QLIT_QDICT(((LiteralQDictEntry[]){
- { "foo", QLIT_QINT(43) },
+ { "foo", QLIT_QNUM(43) },
{ }
})),
},
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
- obj = qobject_from_json(qstring_get_str(str));
+ obj = qobject_from_json(qstring_get_str(str), &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QDECREF(str);
QObject *obj;
gen_test_json(gstr, 10, 100);
- obj = qobject_from_json(gstr->str);
+ obj = qobject_from_json(gstr->str, &error_abort);
g_assert(obj != NULL);
qobject_decref(obj);
{
.encoded = "[43,42]",
.decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(43),
- QLIT_QINT(42),
+ QLIT_QNUM(43),
+ QLIT_QNUM(42),
{ }
})),
},
{
.encoded = "[43]",
.decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(43),
+ QLIT_QNUM(43),
{ }
})),
},
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
- obj = qobject_from_json(qstring_get_str(str));
+ obj = qobject_from_json(qstring_get_str(str), &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QDECREF(str);
{
.encoded = " [ 43 , 42 ]",
.decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(43),
- QLIT_QINT(42),
+ QLIT_QNUM(43),
+ QLIT_QNUM(42),
{ }
})),
},
{
.encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
.decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(43),
+ QLIT_QNUM(43),
QLIT_QDICT(((LiteralQDictEntry[]){
{ "h", QLIT_QSTR("b") },
{ }})),
QLIT_QLIST(((LiteralQObject[]){
{ }})),
- QLIT_QINT(42),
+ QLIT_QNUM(42),
{ }
})),
},
{
.encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
.decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(43),
+ QLIT_QNUM(43),
QLIT_QDICT(((LiteralQDictEntry[]){
{ "h", QLIT_QSTR("b") },
- { "a", QLIT_QINT(32) },
+ { "a", QLIT_QNUM(32) },
{ }})),
QLIT_QLIST(((LiteralQObject[]){
{ }})),
- QLIT_QINT(42),
+ QLIT_QNUM(42),
{ }
})),
},
QObject *obj;
QString *str;
- obj = qobject_from_json(test_cases[i].encoded);
+ obj = qobject_from_json(test_cases[i].encoded, &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
- obj = qobject_from_json(qstring_get_str(str));
+ obj = qobject_from_json(qstring_get_str(str), &error_abort);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QObject *embedded_obj;
QObject *obj;
LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(1),
- QLIT_QINT(2),
+ QLIT_QNUM(1),
+ QLIT_QNUM(2),
QLIT_QLIST(((LiteralQObject[]){
- QLIT_QINT(32),
- QLIT_QINT(42),
+ QLIT_QNUM(32),
+ QLIT_QNUM(42),
{}})),
{}}));
- embedded_obj = qobject_from_json("[32, 42]");
+ embedded_obj = qobject_from_json("[32, 42]", &error_abort);
g_assert(embedded_obj != NULL);
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
static void empty_input(void)
{
const char *empty = "";
-
- QObject *obj = qobject_from_json(empty);
+ QObject *obj = qobject_from_json(empty, &error_abort);
g_assert(obj == NULL);
}
static void unterminated_string(void)
{
- QObject *obj = qobject_from_json("\"abc");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("\"abc", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void unterminated_sq_string(void)
{
- QObject *obj = qobject_from_json("'abc");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("'abc", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void unterminated_escape(void)
{
- QObject *obj = qobject_from_json("\"abc\\\"");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("\"abc\\\"", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void unterminated_array(void)
{
- QObject *obj = qobject_from_json("[32");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("[32", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void unterminated_array_comma(void)
{
- QObject *obj = qobject_from_json("[32,");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("[32,", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void invalid_array_comma(void)
{
- QObject *obj = qobject_from_json("[32,}");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("[32,}", &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
static void unterminated_dict(void)
{
- QObject *obj = qobject_from_json("{'abc':32");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("{'abc':32", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void unterminated_dict_comma(void)
{
- QObject *obj = qobject_from_json("{'abc':32,");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("{'abc':32,", &err);
+ g_assert(!err); /* BUG */
g_assert(obj == NULL);
}
static void invalid_dict_comma(void)
{
- QObject *obj = qobject_from_json("{'abc':32,}");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("{'abc':32,}", &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
static void unterminated_literal(void)
{
- QObject *obj = qobject_from_json("nul");
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("nul", &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
static void limits_nesting(void)
{
+ Error *err = NULL;
enum { max_nesting = 1024 }; /* see qobject/json-streamer.c */
char buf[2 * (max_nesting + 1) + 1];
QObject *obj;
- obj = qobject_from_json(make_nest(buf, max_nesting));
+ obj = qobject_from_json(make_nest(buf, max_nesting), &error_abort);
g_assert(obj != NULL);
qobject_decref(obj);
- obj = qobject_from_json(make_nest(buf, max_nesting + 1));
+ obj = qobject_from_json(make_nest(buf, max_nesting + 1), &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
g_test_add_func("/literals/string/vararg", vararg_string);
g_test_add_func("/literals/number/simple", simple_number);
+ g_test_add_func("/literals/number/large", large_number);
g_test_add_func("/literals/number/float", float_number);
g_test_add_func("/literals/number/vararg", vararg_number);