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 <stdio.h>
34#include <string.h>
35
36static int testnum;
37
38#define	OK()	do {							\
39	printf("ok %d %s\n", testnum, __func__);			\
40	return;								\
41} while (0)
42
43#define	NOTOK(why)	do {						\
44	printf("not ok %d %s # %s\n", testnum, __func__, why);		\
45	return;								\
46} while (0)
47
48#define	TODO()	NOTOK("TODO")
49
50static const char		*ether_line_string =
51				    "01:23:45:67:89:ab ether_line_hostname";
52static const char		*ether_line_hostname = "ether_line_hostname";
53static const struct ether_addr	 ether_line_addr = {
54	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
55};
56
57static void
58test_ether_line(void)
59{
60	struct ether_addr e;
61	char hostname[256];
62
63	testnum++;
64	if (ether_line(ether_line_string, &e, hostname) != 0)
65		NOTOK("returned error");
66	if (bcmp(&e, &ether_line_addr, ETHER_ADDR_LEN) != 0)
67		NOTOK("bad address");
68	if (strcmp(hostname, ether_line_hostname) != 0) {
69		printf("hostname: %s\n", hostname);
70		NOTOK("bad hostname");
71	}
72	OK();
73}
74
75static const char		*ether_line_bad_1_string = "x";
76
77static void
78test_ether_line_bad_1(void)
79{
80	struct ether_addr e;
81	char hostname[256];
82
83	testnum++;
84	if (ether_line(ether_line_bad_1_string, &e, hostname) == 0)
85		NOTOK("returned success");
86	OK();
87}
88
89static const char		*ether_line_bad_2_string = "x x";
90
91static void
92test_ether_line_bad_2(void)
93{
94	struct ether_addr e;
95	char hostname[256];
96
97	testnum++;
98	if (ether_line(ether_line_bad_2_string, &e, hostname) == 0)
99		NOTOK("returned success");
100	OK();
101}
102
103static const char		*ether_aton_string = "01:23:45:67:89:ab";
104static const struct ether_addr	 ether_aton_addr = {
105	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
106};
107
108static void
109test_ether_aton_r(void)
110{
111	struct ether_addr e, *ep;
112
113	testnum++;
114	ep = ether_aton_r(ether_aton_string, &e);
115	if (ep == NULL)
116		NOTOK("returned NULL");
117	if (ep != &e)
118		NOTOK("returned different pointer");
119	if (bcmp(&e, &ether_aton_addr, ETHER_ADDR_LEN) != 0)
120		NOTOK("bad address");
121	OK();
122}
123
124static const char		*ether_aton_bad_string = "x";
125
126static void
127test_ether_aton_r_bad(void)
128{
129	struct ether_addr e, *ep;
130
131	testnum++;
132	ep = ether_aton_r(ether_aton_bad_string, &e);
133	if (ep == &e)
134		NOTOK("returned success");
135	if (ep != NULL)
136		NOTOK("returned different pointer");
137	OK();
138}
139
140static void
141test_ether_aton(void)
142{
143	struct ether_addr *ep;
144
145	testnum++;
146	ep = ether_aton(ether_aton_string);
147	if (ep == NULL)
148		NOTOK("returned NULL");
149	if (bcmp(ep, &ether_aton_addr, ETHER_ADDR_LEN) != 0)
150		NOTOK("bad address");
151	OK();
152}
153
154static void
155test_ether_aton_bad(void)
156{
157	struct ether_addr *ep;
158
159	testnum++;
160	ep = ether_aton(ether_aton_bad_string);
161	if (ep != NULL)
162		NOTOK("returned success");
163	OK();
164}
165
166static const char		*ether_ntoa_string = "01:23:45:67:89:ab";
167static const struct ether_addr	 ether_ntoa_addr = {
168	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
169};
170
171static void
172test_ether_ntoa_r(void)
173{
174	char buf[256], *cp;
175
176	testnum++;
177	cp = ether_ntoa_r(&ether_ntoa_addr, buf);
178	if (cp == NULL)
179		NOTOK("returned NULL");
180	if (cp != buf)
181		NOTOK("returned different pointer");
182	if (strcmp(cp, ether_ntoa_string) != 0)
183		NOTOK("bad string");
184	OK();
185}
186
187static void
188test_ether_ntoa(void)
189{
190	char *cp;
191
192	testnum++;
193	cp = ether_ntoa(&ether_ntoa_addr);
194	if (cp == NULL)
195		NOTOK("returned NULL");
196	if (strcmp(cp, ether_ntoa_string) != 0)
197		NOTOK("bad string");
198	OK();
199}
200
201static void
202test_ether_ntohost(void)
203{
204
205	testnum++;
206	TODO();
207}
208
209static void
210test_ether_hostton(void)
211{
212
213	testnum++;
214	TODO();
215}
216
217int
218main(int argc, char *argv[])
219{
220
221	printf("1..11\n");
222
223	test_ether_line();
224	test_ether_line_bad_1();
225	test_ether_line_bad_2();
226	test_ether_aton_r();
227	test_ether_aton_r_bad();
228	test_ether_aton();
229	test_ether_aton_bad();
230	test_ether_ntoa_r();
231	test_ether_ntoa();
232	test_ether_ntohost();
233	test_ether_hostton();
234	return (0);
235}
236