1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_string_fns.h>
12 #include <cmdline_parse.h>
13 #include <cmdline_parse_etheraddr.h>
15 #include "test_cmdline.h"
17 struct ether_addr_str {
23 const struct ether_addr_str ether_addr_valid_strs[] = {
24 {"01:23:45:67:89:AB", 0xAB8967452301ULL},
25 {"4567:89AB:CDEF", 0xEFCDAB896745ULL},
28 /* valid strings with various garbage at the end.
29 * these strings are still valid because parser checks for
30 * end of token, which is either space chars, null char or
33 const char * ether_addr_garbage_strs[] = {
34 "00:11:22:33:44:55\0garbage",
35 "00:11:22:33:44:55#garbage",
36 "00:11:22:33:44:55 garbage",
37 "00:11:22:33:44:55\tgarbage",
38 "00:11:22:33:44:55\ngarbage",
39 "00:11:22:33:44:55\rgarbage",
42 "00:11:22:33:44:55\t",
43 "00:11:22:33:44:55\n",
44 "00:11:22:33:44:55\r",
46 #define GARBAGE_ETHERADDR 0x554433221100ULL /* corresponding address */
49 const char * ether_addr_invalid_strs[] = {
50 /* valid chars, invalid syntax */
58 "01:23:45:67:89:AB:CD",
59 /* invalid chars, valid syntax */
66 "01:23:45\0:67:89:AB",
68 "random invalid text",
76 is_addr_different(const struct rte_ether_addr addr, uint64_t num)
79 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++, num >>= 8)
80 if (addr.addr_bytes[i] != (num & 0xFF)) {
86 /* test invalid parameters */
88 test_parse_etheraddr_invalid_param(void)
90 char buf[CMDLINE_TEST_BUFSIZE];
91 struct rte_ether_addr result;
95 ret = cmdline_parse_etheraddr(NULL, NULL, NULL, 0);
97 printf("Error: parser accepted null parameters!\n");
102 ret = cmdline_parse_etheraddr(NULL, NULL, (void*)&result,
105 printf("Error: parser accepted null string!\n");
109 /* try null result */
111 /* copy string to buffer */
112 strlcpy(buf, ether_addr_valid_strs[0].str, sizeof(buf));
114 ret = cmdline_parse_etheraddr(NULL, buf, NULL, 0);
116 printf("Error: parser rejected null result!\n");
120 /* token is not used in ether_parse anyway so there's no point in
123 /* test help function */
124 memset(&buf, 0, sizeof(buf));
127 ret = cmdline_get_help_etheraddr(NULL, buf, sizeof(buf));
129 printf("Error: help function failed with valid parameters!\n");
136 /* test valid parameters but invalid data */
138 test_parse_etheraddr_invalid_data(void)
142 struct rte_ether_addr result;
144 /* test full strings */
145 for (i = 0; i < RTE_DIM(ether_addr_invalid_strs); i++) {
147 memset(&result, 0, sizeof(struct rte_ether_addr));
149 ret = cmdline_parse_etheraddr(NULL, ether_addr_invalid_strs[i],
150 (void*)&result, sizeof(result));
152 printf("Error: parsing %s succeeded!\n",
153 ether_addr_invalid_strs[i]);
161 /* test valid parameters and data */
163 test_parse_etheraddr_valid(void)
167 struct rte_ether_addr result;
169 /* test full strings */
170 for (i = 0; i < RTE_DIM(ether_addr_valid_strs); i++) {
172 memset(&result, 0, sizeof(struct rte_ether_addr));
174 ret = cmdline_parse_etheraddr(NULL, ether_addr_valid_strs[i].str,
175 (void*)&result, sizeof(result));
177 printf("Error: parsing %s failed!\n",
178 ether_addr_valid_strs[i].str);
181 if (is_addr_different(result, ether_addr_valid_strs[i].address)) {
182 printf("Error: parsing %s failed: address mismatch!\n",
183 ether_addr_valid_strs[i].str);
188 /* test garbage strings */
189 for (i = 0; i < RTE_DIM(ether_addr_garbage_strs); i++) {
191 memset(&result, 0, sizeof(struct rte_ether_addr));
193 ret = cmdline_parse_etheraddr(NULL, ether_addr_garbage_strs[i],
194 (void*)&result, sizeof(result));
196 printf("Error: parsing %s failed!\n",
197 ether_addr_garbage_strs[i]);
200 if (is_addr_different(result, GARBAGE_ETHERADDR)) {
201 printf("Error: parsing %s failed: address mismatch!\n",
202 ether_addr_garbage_strs[i]);