regression test for ether_aton_r()

This commit is contained in:
christos 2010-05-19 21:55:35 +00:00
parent d0348fc0c0
commit f65592ae19
4 changed files with 150 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# $NetBSD: Makefile,v 1.1 2005/12/14 23:55:02 rpaulo Exp $
# $NetBSD: Makefile,v 1.2 2010/05/19 21:55:35 christos Exp $
.include <bsd.own.mk>
SUBDIR= bpf
SUBDIR= bpf ether_aton_r
.include <bsd.subdir.mk>

View File

@ -0,0 +1,26 @@
# $NetBSD: Makefile,v 1.1 2010/05/19 21:55:36 christos Exp $
#
# To use this, copy / link the cpu_in_cksum.S from the arch subdirectory
# and copy assym.h from the build directory of a kernel.
#
NOMAN= # defined
.include <bsd.own.mk>
PROG= ether_aton_r
SRCS= ether_aton_r.c main.c
WARNS ?= 4
CLEANFILES+=ether_aton_r.c
ether_aton_r.c: autogen ${NETBSDSRCDIR}/sys/net/if_ethersubr.c
${.ALLSRC} ${.TARGET}
regress:
@if ./${PROG} ; then \
echo "PASSED"; \
else \
echo "FAILED"; \
fi
.include <bsd.prog.mk>

View File

@ -0,0 +1,26 @@
#!/bin/sh
cat << __EOF > $2
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>
#define ETHER_ADDR_LEN 6
int ether_aton_r(u_char *dest, size_t len, const char *str);
__EOF
ed $1 << _EOF > /dev/null 2>&1
1
/^ether_aton_r/
-
-
1,.d
/^}$/
+
.,\$d
W $2
q
_EOF

View File

@ -0,0 +1,96 @@
/* $NetBSD: main.c,v 1.1 2010/05/19 21:55:36 christos Exp $ */
/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: main.c,v 1.1 2010/05/19 21:55:36 christos Exp $");
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <err.h>
#include <string.h>
#include <errno.h>
#define ETHER_ADDR_LEN 6
int ether_aton_r(u_char *dest, size_t len, const char *str);
int
main(int argc, char *argv[])
{
static const struct {
u_char res[ETHER_ADDR_LEN];
const char *str;
int error;
} tests[] = {
#define ZERO { 0, 0, 0, 0, 0, 0 }
{ { 0, 1, 0x22, 3, 0x14, 5 }, "0:1:22-3:14:05", 0 },
{ { 0, 1, 0x22, 3, 0x14, 5 }, "000122031405", 0 },
{ ZERO, "0:1:2-3:04:05:06", ENAMETOOLONG },
{ ZERO, "0:1:2-3:04:", ENOBUFS },
{ ZERO, "0:1:2-3:04:x7", EINVAL },
{ ZERO, "1:x-3:04:05:7", EINVAL },
{ ZERO, NULL, 0 },
};
u_char dest[ETHER_ADDR_LEN];
size_t t;
int e, r;
const char *s;
int error = 0;
for (t = 0; tests[t].str; t++) {
s = tests[t].str;
if ((e = tests[t].error) == 0) {
if (ether_aton_r(dest, sizeof(dest), s) != e) {
error++;
warnx("ether_aton_r failed on `%s'", s);
}
if (memcmp(dest, tests[t].res, sizeof(dest)) != 0) {
error++;
warnx("ether_aton_r unexpected result on `%s'",
s);
}
} else {
if ((r = ether_aton_r(dest, sizeof(dest), s)) != e) {
error++;
warnx("ether_aton_r succeeded on `%s' "
"(%d != %d)", s, r, e);
}
}
}
return error;
}