1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <inttypes.h>
8
9 #include <rte_common.h>
10
11 #include <cmdline_parse.h>
12 #include <cmdline_parse_portlist.h>
13
14 #include "test_cmdline.h"
15
16 struct portlist_str {
17 const char * str;
18 uint32_t portmap;
19 };
20
21 /* valid strings */
22 const struct portlist_str portlist_valid_strs[] = {
23 {"0", 0x1U },
24 {"0-10", 0x7FFU},
25 {"10-20", 0x1FFC00U},
26 {"all", UINT32_MAX},
27 {"0,1,2,3", 0xFU},
28 {"0,1-5", 0x3FU},
29 {"0,0,0", 0x1U},
30 {"31,0-10,15", 0x800087FFU},
31 {"0000", 0x1U},
32 {"00,01,02,03", 0xFU},
33 {"000,001,002,003", 0xFU},
34 };
35
36 /* valid strings but with garbage at the end.
37 * these strings should still be valid because parser checks
38 * for end of token, which is either a space/tab, a newline/return,
39 * or a hash sign.
40 */
41
42 const char * portlist_garbage_strs[] = {
43 "0-31 garbage",
44 "0-31#garbage",
45 "0-31\0garbage",
46 "0-31\ngarbage",
47 "0-31\rgarbage",
48 "0-31\tgarbage",
49 "0,1,2,3-31 garbage",
50 "0,1,2,3-31#garbage",
51 "0,1,2,3-31\0garbage",
52 "0,1,2,3-31\ngarbage",
53 "0,1,2,3-31\rgarbage",
54 "0,1,2,3-31\tgarbage",
55 "all garbage",
56 "all#garbage",
57 "all\0garbage",
58 "all\ngarbage",
59 "all\rgarbage",
60 "all\tgarbage",
61 };
62
63 /* invalid strings */
64 const char * portlist_invalid_strs[] = {
65 /* valid syntax, invalid chars */
66 "A-B",
67 "0-S",
68 "1,2,3,4,Q",
69 "A-4,3-15",
70 "0-31invalid",
71 /* valid chars, invalid syntax */
72 "1, 2",
73 "1- 4",
74 ",2",
75 ",2 ",
76 "-1, 4",
77 "5-1",
78 "2-",
79 /* misc */
80 "-"
81 "a",
82 "A",
83 ",",
84 "#",
85 " ",
86 "\0",
87 "",
88 /* too long */
89 "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,"
90 "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 };
92
93 /* test invalid parameters */
94 int
test_parse_portlist_invalid_param(void)95 test_parse_portlist_invalid_param(void)
96 {
97 cmdline_portlist_t result;
98 char buf[CMDLINE_TEST_BUFSIZE];
99 int ret;
100
101 memset(&buf, 0, sizeof(buf));
102 memset(&result, 0, sizeof(cmdline_portlist_t));
103
104 /* try all null */
105 ret = cmdline_parse_portlist(NULL, NULL, NULL, 0);
106 if (ret != -1) {
107 printf("Error: parser accepted null parameters!\n");
108 return -1;
109 }
110
111 /* try null buf */
112 ret = cmdline_parse_portlist(NULL, NULL, (void*)&result,
113 sizeof(result));
114 if (ret != -1) {
115 printf("Error: parser accepted null string!\n");
116 return -1;
117 }
118
119 /* try null result */
120 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[0].str, NULL, 0);
121 if (ret == -1) {
122 printf("Error: parser rejected null result!\n");
123 return -1;
124 }
125
126 /* token is not used in ether_parse anyway so there's no point in
127 * testing it */
128
129 /* test help function */
130
131 /* coverage! */
132 ret = cmdline_get_help_portlist(NULL, buf, sizeof(buf));
133 if (ret < 0) {
134 printf("Error: help function failed with valid parameters!\n");
135 return -1;
136 }
137
138 return 0;
139 }
140
141 /* test valid parameters but invalid data */
142 int
test_parse_portlist_invalid_data(void)143 test_parse_portlist_invalid_data(void)
144 {
145 int ret = 0;
146 unsigned i;
147 cmdline_portlist_t result;
148
149 /* test invalid strings */
150 for (i = 0; i < RTE_DIM(portlist_invalid_strs); i++) {
151
152 memset(&result, 0, sizeof(cmdline_portlist_t));
153
154 ret = cmdline_parse_portlist(NULL, portlist_invalid_strs[i],
155 (void*)&result, sizeof(result));
156 if (ret != -1) {
157 printf("Error: parsing %s succeeded!\n",
158 portlist_invalid_strs[i]);
159 return -1;
160 }
161 }
162
163 return 0;
164 }
165
166 /* test valid parameters and data */
167 int
test_parse_portlist_valid(void)168 test_parse_portlist_valid(void)
169 {
170 int ret = 0;
171 unsigned i;
172 cmdline_portlist_t result;
173
174 /* test full strings */
175 for (i = 0; i < RTE_DIM(portlist_valid_strs); i++) {
176
177 memset(&result, 0, sizeof(cmdline_portlist_t));
178
179 ret = cmdline_parse_portlist(NULL, portlist_valid_strs[i].str,
180 (void*)&result, sizeof(result));
181 if (ret < 0) {
182 printf("Error: parsing %s failed!\n",
183 portlist_valid_strs[i].str);
184 return -1;
185 }
186 if (result.map != portlist_valid_strs[i].portmap) {
187 printf("Error: parsing %s failed: map mismatch!\n",
188 portlist_valid_strs[i].str);
189 return -1;
190 }
191 }
192
193 /* test garbage strings */
194 for (i = 0; i < RTE_DIM(portlist_garbage_strs); i++) {
195
196 memset(&result, 0, sizeof(cmdline_portlist_t));
197
198 ret = cmdline_parse_portlist(NULL, portlist_garbage_strs[i],
199 (void*)&result, sizeof(result));
200 if (ret < 0) {
201 printf("Error: parsing %s failed!\n",
202 portlist_garbage_strs[i]);
203 return -1;
204 }
205 if (result.map != UINT32_MAX) {
206 printf("Error: parsing %s failed: map mismatch!\n",
207 portlist_garbage_strs[i]);
208 return -1;
209 }
210 }
211
212 return 0;
213 }
214