1272343Sngie/*	$NetBSD: t_ether_aton.c,v 1.1 2011/11/01 22:36:53 pgoyette Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie * 3. All advertising materials mentioning features or use of this software
19272343Sngie *    must display the following acknowledgement:
20272343Sngie *        This product includes software developed by the NetBSD
21272343Sngie *        Foundation, Inc. and its contributors.
22272343Sngie * 4. Neither the name of The NetBSD Foundation nor the names of its
23272343Sngie *    contributors may be used to endorse or promote products derived
24272343Sngie *    from this software without specific prior written permission.
25272343Sngie *
26272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36272343Sngie * POSSIBILITY OF SUCH DAMAGE.
37272343Sngie */
38272343Sngie#include <sys/cdefs.h>
39272343Sngie__RCSID("$NetBSD: t_ether_aton.c,v 1.1 2011/11/01 22:36:53 pgoyette Exp $");
40272343Sngie
41272343Sngie#include <atf-c.h>
42272343Sngie#include <stdio.h>
43272343Sngie#include <ctype.h>
44272343Sngie#include <sys/types.h>
45272343Sngie#include <err.h>
46272343Sngie#include <string.h>
47272343Sngie#include <errno.h>
48272343Sngie
49276478Sngie#ifndef __NetBSD__
50276478Sngie#ifdef __linux__
51276478Sngie#include <netinet/ether.h>
52276478Sngie#endif
53276478Sngie#include <net/ethernet.h>
54276478Sngie#endif
55276478Sngie
56276478Sngie#ifdef __NetBSD__
57272343Sngie#define ETHER_ADDR_LEN 6
58272343Sngie
59272343Sngieint ether_aton_r(u_char *dest, size_t len, const char *str);
60276478Sngie#endif
61272343Sngie
62272343Sngiestatic const struct {
63272343Sngie	u_char res[ETHER_ADDR_LEN];
64272343Sngie	const char *str;
65272343Sngie	int error;
66272343Sngie} tests[] = {
67272343Sngie	{ { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }, "01:23:45:67:89:ab", 0 },
68276478Sngie#ifdef __NetBSD__
69272343Sngie	{ { 0x00, 0x01, 0x22, 0x03, 0x14, 0x05 }, "0:1:22-3:14:05", 0 },
70272343Sngie	{ { 0x00, 0x01, 0x22, 0x03, 0x14, 0x05 }, "000122031405", 0 },
71272343Sngie	{ { 0x0a, 0x0B, 0xcc, 0xdD, 0xEE, 0x0f }, "0a0BccdDEE0f", 0 },
72276478Sngie#endif
73272343Sngie#define ZERO { 0, 0, 0, 0, 0, 0 }
74272343Sngie	{ ZERO,	"0:1:2-3:04:05:06", ENAMETOOLONG },
75272343Sngie	{ ZERO,	"0:1:2-3:04:", ENOBUFS },
76272343Sngie	{ ZERO,	"0:1:2-3:04:x7", EINVAL },
77272343Sngie	{ ZERO,	"1:x-3:04:05:7", EINVAL },
78272343Sngie	{ ZERO,	NULL, 0 },
79272343Sngie};
80272343Sngie
81272343SngieATF_TC(tc_ether_aton);
82272343SngieATF_TC_HEAD(tc_ether_aton, tc)
83272343Sngie{
84272343Sngie	atf_tc_set_md_var(tc, "descr", "Check that ether_aton(3) works");
85272343Sngie}
86272343Sngie
87272343SngieATF_TC_BODY(tc_ether_aton, tc)
88272343Sngie{
89276478Sngie#ifdef __NetBSD__
90272343Sngie	u_char dest[ETHER_ADDR_LEN];
91276478Sngie#else
92276478Sngie	struct ether_addr dest;
93276478Sngie#endif
94272343Sngie	size_t t;
95276478Sngie#ifdef __NetBSD__
96272343Sngie	int e, r;
97276478Sngie#else
98276478Sngie	int e;
99276478Sngie	struct ether_addr *r;
100276478Sngie#endif
101272343Sngie	const char *s;
102272343Sngie
103272343Sngie	for (t = 0; tests[t].str; t++) {
104272343Sngie		s = tests[t].str;
105272343Sngie		if ((e = tests[t].error) == 0) {
106276478Sngie#ifdef __NetBSD__
107272343Sngie			if (ether_aton_r(dest, sizeof(dest), s) != e)
108272343Sngie				atf_tc_fail("failed on `%s'", s);
109272343Sngie			if (memcmp(dest, tests[t].res, sizeof(dest)) != 0)
110272343Sngie				atf_tc_fail("unexpected result on `%s'", s);
111276478Sngie#else
112276478Sngie			if (ether_aton_r(s, &dest) == NULL && e == 0)
113276478Sngie				atf_tc_fail("failed on `%s'", s);
114276478Sngie			if (memcmp(&dest, tests[t].res, sizeof(dest)) != 0)
115276478Sngie				atf_tc_fail("unexpected result on `%s'", s);
116276478Sngie#endif
117272343Sngie		} else {
118276478Sngie#ifdef __NetBSD__
119272343Sngie			if ((r = ether_aton_r(dest, sizeof(dest), s)) != e)
120272343Sngie				atf_tc_fail("unexpectedly succeeded on `%s' "
121272343Sngie				    "(%d != %d)", s, r, e);
122276478Sngie#else
123276478Sngie			if ((r = ether_aton_r(s, &dest)) != NULL && e != 0)
124276478Sngie				atf_tc_fail("unexpectedly succeeded on `%s' "
125276478Sngie				    "(%p != %d)", s, r, e);
126276478Sngie#endif
127272343Sngie		}
128272343Sngie	}
129272343Sngie}
130272343Sngie
131272343SngieATF_TP_ADD_TCS(tp)
132272343Sngie{
133272343Sngie
134272343Sngie	ATF_TP_ADD_TC(tp, tc_ether_aton);
135272343Sngie
136272343Sngie        return atf_no_error();
137272343Sngie}
138