2 * Copyright 2014 6WIND S.A.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * - Neither the name of 6WIND S.A. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_common.h>
39 #include <rte_kvargs.h>
43 /* incrementd in handler, to check it is properly called once per
44 * key/value association */
45 static unsigned count;
47 /* this handler increment the "count" variable at each call and check
48 * that the key is "check" and the value is "value%d" */
49 static int check_handler(const char *key, const char *value,
50 __rte_unused void *opaque)
54 /* we check that the value is "check" */
55 if (strcmp(key, "check"))
58 /* we check that the value is "value$(count)" */
59 snprintf(buf, sizeof(buf), "value%d", count);
60 if (strncmp(buf, value, sizeof(buf)))
67 /* test a valid case */
68 static int test_valid_kvargs(void)
70 struct rte_kvargs *kvlist;
72 const char *valid_keys_list[] = { "foo", "check", NULL };
73 const char **valid_keys;
75 /* empty args is valid */
78 kvlist = rte_kvargs_parse(args, valid_keys);
80 printf("rte_kvargs_parse() error");
83 rte_kvargs_free(kvlist);
85 /* first test without valid_keys */
86 args = "foo=1234,check=value0,check=value1";
88 kvlist = rte_kvargs_parse(args, valid_keys);
90 printf("rte_kvargs_parse() error");
93 /* call check_handler() for all entries with key="check" */
95 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
96 printf("rte_kvargs_process() error\n");
97 rte_kvargs_free(kvlist);
101 printf("invalid count value %d after rte_kvargs_process(check)\n",
103 rte_kvargs_free(kvlist);
107 /* call check_handler() for all entries with key="unexistant_key" */
108 if (rte_kvargs_process(kvlist, "unexistant_key", check_handler, NULL) < 0) {
109 printf("rte_kvargs_process() error\n");
110 rte_kvargs_free(kvlist);
114 printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
116 rte_kvargs_free(kvlist);
119 /* count all entries with key="foo" */
120 count = rte_kvargs_count(kvlist, "foo");
122 printf("invalid count value %d after rte_kvargs_count(foo)\n",
124 rte_kvargs_free(kvlist);
127 /* count all entries */
128 count = rte_kvargs_count(kvlist, NULL);
130 printf("invalid count value %d after rte_kvargs_count(NULL)\n",
132 rte_kvargs_free(kvlist);
135 /* count all entries with key="unexistant_key" */
136 count = rte_kvargs_count(kvlist, "unexistant_key");
138 printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
140 rte_kvargs_free(kvlist);
143 rte_kvargs_free(kvlist);
145 /* second test using valid_keys */
146 args = "foo=droids,check=value0,check=value1,check=wrong_value";
147 valid_keys = valid_keys_list;
148 kvlist = rte_kvargs_parse(args, valid_keys);
149 if (kvlist == NULL) {
150 printf("rte_kvargs_parse() error");
153 /* call check_handler() on all entries with key="check", it
154 * should fail as the value is not recognized by the handler */
155 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) == 0) {
156 printf("rte_kvargs_process() is success bu should not\n");
157 rte_kvargs_free(kvlist);
160 count = rte_kvargs_count(kvlist, "check");
162 printf("invalid count value %d after rte_kvargs_count(check)\n",
164 rte_kvargs_free(kvlist);
167 rte_kvargs_free(kvlist);
172 printf("while processing <%s>", args);
173 if (valid_keys != NULL && *valid_keys != NULL) {
174 printf(" using valid_keys=<%s", *valid_keys);
175 while (*(++valid_keys) != NULL)
176 printf(",%s", *valid_keys);
183 /* test several error cases */
184 static int test_invalid_kvargs(void)
186 struct rte_kvargs *kvlist;
187 /* list of argument that should fail */
188 const char *args_list[] = {
189 "wrong-key=x", /* key not in valid_keys_list */
190 "foo=1,foo=", /* empty value */
191 "foo=1,,foo=2", /* empty key/value */
192 "foo=1,foo", /* no value */
193 "foo=1,=2", /* no key */
194 ",=", /* also test with a smiley */
197 const char *valid_keys_list[] = { "foo", "check", NULL };
198 const char **valid_keys = valid_keys_list;
200 for (args = args_list; *args != NULL; args++) {
202 kvlist = rte_kvargs_parse(*args, valid_keys);
203 if (kvlist != NULL) {
204 printf("rte_kvargs_parse() returned 0 (but should not)\n");
205 rte_kvargs_free(kvlist);
212 printf("while processing <%s>", *args);
213 if (valid_keys != NULL && *valid_keys != NULL) {
214 printf(" using valid_keys=<%s", *valid_keys);
215 while (*(++valid_keys) != NULL)
216 printf(",%s", *valid_keys);
226 printf("== test valid case ==\n");
227 if (test_valid_kvargs() < 0)
229 printf("== test invalid case ==\n");
230 if (test_invalid_kvargs() < 0)
235 static struct test_command kvargs_cmd = {
236 .command = "kvargs_autotest",
237 .callback = test_kvargs,
239 REGISTER_TEST_COMMAND(kvargs_cmd);