update Intel copyright years to 2014
[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);
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         if (ret != -1) {
151                 printf("Error: parser accepted null string!\n");
152                 return -1;
153         }
154
155         /* try null result */
156         ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL);
157         if (ret == -1) {
158                 printf("Error: parser rejected null result!\n");
159                 return -1;
160         }
161
162         /* token is not used in ether_parse anyway so there's no point in
163          * testing it */
164
165         /* test help function */
166
167         /* try null buf */
168         ret = cmdline_get_help_portlist(NULL, NULL, sizeof(buf));
169         if (ret != -1) {
170                 printf("Error: help function accepted null buffer!\n");
171                 return -1;
172         }
173
174         /* coverage! */
175         ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
176         if (ret < 0) {
177                 printf("Error: help function failed with valid parameters!\n");
178                 return -1;
179         }
180
181         return 0;
182 }
183
184 /* test valid parameters but invalid data */
185 int
186 test_parse_portlist_invalid_data(void)
187 {
188         int ret = 0;
189         unsigned i;
190         cmdline_portlist_t result;
191
192         /* test invalid strings */
193         for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
194
195                 memset(&result, 0, sizeof(cmdline_portlist_t));
196
197                 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
198                                 (void*)&result);
199                 if (ret != -1) {
200                         printf("Error: parsing %s succeeded!\n",
201                                         portlist_invalid_strs[i]);
202                         return -1;
203                 }
204         }
205
206         return 0;
207 }
208
209 /* test valid parameters and data */
210 int
211 test_parse_portlist_valid(void)
212 {
213         int ret = 0;
214         unsigned i;
215         cmdline_portlist_t result;
216
217         /* test full strings */
218         for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
219
220                 memset(&result, 0, sizeof(cmdline_portlist_t));
221
222                 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
223                                 (void*)&result);
224                 if (ret < 0) {
225                         printf("Error: parsing %s failed!\n",
226                                         portlist_valid_strs[i].str);
227                         return -1;
228                 }
229                 if (result.map != portlist_valid_strs[i].portmap) {
230                         printf("Error: parsing %s failed: map mismatch!\n",
231                                         portlist_valid_strs[i].str);
232                         return -1;
233                 }
234         }
235
236         /* test garbage strings */
237         for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
238
239                 memset(&result, 0, sizeof(cmdline_portlist_t));
240
241                 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
242                                 (void*)&result);
243                 if (ret < 0) {
244                         printf("Error: parsing %s failed!\n",
245                                         portlist_garbage_strs[i]);
246                         return -1;
247                 }
248                 if (result.map != UINT32_MAX) {
249                         printf("Error: parsing %s failed: map mismatch!\n",
250                                         portlist_garbage_strs[i]);
251                         return -1;
252                 }
253         }
254
255         return 0;
256 }