1275988Sngie/* Copyright (c) 2008 The NetBSD Foundation, Inc. 2240116Smarcel * All rights reserved. 3240116Smarcel * 4240116Smarcel * Redistribution and use in source and binary forms, with or without 5240116Smarcel * modification, are permitted provided that the following conditions 6240116Smarcel * are met: 7240116Smarcel * 1. Redistributions of source code must retain the above copyright 8240116Smarcel * notice, this list of conditions and the following disclaimer. 9240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright 10240116Smarcel * notice, this list of conditions and the following disclaimer in the 11240116Smarcel * documentation and/or other materials provided with the distribution. 12240116Smarcel * 13240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 14240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 18240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24275988Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 25240116Smarcel 26275988Sngie#include "atf-c/detail/sanity.h" 27275988Sngie 28240116Smarcel#if defined(HAVE_CONFIG_H) 29275988Sngie#include "config.h" 30240116Smarcel#endif 31240116Smarcel 32240116Smarcel#include <sys/types.h> 33240116Smarcel#include <sys/wait.h> 34240116Smarcel 35240116Smarcel#include <signal.h> 36240116Smarcel#include <stdbool.h> 37240116Smarcel#include <stdlib.h> 38240116Smarcel#include <string.h> 39240116Smarcel#include <unistd.h> 40240116Smarcel 41240116Smarcel#include <atf-c.h> 42240116Smarcel 43275988Sngie#include "atf-c/detail/dynstr.h" 44275988Sngie#include "atf-c/detail/process.h" 45275988Sngie#include "atf-c/detail/test_helpers.h" 46240116Smarcel 47240116Smarcel/* --------------------------------------------------------------------- 48240116Smarcel * Auxiliary functions. 49240116Smarcel * --------------------------------------------------------------------- */ 50240116Smarcel 51240116Smarcelenum type { inv, pre, post, unreachable }; 52240116Smarcel 53240116Smarcelstruct test_data { 54240116Smarcel enum type m_type; 55240116Smarcel bool m_cond; 56240116Smarcel}; 57240116Smarcel 58240116Smarcelstatic void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN; 59240116Smarcel 60240116Smarcelstatic 61240116Smarcelvoid 62240116Smarceldo_test_child(void *v) 63240116Smarcel{ 64240116Smarcel struct test_data *td = v; 65240116Smarcel 66240116Smarcel switch (td->m_type) { 67240116Smarcel case inv: 68240116Smarcel INV(td->m_cond); 69240116Smarcel break; 70240116Smarcel 71240116Smarcel case pre: 72240116Smarcel PRE(td->m_cond); 73240116Smarcel break; 74240116Smarcel 75240116Smarcel case post: 76240116Smarcel POST(td->m_cond); 77240116Smarcel break; 78240116Smarcel 79240116Smarcel case unreachable: 80240116Smarcel if (!td->m_cond) 81240116Smarcel UNREACHABLE; 82240116Smarcel break; 83240116Smarcel } 84240116Smarcel 85240116Smarcel exit(EXIT_SUCCESS); 86240116Smarcel} 87240116Smarcel 88240116Smarcelstatic 89240116Smarcelvoid 90240116Smarceldo_test(enum type t, bool cond) 91240116Smarcel{ 92240116Smarcel atf_process_child_t child; 93240116Smarcel atf_process_status_t status; 94240116Smarcel int nlines; 95260029Sjmmv char *lines[3]; 96240116Smarcel 97240116Smarcel { 98240116Smarcel atf_process_stream_t outsb, errsb; 99240116Smarcel struct test_data td = { t, cond }; 100240116Smarcel 101240116Smarcel RE(atf_process_stream_init_inherit(&outsb)); 102240116Smarcel RE(atf_process_stream_init_capture(&errsb)); 103240116Smarcel RE(atf_process_fork(&child, do_test_child, &outsb, &errsb, &td)); 104240116Smarcel atf_process_stream_fini(&errsb); 105240116Smarcel atf_process_stream_fini(&outsb); 106240116Smarcel } 107240116Smarcel 108240116Smarcel nlines = 0; 109260029Sjmmv while (nlines < 3 && (lines[nlines] = 110260029Sjmmv atf_utils_readline(atf_process_child_stderr(&child))) != NULL) 111240116Smarcel nlines++; 112240116Smarcel ATF_REQUIRE(nlines == 0 || nlines == 3); 113240116Smarcel 114240116Smarcel RE(atf_process_child_wait(&child, &status)); 115240116Smarcel if (!cond) { 116240116Smarcel ATF_REQUIRE(atf_process_status_signaled(&status)); 117240116Smarcel ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT); 118240116Smarcel } else { 119240116Smarcel ATF_REQUIRE(atf_process_status_exited(&status)); 120240116Smarcel ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS); 121240116Smarcel } 122240116Smarcel atf_process_status_fini(&status); 123240116Smarcel 124240116Smarcel if (!cond) { 125240116Smarcel switch (t) { 126240116Smarcel case inv: 127260029Sjmmv ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0])); 128240116Smarcel break; 129240116Smarcel 130240116Smarcel case pre: 131260029Sjmmv ATF_REQUIRE(atf_utils_grep_string("Precondition", lines[0])); 132240116Smarcel break; 133240116Smarcel 134240116Smarcel case post: 135260029Sjmmv ATF_REQUIRE(atf_utils_grep_string("Postcondition", lines[0])); 136240116Smarcel break; 137240116Smarcel 138240116Smarcel case unreachable: 139260029Sjmmv ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0])); 140240116Smarcel break; 141240116Smarcel } 142240116Smarcel 143260029Sjmmv ATF_REQUIRE(atf_utils_grep_string(__FILE__, lines[0])); 144260029Sjmmv ATF_REQUIRE(atf_utils_grep_string(PACKAGE_BUGREPORT, lines[2])); 145240116Smarcel } 146240116Smarcel 147240116Smarcel while (nlines > 0) { 148240116Smarcel nlines--; 149260029Sjmmv free(lines[nlines]); 150240116Smarcel } 151240116Smarcel} 152240116Smarcel 153240116Smarcelstatic 154240116Smarcelvoid 155240116Smarcelrequire_ndebug(void) 156240116Smarcel{ 157240116Smarcel#if defined(NDEBUG) 158240116Smarcel atf_tc_skip("Sanity checks not available; code built with -DNDEBUG"); 159240116Smarcel#endif 160240116Smarcel} 161240116Smarcel 162240116Smarcel/* --------------------------------------------------------------------- 163240116Smarcel * Test cases for the free functions. 164240116Smarcel * --------------------------------------------------------------------- */ 165240116Smarcel 166240116SmarcelATF_TC(inv); 167240116SmarcelATF_TC_HEAD(inv, tc) 168240116Smarcel{ 169240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the INV macro"); 170240116Smarcel} 171240116SmarcelATF_TC_BODY(inv, tc) 172240116Smarcel{ 173240116Smarcel require_ndebug(); 174240116Smarcel 175240116Smarcel do_test(inv, false); 176240116Smarcel do_test(inv, true); 177240116Smarcel} 178240116Smarcel 179240116SmarcelATF_TC(pre); 180240116SmarcelATF_TC_HEAD(pre, tc) 181240116Smarcel{ 182240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the PRE macro"); 183240116Smarcel} 184240116SmarcelATF_TC_BODY(pre, tc) 185240116Smarcel{ 186240116Smarcel require_ndebug(); 187240116Smarcel 188240116Smarcel do_test(pre, false); 189240116Smarcel do_test(pre, true); 190240116Smarcel} 191240116Smarcel 192240116SmarcelATF_TC(post); 193240116SmarcelATF_TC_HEAD(post, tc) 194240116Smarcel{ 195240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the POST macro"); 196240116Smarcel} 197240116SmarcelATF_TC_BODY(post, tc) 198240116Smarcel{ 199240116Smarcel require_ndebug(); 200240116Smarcel 201240116Smarcel do_test(post, false); 202240116Smarcel do_test(post, true); 203240116Smarcel} 204240116Smarcel 205240116SmarcelATF_TC(unreachable); 206240116SmarcelATF_TC_HEAD(unreachable, tc) 207240116Smarcel{ 208240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro"); 209240116Smarcel} 210240116SmarcelATF_TC_BODY(unreachable, tc) 211240116Smarcel{ 212240116Smarcel require_ndebug(); 213240116Smarcel 214240116Smarcel do_test(unreachable, false); 215240116Smarcel do_test(unreachable, true); 216240116Smarcel} 217240116Smarcel 218240116Smarcel/* --------------------------------------------------------------------- 219240116Smarcel * Main. 220240116Smarcel * --------------------------------------------------------------------- */ 221240116Smarcel 222240116SmarcelATF_TP_ADD_TCS(tp) 223240116Smarcel{ 224240116Smarcel ATF_TP_ADD_TC(tp, inv); 225240116Smarcel ATF_TP_ADD_TC(tp, pre); 226240116Smarcel ATF_TP_ADD_TC(tp, post); 227240116Smarcel ATF_TP_ADD_TC(tp, unreachable); 228240116Smarcel 229240116Smarcel return atf_no_error(); 230240116Smarcel} 231