blob: make blob_parse_untrusted more permissive
[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 size_t len = 0;
44 FILE *fd = NULL;
45 char *buf = NULL;
46 struct blob_attr *tb[__FOO_MAX];
47
48 fd = fopen(filename, "r");
49 if (!fd) {
50 fprintf(stderr, "unable to open %s\n", filename);
51 return;
52 }
53
54 buf = malloc(BUF_LEN+1);
55 if (!buf)
56 return;
57
58 len = fread(buf, 1, BUF_LEN, fd);
59 fclose(fd);
60
61 r = blobmsg_parse(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
62 dump_result("blobmsg_parse", r, filename, tb);
63
64 r = blobmsg_parse_array(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
65 dump_result("blobmsg_parse_array", r, filename, tb);
66
67 free(buf);
68 }
69
70 int main(int argc, char *argv[])
71 {
72 if (argc != 2) {
73 fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
74 return 3;
75 }
76
77 test_blobmsg(argv[1]);
78
79 return 0;
80 }