app/test: convert all tests to register system
[dpdk.git] / app / test / test_kvargs.c
1 /*
2  * Copyright 2014 6WIND S.A.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
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
14  *   distribution.
15  *
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.
19  *
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.
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <rte_common.h>
39 #include <rte_kvargs.h>
40
41 #include "test.h"
42
43 /* incrementd in handler, to check it is properly called once per
44  * key/value association */
45 static unsigned count;
46
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)
51 {
52         char buf[16];
53
54         /* we check that the value is "check" */
55         if (strcmp(key, "check"))
56                 return -1;
57
58         /* we check that the value is "value$(count)" */
59         snprintf(buf, sizeof(buf), "value%d", count);
60         if (strncmp(buf, value, sizeof(buf)))
61                 return -1;
62
63         count ++;
64         return 0;
65 }
66
67 /* test a valid case */
68 static int test_valid_kvargs(void)
69 {
70         struct rte_kvargs *kvlist;
71         const char *args;
72         const char *valid_keys_list[] = { "foo", "check", NULL };
73         const char **valid_keys;
74
75         /* empty args is valid */
76         args = "";
77         valid_keys = NULL;
78         kvlist = rte_kvargs_parse(args, valid_keys);
79         if (kvlist == NULL) {
80                 printf("rte_kvargs_parse() error");
81                 rte_kvargs_free(kvlist);
82                 goto fail;
83         }
84         rte_kvargs_free(kvlist);
85
86         /* first test without valid_keys */
87         args = "foo=1234,check=value0,check=value1";
88         valid_keys = NULL;
89         kvlist = rte_kvargs_parse(args, valid_keys);
90         if (kvlist == NULL) {
91                 printf("rte_kvargs_parse() error");
92                 rte_kvargs_free(kvlist);
93                 goto fail;
94         }
95         /* call check_handler() for all entries with key="check" */
96         count = 0;
97         if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) {
98                 printf("rte_kvargs_process() error\n");
99                 rte_kvargs_free(kvlist);
100                 goto fail;
101         }
102         if (count != 2) {
103                 printf("invalid count value %d after rte_kvargs_process(check)\n",
104                         count);
105                 rte_kvargs_free(kvlist);
106                 goto fail;
107         }
108         count = 0;
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);
113                 goto fail;
114         }
115         if (count != 0) {
116                 printf("invalid count value %d after rte_kvargs_process(unexistant_key)\n",
117                         count);
118                 rte_kvargs_free(kvlist);
119                 goto fail;
120         }
121         /* count all entries with key="foo" */
122         count = rte_kvargs_count(kvlist, "foo");
123         if (count != 1) {
124                 printf("invalid count value %d after rte_kvargs_count(foo)\n",
125                         count);
126                 rte_kvargs_free(kvlist);
127                 goto fail;
128         }
129         /* count all entries */
130         count = rte_kvargs_count(kvlist, NULL);
131         if (count != 3) {
132                 printf("invalid count value %d after rte_kvargs_count(NULL)\n",
133                         count);
134                 rte_kvargs_free(kvlist);
135                 goto fail;
136         }
137         /* count all entries with key="unexistant_key" */
138         count = rte_kvargs_count(kvlist, "unexistant_key");
139         if (count != 0) {
140                 printf("invalid count value %d after rte_kvargs_count(unexistant_key)\n",
141                         count);
142                 rte_kvargs_free(kvlist);
143                 goto fail;
144         }
145         rte_kvargs_free(kvlist);
146
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);
154                 goto fail;
155         }
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);
161                 goto fail;
162         }
163         count = rte_kvargs_count(kvlist, "check");
164         if (count != 3) {
165                 printf("invalid count value %d after rte_kvargs_count(check)\n",
166                         count);
167                 rte_kvargs_free(kvlist);
168                 goto fail;
169         }
170         rte_kvargs_free(kvlist);
171
172         return 0;
173
174  fail:
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);
180                 printf(">");
181         }
182         printf("\n");
183         return -1;
184 }
185
186 /* test several error cases */
187 static int test_invalid_kvargs(void)
188 {
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 */
198                 NULL };
199         const char **args;
200         const char *valid_keys_list[] = { "foo", "check", NULL };
201         const char **valid_keys = valid_keys_list;
202
203         for (args = args_list; *args != NULL; args++) {
204
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);
209                         goto fail;
210                 }
211                 return 0;
212         }
213
214  fail:
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);
220                 printf(">");
221         }
222         printf("\n");
223         return -1;
224 }
225
226 static int
227 test_kvargs(void)
228 {
229         printf("== test valid case ==\n");
230         if (test_valid_kvargs() < 0)
231                 return -1;
232         printf("== test invalid case ==\n");
233         if (test_invalid_kvargs() < 0)
234                 return -1;
235         return 0;
236 }
237
238 static struct test_command kvargs_cmd = {
239         .command = "kvargs_autotest",
240         .callback = test_kvargs,
241 };
242 REGISTER_TEST_COMMAND(kvargs_cmd);