ca710fd5866348e98fe084242151afe7f7fa089d
[project/libubox.git] / tests / test-blobmsg-parse.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <libgen.h>
5
6 #include "blobmsg.h"
7
8 enum {
9 FOO_MESSAGE,
10 FOO_LIST,
11 FOO_TESTDATA,
12 __FOO_MAX
13 };
14
15 static const struct blobmsg_policy foo_policy[] = {
16 [FOO_MESSAGE] = {
17 .name = "message",
18 .type = BLOBMSG_TYPE_STRING,
19 },
20 [FOO_LIST] = {
21 .name = "list",
22 .type = BLOBMSG_TYPE_ARRAY,
23 },
24 [FOO_TESTDATA] = {
25 .name = "testdata",
26 .type = BLOBMSG_TYPE_TABLE,
27 },
28 };
29
30 static void dump_result(const char *fn, int r, const char *filename, struct blob_attr **tb)
31 {
32 fprintf(stdout, "%s: %s: %c%c%c (%d)\n", basename((char *) filename), fn,
33 tb[FOO_MESSAGE] ? 'M' : '.',
34 tb[FOO_LIST] ? 'L' : '.',
35 tb[FOO_TESTDATA] ? 'T' : '.',
36 r);
37 }
38
39 static void test_blobmsg(const char *filename)
40 {
41 #define BUF_LEN 256
42 int r = 0;
43 FILE *fd = NULL;
44 size_t len = 0;
45 char buf[BUF_LEN+1] = { 0 };
46 struct blob_attr *tb[__FOO_MAX];
47
48 fd = fopen(filename, "r");
49 if (!fd) {
50 fprintf(stderr, "unable to open %s", filename);
51 return;
52 }
53
54 len = fread(&buf, 1, BUF_LEN, fd);
55 fclose(fd);
56
57 r = blobmsg_parse(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
58 dump_result("blobmsg_parse", r, filename, tb);
59
60 r = blobmsg_parse_array(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
61 dump_result("blobmsg_parse_array", r, filename, tb);
62 }
63
64 int main(int argc, char *argv[])
65 {
66 if (argc != 2) {
67 fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
68 return 3;
69 }
70
71 test_blobmsg(argv[1]);
72
73 return 0;
74 }