Module Name:    src
Committed By:   jruoho
Date:           Fri Jul 15 06:41:30 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/lib/libc/net: Makefile
Added Files:
        src/tests/lib/libc/net: t_getprotoent.c

Log Message:
Few naive unit tests for the getprotoent(3) family.


To generate a diff of this commit:
cvs rdiff -u -r1.373 -r1.374 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.4 -r1.5 src/tests/lib/libc/net/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/net/t_getprotoent.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.373 src/distrib/sets/lists/tests/mi:1.374
--- src/distrib/sets/lists/tests/mi:1.373	Fri Jul  8 06:38:03 2011
+++ src/distrib/sets/lists/tests/mi	Fri Jul 15 06:41:29 2011
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.373 2011/07/08 06:38:03 jruoho Exp $
+# $NetBSD: mi,v 1.374 2011/07/15 06:41:29 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -406,6 +406,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/net/h_nsd_recurse.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/net/h_protoent.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/net/h_servent.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/net/t_getprotoent.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/regex				tests-lib-debug
 ./usr/libdata/debug/usr/tests/lib/libc/regex/h_regex.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/rpc				tests-lib-debug
@@ -1952,6 +1953,7 @@
 ./usr/tests/lib/libc/net/h_nsd_recurse		tests-lib-tests	atf
 ./usr/tests/lib/libc/net/h_protoent		tests-lib-tests		atf
 ./usr/tests/lib/libc/net/h_servent		tests-lib-tests		atf
+./usr/tests/lib/libc/net/t_getprotoent		tests-lib-tests		atf
 ./usr/tests/lib/libc/net/t_nsdispatch		tests-lib-tests		atf
 ./usr/tests/lib/libc/net/t_protoent		tests-lib-tests		atf
 ./usr/tests/lib/libc/net/t_servent		tests-lib-tests		atf

Index: src/tests/lib/libc/net/Makefile
diff -u src/tests/lib/libc/net/Makefile:1.4 src/tests/lib/libc/net/Makefile:1.5
--- src/tests/lib/libc/net/Makefile:1.4	Thu Jan 13 02:24:51 2011
+++ src/tests/lib/libc/net/Makefile	Fri Jul 15 06:41:29 2011
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.4 2011/01/13 02:24:51 pgoyette Exp $
+# $NetBSD: Makefile,v 1.5 2011/07/15 06:41:29 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -8,6 +8,8 @@
 
 TESTSDIR=	${TESTSBASE}/lib/libc/net
 
+TESTS_C+=	t_getprotoent
+
 TESTS_SH+=	t_nsdispatch
 TESTS_SH+=	t_protoent
 TESTS_SH+=	t_servent

Added files:

