New upstream version 18.02
[deb_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         return 0;
141
142  fail:
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);
148                 printf(">");
149         }
150         printf("\n");
151         return -1;
152 }
153
154 /* test several error cases */
155 static int test_invalid_kvargs(void)
156 {
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 */
166                 NULL };
167         const char **args;
168         const char *valid_keys_list[] = { "foo", "check", NULL };
169         const char **valid_keys = valid_keys_list;
170
171         for (args = args_list; *args != NULL; args++) {
172
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);
177                         goto fail;
178                 }
179                 return 0;
180         }
181
182  fail:
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);
188                 printf(">");
189         }
190         printf("\n");
191         return -1;
192 }
193
194 static int
195 test_kvargs(void)
196 {
197         printf("== test valid case ==\n");
198         if (test_valid_kvargs() < 0)
199                 return -1;
200         printf("== test invalid case ==\n");
201         if (test_invalid_kvargs() < 0)
202                 return -1;
203         return 0;
204 }
205
206 REGISTER_TEST_COMMAND(kvargs_autotest, test_kvargs);