xref: /freebsd/lib/libc/tests/net/ether_test.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 
29 #include <net/ethernet.h>
30 
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <atf-c.h>
36 
37 static const char *ether_line_string = "01:23:45:67:89:ab ether_line_hostname";
38 static const char *ether_line_hostname = "ether_line_hostname";
39 static const struct ether_addr	 ether_line_addr = {
40 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
41 };
42 
43 ATF_TC_WITHOUT_HEAD(ether_line);
44 ATF_TC_BODY(ether_line, tc)
45 {
46 	struct ether_addr e;
47 	char hostname[256];
48 
49 	ATF_REQUIRE_MSG(ether_line(ether_line_string, &e, hostname) == 0,
50 	    "ether_line failed; errno=%d", errno);
51 	ATF_REQUIRE_MSG(bcmp(&e, &ether_line_addr, ETHER_ADDR_LEN) == 0,
52 	    "bad address");
53 	ATF_REQUIRE_MSG(strcmp(hostname, ether_line_hostname) == 0,
54 	    "bad hostname");
55 }
56 
57 static const char *ether_line_bad_1_string = "x";
58 
59 ATF_TC_WITHOUT_HEAD(ether_line_bad_1);
60 ATF_TC_BODY(ether_line_bad_1, tc)
61 {
62 	struct ether_addr e;
63 	char hostname[256];
64 
65 	ATF_REQUIRE_MSG(ether_line(ether_line_bad_1_string, &e, hostname) != 0,
66 	    "ether_line succeeded unexpectedly");
67 }
68 
69 static const char *ether_line_bad_2_string = "x x";
70 
71 ATF_TC_WITHOUT_HEAD(ether_line_bad_2);
72 ATF_TC_BODY(ether_line_bad_2, tc)
73 {
74 	struct ether_addr e;
75 	char hostname[256];
76 
77 	ATF_REQUIRE_MSG(ether_line(ether_line_bad_2_string, &e, hostname) != 0,
78 	    "ether_line succeeded unexpectedly");
79 }
80 
81 static const char *ether_aton_string = "01:23:45:67:89:ab";
82 static const struct ether_addr	 ether_aton_addr = {
83 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
84 };
85 
86 ATF_TC_WITHOUT_HEAD(ether_aton_r);
87 ATF_TC_BODY(ether_aton_r, tc)
88 {
89 	struct ether_addr e, *ep;
90 
91 	ep = ether_aton_r(ether_aton_string, &e);
92 
93 	ATF_REQUIRE_MSG(ep != NULL, "ether_aton_r failed; errno=%d", errno);
94 	ATF_REQUIRE_MSG(ep == &e,
95 	    "ether_aton_r returned different pointers; %p != %p", ep, &e);
96 }
97 
98 static const char		*ether_aton_bad_string = "x";
99 
100 ATF_TC_WITHOUT_HEAD(ether_aton_r_bad);
101 ATF_TC_BODY(ether_aton_r_bad, tc)
102 {
103 	struct ether_addr e, *ep;
104 
105 	ep = ether_aton_r(ether_aton_bad_string, &e);
106 	ATF_REQUIRE_MSG(ep == NULL, "ether_aton_r succeeded unexpectedly");
107 }
108 
109 ATF_TC_WITHOUT_HEAD(ether_aton);
110 ATF_TC_BODY(ether_aton, tc)
111 {
112 	struct ether_addr *ep;
113 
114 	ep = ether_aton(ether_aton_string);
115 	ATF_REQUIRE_MSG(ep != NULL, "ether_aton failed");
116 	ATF_REQUIRE_MSG(bcmp(ep, &ether_aton_addr, ETHER_ADDR_LEN) == 0,
117 	    "bad address");
118 }
119 
120 ATF_TC_WITHOUT_HEAD(ether_aton_bad);
121 ATF_TC_BODY(ether_aton_bad, tc)
122 {
123 	struct ether_addr *ep;
124 
125 	ep = ether_aton(ether_aton_bad_string);
126 	ATF_REQUIRE_MSG(ep == NULL, "ether_aton succeeded unexpectedly");
127 }
128 
129 static const char *ether_ntoa_string = "01:23:45:67:89:ab";
130 static const struct ether_addr	 ether_ntoa_addr = {
131 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
132 };
133 
134 ATF_TC_WITHOUT_HEAD(ether_ntoa_r);
135 ATF_TC_BODY(ether_ntoa_r, tc)
136 {
137 	char buf[256], *cp;
138 
139 	cp = ether_ntoa_r(&ether_ntoa_addr, buf);
140 	ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa_r failed");
141 	ATF_REQUIRE_MSG(cp == buf,
142 	    "ether_ntoa_r returned a different pointer; %p != %p", cp, buf);
143 	ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0,
144 	    "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string);
145 }
146 
147 ATF_TC_WITHOUT_HEAD(ether_ntoa);
148 ATF_TC_BODY(ether_ntoa, tc)
149 {
150 	char *cp;
151 
152 	cp = ether_ntoa(&ether_ntoa_addr);
153 	ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa failed");
154 	ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0,
155 	    "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string);
156 }
157 
158 #if 0
159 ATF_TC_WITHOUT_HEAD(ether_ntohost);
160 ATF_TC_BODY(ether_ntohost, tc)
161 {
162 
163 }
164 
165 ATF_TC_WITHOUT_HEAD(ether_hostton);
166 ATF_TC_BODY(ether_hostton, tc)
167 {
168 
169 }
170 #endif
171 
172 ATF_TP_ADD_TCS(tp)
173 {
174 
175 	ATF_TP_ADD_TC(tp, ether_line);
176 	ATF_TP_ADD_TC(tp, ether_line_bad_1);
177 	ATF_TP_ADD_TC(tp, ether_line_bad_2);
178 	ATF_TP_ADD_TC(tp, ether_aton_r);
179 	ATF_TP_ADD_TC(tp, ether_aton_r_bad);
180 	ATF_TP_ADD_TC(tp, ether_aton);
181 	ATF_TP_ADD_TC(tp, ether_aton_bad);
182 	ATF_TP_ADD_TC(tp, ether_ntoa_r);
183 	ATF_TP_ADD_TC(tp, ether_ntoa);
184 #if 0
185 	ATF_TP_ADD_TC(tp, ether_ntohost);
186 	ATF_TP_ADD_TC(tp, ether_hostton);
187 #endif
188 
189 	return (atf_no_error());
190 }
191