Path: blob/main/contrib/kyua/utils/signals/programmer_test.cpp
48196 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/signals/programmer.hpp"2930extern "C" {31#include <signal.h>32#include <unistd.h>33}3435#include <atf-c++.hpp>3637#include "utils/sanity.hpp"3839namespace signals = utils::signals;404142namespace {434445namespace sigchld {464748static bool happened_1;49static bool happened_2;505152void handler_1(const int signo) {53PRE(signo == SIGCHLD);54happened_1 = true;55}565758void handler_2(const int signo) {59PRE(signo == SIGCHLD);60happened_2 = true;61}626364} // namespace sigchld656667} // anonymous namespace686970ATF_TEST_CASE_WITHOUT_HEAD(program_unprogram);71ATF_TEST_CASE_BODY(program_unprogram)72{73signals::programmer programmer(SIGCHLD, sigchld::handler_1);74sigchld::happened_1 = false;75::kill(::getpid(), SIGCHLD);76ATF_REQUIRE(sigchld::happened_1);7778programmer.unprogram();79sigchld::happened_1 = false;80::kill(::getpid(), SIGCHLD);81ATF_REQUIRE(!sigchld::happened_1);82}838485ATF_TEST_CASE_WITHOUT_HEAD(scope);86ATF_TEST_CASE_BODY(scope)87{88{89signals::programmer programmer(SIGCHLD, sigchld::handler_1);90sigchld::happened_1 = false;91::kill(::getpid(), SIGCHLD);92ATF_REQUIRE(sigchld::happened_1);93}9495sigchld::happened_1 = false;96::kill(::getpid(), SIGCHLD);97ATF_REQUIRE(!sigchld::happened_1);98}99100101ATF_TEST_CASE_WITHOUT_HEAD(nested);102ATF_TEST_CASE_BODY(nested)103{104signals::programmer programmer_1(SIGCHLD, sigchld::handler_1);105sigchld::happened_1 = false;106sigchld::happened_2 = false;107::kill(::getpid(), SIGCHLD);108ATF_REQUIRE(sigchld::happened_1);109ATF_REQUIRE(!sigchld::happened_2);110111signals::programmer programmer_2(SIGCHLD, sigchld::handler_2);112sigchld::happened_1 = false;113sigchld::happened_2 = false;114::kill(::getpid(), SIGCHLD);115ATF_REQUIRE(!sigchld::happened_1);116ATF_REQUIRE(sigchld::happened_2);117118programmer_2.unprogram();119sigchld::happened_1 = false;120sigchld::happened_2 = false;121::kill(::getpid(), SIGCHLD);122ATF_REQUIRE(sigchld::happened_1);123ATF_REQUIRE(!sigchld::happened_2);124125programmer_1.unprogram();126sigchld::happened_1 = false;127sigchld::happened_2 = false;128::kill(::getpid(), SIGCHLD);129ATF_REQUIRE(!sigchld::happened_1);130ATF_REQUIRE(!sigchld::happened_2);131}132133134ATF_INIT_TEST_CASES(tcs)135{136ATF_ADD_TEST_CASE(tcs, program_unprogram);137ATF_ADD_TEST_CASE(tcs, scope);138ATF_ADD_TEST_CASE(tcs, nested);139}140141142