tile: fix build
[dpdk.git] / app / test / test_cmdline_portlist.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <inttypes.h>
37
38 #include <cmdline_parse.h>
39 #include <cmdline_parse_portlist.h>
40
41 #include "test_cmdline.h"
42
43 struct portlist_str {
44         const char * str;
45         uint32_t portmap;
46 };
47
48 /* valid strings */
49 const struct portlist_str portlist_valid_strs[] = {
50                 {"0", 0x1U },
51                 {"0-10", 0x7FFU},
52                 {"10-20", 0x1FFC00U},
53                 {"all", UINT32_MAX},
54                 {"0,1,2,3", 0xFU},
55                 {"0,1-5", 0x3FU},
56                 {"0,0,0", 0x1U},
57                 {"31,0-10,15", 0x800087FFU},
58                 {"0000", 0x1U},
59                 {"00,01,02,03", 0xFU},
60                 {"000,001,002,003", 0xFU},
61 };
62
63 /* valid strings but with garbage at the end.
64  * these strings should still be valid because parser checks
65  * for end of token, which is either a space/tab, a newline/return,
66  * or a hash sign.
67  */
68
69 const char * portlist_garbage_strs[] = {
70                 "0-31 garbage",
71                 "0-31#garbage",
72                 "0-31\0garbage",
73                 "0-31\ngarbage",
74                 "0-31\rgarbage",
75                 "0-31\tgarbage",
76                 "0,1,2,3-31 garbage",
77                 "0,1,2,3-31#garbage",
78                 "0,1,2,3-31\0garbage",
79                 "0,1,2,3-31\ngarbage",
80                 "0,1,2,3-31\rgarbage",
81                 "0,1,2,3-31\tgarbage",
82                 "all garbage",
83                 "all#garbage",
84                 "all\0garbage",
85                 "all\ngarbage",
86                 "all\rgarbage",
87                 "all\tgarbage",
88 };
89
90 /* invalid strings */
91 const char * portlist_invalid_strs[] = {
92                 /* valid syntax, invalid chars */
93                 "A-B",
94                 "0-S",
95                 "1,2,3,4,Q",
96                 "A-4,3-15",
97                 "0-31invalid",
98                 /* valid chars, invalid syntax */
99                 "1, 2",
100                 "1- 4",
101                 ",2",
102                 ",2 ",
103                 "-1, 4",
104                 "5-1",
105                 "2-",
106                 /* misc */
107                 "-"
108                 "a",
109                 "A",
110                 ",",
111                 "#",
112                 " ",
113                 "\0",
114                 "",
115                 /* too long */
116                 "0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,"
117                 "0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,2",
118 };
119
120 #define PORTLIST_VALID_STRS_SIZE \
121         (sizeof(portlist_valid_strs) / sizeof(portlist_valid_strs[0]))
122 #define PORTLIST_GARBAGE_STRS_SIZE \
123         (sizeof(portlist_garbage_strs) / sizeof(portlist_garbage_strs[0]))
124 #define PORTLIST_INVALID_STRS_SIZE \
125         (sizeof(portlist_invalid_strs) / sizeof(portlist_invalid_strs[0]))
126
127
128
129
130 /* test invalid parameters */
131 int
132 test_parse_portlist_invalid_param(void)
133 {
134         cmdline_portlist_t result;
135         char buf[CMDLINE_TEST_BUFSIZE];
136         int ret;
137
138         memset(&buf, 0, sizeof(buf));
139         memset(&result, 0, sizeof(cmdline_portlist_t));
140
141         /* try all null */
142         ret = cmdline_parse_portlist(NULL, NULL, NULL, 0);
143         if (ret != -1) {
144                 printf("Error: parser accepted null parameters!\n");
145                 return -1;
146         }
147
148         /* try null buf */
149         ret = cmdline_parse_portlist(NULL, NULL, (void*)&result,
150                 sizeof(result));
151         if (ret != -1) {
152                 printf("Error: parser accepted null string!\n");
153                 return -1;
154         }
155
156         /* try null result */
157         ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL, 0);
158         if (ret == -1) {
159                 printf("Error: parser rejected null result!\n");
160                 return -1;
161         }
162
163         /* token is not used in ether_parse anyway so there's no point in
164          * testing it */
165
166         /* test help function */
167
168         /* coverage! */
169         ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
170         if (ret < 0) {
171                 printf("Error: help function failed with valid parameters!\n");
172                 return -1;
173         }
174
175         return 0;
176 }
177
178 /* test valid parameters but invalid data */
179 int
180 test_parse_portlist_invalid_data(void)
181 {
182         int ret = 0;
183         unsigned i;
184         cmdline_portlist_t result;
185
186         /* test invalid strings */
187         for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
188
189                 memset(&result, 0, sizeof(cmdline_portlist_t));
190
191                 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
192                         (void*)&result, sizeof(result));
193                 if (ret != -1) {
194                         printf("Error: parsing %s succeeded!\n",
195                                         portlist_invalid_strs[i]);
196                         return -1;
197                 }
198         }
199
200         return 0;
201 }
202
203 /* test valid parameters and data */
204 int
205 test_parse_portlist_valid(void)
206 {
207         int ret = 0;
208         unsigned i;
209         cmdline_portlist_t result;
210
211         /* test full strings */
212         for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
213
214                 memset(&result, 0, sizeof(cmdline_portlist_t));
215
216                 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
217                         (void*)&result, sizeof(result));
218                 if (ret < 0) {
219                         printf("Error: parsing %s failed!\n",
220                                         portlist_valid_strs[i].str);
221                         return -1;
222                 }
223                 if (result.map != portlist_valid_strs[i].portmap) {
224                         printf("Error: parsing %s failed: map mismatch!\n",
225                                         portlist_valid_strs[i].str);
226                         return -1;
227                 }
228         }
229
230         /* test garbage strings */
231         for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
232
233                 memset(&result, 0, sizeof(cmdline_portlist_t));
234
235                 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
236                         (void*)&result, sizeof(result));
237                 if (ret < 0) {
238                         printf("Error: parsing %s failed!\n",
239                                         portlist_garbage_strs[i]);
240                         return -1;
241                 }
242                 if (result.map != UINT32_MAX) {
243                         printf("Error: parsing %s failed: map mismatch!\n",
244                                         portlist_garbage_strs[i]);
245                         return -1;
246                 }
247         }
248
249         return 0;
250 }