1313535Sngie/* $NetBSD: t_assert.c,v 1.3 2017/01/10 15:17:57 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32313535Sngie__RCSID("$NetBSD: t_assert.c,v 1.3 2017/01/10 15:17:57 christos Exp $"); 33272343Sngie 34313535Sngie#include <sys/types.h> 35313535Sngie#include <sys/resource.h> 36313535Sngie#include <sys/time.h> 37272343Sngie#include <sys/wait.h> 38272343Sngie 39272343Sngie#include <assert.h> 40272343Sngie#include <atf-c.h> 41272343Sngie#include <signal.h> 42272343Sngie#include <stdlib.h> 43272343Sngie#include <string.h> 44272343Sngie#include <unistd.h> 45272343Sngie 46292852Sngiestatic void 47292852Sngiedisable_corefile(void) 48292852Sngie{ 49292852Sngie struct rlimit limits; 50292852Sngie 51292852Sngie limits.rlim_cur = 0; 52292852Sngie limits.rlim_max = 0; 53292852Sngie 54292852Sngie ATF_REQUIRE(setrlimit(RLIMIT_CORE, &limits) == 0); 55292852Sngie} 56292852Sngie 57272343Sngiestatic void handler(int); 58272343Sngie 59272343Sngiestatic void 60272343Sngiehandler(int signo) 61272343Sngie{ 62272343Sngie /* Nothing. */ 63272343Sngie} 64272343Sngie 65272343SngieATF_TC(assert_false); 66272343SngieATF_TC_HEAD(assert_false, tc) 67272343Sngie{ 68272343Sngie atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #1"); 69272343Sngie} 70272343Sngie 71272343SngieATF_TC_BODY(assert_false, tc) 72272343Sngie{ 73272343Sngie struct sigaction sa; 74272343Sngie pid_t pid; 75272343Sngie int sta; 76272343Sngie 77272343Sngie pid = fork(); 78272343Sngie ATF_REQUIRE(pid >= 0); 79272343Sngie 80272343Sngie if (pid == 0) { 81272343Sngie 82292852Sngie disable_corefile(); 83272343Sngie (void)closefrom(0); 84272343Sngie (void)memset(&sa, 0, sizeof(struct sigaction)); 85272343Sngie 86272343Sngie sa.sa_flags = 0; 87272343Sngie sa.sa_handler = handler; 88272343Sngie 89272343Sngie (void)sigemptyset(&sa.sa_mask); 90272343Sngie (void)sigaction(SIGABRT, &sa, 0); 91272343Sngie 92272343Sngie assert(1 == 1); 93272343Sngie 94272343Sngie _exit(EXIT_SUCCESS); 95272343Sngie } 96272343Sngie 97272343Sngie (void)wait(&sta); 98272343Sngie 99272343Sngie if (WIFSIGNALED(sta) != 0 || WIFEXITED(sta) == 0) 100272343Sngie atf_tc_fail("assert(3) fired haphazardly"); 101272343Sngie} 102272343Sngie 103272343SngieATF_TC(assert_true); 104272343SngieATF_TC_HEAD(assert_true, tc) 105272343Sngie{ 106272343Sngie atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #2"); 107272343Sngie} 108272343Sngie 109272343SngieATF_TC_BODY(assert_true, tc) 110272343Sngie{ 111272343Sngie struct sigaction sa; 112272343Sngie pid_t pid; 113272343Sngie int sta; 114272343Sngie 115272343Sngie pid = fork(); 116272343Sngie ATF_REQUIRE(pid >= 0); 117272343Sngie 118272343Sngie if (pid == 0) { 119272343Sngie 120292852Sngie disable_corefile(); 121272343Sngie (void)closefrom(0); 122272343Sngie (void)memset(&sa, 0, sizeof(struct sigaction)); 123272343Sngie 124272343Sngie sa.sa_flags = 0; 125272343Sngie sa.sa_handler = handler; 126272343Sngie 127272343Sngie (void)sigemptyset(&sa.sa_mask); 128272343Sngie (void)sigaction(SIGABRT, &sa, 0); 129272343Sngie 130272343Sngie assert(1 == 2); 131272343Sngie 132272343Sngie _exit(EXIT_SUCCESS); 133272343Sngie } 134272343Sngie 135272343Sngie (void)wait(&sta); 136272343Sngie 137272343Sngie if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGABRT) 138272343Sngie atf_tc_fail("assert(3) did not fire"); 139272343Sngie} 140272343Sngie 141272343SngieATF_TP_ADD_TCS(tp) 142272343Sngie{ 143272343Sngie 144272343Sngie ATF_TP_ADD_TC(tp, assert_false); 145272343Sngie ATF_TP_ADD_TC(tp, assert_true); 146272343Sngie 147272343Sngie return atf_no_error(); 148272343Sngie} 149