3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-22 06:29:23 +01:00

unit: add checks for improper use of json_iter_parse/next

This adds checks to make sure these API's don't operate on
objects/primitives they aren't supposed to.
This commit is contained in:
James Prestwood 2022-01-06 14:52:58 -08:00 committed by Denis Kenzior
parent 67ded4c2fd
commit b007909c0d

View File

@ -225,12 +225,14 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,
assert(!json_iter_get_uint(i, NULL)); assert(!json_iter_get_uint(i, NULL));
assert(!json_iter_get_boolean(i, NULL)); assert(!json_iter_get_boolean(i, NULL));
assert(!json_iter_get_null(i)); assert(!json_iter_get_null(i));
assert(!json_iter_next(i));
assert(json_iter_get_int(i, &i_val)); assert(json_iter_get_int(i, &i_val));
assert(i_val == -10); assert(i_val == -10);
assert(json_iter_is_valid(ui)); assert(json_iter_is_valid(ui));
assert(!json_iter_get_boolean(ui, NULL)); assert(!json_iter_get_boolean(ui, NULL));
assert(!json_iter_get_null(ui)); assert(!json_iter_get_null(ui));
assert(!json_iter_next(ui));
assert(json_iter_get_int(ui, &i_val)); assert(json_iter_get_int(ui, &i_val));
assert(json_iter_get_uint(ui, &ui_val)); assert(json_iter_get_uint(ui, &ui_val));
assert(i_val == 10 && ui_val == 10); assert(i_val == 10 && ui_val == 10);
@ -239,6 +241,7 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,
assert(!json_iter_get_null(t)); assert(!json_iter_get_null(t));
assert(!json_iter_get_int(t, NULL)); assert(!json_iter_get_int(t, NULL));
assert(!json_iter_get_uint(t, NULL)); assert(!json_iter_get_uint(t, NULL));
assert(!json_iter_next(t));
assert(json_iter_get_boolean(t, &b_val)); assert(json_iter_get_boolean(t, &b_val));
assert(b_val == true); assert(b_val == true);
@ -246,6 +249,7 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,
assert(!json_iter_get_null(f)); assert(!json_iter_get_null(f));
assert(!json_iter_get_int(f, NULL)); assert(!json_iter_get_int(f, NULL));
assert(!json_iter_get_uint(f, NULL)); assert(!json_iter_get_uint(f, NULL));
assert(!json_iter_next(f));
assert(json_iter_get_boolean(f, &b_val)); assert(json_iter_get_boolean(f, &b_val));
assert(b_val == false); assert(b_val == false);
@ -253,10 +257,12 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,
assert(!json_iter_get_int(null, NULL)); assert(!json_iter_get_int(null, NULL));
assert(!json_iter_get_uint(null, NULL)); assert(!json_iter_get_uint(null, NULL));
assert(!json_iter_get_boolean(null, NULL)); assert(!json_iter_get_boolean(null, NULL));
assert(!json_iter_next(null));
assert(json_iter_get_null(null)); assert(json_iter_get_null(null));
if (obj) { if (obj) {
assert(json_iter_is_valid(obj)); assert(json_iter_is_valid(obj));
assert(!json_iter_next(obj));
assert(json_iter_parse(obj, assert(json_iter_parse(obj,
JSON_MANDATORY("null_val", JSON_PRIMITIVE, null), JSON_MANDATORY("null_val", JSON_PRIMITIVE, null),
JSON_MANDATORY("false_val", JSON_PRIMITIVE, f), JSON_MANDATORY("false_val", JSON_PRIMITIVE, f),
@ -336,6 +342,7 @@ static void test_json_arrays(const void *data)
while (json_iter_next(&ui_array)) { while (json_iter_next(&ui_array)) {
assert(json_iter_get_type(&ui_array) == JSON_PRIMITIVE); assert(json_iter_get_type(&ui_array) == JSON_PRIMITIVE);
assert(!json_iter_parse(&ui_array, JSON_UNDEFINED));
assert(json_iter_get_uint(&ui_array, &ui)); assert(json_iter_get_uint(&ui_array, &ui));
assert(ui == (unsigned int) count); assert(ui == (unsigned int) count);
count++; count++;
@ -345,6 +352,7 @@ static void test_json_arrays(const void *data)
while (json_iter_next(&i_array)) { while (json_iter_next(&i_array)) {
assert(json_iter_get_type(&i_array) == JSON_PRIMITIVE); assert(json_iter_get_type(&i_array) == JSON_PRIMITIVE);
assert(!json_iter_parse(&i_array, JSON_UNDEFINED));
assert(json_iter_get_int(&i_array, &i)); assert(json_iter_get_int(&i_array, &i));
assert(i == count); assert(i == count);
count--; count--;
@ -354,6 +362,7 @@ static void test_json_arrays(const void *data)
while (json_iter_next(&b_array)) { while (json_iter_next(&b_array)) {
assert(json_iter_get_type(&b_array) == JSON_PRIMITIVE); assert(json_iter_get_type(&b_array) == JSON_PRIMITIVE);
assert(!json_iter_parse(&b_array, JSON_UNDEFINED));
assert(json_iter_get_boolean(&b_array, &b)); assert(json_iter_get_boolean(&b_array, &b));
assert(b == count % 2 ? false : true); assert(b == count % 2 ? false : true);
count++; count++;
@ -363,6 +372,7 @@ static void test_json_arrays(const void *data)
while (json_iter_next(&n_array)) { while (json_iter_next(&n_array)) {
assert(json_iter_get_type(&n_array) == JSON_PRIMITIVE); assert(json_iter_get_type(&n_array) == JSON_PRIMITIVE);
assert(!json_iter_parse(&n_array, JSON_UNDEFINED));
assert(json_iter_get_null(&n_array)); assert(json_iter_get_null(&n_array));
count++; count++;
} }
@ -407,6 +417,8 @@ static void test_json_arrays(const void *data)
count = 0; count = 0;
assert(!json_iter_parse(&obj_array, JSON_UNDEFINED));
while (json_iter_next(&obj_array)) { while (json_iter_next(&obj_array)) {
struct json_iter object; struct json_iter object;
@ -475,17 +487,19 @@ static void test_json_nested_arrays(const void *data)
count2++; count2++;
} }
/*
* TODO: add checks for object iteration. Currently these will
* just count all tokens in the object.
*/
switch (count) { switch (count) {
case 0: case 0:
assert(count2 == 0); assert(count2 == 0);
break; break;
case 1:
assert(count2 == 0);
break;
case 2: case 2:
assert(count2 == 2); assert(count2 == 2);
break; break;
case 3:
assert(count2 == 0);
break;
case 4: case 4:
assert(count2 == 2); assert(count2 == 2);
break; break;