xref: /dpdk/app/test/test_cmdline_etheraddr.c (revision c10a2bf31bfdb5646e280165fec19691585aa43d)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
5a9de470cSBruce Richardson #include <stdio.h>
6a9de470cSBruce Richardson #include <string.h>
7a9de470cSBruce Richardson #include <inttypes.h>
8a9de470cSBruce Richardson 
9a9de470cSBruce Richardson #include <rte_ether.h>
10a9de470cSBruce Richardson #include <rte_string_fns.h>
11a9de470cSBruce Richardson 
12a9de470cSBruce Richardson #include <cmdline_parse.h>
13a9de470cSBruce Richardson #include <cmdline_parse_etheraddr.h>
14a9de470cSBruce Richardson 
15a9de470cSBruce Richardson #include "test_cmdline.h"
16a9de470cSBruce Richardson 
17a9de470cSBruce Richardson struct ether_addr_str {
18a9de470cSBruce Richardson 	const char * str;
19a9de470cSBruce Richardson 	uint64_t address;
20a9de470cSBruce Richardson };
21a9de470cSBruce Richardson 
22a9de470cSBruce Richardson /* valid strings */
23*c10a2bf3SStephen Hemminger static const struct ether_addr_str ether_addr_valid_strs[] = {
24a9de470cSBruce Richardson 		{"01:23:45:67:89:AB", 0xAB8967452301ULL},
25a9de470cSBruce Richardson 		{"4567:89AB:CDEF", 0xEFCDAB896745ULL},
26a9de470cSBruce Richardson };
27a9de470cSBruce Richardson 
28a9de470cSBruce Richardson /* valid strings with various garbage at the end.
29a9de470cSBruce Richardson  * these strings are still valid because parser checks for
30a9de470cSBruce Richardson  * end of token, which is either space chars, null char or
31a9de470cSBruce Richardson  * a hash sign.
32a9de470cSBruce Richardson  */
33*c10a2bf3SStephen Hemminger static const char * const ether_addr_garbage_strs[] = {
34a9de470cSBruce Richardson 		"00:11:22:33:44:55\0garbage",
35a9de470cSBruce Richardson 		"00:11:22:33:44:55#garbage",
36a9de470cSBruce Richardson 		"00:11:22:33:44:55 garbage",
37a9de470cSBruce Richardson 		"00:11:22:33:44:55\tgarbage",
38a9de470cSBruce Richardson 		"00:11:22:33:44:55\ngarbage",
39a9de470cSBruce Richardson 		"00:11:22:33:44:55\rgarbage",
40a9de470cSBruce Richardson 		"00:11:22:33:44:55#",
41a9de470cSBruce Richardson 		"00:11:22:33:44:55 ",
42a9de470cSBruce Richardson 		"00:11:22:33:44:55\t",
43a9de470cSBruce Richardson 		"00:11:22:33:44:55\n",
44a9de470cSBruce Richardson 		"00:11:22:33:44:55\r",
45a9de470cSBruce Richardson };
46a9de470cSBruce Richardson #define GARBAGE_ETHERADDR 0x554433221100ULL /* corresponding address */
47a9de470cSBruce Richardson 
48a9de470cSBruce Richardson 
49*c10a2bf3SStephen Hemminger static const char * const ether_addr_invalid_strs[] = {
50a9de470cSBruce Richardson 		/* valid chars, invalid syntax */
51a9de470cSBruce Richardson 		"0123:45:67:89:AB",
52a9de470cSBruce Richardson 		"01:23:4567:89:AB",
53a9de470cSBruce Richardson 		"01:23:45:67:89AB",
54a9de470cSBruce Richardson 		"012:345:678:9AB",
55a9de470cSBruce Richardson 		"01:23:45:67:89:ABC",
56a9de470cSBruce Richardson 		"01:23:45:67:89",
57a9de470cSBruce Richardson 		"01:23:45:67:89:AB:CD",
58a9de470cSBruce Richardson 		/* invalid chars, valid syntax */
59a9de470cSBruce Richardson 		"IN:VA:LI:DC:HA:RS",
60a9de470cSBruce Richardson 		"INVA:LIDC:HARS",
61a9de470cSBruce Richardson 		/* misc */
62a9de470cSBruce Richardson 		"01 23 45 67 89 AB",
63a9de470cSBruce Richardson 		"01,23,45,67,89,AB",
64a9de470cSBruce Richardson 		"01:23:45\0:67:89:AB",
65a9de470cSBruce Richardson 		"01:23:45#:67:89:AB",
66a9de470cSBruce Richardson 		"random invalid text",
67a9de470cSBruce Richardson 		"random text",
68a9de470cSBruce Richardson 		"",
69a9de470cSBruce Richardson 		"\0",
70a9de470cSBruce Richardson 		" ",
71a9de470cSBruce Richardson };
72a9de470cSBruce Richardson 
73a9de470cSBruce Richardson static int
is_addr_different(const struct rte_ether_addr addr,uint64_t num)746d13ea8eSOlivier Matz is_addr_different(const struct rte_ether_addr addr, uint64_t num)
75a9de470cSBruce Richardson {
76a9de470cSBruce Richardson 	int i;
7735b2d13fSOlivier Matz 	for (i = 0; i < RTE_ETHER_ADDR_LEN; i++, num >>= 8)
78a9de470cSBruce Richardson 		if (addr.addr_bytes[i] != (num & 0xFF)) {
79a9de470cSBruce Richardson 			return 1;
80a9de470cSBruce Richardson 		}
81a9de470cSBruce Richardson 	return 0;
82a9de470cSBruce Richardson }
83a9de470cSBruce Richardson 
84a9de470cSBruce Richardson /* test invalid parameters */
85a9de470cSBruce Richardson int
test_parse_etheraddr_invalid_param(void)86a9de470cSBruce Richardson test_parse_etheraddr_invalid_param(void)
87a9de470cSBruce Richardson {
88a9de470cSBruce Richardson 	char buf[CMDLINE_TEST_BUFSIZE];
896d13ea8eSOlivier Matz 	struct rte_ether_addr result;
90a9de470cSBruce Richardson 	int ret = 0;
91a9de470cSBruce Richardson 
92a9de470cSBruce Richardson 	/* try all null */
93a9de470cSBruce Richardson 	ret = cmdline_parse_etheraddr(NULL, NULL, NULL, 0);
94a9de470cSBruce Richardson 	if (ret != -1) {
95a9de470cSBruce Richardson 		printf("Error: parser accepted null parameters!\n");
96a9de470cSBruce Richardson 		return -1;
97a9de470cSBruce Richardson 	}
98a9de470cSBruce Richardson 
99a9de470cSBruce Richardson 	/* try null buf */
100a9de470cSBruce Richardson 	ret = cmdline_parse_etheraddr(NULL, NULL, (void*)&result,
101a9de470cSBruce Richardson 		sizeof(result));
102a9de470cSBruce Richardson 	if (ret != -1) {
103a9de470cSBruce Richardson 		printf("Error: parser accepted null string!\n");
104a9de470cSBruce Richardson 		return -1;
105a9de470cSBruce Richardson 	}
106a9de470cSBruce Richardson 
107a9de470cSBruce Richardson 	/* try null result */
108a9de470cSBruce Richardson 
109a9de470cSBruce Richardson 	/* copy string to buffer */
110f9acaf84SBruce Richardson 	strlcpy(buf, ether_addr_valid_strs[0].str, sizeof(buf));
111a9de470cSBruce Richardson 
112a9de470cSBruce Richardson 	ret = cmdline_parse_etheraddr(NULL, buf, NULL, 0);
113a9de470cSBruce Richardson 	if (ret == -1) {
114a9de470cSBruce Richardson 		printf("Error: parser rejected null result!\n");
115a9de470cSBruce Richardson 		return -1;
116a9de470cSBruce Richardson 	}
117a9de470cSBruce Richardson 
118a9de470cSBruce Richardson 	/* token is not used in ether_parse anyway so there's no point in
119a9de470cSBruce Richardson 	 * testing it */
120a9de470cSBruce Richardson 
121a9de470cSBruce Richardson 	/* test help function */
122a9de470cSBruce Richardson 	memset(&buf, 0, sizeof(buf));
123a9de470cSBruce Richardson 
124a9de470cSBruce Richardson 	/* coverage! */
125a9de470cSBruce Richardson 	ret = cmdline_get_help_etheraddr(NULL, buf, sizeof(buf));
126a9de470cSBruce Richardson 	if (ret < 0) {
127a9de470cSBruce Richardson 		printf("Error: help function failed with valid parameters!\n");
128a9de470cSBruce Richardson 		return -1;
129a9de470cSBruce Richardson 	}
130a9de470cSBruce Richardson 
131a9de470cSBruce Richardson 	return 0;
132a9de470cSBruce Richardson }
133a9de470cSBruce Richardson 
134a9de470cSBruce Richardson /* test valid parameters but invalid data */
135a9de470cSBruce Richardson int
test_parse_etheraddr_invalid_data(void)136a9de470cSBruce Richardson test_parse_etheraddr_invalid_data(void)
137a9de470cSBruce Richardson {
138a9de470cSBruce Richardson 	int ret = 0;
139a9de470cSBruce Richardson 	unsigned i;
1406d13ea8eSOlivier Matz 	struct rte_ether_addr result;
141a9de470cSBruce Richardson 
142a9de470cSBruce Richardson 	/* test full strings */
14371bdd8a1SPavan Nikhilesh 	for (i = 0; i < RTE_DIM(ether_addr_invalid_strs); i++) {
144a9de470cSBruce Richardson 
1456d13ea8eSOlivier Matz 		memset(&result, 0, sizeof(struct rte_ether_addr));
146a9de470cSBruce Richardson 
147a9de470cSBruce Richardson 		ret = cmdline_parse_etheraddr(NULL, ether_addr_invalid_strs[i],
148a9de470cSBruce Richardson 			(void*)&result, sizeof(result));
149a9de470cSBruce Richardson 		if (ret != -1) {
150a9de470cSBruce Richardson 			printf("Error: parsing %s succeeded!\n",
151a9de470cSBruce Richardson 					ether_addr_invalid_strs[i]);
152a9de470cSBruce Richardson 			return -1;
153a9de470cSBruce Richardson 		}
154a9de470cSBruce Richardson 	}
155a9de470cSBruce Richardson 
156a9de470cSBruce Richardson 	return 0;
157a9de470cSBruce Richardson }
158a9de470cSBruce Richardson 
159a9de470cSBruce Richardson /* test valid parameters and data */
160a9de470cSBruce Richardson int
test_parse_etheraddr_valid(void)161a9de470cSBruce Richardson test_parse_etheraddr_valid(void)
162a9de470cSBruce Richardson {
163a9de470cSBruce Richardson 	int ret = 0;
164a9de470cSBruce Richardson 	unsigned i;
1656d13ea8eSOlivier Matz 	struct rte_ether_addr result;
166a9de470cSBruce Richardson 
167a9de470cSBruce Richardson 	/* test full strings */
16871bdd8a1SPavan Nikhilesh 	for (i = 0; i < RTE_DIM(ether_addr_valid_strs); i++) {
169a9de470cSBruce Richardson 
1706d13ea8eSOlivier Matz 		memset(&result, 0, sizeof(struct rte_ether_addr));
171a9de470cSBruce Richardson 
172a9de470cSBruce Richardson 		ret = cmdline_parse_etheraddr(NULL, ether_addr_valid_strs[i].str,
173a9de470cSBruce Richardson 			(void*)&result, sizeof(result));
174a9de470cSBruce Richardson 		if (ret < 0) {
175a9de470cSBruce Richardson 			printf("Error: parsing %s failed!\n",
176a9de470cSBruce Richardson 					ether_addr_valid_strs[i].str);
177a9de470cSBruce Richardson 			return -1;
178a9de470cSBruce Richardson 		}
179a9de470cSBruce Richardson 		if (is_addr_different(result, ether_addr_valid_strs[i].address)) {
180a9de470cSBruce Richardson 			printf("Error: parsing %s failed: address mismatch!\n",
181a9de470cSBruce Richardson 					ether_addr_valid_strs[i].str);
182a9de470cSBruce Richardson 			return -1;
183a9de470cSBruce Richardson 		}
184a9de470cSBruce Richardson 	}
185a9de470cSBruce Richardson 
186a9de470cSBruce Richardson 	/* test garbage strings */
18771bdd8a1SPavan Nikhilesh 	for (i = 0; i < RTE_DIM(ether_addr_garbage_strs); i++) {
188a9de470cSBruce Richardson 
1896d13ea8eSOlivier Matz 		memset(&result, 0, sizeof(struct rte_ether_addr));
190a9de470cSBruce Richardson 
191a9de470cSBruce Richardson 		ret = cmdline_parse_etheraddr(NULL, ether_addr_garbage_strs[i],
192a9de470cSBruce Richardson 			(void*)&result, sizeof(result));
193a9de470cSBruce Richardson 		if (ret < 0) {
194a9de470cSBruce Richardson 			printf("Error: parsing %s failed!\n",
195a9de470cSBruce Richardson 					ether_addr_garbage_strs[i]);
196a9de470cSBruce Richardson 			return -1;
197a9de470cSBruce Richardson 		}
198a9de470cSBruce Richardson 		if (is_addr_different(result, GARBAGE_ETHERADDR)) {
199a9de470cSBruce Richardson 			printf("Error: parsing %s failed: address mismatch!\n",
200a9de470cSBruce Richardson 					ether_addr_garbage_strs[i]);
201a9de470cSBruce Richardson 			return -1;
202a9de470cSBruce Richardson 		}
203a9de470cSBruce Richardson 	}
204a9de470cSBruce Richardson 
205a9de470cSBruce Richardson 	return 0;
206a9de470cSBruce Richardson }
207