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