]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/netbsd-tests/lib/libc/gen/t_assert.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / contrib / netbsd-tests / lib / libc / gen / t_assert.c
1 /* $NetBSD: t_assert.c,v 1.2 2011/06/14 05:28:00 jruoho Exp $ */
2
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_assert.c,v 1.2 2011/06/14 05:28:00 jruoho Exp $");
33
34 #include <sys/wait.h>
35
36 #include <assert.h>
37 #include <atf-c.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #ifdef __FreeBSD__
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47
48 static void
49 disable_corefile(void)
50 {
51         struct rlimit limits;
52
53         limits.rlim_cur = 0;
54         limits.rlim_max = 0;
55
56         ATF_REQUIRE(setrlimit(RLIMIT_CORE, &limits) == 0);
57 }
58 #endif
59
60 static void             handler(int);
61
62 static void
63 handler(int signo)
64 {
65         /* Nothing. */
66 }
67
68 ATF_TC(assert_false);
69 ATF_TC_HEAD(assert_false, tc)
70 {
71         atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #1");
72 }
73
74 ATF_TC_BODY(assert_false, tc)
75 {
76         struct sigaction sa;
77         pid_t pid;
78         int sta;
79
80         pid = fork();
81         ATF_REQUIRE(pid >= 0);
82
83         if (pid == 0) {
84
85 #ifdef __FreeBSD__
86                 disable_corefile();
87 #endif
88                 (void)closefrom(0);
89                 (void)memset(&sa, 0, sizeof(struct sigaction));
90
91                 sa.sa_flags = 0;
92                 sa.sa_handler = handler;
93
94                 (void)sigemptyset(&sa.sa_mask);
95                 (void)sigaction(SIGABRT, &sa, 0);
96
97                 assert(1 == 1);
98
99                 _exit(EXIT_SUCCESS);
100         }
101
102         (void)wait(&sta);
103
104         if (WIFSIGNALED(sta) != 0 || WIFEXITED(sta) == 0)
105                 atf_tc_fail("assert(3) fired haphazardly");
106 }
107
108 ATF_TC(assert_true);
109 ATF_TC_HEAD(assert_true, tc)
110 {
111         atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #2");
112 }
113
114 ATF_TC_BODY(assert_true, tc)
115 {
116         struct sigaction sa;
117         pid_t pid;
118         int sta;
119
120         pid = fork();
121         ATF_REQUIRE(pid >= 0);
122
123         if (pid == 0) {
124
125 #ifdef __FreeBSD__
126                 disable_corefile();
127 #endif
128                 (void)closefrom(0);
129                 (void)memset(&sa, 0, sizeof(struct sigaction));
130
131                 sa.sa_flags = 0;
132                 sa.sa_handler = handler;
133
134                 (void)sigemptyset(&sa.sa_mask);
135                 (void)sigaction(SIGABRT, &sa, 0);
136
137                 assert(1 == 2);
138
139                 _exit(EXIT_SUCCESS);
140         }
141
142         (void)wait(&sta);
143
144         if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGABRT)
145                 atf_tc_fail("assert(3) did not fire");
146 }
147
148 ATF_TP_ADD_TCS(tp)
149 {
150
151         ATF_TP_ADD_TC(tp, assert_false);
152         ATF_TP_ADD_TC(tp, assert_true);
153
154         return atf_no_error();
155 }