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)))
38 /* test a valid case */
39 static int test_valid_kvargs(void)
41 struct rte_kvargs *kvlist;
43 const char *valid_keys_list[] = { "foo", "check", NULL };
44 const char **valid_keys;
46 /* empty args is valid */
49 kvlist = rte_kvargs_parse(args, valid_keys);
51 printf("rte_kvargs_parse() error");
54 rte_kvargs_free(kvlist);
56 /* first test without valid_keys */
57 args = "foo=1234,check=value0,check=value1";
59 kvlist = rte_kvargs_parse(args, valid_keys);
61 printf("rte_kvargs_parse() error");
64 /* call check_handler() for all entries with key="check" */
66 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
67 printf("rte_kvargs_process() error\n");
68 rte_kvargs_free(kvlist);
72 printf("invalid count value %d after rte_kvargs_process(check)\n",
74 rte_kvargs_free(kvlist);
78 /* call check_handler() for all entries with key="unexistant_key" */
79 if (rte_kvargs_process(kvlist, "unexistant_key", check_handler, NULL) < 0) {
80 printf("rte_kvargs_process() error\n");
81 rte_kvargs_free(kvlist);
85 printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
87 rte_kvargs_free(kvlist);
90 /* count all entries with key="foo" */
91 count = rte_kvargs_count(kvlist, "foo");
93 printf("invalid count value %d after rte_kvargs_count(foo)\n",
95 rte_kvargs_free(kvlist);
98 /* count all entries */
99 count = rte_kvargs_count(kvlist, NULL);
101 printf("invalid count value %d after rte_kvargs_count(NULL)\n",
103 rte_kvargs_free(kvlist);
106 /* count all entries with key="unexistant_key" */
107 count = rte_kvargs_count(kvlist, "unexistant_key");
109 printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
111 rte_kvargs_free(kvlist);
114 rte_kvargs_free(kvlist);
116 /* second test using valid_keys */
117 args = "foo=droids,check=value0,check=value1,check=wrong_value";
118 valid_keys = valid_keys_list;
119 kvlist = rte_kvargs_parse(args, valid_keys);
120 if (kvlist == NULL) {
121 printf("rte_kvargs_parse() error");
124 /* call check_handler() on all entries with key="check", it
125 * should fail as the value is not recognized by the handler */
126 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) == 0) {
127 printf("rte_kvargs_process() is success bu should not\n");
128 rte_kvargs_free(kvlist);
131 count = rte_kvargs_count(kvlist, "check");
133 printf("invalid count value %d after rte_kvargs_count(check)\n",
135 rte_kvargs_free(kvlist);
138 rte_kvargs_free(kvlist);
140 /* third test using list as value */
141 args = "foo=[0,1],check=value2";
142 valid_keys = valid_keys_list;
143 kvlist = rte_kvargs_parse(args, valid_keys);
144 if (kvlist == NULL) {
145 printf("rte_kvargs_parse() error\n");
148 if (strcmp(kvlist->pairs[0].value, "[0,1]") != 0) {
149 printf("wrong value %s", kvlist->pairs[0].value);
152 count = kvlist->count;
154 printf("invalid count value %d\n", count);
155 rte_kvargs_free(kvlist);
158 rte_kvargs_free(kvlist);
160 /* test using empty string (it is valid) */
162 kvlist = rte_kvargs_parse(args, NULL);
163 if (kvlist == NULL) {
164 printf("rte_kvargs_parse() error\n");
167 if (rte_kvargs_count(kvlist, NULL) != 0) {
168 printf("invalid count value\n");
171 rte_kvargs_free(kvlist);
173 /* test using empty elements (it is valid) */
174 args = "foo=1,,check=value2,,";
175 kvlist = rte_kvargs_parse(args, NULL);
176 if (kvlist == NULL) {
177 printf("rte_kvargs_parse() error\n");
180 if (rte_kvargs_count(kvlist, NULL) != 2) {
181 printf("invalid count value\n");
184 if (rte_kvargs_count(kvlist, "foo") != 1) {
185 printf("invalid count value for 'foo'\n");
188 if (rte_kvargs_count(kvlist, "check") != 1) {
189 printf("invalid count value for 'check'\n");
192 rte_kvargs_free(kvlist);
197 printf("while processing <%s>", args);
198 if (valid_keys != NULL && *valid_keys != NULL) {
199 printf(" using valid_keys=<%s", *valid_keys);
200 while (*(++valid_keys) != NULL)
201 printf(",%s", *valid_keys);
208 /* test several error cases */
209 static int test_invalid_kvargs(void)
211 struct rte_kvargs *kvlist;
212 /* list of argument that should fail */
213 const char *args_list[] = {
214 "wrong-key=x", /* key not in valid_keys_list */
215 "foo=1,foo=", /* empty value */
216 "foo=1,foo", /* no value */
217 "foo=1,=2", /* no key */
218 "foo=[1,2", /* no closing bracket in value */
219 ",=", /* also test with a smiley */
220 "foo=[", /* no value in list and no closing bracket */
223 const char *valid_keys_list[] = { "foo", "check", NULL };
224 const char **valid_keys = valid_keys_list;
226 for (args = args_list; *args != NULL; args++) {
228 kvlist = rte_kvargs_parse(*args, valid_keys);
229 if (kvlist != NULL) {
230 printf("rte_kvargs_parse() returned 0 (but should not)\n");
231 rte_kvargs_free(kvlist);
238 printf("while processing <%s>", *args);
239 if (valid_keys != NULL && *valid_keys != NULL) {
240 printf(" using valid_keys=<%s", *valid_keys);
241 while (*(++valid_keys) != NULL)
242 printf(",%s", *valid_keys);
252 printf("== test valid case ==\n");
253 if (test_valid_kvargs() < 0)
255 printf("== test invalid case ==\n");
256 if (test_invalid_kvargs() < 0)
261 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);