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");
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);
163 printf("while processing <%s>", args);
164 if (valid_keys != NULL && *valid_keys != NULL) {
165 printf(" using valid_keys=<%s", *valid_keys);
166 while (*(++valid_keys) != NULL)
167 printf(",%s", *valid_keys);
174 /* test several error cases */
175 static int test_invalid_kvargs(void)
177 struct rte_kvargs *kvlist;
178 /* list of argument that should fail */
179 const char *args_list[] = {
180 "wrong-key=x", /* key not in valid_keys_list */
181 "foo=1,foo=", /* empty value */
182 "foo=1,,foo=2", /* empty key/value */
183 "foo=1,foo", /* no value */
184 "foo=1,=2", /* no key */
185 "foo=[1,2", /* no closing bracket in value */
186 ",=", /* also test with a smiley */
189 const char *valid_keys_list[] = { "foo", "check", NULL };
190 const char **valid_keys = valid_keys_list;
192 for (args = args_list; *args != NULL; args++) {
194 kvlist = rte_kvargs_parse(*args, valid_keys);
195 if (kvlist != NULL) {
196 printf("rte_kvargs_parse() returned 0 (but should not)\n");
197 rte_kvargs_free(kvlist);
204 printf("while processing <%s>", *args);
205 if (valid_keys != NULL && *valid_keys != NULL) {
206 printf(" using valid_keys=<%s", *valid_keys);
207 while (*(++valid_keys) != NULL)
208 printf(",%s", *valid_keys);
218 printf("== test valid case ==\n");
219 if (test_valid_kvargs() < 0)
221 printf("== test invalid case ==\n");
222 if (test_invalid_kvargs() < 0)
227 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);