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);
135 printf("Error: parser accepted null parameters!\n");
140 ret = cmdline_parse_etheraddr(NULL, NULL, (void*)&result);
142 printf("Error: parser accepted null string!\n");
146 /* try null result */
148 /* copy string to buffer */
149 snprintf(buf, sizeof(buf), "%s",
150 ether_addr_valid_strs[0].str);
152 ret = cmdline_parse_etheraddr(NULL, buf, NULL);
154 printf("Error: parser rejected null result!\n");
158 /* token is not used in ether_parse anyway so there's no point in
161 /* test help function */
162 memset(&buf, 0, sizeof(buf));
165 ret = cmdline_get_help_etheraddr(NULL, NULL, sizeof(buf));
167 printf("Error: help function accepted null buffer!\n");
172 ret = cmdline_get_help_etheraddr(NULL, buf, sizeof(buf));
174 printf("Error: help function failed with valid parameters!\n");
181 /* test valid parameters but invalid data */
183 test_parse_etheraddr_invalid_data(void)
187 struct ether_addr result;
189 /* test full strings */
190 for (i = 0; i < ETHERADDR_INVALID_STRS_SIZE; i++) {
192 memset(&result, 0, sizeof(struct ether_addr));
194 ret = cmdline_parse_etheraddr(NULL, ether_addr_invalid_strs[i],
197 printf("Error: parsing %s succeeded!\n",
198 ether_addr_invalid_strs[i]);
206 /* test valid parameters and data */
208 test_parse_etheraddr_valid(void)
212 struct ether_addr result;
214 /* test full strings */
215 for (i = 0; i < ETHERADDR_VALID_STRS_SIZE; i++) {
217 memset(&result, 0, sizeof(struct ether_addr));
219 ret = cmdline_parse_etheraddr(NULL, ether_addr_valid_strs[i].str,
222 printf("Error: parsing %s failed!\n",
223 ether_addr_valid_strs[i].str);
226 if (is_addr_different(result, ether_addr_valid_strs[i].address)) {
227 printf("Error: parsing %s failed: address mismatch!\n",
228 ether_addr_valid_strs[i].str);
233 /* test garbage strings */
234 for (i = 0; i < ETHERADDR_GARBAGE_STRS_SIZE; i++) {
236 memset(&result, 0, sizeof(struct ether_addr));
238 ret = cmdline_parse_etheraddr(NULL, ether_addr_garbage_strs[i],
241 printf("Error: parsing %s failed!\n",
242 ether_addr_garbage_strs[i]);
245 if (is_addr_different(result, GARBAGE_ETHERADDR)) {
246 printf("Error: parsing %s failed: address mismatch!\n",
247 ether_addr_garbage_strs[i]);