test: fix autotest list
[dpdk.git] / test / test / test_kvargs.c
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" */
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 */
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");
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         return 0;
161
162  fail:
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);
168                 printf(">");
169         }
170         printf("\n");
171         return -1;
172 }
173
174 /* test several error cases */
175 static int test_invalid_kvargs(void)
176 {
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 */
187                 NULL };
188         const char **args;
189         const char *valid_keys_list[] = { "foo", "check", NULL };
190         const char **valid_keys = valid_keys_list;
191
192         for (args = args_list; *args != NULL; args++) {
193
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);
198                         goto fail;
199                 }
200                 return 0;
201         }
202
203  fail:
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);
209                 printf(">");
210         }
211         printf("\n");
212         return -1;
213 }
214
215 static int
216 test_kvargs(void)
217 {
218         printf("== test valid case ==\n");
219         if (test_valid_kvargs() < 0)
220                 return -1;
221         printf("== test invalid case ==\n");
222         if (test_invalid_kvargs() < 0)
223                 return -1;
224         return 0;
225 }
226
227 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);