tests: blobmsg/json: add more test cases

* add missing test with sanitizers
 * add test case for blobmsg_add_json_from_string
 * add test cases for all numeric types
 * print types for each variable

Signed-off-by: Petr Štetiar <ynezz@true.cz>
This commit is contained in:
Petr Štetiar 2020-01-12 15:31:49 +01:00
parent 379cd33d19
commit 20a070f081
2 changed files with 275 additions and 27 deletions

View file

@ -1,32 +1,255 @@
check that blobmsg is producing expected results: check that blobmsg is producing expected results:
$ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH"
$ valgrind --quiet --leak-check=full test-blobmsg $ valgrind --quiet --leak-check=full test-blobmsg
[*] blobmsg dump:
Message: Hello, world! Message: Hello, world!
List: { List: {
0 0 (i8)
1 100 (i8)
2 -128 (i8)
133.700000 127 (i8)
-32768 (i16)
32767 (i16)
-2147483648 (i32)
2147483647 (i32)
-9223372036854775808 (i64)
9223372036854775807 (i64)
133.700000 (dbl)
} }
Testdata: { Testdata: {
\tdouble : 133.700000 (esc) \tdouble : 133.700000 (dbl) (esc)
\thello : 1 (esc) \tfoo : 0 (i8) (esc)
\tworld : 2 (esc) \tpoo : 100 (i8) (esc)
\tmoo-min : -128 (i8) (esc)
\tmoo-max : 127 (i8) (esc)
\tbar-min : -32768 (i16) (esc)
\tbar-max : 32767 (i16) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
32768 (i32)
32767 (i32)
-2147483648 (i32)
2147483647 (i32)
-2147483648 (i32)
2147483647 (i32)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tbar-min : 32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -2147483648 (i32) (esc)
\ttaz-max : 2147483647 (i32) (esc)
\tworld : 2 (str) (esc)
} }
json: {"message":"Hello, world!","testdata":{"double":133.700000,"hello":1,"world":"2"},"list":[0,1,2,133.700000]}
$ test-blobmsg-san $ test-blobmsg-san
[*] blobmsg dump:
Message: Hello, world! Message: Hello, world!
List: { List: {
0 0 (i8)
1 100 (i8)
2 -128 (i8)
133.700000 127 (i8)
-32768 (i16)
32767 (i16)
-2147483648 (i32)
2147483647 (i32)
-9223372036854775808 (i64)
9223372036854775807 (i64)
133.700000 (dbl)
} }
Testdata: { Testdata: {
\tdouble : 133.700000 (esc) \tdouble : 133.700000 (dbl) (esc)
\thello : 1 (esc) \tfoo : 0 (i8) (esc)
\tworld : 2 (esc) \tpoo : 100 (i8) (esc)
\tmoo-min : -128 (i8) (esc)
\tmoo-max : 127 (i8) (esc)
\tbar-min : -32768 (i16) (esc)
\tbar-max : 32767 (i16) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
32768 (i32)
32767 (i32)
-2147483648 (i32)
2147483647 (i32)
-2147483648 (i32)
2147483647 (i32)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tbar-min : 32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -2147483648 (i32) (esc)
\ttaz-max : 2147483647 (i32) (esc)
\tworld : 2 (str) (esc)
}
$ test-blobmsg-san
[*] blobmsg dump:
Message: Hello, world!
List: {
0 (i8)
100 (i8)
-128 (i8)
127 (i8)
-32768 (i16)
32767 (i16)
-2147483648 (i32)
2147483647 (i32)
-9223372036854775808 (i64)
9223372036854775807 (i64)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 100 (i8) (esc)
\tmoo-min : -128 (i8) (esc)
\tmoo-max : 127 (i8) (esc)
\tbar-min : -32768 (i16) (esc)
\tbar-max : 32767 (i16) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
32768 (i32)
32767 (i32)
-2147483648 (i32)
2147483647 (i32)
-2147483648 (i32)
2147483647 (i32)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tbar-min : 32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -2147483648 (i32) (esc)
\ttaz-max : 2147483647 (i32) (esc)
\tworld : 2 (str) (esc)
}
$ test-blobmsg-san
[*] blobmsg dump:
Message: Hello, world!
List: {
0 (i8)
100 (i8)
-128 (i8)
127 (i8)
-32768 (i16)
32767 (i16)
-2147483648 (i32)
2147483647 (i32)
-9223372036854775808 (i64)
9223372036854775807 (i64)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 100 (i8) (esc)
\tmoo-min : -128 (i8) (esc)
\tmoo-max : 127 (i8) (esc)
\tbar-min : -32768 (i16) (esc)
\tbar-max : 32767 (i16) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
32768 (i32)
32767 (i32)
-2147483648 (i32)
2147483647 (i32)
-2147483648 (i32)
2147483647 (i32)
133.700000 (dbl)
}
Testdata: {
\tdouble : 133.700000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tbar-min : 32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -2147483648 (i32) (esc)
\ttaz-max : 2147483647 (i32) (esc)
\tworld : 2 (str) (esc)
} }
json: {"message":"Hello, world!","testdata":{"double":133.700000,"hello":1,"world":"2"},"list":[0,1,2,133.700000]}

