libubox
C utility functions for OpenWrt.
test-blobmsg-parse.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <libgen.h>
5 
6 #include "blobmsg.h"
7 
8 enum {
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 
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 }
int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
Definition: blobmsg.c:140
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
Definition: blobmsg.c:166
@ BLOBMSG_TYPE_TABLE
Definition: blobmsg.h:28
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27
Definition: blob.h:52
const char * name
Definition: blobmsg.h:47
int main(int argc, char *argv[])
static void dump_result(const char *fn, int r, const char *filename, struct blob_attr **tb)
static void test_blobmsg(const char *filename)
#define BUF_LEN
static const struct blobmsg_policy foo_policy[]
@ FOO_TESTDATA
@ __FOO_MAX
@ FOO_MESSAGE
@ FOO_LIST
#define ARRAY_SIZE(x)
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($
Definition: CMakeLists.txt:1
int fd
Definition: udebug-priv.h:27