libubox
C utility functions for OpenWrt.
test-blobmsg_check_array.c
Go to the documentation of this file.
1 #include <stdio.h>
2 
3 #include "blobmsg.h"
4 
5 /*
6  * This test tests a blob of this form...
7  *
8  {
9  "array_a" : [
10  {
11  "array_b": [
12  "1"
13  ]
14  }
15  ]
16  }
17  *
18  */
19 
20 
21 enum {
22  ARRAY_A = 0,
23  ARRAY_B = 0,
24 };
25 
26 static char const array_a[] = "array_a";
27 static char const array_b[] = "array_b";
28 
29 static const struct blobmsg_policy pol_a[] = {
30  [ARRAY_A] = {
31  .name = array_a,
32  .type = BLOBMSG_TYPE_ARRAY
33  }
34 };
35 
36 static const struct blobmsg_policy pol_b[] = {
37  [ARRAY_B] = {
38  .name = array_b,
39  .type = BLOBMSG_TYPE_ARRAY
40  }
41 };
42 
43 #ifndef ARRAY_SIZE
44 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #endif
46 
47 static int
49 {
50  struct blob_attr *cur;
51  size_t rem;
52  int entry_number = 0;
53 
54  blobmsg_for_each_attr(cur, attr, rem) {
55  int failed = 0;
56 
57  fprintf(stderr, "Process %s: entry %d\n", array_a, entry_number);
58 
59  struct blob_attr *tb[ARRAY_SIZE(pol_b)];
60 
62  blobmsg_data(cur), blobmsg_data_len(cur)) != 0) {
63  fprintf(stderr, "Policy %s parse failed\n", array_b);
64  return -1;
65  }
66 
67  if (tb[ARRAY_B] == NULL) {
68  fprintf(stderr, "%s not found\n", array_b);
69  return -1;
70  }
71 
72  /*
73  * This is the test that fails when blobmsg_check_array() passes the
74  * length obtained by blob_len(attr).
75  * It succeeds when blobmsg_check_array() uses blob_len(attr), which is
76  * equivalent to the origianl code, pre the length check changes.
77  */
79  fprintf(stderr, "Failed blobmsg_check_array() (STRING) on %s\n",
80  array_b);
81  failed = 1;
82  }
83 
84  /*
85  * Continue outputting the strings even though the test above might
86  * have failed.
87  * This will show that the array does actually contain the expected
88  * string.
89  */
90 
91  struct blob_attr *cur2;
92  size_t rem2;
93 
94  blobmsg_for_each_attr(cur2, tb[ARRAY_B], rem2) {
95  fprintf(stderr, "%s contains string: %s\n",
97  }
98 
99 
100  entry_number++;
101 
102  if (failed)
103  return -1;
104  }
105 
106  return 0;
107 }
108 
109 static int
111 {
112  struct blob_attr *tb[ARRAY_SIZE(pol_a)];
113 
115  blob_data(buf->head), blobmsg_data_len(buf->head)) != 0) {
116  fprintf(stderr, "Policy %s parse failed\n", array_a);
117  return -1;
118  }
119 
120  if (tb[ARRAY_A] == NULL) {
121  fprintf(stderr, "%s not found\n", array_a);
122  return -1;
123  }
124 
125  int const result = blobmsg_check_array(tb[ARRAY_A], BLOBMSG_TYPE_TABLE);
126 
127  if (result < 0) {
128  fprintf(stderr, "Failed blobmsg_check_array() (TABLE) on %s (%d)\n",
129  array_a, result);
130  return -1;
131  }
132 
133  return check_table_a_entries(tb[ARRAY_A]);
134 }
135 
136 static void
137 fill_message(struct blob_buf * const buf)
138 {
139  void * const tbl_a = blobmsg_open_array(buf, "array_a");
140  void * const obj = blobmsg_open_table(buf, NULL);
141 
142  void * const tbl_b = blobmsg_open_array(buf, "array_b");
143 
144  blobmsg_add_string(buf, NULL, "1");
145 
146  blobmsg_close_array(buf, tbl_b);
147 
148  blobmsg_close_table(buf, obj);
149 
150  blobmsg_close_array(buf, tbl_a);
151 }
152 
153 int main(int argc, char **argv)
154 {
155  int result;
156  static struct blob_buf buf;
157 
159  fill_message(&buf);
160 
161  result = check_message(&buf);
162  if (result == 0)
163  fprintf(stderr, "blobmsg_check_array() test passed\n");
164 
165  if (buf.buf != NULL)
166  free(buf.buf);
167 
168  return result ? EXIT_FAILURE : EXIT_SUCCESS;
169 }
static void * blob_data(const struct blob_attr *attr)
Definition: blob.h:75
int blobmsg_check_array(const struct blob_attr *attr, int type)
Definition: blobmsg.c:87
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
Definition: blobmsg.c:166
static size_t blobmsg_data_len(const struct blob_attr *attr)
Definition: blobmsg.h:95
static void * blobmsg_open_table(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:256
static void blobmsg_close_table(struct blob_buf *buf, void *cookie)
Definition: blobmsg.h:268
static char * blobmsg_get_string(struct blob_attr *attr)
Definition: blobmsg.h:348
static int blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
Definition: blobmsg.h:235
static void blobmsg_close_array(struct blob_buf *buf, void *cookie)
Definition: blobmsg.h:262
#define blobmsg_for_each_attr(pos, attr, rem)
Definition: blobmsg.h:364
static int blobmsg_buf_init(struct blob_buf *buf)
Definition: blobmsg.h:273
@ BLOBMSG_TYPE_TABLE
Definition: blobmsg.h:28
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27
static void * blobmsg_open_array(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:250
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
Definition: blob.h:52
Definition: blob.h:64
void * buf
Definition: blob.h:68
struct blob_attr * head
Definition: blob.h:65
const char * name
Definition: blobmsg.h:47
static char const array_a[]
static void fill_message(struct blob_buf *const buf)
int main(int argc, char **argv)
static const struct blobmsg_policy pol_b[]
static int check_table_a_entries(struct blob_attr *attr)
#define ARRAY_SIZE(x)
static int check_message(struct blob_buf *buf)
static char const array_b[]
static const struct blobmsg_policy pol_a[]