1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2014 6WIND S.A.
9 #include <rte_common.h>
10 #include <rte_kvargs.h>
14 /* incrementd in handler, to check it is properly called once per
15 * key/value association */
16 static unsigned count;
18 /* this handler increment the "count" variable at each call and check
19 * that the key is "check" and the value is "value%d" */
20 static int check_handler(const char *key, const char *value,
21 __rte_unused void *opaque)
25 /* we check that the value is "check" */
26 if (strcmp(key, "check"))
29 /* we check that the value is "value$(count)" */
30 snprintf(buf, sizeof(buf), "value%d", count);
31 if (strncmp(buf, value, sizeof(buf)))
39 static int test_kvargs_parsing(const char *args, unsigned int n)
41 struct rte_kvargs *kvlist;
43 kvlist = rte_kvargs_parse(args, NULL);
45 printf("rte_kvargs_parse() error: %s\n", args);
48 if (kvlist->count != n) {
49 printf("invalid count value %d: %s\n", kvlist->count, args);
50 rte_kvargs_free(kvlist);
53 rte_kvargs_free(kvlist);
57 /* test a valid case */
58 static int test_valid_kvargs(void)
60 struct rte_kvargs *kvlist;
62 const char *valid_keys_list[] = { "foo", "check", NULL };
63 const char **valid_keys;
65 unsigned int expected;
78 /* empty args is valid */
81 kvlist = rte_kvargs_parse(args, valid_keys);
83 printf("rte_kvargs_parse() error");
86 rte_kvargs_free(kvlist);
88 /* first test without valid_keys */
89 args = "foo=1234,check=value0,check=value1";
91 kvlist = rte_kvargs_parse(args, valid_keys);
93 printf("rte_kvargs_parse() error");
96 /* call check_handler() for all entries with key="check" */
98 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
99 printf("rte_kvargs_process() error\n");
100 rte_kvargs_free(kvlist);
104 printf("invalid count value %d after rte_kvargs_process(check)\n",
106 rte_kvargs_free(kvlist);
110 /* call check_handler() for all entries with key="unexistant_key" */
111 if (rte_kvargs_process(kvlist, "unexistant_key", check_handler, NULL) < 0) {
112 printf("rte_kvargs_process() error\n");
113 rte_kvargs_free(kvlist);
117 printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
119 rte_kvargs_free(kvlist);
122 /* count all entries with key="foo" */
123 count = rte_kvargs_count(kvlist, "foo");
125 printf("invalid count value %d after rte_kvargs_count(foo)\n",
127 rte_kvargs_free(kvlist);
130 /* count all entries */
131 count = rte_kvargs_count(kvlist, NULL);
133 printf("invalid count value %d after rte_kvargs_count(NULL)\n",
135 rte_kvargs_free(kvlist);
138 /* count all entries with key="unexistant_key" */
139 count = rte_kvargs_count(kvlist, "unexistant_key");
141 printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
143 rte_kvargs_free(kvlist);
146 rte_kvargs_free(kvlist);
148 /* second test using valid_keys */
149 args = "foo=droids,check=value0,check=value1,check=wrong_value";
150 valid_keys = valid_keys_list;
151 kvlist = rte_kvargs_parse(args, valid_keys);
152 if (kvlist == NULL) {
153 printf("rte_kvargs_parse() error");
156 /* call check_handler() on all entries with key="check", it
157 * should fail as the value is not recognized by the handler */
158 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) == 0) {
159 printf("rte_kvargs_process() is success bu should not\n");
160 rte_kvargs_free(kvlist);
163 count = rte_kvargs_count(kvlist, "check");
165 printf("invalid count value %d after rte_kvargs_count(check)\n",
167 rte_kvargs_free(kvlist);
170 rte_kvargs_free(kvlist);
172 /* third test using list as value */
173 args = "foo=[0,1],check=value2";
174 valid_keys = valid_keys_list;
175 kvlist = rte_kvargs_parse(args, valid_keys);
176 if (kvlist == NULL) {
177 printf("rte_kvargs_parse() error\n");
180 if (strcmp(kvlist->pairs[0].value, "[0,1]") != 0) {
181 printf("wrong value %s", kvlist->pairs[0].value);
184 count = kvlist->count;
186 printf("invalid count value %d\n", count);
187 rte_kvargs_free(kvlist);
190 rte_kvargs_free(kvlist);
192 /* test using empty string (it is valid) */
194 kvlist = rte_kvargs_parse(args, NULL);
195 if (kvlist == NULL) {
196 printf("rte_kvargs_parse() error\n");
199 if (rte_kvargs_count(kvlist, NULL) != 0) {
200 printf("invalid count value\n");
203 rte_kvargs_free(kvlist);
205 /* test using empty elements (it is valid) */
206 args = "foo=1,,check=value2,,";
207 kvlist = rte_kvargs_parse(args, NULL);
208 if (kvlist == NULL) {
209 printf("rte_kvargs_parse() error\n");
212 if (rte_kvargs_count(kvlist, NULL) != 2) {
213 printf("invalid count value\n");
216 if (rte_kvargs_count(kvlist, "foo") != 1) {
217 printf("invalid count value for 'foo'\n");
220 if (rte_kvargs_count(kvlist, "check") != 1) {
221 printf("invalid count value for 'check'\n");
224 rte_kvargs_free(kvlist);
228 for (i = 0; i < RTE_DIM(valid_inputs); ++i) {
229 args = valid_inputs[i].input;
230 if (test_kvargs_parsing(args, valid_inputs[i].expected))
237 printf("while processing <%s>", args);
238 if (valid_keys != NULL && *valid_keys != NULL) {
239 printf(" using valid_keys=<%s", *valid_keys);
240 while (*(++valid_keys) != NULL)
241 printf(",%s", *valid_keys);
248 /* test several error cases */
249 static int test_invalid_kvargs(void)
251 struct rte_kvargs *kvlist;
252 /* list of argument that should fail */
253 const char *args_list[] = {
254 "wrong-key=x", /* key not in valid_keys_list */
257 const char *valid_keys_list[] = { "foo", "check", NULL };
258 const char **valid_keys = valid_keys_list;
260 for (args = args_list; *args != NULL; args++) {
262 kvlist = rte_kvargs_parse(*args, valid_keys);
263 if (kvlist != NULL) {
264 printf("rte_kvargs_parse() returned 0 (but should not)\n");
265 rte_kvargs_free(kvlist);
272 printf("while processing <%s>", *args);
273 if (valid_keys != NULL && *valid_keys != NULL) {
274 printf(" using valid_keys=<%s", *valid_keys);
275 while (*(++valid_keys) != NULL)
276 printf(",%s", *valid_keys);
286 printf("== test valid case ==\n");
287 if (test_valid_kvargs() < 0)
289 printf("== test invalid case ==\n");
290 if (test_invalid_kvargs() < 0)
295 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);