01bd3e6d4d3fc3a8e22af4dbf939f58de3364a47
[dpdk.git] / app / test / test_cmdline_portlist.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <string.h>
37 #include <inttypes.h>
38
39 #include <cmdline_parse.h>
40 #include <cmdline_parse_portlist.h>
41
42 #include "test_cmdline.h"
43
44 struct portlist_str {
45         const char * str;
46         uint32_t portmap;
47 };
48
49 /* valid strings */
50 const struct portlist_str portlist_valid_strs[] = {
51                 {"0", 0x1U },
52                 {"0-10", 0x7FFU},
53                 {"10-20", 0x1FFC00U},
54                 {"all", UINT32_MAX},
55                 {"0,1,2,3", 0xFU},
56                 {"0,1-5", 0x3FU},
57                 {"0,0,0", 0x1U},
58                 {"31,0-10,15", 0x800087FFU},
59                 {"0000", 0x1U},
60                 {"00,01,02,03", 0xFU},
61                 {"000,001,002,003", 0xFU},
62 };
63
64 /* valid strings but with garbage at the end.
65  * these strings should still be valid because parser checks
66  * for end of token, which is either a space/tab, a newline/return,
67  * or a hash sign.
68  */
69
70 const char * portlist_garbage_strs[] = {
71                 "0-31 garbage",
72                 "0-31#garbage",
73                 "0-31\0garbage",
74                 "0-31\ngarbage",
75                 "0-31\rgarbage",
76                 "0-31\tgarbage",
77                 "0,1,2,3-31 garbage",
78                 "0,1,2,3-31#garbage",
79                 "0,1,2,3-31\0garbage",
80                 "0,1,2,3-31\ngarbage",
81                 "0,1,2,3-31\rgarbage",
82                 "0,1,2,3-31\tgarbage",
83                 "all garbage",
84                 "all#garbage",
85                 "all\0garbage",
86                 "all\ngarbage",
87                 "all\rgarbage",
88                 "all\tgarbage",
89 };
90
91 /* invalid strings */
92 const char * portlist_invalid_strs[] = {
93                 /* valid syntax, invalid chars */
94                 "A-B",
95                 "0-S",
96                 "1,2,3,4,Q",
97                 "A-4,3-15",
98                 "0-31invalid",
99                 /* valid chars, invalid syntax */
100                 "1, 2",
101                 "1- 4",
102                 ",2",
103                 ",2 ",
104                 "-1, 4",
105                 "5-1",
106                 "2-",
107                 /* misc */
108                 "-"
109                 "a",
110                 "A",
111                 ",",
112                 "#",
113                 " ",
114                 "\0",
115                 "",
116                 /* too long */
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,"
118                 "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",
119 };
120
121 #define PORTLIST_VALID_STRS_SIZE \
122         (sizeof(portlist_valid_strs) / sizeof(portlist_valid_strs[0]))
123 #define PORTLIST_GARBAGE_STRS_SIZE \
124         (sizeof(portlist_garbage_strs) / sizeof(portlist_garbage_strs[0]))
125 #define PORTLIST_INVALID_STRS_SIZE \
126         (sizeof(portlist_invalid_strs) / sizeof(portlist_invalid_strs[0]))
127
128
129
130
131 /* test invalid parameters */
132 int
133 test_parse_portlist_invalid_param(void)
134 {
135         cmdline_portlist_t result;
136         char buf[CMDLINE_TEST_BUFSIZE];
137         int ret;
138
139         memset(&buf, 0, sizeof(buf));
140         memset(&result, 0, sizeof(cmdline_portlist_t));
141
142         /* try all null */
143         ret = cmdline_parse_portlist(NULL, NULL, NULL);
144         if (ret != -1) {
145                 printf("Error: parser accepted null parameters!\n");
146                 return -1;
147         }
148
149         /* try null buf */
150         ret = cmdline_parse_portlist(NULL, NULL, (void*)&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);
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         /* try null buf */
169         ret = cmdline_get_help_portlist(NULL, NULL, sizeof(buf));
170         if (ret != -1) {
171                 printf("Error: help function accepted null buffer!\n");
172                 return -1;
173         }
174
175         /* coverage! */
176         ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
177         if (ret < 0) {
178                 printf("Error: help function failed with valid parameters!\n");
179                 return -1;
180         }
181
182         return 0;
183 }
184
185 /* test valid parameters but invalid data */
186 int
187 test_parse_portlist_invalid_data(void)
188 {
189         int ret = 0;
190         unsigned i;
191         cmdline_portlist_t result;
192
193         /* test invalid strings */
194         for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
195
196                 memset(&result, 0, sizeof(cmdline_portlist_t));
197
198                 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
199                                 (void*)&result);
200                 if (ret != -1) {
201                         printf("Error: parsing %s succeeded!\n",
202                                         portlist_invalid_strs[i]);
203                         return -1;
204                 }
205         }
206
207         return 0;
208 }
209
210 /* test valid parameters and data */
211 int
212 test_parse_portlist_valid(void)
213 {
214         int ret = 0;
215         unsigned i;
216         cmdline_portlist_t result;
217
218         /* test full strings */
219         for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
220
221                 memset(&result, 0, sizeof(cmdline_portlist_t));
222
223                 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
224                                 (void*)&result);
225                 if (ret < 0) {
226                         printf("Error: parsing %s failed!\n",
227                                         portlist_valid_strs[i].str);
228                         return -1;
229                 }
230                 if (result.map != portlist_valid_strs[i].portmap) {
231                         printf("Error: parsing %s failed: map mismatch!\n",
232                                         portlist_valid_strs[i].str);
233                         return -1;
234                 }
235         }
236
237         /* test garbage strings */
238         for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
239
240                 memset(&result, 0, sizeof(cmdline_portlist_t));
241
242                 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
243                                 (void*)&result);
244                 if (ret < 0) {
245                         printf("Error: parsing %s failed!\n",
246                                         portlist_garbage_strs[i]);
247                         return -1;
248                 }
249                 if (result.map != UINT32_MAX) {
250                         printf("Error: parsing %s failed: map mismatch!\n",
251                                         portlist_garbage_strs[i]);
252                         return -1;
253                 }
254         }
255
256         return 0;
257 }