Index: src/tests/lib/libc/net/t_getprotoent.c
diff -u /dev/null src/tests/lib/libc/net/t_getprotoent.c:1.1
--- /dev/null	Fri Jul 15 06:41:30 2011
+++ src/tests/lib/libc/net/t_getprotoent.c	Fri Jul 15 06:41:29 2011
@@ -0,0 +1,233 @@
+/* $NetBSD: t_getprotoent.c,v 1.1 2011/07/15 06:41:29 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * 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.
+ *
+ * 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: t_getprotoent.c,v 1.1 2011/07/15 06:41:29 jruoho Exp $");
+
+#include <atf-c.h>
+#include <netdb.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+static const struct {
+	const char *name;
+	int	    number;
+} protos[] = {
+
+	{ "icmp", 1 },	{ "tcp", 6 },	{ "udp", 17 },	{ "gre", 47 },
+	{ "esp", 50 },	{ "ah", 51 },	{ "sctp", 132},	{ "ipv6-icmp", 58 }
+};
+
+ATF_TC(endprotoent_rewind);
+ATF_TC_HEAD(endprotoent_rewind, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Check that endprotoent(3) rewinds");
+}
+
+ATF_TC_BODY(endprotoent_rewind, tc)
+{
+	struct protoent *p;
+	int i = 0;
+
+	setprotoent(0);
+
+	while ((p = getprotoent()) != NULL && i <= 10) {
+		ATF_REQUIRE(p->p_proto == i);
+		i++;
+	}
+
+	i = 0;
+	endprotoent();
+
+	while ((p = getprotoent()) != NULL && i <= 10) {
+		ATF_REQUIRE(p->p_proto == i);
+		i++;
+	}
+
+	endprotoent();
+}
+
+ATF_TC(getprotobyname_basic);
+ATF_TC_HEAD(getprotobyname_basic, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A naive test of getprotobyname(3)");
+}
+
+ATF_TC_BODY(getprotobyname_basic, tc)
+{
+	struct protoent *p;
+	size_t i;
+
+	for (i = 0; i < __arraycount(protos); i++) {
+
+		p = getprotobyname(protos[i].name);
+
+		ATF_REQUIRE(p != NULL);
+		ATF_REQUIRE(p->p_proto == protos[i].number);
+		ATF_REQUIRE(strcmp(p->p_name, protos[i].name) == 0);
+	}
+
+	endprotoent();
+}
+
+ATF_TC(getprotobyname_err);
+ATF_TC_HEAD(getprotobyname_err, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test EOF from getprotobyname(3)");
+}
+
+ATF_TC_BODY(getprotobyname_err, tc)
+{
+	static const char * name[] =
+	    { "xxx", "yyy", "xyz", ".as.d}9x.._?!!#�,�^//&%%,",
+	      "0", "", "tCp", "uDp", "t c p", "tcp ", " tcp" };
+
+	size_t i;
+
+	for (i = 0; i < __arraycount(name); i++)
+		ATF_REQUIRE(getprotobyname(name[i]) == NULL);
+
+	endprotoent();
+}
+
+ATF_TC(getprotobynumber_basic);
+ATF_TC_HEAD(getprotobynumber_basic, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A naive test of getprotobynumber(3)");
+}
+
+ATF_TC_BODY(getprotobynumber_basic, tc)
+{
+	struct protoent *p;
+	size_t i;
+
+	/*
+	 * No ATF_CHECK() due static storage.
+	 */
+	for (i = 0; i < __arraycount(protos); i++) {
+
+		p = getprotobynumber(protos[i].number);
+
+		ATF_REQUIRE(p != NULL);
+		ATF_REQUIRE(p->p_proto == protos[i].number);
+		ATF_REQUIRE(strcmp(p->p_name, protos[i].name) == 0);
+	}
+
+	endprotoent();
+}
+
+ATF_TC(getprotobynumber_err);
+ATF_TC_HEAD(getprotobynumber_err, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test EOF from getprotobynumber(3)");
+}
+
+ATF_TC_BODY(getprotobynumber_err, tc)
+{
+	static const int number[] = { -1, -99999, INT_MAX, 1000000000 };
+	size_t i;
+
+	for (i = 0; i < __arraycount(number); i++)
+		ATF_REQUIRE(getprotobynumber(number[i]) == NULL);
+
+	endprotoent();
+}
+
+ATF_TC(getprotoent_next);
+ATF_TC_HEAD(getprotoent_next, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "getprotoent(3) returns next line?");
+}
+
+ATF_TC_BODY(getprotoent_next, tc)
+{
+	struct protoent *p;
+	int i = 0;
+
+	/*
+	 * The range [0, 60] is already reserved by IANA.
+	 */
+	while ((p = getprotoent()) != NULL && i <= 60) {
+		ATF_CHECK(p->p_proto == i);
+		i++;
+	}
+
+	endprotoent();
+}
+
+ATF_TC(setprotoent_rewind);
+ATF_TC_HEAD(setprotoent_rewind, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Check that setprotoent(3) rewinds");
+}
+
+ATF_TC_BODY(setprotoent_rewind, tc)
+{
+	struct protoent *p;
+
+	setprotoent(0);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 0);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 1);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 2);
+
+	setprotoent(0);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 0);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 1);
+
+	p = getprotoent();
+	ATF_REQUIRE(p->p_proto == 2);
+
+	endprotoent();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, getprotobyname_basic);
+	ATF_TP_ADD_TC(tp, getprotobyname_err);
+	ATF_TP_ADD_TC(tp, getprotobynumber_basic);
+	ATF_TP_ADD_TC(tp, getprotobynumber_err);
+	ATF_TP_ADD_TC(tp, endprotoent_rewind);
+	ATF_TP_ADD_TC(tp, getprotoent_next);
+	ATF_TP_ADD_TC(tp, setprotoent_rewind);
+
+	return atf_no_error();
+}

Reply via email to