1 /* $NetBSD: t_getprotoent.c,v 1.2 2012/04/04 10:03:53 joerg Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getprotoent.c,v 1.2 2012/04/04 10:03:53 joerg Exp $");
45 { "icmp", 1 }, { "tcp", 6 }, { "udp", 17 }, { "gre", 47 },
46 { "esp", 50 }, { "ah", 51 }, { "sctp", 132}, { "ipv6-icmp", 58 }
49 ATF_TC(endprotoent_rewind
);
50 ATF_TC_HEAD(endprotoent_rewind
, tc
)
52 atf_tc_set_md_var(tc
, "descr", "Check that endprotoent(3) rewinds");
55 ATF_TC_BODY(endprotoent_rewind
, tc
)
62 while ((p
= getprotoent()) != NULL
&& i
<= 10) {
63 ATF_REQUIRE(p
->p_proto
== i
);
70 while ((p
= getprotoent()) != NULL
&& i
<= 10) {
71 ATF_REQUIRE(p
->p_proto
== i
);
78 ATF_TC(getprotobyname_basic
);
79 ATF_TC_HEAD(getprotobyname_basic
, tc
)
81 atf_tc_set_md_var(tc
, "descr", "A naive test of getprotobyname(3)");
84 ATF_TC_BODY(getprotobyname_basic
, tc
)
89 for (i
= 0; i
< __arraycount(protos
); i
++) {
91 p
= getprotobyname(protos
[i
].name
);
93 ATF_REQUIRE(p
!= NULL
);
94 ATF_REQUIRE(p
->p_proto
== protos
[i
].number
);
95 ATF_REQUIRE(strcmp(p
->p_name
, protos
[i
].name
) == 0);
101 ATF_TC(getprotobyname_err
);
102 ATF_TC_HEAD(getprotobyname_err
, tc
)
104 atf_tc_set_md_var(tc
, "descr", "Test EOF from getprotobyname(3)");
107 ATF_TC_BODY(getprotobyname_err
, tc
)
109 static const char * name
[] =
110 { "xxx", "yyy", "xyz", ".as.d}9x.._?!!#\xa4,\xa8^//&%%,",
111 "0", "", "tCp", "uDp", "t c p", "tcp ", " tcp" };
115 for (i
= 0; i
< __arraycount(name
); i
++)
116 ATF_REQUIRE(getprotobyname(name
[i
]) == NULL
);
121 ATF_TC(getprotobynumber_basic
);
122 ATF_TC_HEAD(getprotobynumber_basic
, tc
)
124 atf_tc_set_md_var(tc
, "descr", "A naive test of getprotobynumber(3)");
127 ATF_TC_BODY(getprotobynumber_basic
, tc
)
133 * No ATF_CHECK() due static storage.
135 for (i
= 0; i
< __arraycount(protos
); i
++) {
137 p
= getprotobynumber(protos
[i
].number
);
139 ATF_REQUIRE(p
!= NULL
);
140 ATF_REQUIRE(p
->p_proto
== protos
[i
].number
);
141 ATF_REQUIRE(strcmp(p
->p_name
, protos
[i
].name
) == 0);
147 ATF_TC(getprotobynumber_err
);
148 ATF_TC_HEAD(getprotobynumber_err
, tc
)
150 atf_tc_set_md_var(tc
, "descr", "Test EOF from getprotobynumber(3)");
153 ATF_TC_BODY(getprotobynumber_err
, tc
)
155 static const int number
[] = { -1, -99999, INT_MAX
, 1000000000 };
158 for (i
= 0; i
< __arraycount(number
); i
++)
159 ATF_REQUIRE(getprotobynumber(number
[i
]) == NULL
);
164 ATF_TC(getprotoent_next
);
165 ATF_TC_HEAD(getprotoent_next
, tc
)
167 atf_tc_set_md_var(tc
, "descr", "getprotoent(3) returns next line?");
170 ATF_TC_BODY(getprotoent_next
, tc
)
176 * The range [0, 60] is already reserved by IANA.
178 while ((p
= getprotoent()) != NULL
&& i
<= 60) {
179 ATF_CHECK(p
->p_proto
== i
);
186 ATF_TC(setprotoent_rewind
);
187 ATF_TC_HEAD(setprotoent_rewind
, tc
)
189 atf_tc_set_md_var(tc
, "descr", "Check that setprotoent(3) rewinds");
192 ATF_TC_BODY(setprotoent_rewind
, tc
)
199 ATF_REQUIRE(p
->p_proto
== 0);
202 ATF_REQUIRE(p
->p_proto
== 1);
205 ATF_REQUIRE(p
->p_proto
== 2);
210 ATF_REQUIRE(p
->p_proto
== 0);
213 ATF_REQUIRE(p
->p_proto
== 1);
216 ATF_REQUIRE(p
->p_proto
== 2);
224 ATF_TP_ADD_TC(tp
, getprotobyname_basic
);
225 ATF_TP_ADD_TC(tp
, getprotobyname_err
);
226 ATF_TP_ADD_TC(tp
, getprotobynumber_basic
);
227 ATF_TP_ADD_TC(tp
, getprotobynumber_err
);
228 ATF_TP_ADD_TC(tp
, endprotoent_rewind
);
229 ATF_TP_ADD_TC(tp
, getprotoent_next
);
230 ATF_TP_ADD_TC(tp
, setprotoent_rewind
);
232 return atf_no_error();