libubox
C utility functions for OpenWrt.
test-blobmsg_check_array.c File Reference
#include <stdio.h>
#include "blobmsg.h"

Go to the source code of this file.

Macros

#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
 

Enumerations

enum  { ARRAY_A = 0 , ARRAY_B = 0 }
 

Functions

static int check_table_a_entries (struct blob_attr *attr)
 
static int check_message (struct blob_buf *buf)
 
static void fill_message (struct blob_buf *const buf)
 
int main (int argc, char **argv)
 

Variables

static char const array_a [] = "array_a"
 
static char const array_b [] = "array_b"
 
static const struct blobmsg_policy pol_a []
 
static const struct blobmsg_policy pol_b []
 

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   x)    (sizeof(x) / sizeof((x)[0]))

Definition at line 44 of file test-blobmsg_check_array.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ARRAY_A 
ARRAY_B 

Definition at line 21 of file test-blobmsg_check_array.c.

21  {
22  ARRAY_A = 0,
23  ARRAY_B = 0,
24 };

Function Documentation

◆ check_message()

static int check_message ( struct blob_buf buf)
static

Definition at line 110 of file test-blobmsg_check_array.c.

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 }
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
@ BLOBMSG_TYPE_TABLE
Definition: blobmsg.h:28
Definition: blob.h:52
struct blob_attr * head
Definition: blob.h:65
static char const array_a[]
static int check_table_a_entries(struct blob_attr *attr)
#define ARRAY_SIZE(x)
static const struct blobmsg_policy pol_a[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_table_a_entries()

static int check_table_a_entries ( struct blob_attr attr)
static

Definition at line 48 of file test-blobmsg_check_array.c.

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 }
static char * blobmsg_get_string(struct blob_attr *attr)
Definition: blobmsg.h:348
#define blobmsg_for_each_attr(pos, attr, rem)
Definition: blobmsg.h:364
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
static const struct blobmsg_policy pol_b[]
static char const array_b[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_message()

static void fill_message ( struct blob_buf *const  buf)
static

Definition at line 137 of file test-blobmsg_check_array.c.

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 }
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 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
static void * blobmsg_open_array(struct blob_buf *buf, const char *name)
Definition: blobmsg.h:250
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 153 of file test-blobmsg_check_array.c.

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 int blobmsg_buf_init(struct blob_buf *buf)
Definition: blobmsg.h:273
Definition: blob.h:64
void * buf
Definition: blob.h:68
static void fill_message(struct blob_buf *const buf)
static int check_message(struct blob_buf *buf)
Here is the call graph for this function:

Variable Documentation

◆ array_a

char const array_a[] = "array_a"
static

Definition at line 26 of file test-blobmsg_check_array.c.

◆ array_b

char const array_b[] = "array_b"
static

Definition at line 27 of file test-blobmsg_check_array.c.

◆ pol_a

const struct blobmsg_policy pol_a[]
static
Initial value:
= {
[ARRAY_A] = {
.name = array_a,
}
}
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27

Definition at line 27 of file test-blobmsg_check_array.c.

◆ pol_b

const struct blobmsg_policy pol_b[]
static
Initial value:
= {
[ARRAY_B] = {
.name = array_b,
}
}

Definition at line 27 of file test-blobmsg_check_array.c.