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 <rte_ether.h>
39 #include <rte_string_fns.h>
41 #include <cmdline_parse.h>
42 #include <cmdline_parse_etheraddr.h>
44 #include "test_cmdline.h"
46 struct ether_addr_str {
52 const struct ether_addr_str ether_addr_valid_strs[] = {
53 {"01:23:45:67:89:AB", 0xAB8967452301ULL},
54 {"4567:89AB:CDEF", 0xEFCDAB896745ULL},
57 /* valid strings with various garbage at the end.
58 * these strings are still valid because parser checks for
59 * end of token, which is either space chars, null char or
62 const char * ether_addr_garbage_strs[] = {
63 "00:11:22:33:44:55\0garbage",
64 "00:11:22:33:44:55#garbage",
65 "00:11:22:33:44:55 garbage",
66 "00:11:22:33:44:55\tgarbage",
67 "00:11:22:33:44:55\ngarbage",
68 "00:11:22:33:44:55\rgarbage",
71 "00:11:22:33:44:55\t",
72 "00:11:22:33:44:55\n",
73 "00:11:22:33:44:55\r",
75 #define GARBAGE_ETHERADDR 0x554433221100ULL /* corresponding address */
78 const char * ether_addr_invalid_strs[] = {
79 /* valid chars, invalid syntax */
87 "01:23:45:67:89:AB:CD",
88 /* invalid chars, valid syntax */
95 "01:23:45\0:67:89:AB",
97 "random invalid text",
104 #define ETHERADDR_VALID_STRS_SIZE \
105 (sizeof(ether_addr_valid_strs) / sizeof(ether_addr_valid_strs[0]))
106 #define ETHERADDR_GARBAGE_STRS_SIZE \
107 (sizeof(ether_addr_garbage_strs) / sizeof(ether_addr_garbage_strs[0]))
108 #define ETHERADDR_INVALID_STRS_SIZE \
109 (sizeof(ether_addr_invalid_strs) / sizeof(ether_addr_invalid_strs[0]))
114 is_addr_different(const struct ether_addr addr, uint64_t num)
117 for (i = 0; i < ETHER_ADDR_LEN; i++, num >>= 8)
118 if (addr.addr_bytes[i] != (num & 0xFF)) {
124 /* test invalid parameters */
126 test_parse_etheraddr_invalid_param(void)
128 char buf[CMDLINE_TEST_BUFSIZE];
129 struct ether_addr result;
133 ret = cmdline_parse_etheraddr(NULL, NULL, NULL, 0);
135 printf("Error: parser accepted null parameters!\n");
140 ret = cmdline_parse_etheraddr(NULL, NULL, (void*)&result,
143 printf("Error: parser accepted null string!\n");
147 /* try null result */
149 /* copy string to buffer */
150 snprintf(buf, sizeof(buf), "%s",
151 ether_addr_valid_strs[0].str);
153 ret = cmdline_parse_etheraddr(NULL, buf, NULL, 0);
155 printf("Error: parser rejected null result!\n");
159 /* token is not used in ether_parse anyway so there's no point in
162 /* test help function */
163 memset(&buf, 0, sizeof(buf));
166 ret = cmdline_get_help_etheraddr(NULL, buf, sizeof(buf));
168 printf("Error: help function failed with valid parameters!\n");
175 /* test valid parameters but invalid data */
177 test_parse_etheraddr_invalid_data(void)
181 struct ether_addr result;
183 /* test full strings */
184 for (i = 0; i < ETHERADDR_INVALID_STRS_SIZE; i++) {
186 memset(&result, 0, sizeof(struct ether_addr));
188 ret = cmdline_parse_etheraddr(NULL, ether_addr_invalid_strs[i],
189 (void*)&result, sizeof(result));
191 printf("Error: parsing %s succeeded!\n",
192 ether_addr_invalid_strs[i]);
200 /* test valid parameters and data */
202 test_parse_etheraddr_valid(void)
206 struct ether_addr result;
208 /* test full strings */
209 for (i = 0; i < ETHERADDR_VALID_STRS_SIZE; i++) {
211 memset(&result, 0, sizeof(struct ether_addr));
213 ret = cmdline_parse_etheraddr(NULL, ether_addr_valid_strs[i].str,
214 (void*)&result, sizeof(result));
216 printf("Error: parsing %s failed!\n",
217 ether_addr_valid_strs[i].str);
220 if (is_addr_different(result, ether_addr_valid_strs[i].address)) {
221 printf("Error: parsing %s failed: address mismatch!\n",
222 ether_addr_valid_strs[i].str);
227 /* test garbage strings */
228 for (i = 0; i < ETHERADDR_GARBAGE_STRS_SIZE; i++) {
230 memset(&result, 0, sizeof(struct ether_addr));
232 ret = cmdline_parse_etheraddr(NULL, ether_addr_garbage_strs[i],
233 (void*)&result, sizeof(result));
235 printf("Error: parsing %s failed!\n",
236 ether_addr_garbage_strs[i]);
239 if (is_addr_different(result, GARBAGE_ETHERADDR)) {
240 printf("Error: parsing %s failed: address mismatch!\n",
241 ether_addr_garbage_strs[i]);