4 * Copyright(c) 2010-2014 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.
38 #include <cmdline_parse.h>
39 #include <cmdline_parse_portlist.h>
41 #include "test_cmdline.h"
49 const struct portlist_str portlist_valid_strs[] = {
57 {"31,0-10,15", 0x800087FFU},
59 {"00,01,02,03", 0xFU},
60 {"000,001,002,003", 0xFU},
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,
69 const char * portlist_garbage_strs[] = {
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",
91 const char * portlist_invalid_strs[] = {
92 /* valid syntax, invalid chars */
98 /* valid chars, invalid syntax */
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",
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]))
130 /* test invalid parameters */
132 test_parse_portlist_invalid_param(void)
134 cmdline_portlist_t result;
135 char buf[CMDLINE_TEST_BUFSIZE];
138 memset(&buf, 0, sizeof(buf));
139 memset(&result, 0, sizeof(cmdline_portlist_t));
142 ret = cmdline_parse_portlist(NULL, NULL, NULL);
144 printf("Error: parser accepted null parameters!\n");
149 ret = cmdline_parse_portlist(NULL, NULL, (void*)&result);
151 printf("Error: parser accepted null string!\n");
155 /* try null result */
156 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL);
158 printf("Error: parser rejected null result!\n");
162 /* token is not used in ether_parse anyway so there's no point in
165 /* test help function */
168 ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
170 printf("Error: help function failed with valid parameters!\n");
177 /* test valid parameters but invalid data */
179 test_parse_portlist_invalid_data(void)
183 cmdline_portlist_t result;
185 /* test invalid strings */
186 for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
188 memset(&result, 0, sizeof(cmdline_portlist_t));
190 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
193 printf("Error: parsing %s succeeded!\n",
194 portlist_invalid_strs[i]);
202 /* test valid parameters and data */
204 test_parse_portlist_valid(void)
208 cmdline_portlist_t result;
210 /* test full strings */
211 for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
213 memset(&result, 0, sizeof(cmdline_portlist_t));
215 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
218 printf("Error: parsing %s failed!\n",
219 portlist_valid_strs[i].str);
222 if (result.map != portlist_valid_strs[i].portmap) {
223 printf("Error: parsing %s failed: map mismatch!\n",
224 portlist_valid_strs[i].str);
229 /* test garbage strings */
230 for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
232 memset(&result, 0, sizeof(cmdline_portlist_t));
234 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
237 printf("Error: parsing %s failed!\n",
238 portlist_garbage_strs[i]);
241 if (result.map != UINT32_MAX) {
242 printf("Error: parsing %s failed: map mismatch!\n",
243 portlist_garbage_strs[i]);