xref: /f-stack/dpdk/app/test/test_kvargs.c (revision 0c6bd470)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2014 6WIND S.A.
3  */
4 
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 
9 #include <rte_common.h>
10 #include <rte_kvargs.h>
11 
12 #include "test.h"
13 
14 /* incrementd in handler, to check it is properly called once per
15  * key/value association */
16 static unsigned count;
17 
18 /* this handler increment the "count" variable at each call and check
19  * that the key is "check" and the value is "value%d" */
check_handler(const char * key,const char * value,__rte_unused void * opaque)20 static int check_handler(const char *key, const char *value,
21 	__rte_unused void *opaque)
22 {
23 	char buf[16];
24 
25 	/* we check that the value is "check" */
26 	if (strcmp(key, "check"))
27 		return -1;
28 
29 	/* we check that the value is "value$(count)" */
30 	snprintf(buf, sizeof(buf), "value%d", count);
31 	if (strncmp(buf, value, sizeof(buf)))
32 		return -1;
33 
34 	count ++;
35 	return 0;
36 }
37 
38 /* test a valid case */
test_valid_kvargs(void)39 static int test_valid_kvargs(void)
40 {
41 	struct rte_kvargs *kvlist;
42 	const char *args;
43 	const char *valid_keys_list[] = { "foo", "check", NULL };
44 	const char **valid_keys;
45 
46 	/* empty args is valid */
47 	args = "";
48 	valid_keys = NULL;
49 	kvlist = rte_kvargs_parse(args, valid_keys);
50 	if (kvlist == NULL) {
51 		printf("rte_kvargs_parse() error");
52 		goto fail;
53 	}
54 	rte_kvargs_free(kvlist);
55 
56 	/* first test without valid_keys */
57 	args = "foo=1234,check=value0,check=value1";
58 	valid_keys = NULL;
59 	kvlist = rte_kvargs_parse(args, valid_keys);
60 	if (kvlist == NULL) {
61 		printf("rte_kvargs_parse() error");
62 		goto fail;
63 	}
64 	/* call check_handler() for all entries with key="check" */
65 	count = 0;
66 	if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
67 		printf("rte_kvargs_process() error\n");
68 		rte_kvargs_free(kvlist);
69 		goto fail;
70 	}
71 	if (count != 2) {
72 		printf("invalid count value %d after rte_kvargs_process(check)\n",
73 			count);
74 		rte_kvargs_free(kvlist);
75 		goto fail;
76 	}
77 	count = 0;
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);
82 		goto fail;
83 	}
84 	if (count != 0) {
85 		printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
86 			count);
87 		rte_kvargs_free(kvlist);
88 		goto fail;
89 	}
90 	/* count all entries with key="foo" */
91 	count = rte_kvargs_count(kvlist, "foo");
92 	if (count != 1) {
93 		printf("invalid count value %d after rte_kvargs_count(foo)\n",
94 			count);
95 		rte_kvargs_free(kvlist);
96 		goto fail;
97 	}
98 	/* count all entries */
99 	count = rte_kvargs_count(kvlist, NULL);
100 	if (count != 3) {
101 		printf("invalid count value %d after rte_kvargs_count(NULL)\n",
102 			count);
103 		rte_kvargs_free(kvlist);
104 		goto fail;
105 	}
106 	/* count all entries with key="unexistant_key" */
107 	count = rte_kvargs_count(kvlist, "unexistant_key");
108 	if (count != 0) {
109 		printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
110 			count);
111 		rte_kvargs_free(kvlist);
112 		goto fail;
113 	}
114 	rte_kvargs_free(kvlist);
115 
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");
122 		goto fail;
123 	}
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);
129 		goto fail;
130 	}
131 	count = rte_kvargs_count(kvlist, "check");
132 	if (count != 3) {
133 		printf("invalid count value %d after rte_kvargs_count(check)\n",
134 			count);
135 		rte_kvargs_free(kvlist);
136 		goto fail;
137 	}
138 	rte_kvargs_free(kvlist);
139 
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");
146 		goto fail;
147 	}
148 	if (strcmp(kvlist->pairs[0].value, "[0,1]") != 0) {
149 		printf("wrong value %s", kvlist->pairs[0].value);
150 		goto fail;
151 	}
152 	count = kvlist->count;
153 	if (count != 2) {
154 		printf("invalid count value %d\n", count);
155 		rte_kvargs_free(kvlist);
156 		goto fail;
157 	}
158 	rte_kvargs_free(kvlist);
159 
160 	/* test using empty string (it is valid) */
161 	args = "";
162 	kvlist = rte_kvargs_parse(args, NULL);
163 	if (kvlist == NULL) {
164 		printf("rte_kvargs_parse() error\n");
165 		goto fail;
166 	}
167 	if (rte_kvargs_count(kvlist, NULL) != 0) {
168 		printf("invalid count value\n");
169 		goto fail;
170 	}
171 	rte_kvargs_free(kvlist);
172 
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");
178 		goto fail;
179 	}
180 	if (rte_kvargs_count(kvlist, NULL) != 2) {
181 		printf("invalid count value\n");
182 		goto fail;
183 	}
184 	if (rte_kvargs_count(kvlist, "foo") != 1) {
185 		printf("invalid count value for 'foo'\n");
186 		goto fail;
187 	}
188 	if (rte_kvargs_count(kvlist, "check") != 1) {
189 		printf("invalid count value for 'check'\n");
190 		goto fail;
191 	}
192 	rte_kvargs_free(kvlist);
193 
194 	return 0;
195 
196  fail:
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);
202 		printf(">");
203 	}
204 	printf("\n");
205 	return -1;
206 }
207 
208 /* test several error cases */
test_invalid_kvargs(void)209 static int test_invalid_kvargs(void)
210 {
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 */
221 		NULL };
222 	const char **args;
223 	const char *valid_keys_list[] = { "foo", "check", NULL };
224 	const char **valid_keys = valid_keys_list;
225 
226 	for (args = args_list; *args != NULL; args++) {
227 
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);
232 			goto fail;
233 		}
234 	}
235 	return 0;
236 
237  fail:
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);
243 		printf(">");
244 	}
245 	printf("\n");
246 	return -1;
247 }
248 
249 static int
test_kvargs(void)250 test_kvargs(void)
251 {
252 	printf("== test valid case ==\n");
253 	if (test_valid_kvargs() < 0)
254 		return -1;
255 	printf("== test invalid case ==\n");
256 	if (test_invalid_kvargs() < 0)
257 		return -1;
258 	return 0;
259 }
260 
261 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);
262