1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <cmdline_parse.h>
10 #include <cmdline_parse_portlist.h>
12 #include "test_cmdline.h"
20 const struct portlist_str portlist_valid_strs[] = {
28 {"31,0-10,15", 0x800087FFU},
30 {"00,01,02,03", 0xFU},
31 {"000,001,002,003", 0xFU},
34 /* valid strings but with garbage at the end.
35 * these strings should still be valid because parser checks
36 * for end of token, which is either a space/tab, a newline/return,
40 const char * portlist_garbage_strs[] = {
49 "0,1,2,3-31\0garbage",
50 "0,1,2,3-31\ngarbage",
51 "0,1,2,3-31\rgarbage",
52 "0,1,2,3-31\tgarbage",
62 const char * portlist_invalid_strs[] = {
63 /* valid syntax, invalid chars */
69 /* valid chars, invalid syntax */
87 "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,"
88 "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",
91 #define PORTLIST_VALID_STRS_SIZE \
92 (sizeof(portlist_valid_strs) / sizeof(portlist_valid_strs[0]))
93 #define PORTLIST_GARBAGE_STRS_SIZE \
94 (sizeof(portlist_garbage_strs) / sizeof(portlist_garbage_strs[0]))
95 #define PORTLIST_INVALID_STRS_SIZE \
96 (sizeof(portlist_invalid_strs) / sizeof(portlist_invalid_strs[0]))
101 /* test invalid parameters */
103 test_parse_portlist_invalid_param(void)
105 cmdline_portlist_t result;
106 char buf[CMDLINE_TEST_BUFSIZE];
109 memset(&buf, 0, sizeof(buf));
110 memset(&result, 0, sizeof(cmdline_portlist_t));
113 ret = cmdline_parse_portlist(NULL, NULL, NULL, 0);
115 printf("Error: parser accepted null parameters!\n");
120 ret = cmdline_parse_portlist(NULL, NULL, (void*)&result,
123 printf("Error: parser accepted null string!\n");
127 /* try null result */
128 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL, 0);
130 printf("Error: parser rejected null result!\n");
134 /* token is not used in ether_parse anyway so there's no point in
137 /* test help function */
140 ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
142 printf("Error: help function failed with valid parameters!\n");
149 /* test valid parameters but invalid data */
151 test_parse_portlist_invalid_data(void)
155 cmdline_portlist_t result;
157 /* test invalid strings */
158 for (i = 0; i < PORTLIST_INVALID_STRS_SIZE; i++) {
160 memset(&result, 0, sizeof(cmdline_portlist_t));
162 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
163 (void*)&result, sizeof(result));
165 printf("Error: parsing %s succeeded!\n",
166 portlist_invalid_strs[i]);
174 /* test valid parameters and data */
176 test_parse_portlist_valid(void)
180 cmdline_portlist_t result;
182 /* test full strings */
183 for (i = 0; i < PORTLIST_VALID_STRS_SIZE; i++) {
185 memset(&result, 0, sizeof(cmdline_portlist_t));
187 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
188 (void*)&result, sizeof(result));
190 printf("Error: parsing %s failed!\n",
191 portlist_valid_strs[i].str);
194 if (result.map != portlist_valid_strs[i].portmap) {
195 printf("Error: parsing %s failed: map mismatch!\n",
196 portlist_valid_strs[i].str);
201 /* test garbage strings */
202 for (i = 0; i < PORTLIST_GARBAGE_STRS_SIZE; i++) {
204 memset(&result, 0, sizeof(cmdline_portlist_t));
206 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
207 (void*)&result, sizeof(result));
209 printf("Error: parsing %s failed!\n",
210 portlist_garbage_strs[i]);
213 if (result.map != UINT32_MAX) {
214 printf("Error: parsing %s failed: map mismatch!\n",
215 portlist_garbage_strs[i]);