View file

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#include "blobmsg.h" #include "blobmsg.h"
@ -35,22 +36,22 @@ static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
int type = blobmsg_type(data); int type = blobmsg_type(data);
switch(type) { switch(type) {
case BLOBMSG_TYPE_STRING: case BLOBMSG_TYPE_STRING:
indent_printf(indent, "%s\n", blobmsg_get_string(data)); indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
break; break;
case BLOBMSG_TYPE_INT8: case BLOBMSG_TYPE_INT8:
indent_printf(indent, "%d\n", blobmsg_get_u8(data)); indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data));
break; break;
case BLOBMSG_TYPE_INT16: case BLOBMSG_TYPE_INT16:
indent_printf(indent, "%d\n", blobmsg_get_u16(data)); indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data));
break; break;
case BLOBMSG_TYPE_INT32: case BLOBMSG_TYPE_INT32:
indent_printf(indent, "%d\n", blobmsg_get_u32(data)); indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data));
break; break;
case BLOBMSG_TYPE_INT64: case BLOBMSG_TYPE_INT64:
indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data)); indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data));
break; break;
case BLOBMSG_TYPE_DOUBLE: case BLOBMSG_TYPE_DOUBLE:
indent_printf(indent, "%lf\n", blobmsg_get_double(data)); indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
break; break;
case BLOBMSG_TYPE_TABLE: case BLOBMSG_TYPE_TABLE:
case BLOBMSG_TYPE_ARRAY: case BLOBMSG_TYPE_ARRAY:
@ -117,14 +118,30 @@ fill_message(struct blob_buf *buf)
tbl = blobmsg_open_table(buf, "testdata"); tbl = blobmsg_open_table(buf, "testdata");
blobmsg_add_double(buf, "double", 1.337e2); blobmsg_add_double(buf, "double", 1.337e2);
blobmsg_add_u32(buf, "hello", 1); blobmsg_add_u8(buf, "foo", 0);
blobmsg_add_u8(buf, "poo", 100);
blobmsg_add_u8(buf, "moo-min", INT8_MIN);
blobmsg_add_u8(buf, "moo-max", INT8_MAX);
blobmsg_add_u16(buf, "bar-min", INT16_MIN);
blobmsg_add_u16(buf, "bar-max", INT16_MAX);
blobmsg_add_u32(buf, "baz-min", INT32_MIN);
blobmsg_add_u32(buf, "baz-max", INT32_MAX);
blobmsg_add_u64(buf, "taz-min", INT64_MIN);
blobmsg_add_u64(buf, "taz-max", INT64_MAX);
blobmsg_add_string(buf, "world", "2"); blobmsg_add_string(buf, "world", "2");
blobmsg_close_table(buf, tbl); blobmsg_close_table(buf, tbl);
tbl = blobmsg_open_array(buf, "list"); tbl = blobmsg_open_array(buf, "list");
blobmsg_add_u32(buf, NULL, 0); blobmsg_add_u8(buf, NULL, 0);
blobmsg_add_u32(buf, NULL, 1); blobmsg_add_u8(buf, NULL, 100);
blobmsg_add_u32(buf, NULL, 2); blobmsg_add_u8(buf, NULL, INT8_MIN);
blobmsg_add_u8(buf, NULL, INT8_MAX);
blobmsg_add_u16(buf, NULL, INT16_MIN);
blobmsg_add_u16(buf, NULL, INT16_MAX);
blobmsg_add_u32(buf, NULL, INT32_MIN);
blobmsg_add_u32(buf, NULL, INT32_MAX);
blobmsg_add_u64(buf, NULL, INT64_MIN);
blobmsg_add_u64(buf, NULL, INT64_MAX);
blobmsg_add_double(buf, "double", 1.337e2); blobmsg_add_double(buf, "double", 1.337e2);
blobmsg_close_table(buf, tbl); blobmsg_close_table(buf, tbl);
} }
@ -136,13 +153,21 @@ int main(int argc, char **argv)
blobmsg_buf_init(&buf); blobmsg_buf_init(&buf);
fill_message(&buf); fill_message(&buf);
fprintf(stderr, "[*] blobmsg dump:\n");
dump_message(&buf); dump_message(&buf);
json = blobmsg_format_json(buf.head, true); json = blobmsg_format_json(buf.head, true);
if (!json) if (!json)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
fprintf(stderr, "json: %s\n", json); fprintf(stderr, "\n[*] blobmsg to json: %s\n", json);
blobmsg_buf_init(&buf);
if (!blobmsg_add_json_from_string(&buf, json))
exit(EXIT_FAILURE);
fprintf(stderr, "\n[*] blobmsg from json:\n");
dump_message(&buf);
if (buf.buf) if (buf.buf)
free(buf.buf); free(buf.buf);