Path: blob/main/contrib/atf/test-programs/cpp_helpers.cpp
39507 views
// Copyright (c) 2007 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.2425extern "C" {26#include <signal.h>27#include <unistd.h>28}2930#include <cstdlib>31#include <fstream>32#include <iostream>3334#include <atf-c++.hpp>3536#include "atf-c++/detail/fs.hpp"3738// ------------------------------------------------------------------------39// Helper tests for "t_config".40// ------------------------------------------------------------------------4142ATF_TEST_CASE(config_unset);43ATF_TEST_CASE_HEAD(config_unset)44{45set_md_var("descr", "Helper test case for the t_config test program");46}47ATF_TEST_CASE_BODY(config_unset)48{49ATF_REQUIRE(!has_config_var("test"));50}5152ATF_TEST_CASE(config_empty);53ATF_TEST_CASE_HEAD(config_empty)54{55set_md_var("descr", "Helper test case for the t_config test program");56}57ATF_TEST_CASE_BODY(config_empty)58{59ATF_REQUIRE_EQ(get_config_var("test"), "");60}6162ATF_TEST_CASE(config_value);63ATF_TEST_CASE_HEAD(config_value)64{65set_md_var("descr", "Helper test case for the t_config test program");66}67ATF_TEST_CASE_BODY(config_value)68{69ATF_REQUIRE_EQ(get_config_var("test"), "foo");70}7172ATF_TEST_CASE(config_multi_value);73ATF_TEST_CASE_HEAD(config_multi_value)74{75set_md_var("descr", "Helper test case for the t_config test program");76}77ATF_TEST_CASE_BODY(config_multi_value)78{79ATF_REQUIRE_EQ(get_config_var("test"), "foo bar");80}8182// ------------------------------------------------------------------------83// Helper tests for "t_expect".84// ------------------------------------------------------------------------8586ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_and_pass);87ATF_TEST_CASE_BODY(expect_pass_and_pass)88{89expect_pass();90}9192ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_requirement);93ATF_TEST_CASE_BODY(expect_pass_but_fail_requirement)94{95expect_pass();96fail("Some reason");97}9899ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_check);100ATF_TEST_CASE_BODY(expect_pass_but_fail_check)101{102expect_pass();103fail_nonfatal("Some reason");104}105106ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_requirement);107ATF_TEST_CASE_BODY(expect_fail_and_fail_requirement)108{109expect_fail("Fail reason");110fail("The failure");111expect_pass();112}113114ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_check);115ATF_TEST_CASE_BODY(expect_fail_and_fail_check)116{117expect_fail("Fail first");118fail_nonfatal("abc");119expect_pass();120121expect_fail("And fail again");122fail_nonfatal("def");123expect_pass();124}125126ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_but_pass);127ATF_TEST_CASE_BODY(expect_fail_but_pass)128{129expect_fail("Fail first");130fail_nonfatal("abc");131expect_pass();132133expect_fail("Will not fail");134expect_pass();135136expect_fail("And fail again");137fail_nonfatal("def");138expect_pass();139}140141ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_any_and_exit);142ATF_TEST_CASE_BODY(expect_exit_any_and_exit)143{144expect_exit(-1, "Call will exit");145std::exit(EXIT_SUCCESS);146}147148ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_code_and_exit);149ATF_TEST_CASE_BODY(expect_exit_code_and_exit)150{151expect_exit(123, "Call will exit");152std::exit(123);153}154155ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_but_pass);156ATF_TEST_CASE_BODY(expect_exit_but_pass)157{158expect_exit(-1, "Call won't exit");159}160161ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_any_and_signal);162ATF_TEST_CASE_BODY(expect_signal_any_and_signal)163{164expect_signal(-1, "Call will signal");165::kill(getpid(), SIGKILL);166}167168ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_no_and_signal);169ATF_TEST_CASE_BODY(expect_signal_no_and_signal)170{171expect_signal(SIGHUP, "Call will signal");172::kill(getpid(), SIGHUP);173}174175ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_but_pass);176ATF_TEST_CASE_BODY(expect_signal_but_pass)177{178expect_signal(-1, "Call won't signal");179}180181ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_exit);182ATF_TEST_CASE_BODY(expect_death_and_exit)183{184expect_death("Exit case");185std::exit(123);186}187188ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_signal);189ATF_TEST_CASE_BODY(expect_death_and_signal)190{191expect_death("Signal case");192kill(getpid(), SIGKILL);193}194195ATF_TEST_CASE_WITHOUT_HEAD(expect_death_but_pass);196ATF_TEST_CASE_BODY(expect_death_but_pass)197{198expect_death("Call won't die");199}200201ATF_TEST_CASE(expect_timeout_and_hang);202ATF_TEST_CASE_HEAD(expect_timeout_and_hang)203{204set_md_var("timeout", "1");205}206ATF_TEST_CASE_BODY(expect_timeout_and_hang)207{208expect_timeout("Will overrun");209::sleep(5);210}211212ATF_TEST_CASE(expect_timeout_but_pass);213ATF_TEST_CASE_HEAD(expect_timeout_but_pass)214{215set_md_var("timeout", "1");216}217ATF_TEST_CASE_BODY(expect_timeout_but_pass)218{219expect_timeout("Will just exit");220}221222// ------------------------------------------------------------------------223// Helper tests for "t_meta_data".224// ------------------------------------------------------------------------225226ATF_TEST_CASE(metadata_no_descr);227ATF_TEST_CASE_HEAD(metadata_no_descr)228{229}230ATF_TEST_CASE_BODY(metadata_no_descr)231{232}233234ATF_TEST_CASE_WITHOUT_HEAD(metadata_no_head);235ATF_TEST_CASE_BODY(metadata_no_head)236{237}238239// ------------------------------------------------------------------------240// Helper tests for "t_srcdir".241// ------------------------------------------------------------------------242243ATF_TEST_CASE(srcdir_exists);244ATF_TEST_CASE_HEAD(srcdir_exists)245{246set_md_var("descr", "Helper test case for the t_srcdir test program");247}248ATF_TEST_CASE_BODY(srcdir_exists)249{250if (!atf::fs::exists(atf::fs::path(get_config_var("srcdir")) /251"datafile"))252ATF_FAIL("Cannot find datafile");253}254255// ------------------------------------------------------------------------256// Helper tests for "t_result".257// ------------------------------------------------------------------------258259ATF_TEST_CASE(result_pass);260ATF_TEST_CASE_HEAD(result_pass) { }261ATF_TEST_CASE_BODY(result_pass)262{263std::cout << "msg\n";264}265266ATF_TEST_CASE(result_fail);267ATF_TEST_CASE_HEAD(result_fail) { }268ATF_TEST_CASE_BODY(result_fail)269{270std::cout << "msg\n";271ATF_FAIL("Failure reason");272}273274ATF_TEST_CASE(result_skip);275ATF_TEST_CASE_HEAD(result_skip) { }276ATF_TEST_CASE_BODY(result_skip)277{278std::cout << "msg\n";279ATF_SKIP("Skipped reason");280}281282ATF_TEST_CASE(result_newlines_fail);283ATF_TEST_CASE_HEAD(result_newlines_fail)284{285set_md_var("descr", "Helper test case for the t_result test program");286}287ATF_TEST_CASE_BODY(result_newlines_fail)288{289ATF_FAIL("First line\nSecond line");290}291292ATF_TEST_CASE(result_newlines_skip);293ATF_TEST_CASE_HEAD(result_newlines_skip)294{295set_md_var("descr", "Helper test case for the t_result test program");296}297ATF_TEST_CASE_BODY(result_newlines_skip)298{299ATF_SKIP("First line\nSecond line");300}301302ATF_TEST_CASE(result_exception);303ATF_TEST_CASE_HEAD(result_exception) { }304ATF_TEST_CASE_BODY(result_exception)305{306throw std::runtime_error("This is unhandled");307}308309// ------------------------------------------------------------------------310// Main.311// ------------------------------------------------------------------------312313ATF_INIT_TEST_CASES(tcs)314{315// Add helper tests for t_config.316ATF_ADD_TEST_CASE(tcs, config_unset);317ATF_ADD_TEST_CASE(tcs, config_empty);318ATF_ADD_TEST_CASE(tcs, config_value);319ATF_ADD_TEST_CASE(tcs, config_multi_value);320321// Add helper tests for t_expect.322ATF_ADD_TEST_CASE(tcs, expect_pass_and_pass);323ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_requirement);324ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_check);325ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_requirement);326ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_check);327ATF_ADD_TEST_CASE(tcs, expect_fail_but_pass);328ATF_ADD_TEST_CASE(tcs, expect_exit_any_and_exit);329ATF_ADD_TEST_CASE(tcs, expect_exit_code_and_exit);330ATF_ADD_TEST_CASE(tcs, expect_exit_but_pass);331ATF_ADD_TEST_CASE(tcs, expect_signal_any_and_signal);332ATF_ADD_TEST_CASE(tcs, expect_signal_no_and_signal);333ATF_ADD_TEST_CASE(tcs, expect_signal_but_pass);334ATF_ADD_TEST_CASE(tcs, expect_death_and_exit);335ATF_ADD_TEST_CASE(tcs, expect_death_and_signal);336ATF_ADD_TEST_CASE(tcs, expect_death_but_pass);337ATF_ADD_TEST_CASE(tcs, expect_timeout_and_hang);338ATF_ADD_TEST_CASE(tcs, expect_timeout_but_pass);339340// Add helper tests for t_meta_data.341ATF_ADD_TEST_CASE(tcs, metadata_no_descr);342ATF_ADD_TEST_CASE(tcs, metadata_no_head);343344// Add helper tests for t_srcdir.345ATF_ADD_TEST_CASE(tcs, srcdir_exists);346347// Add helper tests for t_result.348ATF_ADD_TEST_CASE(tcs, result_pass);349ATF_ADD_TEST_CASE(tcs, result_fail);350ATF_ADD_TEST_CASE(tcs, result_skip);351ATF_ADD_TEST_CASE(tcs, result_newlines_fail);352ATF_ADD_TEST_CASE(tcs, result_newlines_skip);353ATF_ADD_TEST_CASE(tcs, result_exception);354}355356357