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");
81 rte_kvargs_free(kvlist);
84 rte_kvargs_free(kvlist);
86 /* first test without valid_keys */
87 args = "foo=1234,check=value0,check=value1";
89 kvlist = rte_kvargs_parse(args, valid_keys);
91 printf("rte_kvargs_parse() error");
92 rte_kvargs_free(kvlist);
95 /* call check_handler() for all entries with key="check" */
97 if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
98 printf("rte_kvargs_process() error\n");
99 rte_kvargs_free(kvlist);
103 printf("invalid count value %d after rte_kvargs_process(check)\n",
105 rte_kvargs_free(kvlist);
109 /* call check_handler() for all entries with key="unexistant_key" */
110 if (rte_kvargs_process(kvlist, "unexistant_key", check_handler, NULL) < 0) {
111 printf("rte_kvargs_process() error\n");
112 rte_kvargs_free(kvlist);
116 printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
118 rte_kvargs_free(kvlist);
121 /* count all entries with key="foo" */
122 count = rte_kvargs_count(kvlist, "foo");
124 printf("invalid count value %d after rte_kvargs_count(foo)\n",
126 rte_kvargs_free(kvlist);
129 /* count all entries */
130 count = rte_kvargs_count(kvlist, NULL);
132 printf("invalid count value %d after rte_kvargs_count(NULL)\n",
134 rte_kvargs_free(kvlist);
137 /* count all entries with key="unexistant_key" */
138 count = rte_kvargs_count(kvlist, "unexistant_key");
140 printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
142 rte_kvargs_free(kvlist);
145 rte_kvargs_free(kvlist);
147 /* second test using valid_keys */
148 args = "foo=droids,check=value0,check=value1,check=wrong_value";
149 valid_keys = valid_keys_list;
150 kvlist = rte_kvargs_parse(args, valid_keys);
151 if (kvlist == NULL) {
152 printf("rte_kvargs_parse() error");
153 rte_kvargs_free(kvlist);
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);
175 printf("while processing <%s>", args);
176 if (valid_keys != NULL && *valid_keys != NULL) {
177 printf(" using valid_keys=<%s", *valid_keys);
178 while (*(++valid_keys) != NULL)
179 printf(",%s", *valid_keys);
186 /* test several error cases */
187 static int test_invalid_kvargs(void)
189 struct rte_kvargs *kvlist;
190 /* list of argument that should fail */
191 const char *args_list[] = {
192 "wrong-key=x", /* key not in valid_keys_list */
193 "foo=1,foo=", /* empty value */
194 "foo=1,,foo=2", /* empty key/value */
195 "foo=1,foo", /* no value */
196 "foo=1,=2", /* no key */
197 ",=", /* also test with a smiley */
200 const char *valid_keys_list[] = { "foo", "check", NULL };
201 const char **valid_keys = valid_keys_list;
203 for (args = args_list; *args != NULL; args++) {
205 kvlist = rte_kvargs_parse(*args, valid_keys);
206 if (kvlist != NULL) {
207 printf("rte_kvargs_parse() returned 0 (but should not)\n");
208 rte_kvargs_free(kvlist);
215 printf("while processing <%s>", *args);
216 if (valid_keys != NULL && *valid_keys != NULL) {
217 printf(" using valid_keys=<%s", *valid_keys);
218 while (*(++valid_keys) != NULL)
219 printf(",%s", *valid_keys);
229 printf("== test valid case ==\n");
230 if (test_valid_kvargs() < 0)
232 printf("== test invalid case ==\n");
233 if (test_invalid_kvargs() < 0)
238 static struct test_command kvargs_cmd = {
239 .command = "kvargs_autotest",
240 .callback = test_kvargs,
242 REGISTER_TEST_COMMAND(kvargs_cmd);