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);
143 printf("while processing <%s>", args);
144 if (valid_keys != NULL && *valid_keys != NULL) {
145 printf(" using valid_keys=<%s", *valid_keys);
146 while (*(++valid_keys) != NULL)
147 printf(",%s", *valid_keys);
154 /* test several error cases */
155 static int test_invalid_kvargs(void)
157 struct rte_kvargs *kvlist;
158 /* list of argument that should fail */
159 const char *args_list[] = {
160 "wrong-key=x", /* key not in valid_keys_list */
161 "foo=1,foo=", /* empty value */
162 "foo=1,,foo=2", /* empty key/value */
163 "foo=1,foo", /* no value */
164 "foo=1,=2", /* no key */
165 ",=", /* also test with a smiley */
168 const char *valid_keys_list[] = { "foo", "check", NULL };
169 const char **valid_keys = valid_keys_list;
171 for (args = args_list; *args != NULL; args++) {
173 kvlist = rte_kvargs_parse(*args, valid_keys);
174 if (kvlist != NULL) {
175 printf("rte_kvargs_parse() returned 0 (but should not)\n");
176 rte_kvargs_free(kvlist);
183 printf("while processing <%s>", *args);
184 if (valid_keys != NULL && *valid_keys != NULL) {
185 printf(" using valid_keys=<%s", *valid_keys);
186 while (*(++valid_keys) != NULL)
187 printf(",%s", *valid_keys);
197 printf("== test valid case ==\n");
198 if (test_valid_kvargs() < 0)
200 printf("== test invalid case ==\n");
201 if (test_invalid_kvargs() < 0)
206 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);