add cram based unit tests
[project/libubox.git] / tests / test-blobmsg.c
1 #include <stdio.h>
2 #include <inttypes.h>
3
4 #include "blobmsg.h"
5 #include "blobmsg_json.h"
6
7 static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
8
9 #define indent_printf(indent, ...) do { \
10 if (indent > 0) \
11 fwrite(indent_str, indent, 1, stderr); \
12 fprintf(stderr, __VA_ARGS__); \
13 } while(0)
14
15 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
16
17 static void
18 dump_table(struct blob_attr *head, size_t len, int indent, bool array)
19 {
20 struct blob_attr *attr;
21 struct blobmsg_hdr *hdr;
22
23 indent_printf(indent, "{\n");
24 __blob_for_each_attr(attr, head, len) {
25 hdr = blob_data(attr);
26 if (!array)
27 indent_printf(indent + 1, "%s : ", hdr->name);
28 dump_attr_data(attr, 0, indent + 1);
29 }
30 indent_printf(indent, "}\n");
31 }
32
33 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
34 {
35 int type = blobmsg_type(data);
36 switch(type) {
37 case BLOBMSG_TYPE_STRING:
38 indent_printf(indent, "%s\n", blobmsg_get_string(data));
39 break;
40 case BLOBMSG_TYPE_INT8:
41 indent_printf(indent, "%d\n", blobmsg_get_u8(data));
42 break;
43 case BLOBMSG_TYPE_INT16:
44 indent_printf(indent, "%d\n", blobmsg_get_u16(data));
45 break;
46 case BLOBMSG_TYPE_INT32:
47 indent_printf(indent, "%d\n", blobmsg_get_u32(data));
48 break;
49 case BLOBMSG_TYPE_INT64:
50 indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data));
51 break;
52 case BLOBMSG_TYPE_DOUBLE:
53 indent_printf(indent, "%lf\n", blobmsg_get_double(data));
54 break;
55 case BLOBMSG_TYPE_TABLE:
56 case BLOBMSG_TYPE_ARRAY:
57 if (!indent)
58 indent_printf(indent, "\n");
59 dump_table(blobmsg_data(data), blobmsg_data_len(data),
60 next_indent, type == BLOBMSG_TYPE_ARRAY);
61 break;
62 }
63 }
64
65 enum {
66 FOO_MESSAGE,
67 FOO_LIST,
68 FOO_TESTDATA
69 };
70
71 static const struct blobmsg_policy pol[] = {
72 [FOO_MESSAGE] = {
73 .name = "message",
74 .type = BLOBMSG_TYPE_STRING,
75 },
76 [FOO_LIST] = {
77 .name = "list",
78 .type = BLOBMSG_TYPE_ARRAY,
79 },
80 [FOO_TESTDATA] = {
81 .name = "testdata",
82 .type = BLOBMSG_TYPE_TABLE,
83 },
84 };
85
86 #ifndef ARRAY_SIZE
87 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
88 #endif
89
90 static void dump_message(struct blob_buf *buf)
91 {
92 struct blob_attr *tb[ARRAY_SIZE(pol)];
93
94 if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
95 fprintf(stderr, "Parse failed\n");
96 return;
97 }
98 if (tb[FOO_MESSAGE])
99 fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
100
101 if (tb[FOO_LIST]) {
102 fprintf(stderr, "List: ");
103 dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true);
104 }
105 if (tb[FOO_TESTDATA]) {
106 fprintf(stderr, "Testdata: ");
107 dump_table(blobmsg_data(tb[FOO_TESTDATA]), blobmsg_data_len(tb[FOO_TESTDATA]), 0, false);
108 }
109 }
110
111 static void
112 fill_message(struct blob_buf *buf)
113 {
114 void *tbl;
115
116 blobmsg_add_string(buf, "message", "Hello, world!");
117
118 tbl = blobmsg_open_table(buf, "testdata");
119 blobmsg_add_double(buf, "double", 1.337e2);
120 blobmsg_add_u32(buf, "hello", 1);
121 blobmsg_add_string(buf, "world", "2");
122 blobmsg_close_table(buf, tbl);
123
124 tbl = blobmsg_open_array(buf, "list");
125 blobmsg_add_u32(buf, NULL, 0);
126 blobmsg_add_u32(buf, NULL, 1);
127 blobmsg_add_u32(buf, NULL, 2);
128 blobmsg_add_double(buf, "double", 1.337e2);
129 blobmsg_close_table(buf, tbl);
130 }
131
132 int main(int argc, char **argv)
133 {
134 char *json = NULL;
135 static struct blob_buf buf;
136
137 blobmsg_buf_init(&buf);
138 fill_message(&buf);
139 dump_message(&buf);
140
141 json = blobmsg_format_json(buf.head, true);
142 if (!json)
143 exit(EXIT_FAILURE);
144
145 fprintf(stderr, "json: %s\n", json);
146
147 if (buf.buf)
148 free(buf.buf);
149 free(json);
150
151 return 0;
152 }