diff mbox series

[OpenWrt-Devel,libubox,2/4] tests: blobmsg/json: add more test cases

Message ID 20200112225515.11654-3-ynezz@true.cz
State Accepted
Delegated to: Petr Štetiar
Headers show
Series more tests and fixes | expand

Commit Message

Petr Štetiar Jan. 12, 2020, 10:55 p.m. UTC
* 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>
---
 tests/cram/test_blobmsg.t | 255 +++++++++++++++++++++++++++++++++++---
 tests/test-blobmsg.c      |  47 +++++--
 2 files changed, 275 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t
index 3a5801a5a0f4..9ab2acf6e2d8 100644
--- a/tests/cram/test_blobmsg.t
+++ b/tests/cram/test_blobmsg.t
@@ -1,32 +1,255 @@ 
 check that blobmsg is producing expected results:
 
   $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH"
+
   $ valgrind --quiet --leak-check=full test-blobmsg
+  [*] 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
-  1
-  2
-  133.700000
+  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 (esc)
-  \thello : 1 (esc)
-  \tworld : 2 (esc)
+  \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
+  [*] 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
-  1
-  2
-  133.700000
+  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 (esc)
-  \thello : 1 (esc)
-  \tworld : 2 (esc)
+  \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]}
diff --git a/tests/test-blobmsg.c b/tests/test-blobmsg.c
index 5e99dc2a42a6..58b0bc4eb574 100644
--- a/tests/test-blobmsg.c
+++ b/tests/test-blobmsg.c
@@ -1,4 +1,5 @@ 
 #include <stdio.h>
+#include <stdint.h>
 #include <inttypes.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);
 	switch(type) {
 	case BLOBMSG_TYPE_STRING:
-		indent_printf(indent, "%s\n", blobmsg_get_string(data));
+		indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
 		break;
 	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;
 	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;
 	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;
 	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;
 	case BLOBMSG_TYPE_DOUBLE:
-		indent_printf(indent, "%lf\n", blobmsg_get_double(data));
+		indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
 		break;
 	case BLOBMSG_TYPE_TABLE:
 	case BLOBMSG_TYPE_ARRAY:
@@ -117,14 +118,30 @@  fill_message(struct blob_buf *buf)
 
 	tbl = blobmsg_open_table(buf, "testdata");
 	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_close_table(buf, tbl);
 
 	tbl = blobmsg_open_array(buf, "list");
-	blobmsg_add_u32(buf, NULL, 0);
-	blobmsg_add_u32(buf, NULL, 1);
-	blobmsg_add_u32(buf, NULL, 2);
+	blobmsg_add_u8(buf, NULL, 0);
+	blobmsg_add_u8(buf, NULL, 100);
+	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_close_table(buf, tbl);
 }
@@ -136,13 +153,21 @@  int main(int argc, char **argv)
 
 	blobmsg_buf_init(&buf);
 	fill_message(&buf);
+	fprintf(stderr, "[*] blobmsg dump:\n");
 	dump_message(&buf);
 
 	json = blobmsg_format_json(buf.head, true);
 	if (!json)
 		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)
 		free(buf.buf);