update copyright date to 2013
[dpdk.git] / app / test / test_cmdline_etheraddr.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <inttypes.h>
38
39 #include <rte_ether.h>
40 #include <rte_string_fns.h>
41
42 #include <cmdline_parse.h>
43 #include <cmdline_parse_etheraddr.h>
44
45 #include "test_cmdline.h"
46
47 struct ether_addr_str {
48         const char * str;
49         uint64_t address;
50 };
51
52 /* valid strings */
53 const struct ether_addr_str ether_addr_valid_strs[] = {
54                 {"01:23:45:67:89:AB", 0xAB8967452301ULL},
55                 {"4567:89AB:CDEF", 0xEFCDAB896745ULL},
56 };
57
58 /* valid strings with various garbage at the end.
59  * these strings are still valid because parser checks for
60  * end of token, which is either space chars, null char or
61  * a hash sign.
62  */
63 const char * ether_addr_garbage_strs[] = {
64                 "00:11:22:33:44:55\0garbage",
65                 "00:11:22:33:44:55#garbage",
66                 "00:11:22:33:44:55 garbage",
67                 "00:11:22:33:44:55\tgarbage",
68                 "00:11:22:33:44:55\ngarbage",
69                 "00:11:22:33:44:55\rgarbage",
70                 "00:11:22:33:44:55#",
71                 "00:11:22:33:44:55 ",
72                 "00:11:22:33:44:55\t",
73                 "00:11:22:33:44:55\n",
74                 "00:11:22:33:44:55\r",
75 };
76 #define GARBAGE_ETHERADDR 0x554433221100ULL /* corresponding address */
77
78
79 const char * ether_addr_invalid_strs[] = {
80                 /* valid chars, invalid syntax */
81                 "0123:45:67:89:AB",
82                 "01:23:4567:89:AB",
83                 "01:23:45:67:89AB",
84                 "012:345:678:9AB",
85                 "01:23:45:67:89:ABC",
86                 "01:23:45:67:89:A",
87                 "01:23:45:67:89",
88                 "01:23:45:67:89:AB:CD",
89                 /* invalid chars, valid syntax */
90                 "IN:VA:LI:DC:HA:RS",
91                 "INVA:LIDC:HARS",
92                 /* misc */
93                 "01 23 45 67 89 AB",
94                 "01.23.45.67.89.AB",
95                 "01,23,45,67,89,AB",
96                 "01:23:45\0:67:89:AB",
97                 "01:23:45#:67:89:AB",
98                 "random invalid text",
99                 "random text",
100                 "",
101                 "\0",
102                 " ",
103 };
104
105 #define ETHERADDR_VALID_STRS_SIZE \
106         (sizeof(ether_addr_valid_strs) / sizeof(ether_addr_valid_strs[0]))
107 #define ETHERADDR_GARBAGE_STRS_SIZE \
108         (sizeof(ether_addr_garbage_strs) / sizeof(ether_addr_garbage_strs[0]))
109 #define ETHERADDR_INVALID_STRS_SIZE \
110         (sizeof(ether_addr_invalid_strs) / sizeof(ether_addr_invalid_strs[0]))
111
112
113
114 static int
115 is_addr_different(const struct ether_addr addr, uint64_t num)
116 {
117         int i;
118         for (i = 0; i < ETHER_ADDR_LEN; i++, num >>= 8)
119                 if (addr.addr_bytes[i] != (num & 0xFF)) {
120                         return 1;
121                 }
122         return 0;
123 }
124
125 /* test invalid parameters */
126 int
127 test_parse_etheraddr_invalid_param(void)
128 {
129         char buf[CMDLINE_TEST_BUFSIZE];
130         struct ether_addr result;
131         int ret = 0;
132
133         /* try all null */
134         ret = cmdline_parse_etheraddr(NULL, NULL, NULL);
135         if (ret != -1) {
136                 printf("Error: parser accepted null parameters!\n");
137                 return -1;
138         }
139
140         /* try null buf */
141         ret = cmdline_parse_etheraddr(NULL, NULL, (void*)&result);
142         if (ret != -1) {
143                 printf("Error: parser accepted null string!\n");
144                 return -1;
145         }
146
147         /* try null result */
148
149         /* copy string to buffer */
150         rte_snprintf(buf, sizeof(buf), "%s",
151                         ether_addr_valid_strs[0]);
152
153         ret = cmdline_parse_etheraddr(NULL, buf, NULL);
154         if (ret == -1) {
155                 printf("Error: parser rejected null result!\n");
156                 return -1;
157         }
158
159         /* token is not used in ether_parse anyway so there's no point in
160          * testing it */
161
162         /* test help function */
163         memset(&buf, 0, sizeof(buf));
164
165         /* try null buf */
166         ret = cmdline_get_help_etheraddr(NULL, NULL, sizeof(buf));
167         if (ret != -1) {
168                 printf("Error: help function accepted null buffer!\n");
169                 return -1;
170         }
171
172         /* coverage! */
173         ret = cmdline_get_help_etheraddr(NULL, buf, sizeof(buf));
174         if (ret < 0) {
175                 printf("Error: help function failed with valid parameters!\n");
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 /* test valid parameters but invalid data */
183 int
184 test_parse_etheraddr_invalid_data(void)
185 {
186         int ret = 0;
187         unsigned i;
188         struct ether_addr result;
189
190         /* test full strings */
191         for (i = 0; i < ETHERADDR_INVALID_STRS_SIZE; i++) {
192
193                 memset(&result, 0, sizeof(struct ether_addr));
194
195                 ret = cmdline_parse_etheraddr(NULL, ether_addr_invalid_strs[i],
196                                 (void*)&result);
197                 if (ret != -1) {
198                         printf("Error: parsing %s succeeded!\n",
199                                         ether_addr_invalid_strs[i]);
200                         return -1;
201                 }
202         }
203
204         return 0;
205 }
206
207 /* test valid parameters and data */
208 int
209 test_parse_etheraddr_valid(void)
210 {
211         int ret = 0;
212         unsigned i;
213         struct ether_addr result;
214
215         /* test full strings */
216         for (i = 0; i < ETHERADDR_VALID_STRS_SIZE; i++) {
217
218                 memset(&result, 0, sizeof(struct ether_addr));
219
220                 ret = cmdline_parse_etheraddr(NULL, ether_addr_valid_strs[i].str,
221                                 (void*)&result);
222                 if (ret < 0) {
223                         printf("Error: parsing %s failed!\n",
224                                         ether_addr_valid_strs[i].str);
225                         return -1;
226                 }
227                 if (is_addr_different(result, ether_addr_valid_strs[i].address)) {
228                         printf("Error: parsing %s failed: address mismatch!\n",
229                                         ether_addr_valid_strs[i].str);
230                         return -1;
231                 }
232         }
233
234         /* test garbage strings */
235         for (i = 0; i < ETHERADDR_GARBAGE_STRS_SIZE; i++) {
236
237                 memset(&result, 0, sizeof(struct ether_addr));
238
239                 ret = cmdline_parse_etheraddr(NULL, ether_addr_garbage_strs[i],
240                                 (void*)&result);
241                 if (ret < 0) {
242                         printf("Error: parsing %s failed!\n",
243                                         ether_addr_garbage_strs[i]);
244                         return -1;
245                 }
246                 if (is_addr_different(result, GARBAGE_ETHERADDR)) {
247                         printf("Error: parsing %s failed: address mismatch!\n",
248                                         ether_addr_garbage_strs[i]);
249                         return -1;
250                 }
251         }
252
253         return 0;
254 }