xref: /freebsd/lib/libc/tests/net/ether_test.c (revision 0957b409)
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  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 
31 #include <net/ethernet.h>
32 
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include <atf-c.h>
38 
39 static const char *ether_line_string = "01:23:45:67:89:ab ether_line_hostname";
40 static const char *ether_line_hostname = "ether_line_hostname";
41 static const struct ether_addr	 ether_line_addr = {
42 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
43 };
44 
45 ATF_TC_WITHOUT_HEAD(ether_line);
46 ATF_TC_BODY(ether_line, tc)
47 {
48 	struct ether_addr e;
49 	char hostname[256];
50 
51 	ATF_REQUIRE_MSG(ether_line(ether_line_string, &e, hostname) == 0,
52 	    "ether_line failed; errno=%d", errno);
53 	ATF_REQUIRE_MSG(bcmp(&e, &ether_line_addr, ETHER_ADDR_LEN) == 0,
54 	    "bad address");
55 	ATF_REQUIRE_MSG(strcmp(hostname, ether_line_hostname) == 0,
56 	    "bad hostname");
57 }
58 
59 static const char *ether_line_bad_1_string = "x";
60 
61 ATF_TC_WITHOUT_HEAD(ether_line_bad_1);
62 ATF_TC_BODY(ether_line_bad_1, tc)
63 {
64 	struct ether_addr e;
65 	char hostname[256];
66 
67 	ATF_REQUIRE_MSG(ether_line(ether_line_bad_1_string, &e, hostname) != 0,
68 	    "ether_line succeeded unexpectedly");
69 }
70 
71 static const char *ether_line_bad_2_string = "x x";
72 
73 ATF_TC_WITHOUT_HEAD(ether_line_bad_2);
74 ATF_TC_BODY(ether_line_bad_2, tc)
75 {
76 	struct ether_addr e;
77 	char hostname[256];
78 
79 	ATF_REQUIRE_MSG(ether_line(ether_line_bad_2_string, &e, hostname) != 0,
80 	    "ether_line succeeded unexpectedly");
81 }
82 
83 static const char *ether_aton_string = "01:23:45:67:89:ab";
84 static const struct ether_addr	 ether_aton_addr = {
85 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
86 };
87 
88 ATF_TC_WITHOUT_HEAD(ether_aton_r);
89 ATF_TC_BODY(ether_aton_r, tc)
90 {
91 	struct ether_addr e, *ep;
92 
93 	ep = ether_aton_r(ether_aton_string, &e);
94 
95 	ATF_REQUIRE_MSG(ep != NULL, "ether_aton_r failed; errno=%d", errno);
96 	ATF_REQUIRE_MSG(ep == &e,
97 	    "ether_aton_r returned different pointers; %p != %p", ep, &e);
98 }
99 
100 static const char		*ether_aton_bad_string = "x";
101 
102 ATF_TC_WITHOUT_HEAD(ether_aton_r_bad);
103 ATF_TC_BODY(ether_aton_r_bad, tc)
104 {
105 	struct ether_addr e, *ep;
106 
107 	ep = ether_aton_r(ether_aton_bad_string, &e);
108 	ATF_REQUIRE_MSG(ep == NULL, "ether_aton_r succeeded unexpectedly");
109 }
110 
111 ATF_TC_WITHOUT_HEAD(ether_aton);
112 ATF_TC_BODY(ether_aton, tc)
113 {
114 	struct ether_addr *ep;
115 
116 	ep = ether_aton(ether_aton_string);
117 	ATF_REQUIRE_MSG(ep != NULL, "ether_aton failed");
118 	ATF_REQUIRE_MSG(bcmp(ep, &ether_aton_addr, ETHER_ADDR_LEN) == 0,
119 	    "bad address");
120 }
121 
122 ATF_TC_WITHOUT_HEAD(ether_aton_bad);
123 ATF_TC_BODY(ether_aton_bad, tc)
124 {
125 	struct ether_addr *ep;
126 
127 	ep = ether_aton(ether_aton_bad_string);
128 	ATF_REQUIRE_MSG(ep == NULL, "ether_aton succeeded unexpectedly");
129 }
130 
131 static const char *ether_ntoa_string = "01:23:45:67:89:ab";
132 static const struct ether_addr	 ether_ntoa_addr = {
133 	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
134 };
135 
136 ATF_TC_WITHOUT_HEAD(ether_ntoa_r);
137 ATF_TC_BODY(ether_ntoa_r, tc)
138 {
139 	char buf[256], *cp;
140 
141 	cp = ether_ntoa_r(&ether_ntoa_addr, buf);
142 	ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa_r failed");
143 	ATF_REQUIRE_MSG(cp == buf,
144 	    "ether_ntoa_r returned a different pointer; %p != %p", cp, buf);
145 	ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0,
146 	    "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string);
147 }
148 
149 ATF_TC_WITHOUT_HEAD(ether_ntoa);
150 ATF_TC_BODY(ether_ntoa, tc)
151 {
152 	char *cp;
153 
154 	cp = ether_ntoa(&ether_ntoa_addr);
155 	ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa failed");
156 	ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0,
157 	    "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string);
158 }
159 
160 #if 0
161 ATF_TC_WITHOUT_HEAD(ether_ntohost);
162 ATF_TC_BODY(ether_ntohost, tc)
163 {
164 
165 }
166 
167 ATF_TC_WITHOUT_HEAD(ether_hostton);
168 ATF_TC_BODY(ether_hostton, tc)
169 {
170 
171 }
172 #endif
173 
174 ATF_TP_ADD_TCS(tp)
175 {
176 
177 	ATF_TP_ADD_TC(tp, ether_line);
178 	ATF_TP_ADD_TC(tp, ether_line_bad_1);
179 	ATF_TP_ADD_TC(tp, ether_line_bad_2);
180 	ATF_TP_ADD_TC(tp, ether_aton_r);
181 	ATF_TP_ADD_TC(tp, ether_aton_r_bad);
182 	ATF_TP_ADD_TC(tp, ether_aton);
183 	ATF_TP_ADD_TC(tp, ether_aton_bad);
184 	ATF_TP_ADD_TC(tp, ether_ntoa_r);
185 	ATF_TP_ADD_TC(tp, ether_ntoa);
186 #if 0
187 	ATF_TP_ADD_TC(tp, ether_ntohost);
188 	ATF_TP_ADD_TC(tp, ether_hostton);
189 #endif
190 
191 	return (atf_no_error());
192 }
193