4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <cmdline_parse.h>
40 #include <cmdline_parse_portlist.h>
42 #include "test_cmdline.h"
50 const struct portlist_str portlist_valid_strs[] = {
58 {"31,0-10,15", 0x800087FFU},
60 {"00,01,02,03", 0xFU},
61 {"000,001,002,003", 0xFU},
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,
70 const char * portlist_garbage_strs[] = {
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",
92 const char * portlist_invalid_strs[] = {
93 /* valid syntax, invalid chars */
99 /* valid chars, invalid syntax */
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",
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]))
131 /* test invalid parameters */
133 test_parse_portlist_invalid_param(void)
135 cmdline_portlist_t result;
136 char buf[CMDLINE_TEST_BUFSIZE];
139 memset(&buf, 0, sizeof(buf));
140 memset(&result, 0, sizeof(cmdline_portlist_t));
143 ret = cmdline_parse_portlist(NULL, NULL, NULL);
145 printf("Error: parser accepted null parameters!\n");
150 ret = cmdline_parse_portlist(NULL, NULL, (void*)&result);
152 printf("Error: parser accepted null string!\n");
156 /* try null result */
157 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL);
159 printf("Error: parser rejected null result!\n");
163 /* token is not used in ether_parse anyway so there's no point in
166 /* test help function */
169 ret = cmdline_get_help_portlist(NULL, NULL, sizeof(buf));
171 printf("Error: help function accepted null buffer!\n");
176 ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
178 printf("Error: help function failed with valid parameters!\n");
185 /* test valid parameters but invalid data */
187 test_parse_portlist_invalid_data(void)
191 cmdline_portlist_t result;
193 /* test invalid strings */
194 for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
196 memset(&result, 0, sizeof(cmdline_portlist_t));
198 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
201 printf("Error: parsing %s succeeded!\n",
202 portlist_invalid_strs[i]);
210 /* test valid parameters and data */
212 test_parse_portlist_valid(void)
216 cmdline_portlist_t result;
218 /* test full strings */
219 for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
221 memset(&result, 0, sizeof(cmdline_portlist_t));
223 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
226 printf("Error: parsing %s failed!\n",
227 portlist_valid_strs[i].str);
230 if (result.map != portlist_valid_strs[i].portmap) {
231 printf("Error: parsing %s failed: map mismatch!\n",
232 portlist_valid_strs[i].str);
237 /* test garbage strings */
238 for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
240 memset(&result, 0, sizeof(cmdline_portlist_t));
242 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
245 printf("Error: parsing %s failed!\n",
246 portlist_garbage_strs[i]);
249 if (result.map != UINT32_MAX) {
250 printf("Error: parsing %s failed: map mismatch!\n",
251 portlist_garbage_strs[i]);