Path: blob/main/contrib/atf/atf-c/detail/sanity_test.c
39507 views
/* Copyright (c) 2008 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/detail/sanity.h"2627#if defined(HAVE_CONFIG_H)28#include "config.h"29#endif3031#include <sys/types.h>32#include <sys/wait.h>3334#include <signal.h>35#include <stdbool.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#include <atf-c.h>4142#include "atf-c/detail/dynstr.h"43#include "atf-c/detail/process.h"44#include "atf-c/detail/test_helpers.h"4546/* ---------------------------------------------------------------------47* Auxiliary functions.48* --------------------------------------------------------------------- */4950enum type { inv, pre, post, unreachable };5152struct test_data {53enum type m_type;54bool m_cond;55};5657static void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;5859static60void61do_test_child(void *v)62{63struct test_data *td = v;6465switch (td->m_type) {66case inv:67INV(td->m_cond);68break;6970case pre:71PRE(td->m_cond);72break;7374case post:75POST(td->m_cond);76break;7778case unreachable:79if (!td->m_cond)80UNREACHABLE;81break;82}8384exit(EXIT_SUCCESS);85}8687static88void89do_test(enum type t, bool cond)90{91atf_process_child_t child;92atf_process_status_t status;93int nlines;94char *lines[3];9596{97atf_process_stream_t outsb, errsb;98struct test_data td = { t, cond };99100RE(atf_process_stream_init_inherit(&outsb));101RE(atf_process_stream_init_capture(&errsb));102RE(atf_process_fork(&child, do_test_child, &outsb, &errsb, &td));103atf_process_stream_fini(&errsb);104atf_process_stream_fini(&outsb);105}106107nlines = 0;108while (nlines < 3 && (lines[nlines] =109atf_utils_readline(atf_process_child_stderr(&child))) != NULL)110nlines++;111ATF_REQUIRE(nlines == 0 || nlines == 3);112113RE(atf_process_child_wait(&child, &status));114if (!cond) {115ATF_REQUIRE(atf_process_status_signaled(&status));116ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT);117} else {118ATF_REQUIRE(atf_process_status_exited(&status));119ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS);120}121atf_process_status_fini(&status);122123if (!cond) {124switch (t) {125case inv:126ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));127break;128129case pre:130ATF_REQUIRE(atf_utils_grep_string("Precondition", lines[0]));131break;132133case post:134ATF_REQUIRE(atf_utils_grep_string("Postcondition", lines[0]));135break;136137case unreachable:138ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));139break;140}141142ATF_REQUIRE(atf_utils_grep_string(__FILE__, lines[0]));143ATF_REQUIRE(atf_utils_grep_string(PACKAGE_BUGREPORT, lines[2]));144}145146while (nlines > 0) {147nlines--;148free(lines[nlines]);149}150}151152static153void154require_ndebug(void)155{156#if defined(NDEBUG)157atf_tc_skip("Sanity checks not available; code built with -DNDEBUG");158#endif159}160161/* ---------------------------------------------------------------------162* Test cases for the free functions.163* --------------------------------------------------------------------- */164165ATF_TC(inv);166ATF_TC_HEAD(inv, tc)167{168atf_tc_set_md_var(tc, "descr", "Tests the INV macro");169}170ATF_TC_BODY(inv, tc)171{172require_ndebug();173174do_test(inv, false);175do_test(inv, true);176}177178ATF_TC(pre);179ATF_TC_HEAD(pre, tc)180{181atf_tc_set_md_var(tc, "descr", "Tests the PRE macro");182}183ATF_TC_BODY(pre, tc)184{185require_ndebug();186187do_test(pre, false);188do_test(pre, true);189}190191ATF_TC(post);192ATF_TC_HEAD(post, tc)193{194atf_tc_set_md_var(tc, "descr", "Tests the POST macro");195}196ATF_TC_BODY(post, tc)197{198require_ndebug();199200do_test(post, false);201do_test(post, true);202}203204ATF_TC(unreachable);205ATF_TC_HEAD(unreachable, tc)206{207atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro");208}209ATF_TC_BODY(unreachable, tc)210{211require_ndebug();212213do_test(unreachable, false);214do_test(unreachable, true);215}216217/* ---------------------------------------------------------------------218* Main.219* --------------------------------------------------------------------- */220221ATF_TP_ADD_TCS(tp)222{223ATF_TP_ADD_TC(tp, inv);224ATF_TP_ADD_TC(tp, pre);225ATF_TP_ADD_TC(tp, post);226ATF_TP_ADD_TC(tp, unreachable);227228return atf_no_error();229}230231232