xref: /netbsd-src/tests/lib/libc/gen/t_assert.c (revision 473b6e44f3e893f83d25f01ccab5ecd9fceda2aa)
1*473b6e44Schristos /* $NetBSD: t_assert.c,v 1.3 2017/01/10 15:17:57 christos Exp $ */
2280c25b2Sjruoho 
3280c25b2Sjruoho /*-
4280c25b2Sjruoho  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5280c25b2Sjruoho  * All rights reserved.
6280c25b2Sjruoho  *
7280c25b2Sjruoho  * This code is derived from software contributed to The NetBSD Foundation
8280c25b2Sjruoho  * by Jukka Ruohonen.
9280c25b2Sjruoho  *
10280c25b2Sjruoho  * Redistribution and use in source and binary forms, with or without
11280c25b2Sjruoho  * modification, are permitted provided that the following conditions
12280c25b2Sjruoho  * are met:
13280c25b2Sjruoho  * 1. Redistributions of source code must retain the above copyright
14280c25b2Sjruoho  *    notice, this list of conditions and the following disclaimer.
15280c25b2Sjruoho  * 2. Redistributions in binary form must reproduce the above copyright
16280c25b2Sjruoho  *    notice, this list of conditions and the following disclaimer in the
17280c25b2Sjruoho  *    documentation and/or other materials provided with the distribution.
18280c25b2Sjruoho  *
19280c25b2Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20280c25b2Sjruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21280c25b2Sjruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22280c25b2Sjruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23280c25b2Sjruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24280c25b2Sjruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25280c25b2Sjruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26280c25b2Sjruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27280c25b2Sjruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28280c25b2Sjruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29280c25b2Sjruoho  * POSSIBILITY OF SUCH DAMAGE.
30280c25b2Sjruoho  */
31280c25b2Sjruoho #include <sys/cdefs.h>
32*473b6e44Schristos __RCSID("$NetBSD: t_assert.c,v 1.3 2017/01/10 15:17:57 christos Exp $");
33280c25b2Sjruoho 
34*473b6e44Schristos #include <sys/types.h>
35*473b6e44Schristos #include <sys/resource.h>
36*473b6e44Schristos #include <sys/time.h>
37280c25b2Sjruoho #include <sys/wait.h>
38280c25b2Sjruoho 
39280c25b2Sjruoho #include <assert.h>
40280c25b2Sjruoho #include <atf-c.h>
41280c25b2Sjruoho #include <signal.h>
42280c25b2Sjruoho #include <stdlib.h>
43280c25b2Sjruoho #include <string.h>
44280c25b2Sjruoho #include <unistd.h>
45280c25b2Sjruoho 
46*473b6e44Schristos static void
disable_corefile(void)47*473b6e44Schristos disable_corefile(void)
48*473b6e44Schristos {
49*473b6e44Schristos 	struct rlimit limits;
50*473b6e44Schristos 
51*473b6e44Schristos 	limits.rlim_cur = 0;
52*473b6e44Schristos 	limits.rlim_max = 0;
53*473b6e44Schristos 
54*473b6e44Schristos 	ATF_REQUIRE(setrlimit(RLIMIT_CORE, &limits) == 0);
55*473b6e44Schristos }
56*473b6e44Schristos 
57280c25b2Sjruoho static void		handler(int);
58280c25b2Sjruoho 
59280c25b2Sjruoho static void
handler(int signo)60280c25b2Sjruoho handler(int signo)
61280c25b2Sjruoho {
62280c25b2Sjruoho 	/* Nothing. */
63280c25b2Sjruoho }
64280c25b2Sjruoho 
65280c25b2Sjruoho ATF_TC(assert_false);
ATF_TC_HEAD(assert_false,tc)66280c25b2Sjruoho ATF_TC_HEAD(assert_false, tc)
67280c25b2Sjruoho {
68280c25b2Sjruoho 	atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #1");
69280c25b2Sjruoho }
70280c25b2Sjruoho 
ATF_TC_BODY(assert_false,tc)71280c25b2Sjruoho ATF_TC_BODY(assert_false, tc)
72280c25b2Sjruoho {
73280c25b2Sjruoho 	struct sigaction sa;
74280c25b2Sjruoho 	pid_t pid;
75280c25b2Sjruoho 	int sta;
76280c25b2Sjruoho 
77280c25b2Sjruoho 	pid = fork();
78280c25b2Sjruoho 	ATF_REQUIRE(pid >= 0);
79280c25b2Sjruoho 
80280c25b2Sjruoho 	if (pid == 0) {
81280c25b2Sjruoho 
82*473b6e44Schristos 		disable_corefile();
83280c25b2Sjruoho 		(void)closefrom(0);
84280c25b2Sjruoho 		(void)memset(&sa, 0, sizeof(struct sigaction));
85280c25b2Sjruoho 
86280c25b2Sjruoho 		sa.sa_flags = 0;
87280c25b2Sjruoho 		sa.sa_handler = handler;
88280c25b2Sjruoho 
89519d4f30Sjruoho 		(void)sigemptyset(&sa.sa_mask);
90519d4f30Sjruoho 		(void)sigaction(SIGABRT, &sa, 0);
91280c25b2Sjruoho 
92280c25b2Sjruoho 		assert(1 == 1);
93280c25b2Sjruoho 
94280c25b2Sjruoho 		_exit(EXIT_SUCCESS);
95280c25b2Sjruoho 	}
96280c25b2Sjruoho 
97280c25b2Sjruoho 	(void)wait(&sta);
98280c25b2Sjruoho 
99280c25b2Sjruoho 	if (WIFSIGNALED(sta) != 0 || WIFEXITED(sta) == 0)
100280c25b2Sjruoho 		atf_tc_fail("assert(3) fired haphazardly");
101280c25b2Sjruoho }
102280c25b2Sjruoho 
103280c25b2Sjruoho ATF_TC(assert_true);
ATF_TC_HEAD(assert_true,tc)104280c25b2Sjruoho ATF_TC_HEAD(assert_true, tc)
105280c25b2Sjruoho {
106280c25b2Sjruoho 	atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #2");
107280c25b2Sjruoho }
108280c25b2Sjruoho 
ATF_TC_BODY(assert_true,tc)109280c25b2Sjruoho ATF_TC_BODY(assert_true, tc)
110280c25b2Sjruoho {
111280c25b2Sjruoho 	struct sigaction sa;
112280c25b2Sjruoho 	pid_t pid;
113280c25b2Sjruoho 	int sta;
114280c25b2Sjruoho 
115280c25b2Sjruoho 	pid = fork();
116280c25b2Sjruoho 	ATF_REQUIRE(pid >= 0);
117280c25b2Sjruoho 
118280c25b2Sjruoho 	if (pid == 0) {
119280c25b2Sjruoho 
120*473b6e44Schristos 		disable_corefile();
121280c25b2Sjruoho 		(void)closefrom(0);
122280c25b2Sjruoho 		(void)memset(&sa, 0, sizeof(struct sigaction));
123280c25b2Sjruoho 
124280c25b2Sjruoho 		sa.sa_flags = 0;
125280c25b2Sjruoho 		sa.sa_handler = handler;
126280c25b2Sjruoho 
127519d4f30Sjruoho 		(void)sigemptyset(&sa.sa_mask);
128519d4f30Sjruoho 		(void)sigaction(SIGABRT, &sa, 0);
129280c25b2Sjruoho 
130280c25b2Sjruoho 		assert(1 == 2);
131280c25b2Sjruoho 
132280c25b2Sjruoho 		_exit(EXIT_SUCCESS);
133280c25b2Sjruoho 	}
134280c25b2Sjruoho 
135280c25b2Sjruoho 	(void)wait(&sta);
136280c25b2Sjruoho 
137280c25b2Sjruoho 	if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGABRT)
138280c25b2Sjruoho 		atf_tc_fail("assert(3) did not fire");
139280c25b2Sjruoho }
140280c25b2Sjruoho 
ATF_TP_ADD_TCS(tp)141280c25b2Sjruoho ATF_TP_ADD_TCS(tp)
142280c25b2Sjruoho {
143280c25b2Sjruoho 
144280c25b2Sjruoho 	ATF_TP_ADD_TC(tp, assert_false);
145280c25b2Sjruoho 	ATF_TP_ADD_TC(tp, assert_true);
146280c25b2Sjruoho 
147280c25b2Sjruoho 	return atf_no_error();
148280c25b2Sjruoho }
149