Path: blob/main/contrib/kyua/utils/signals/misc_test.cpp
48199 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/misc.hpp"2930extern "C" {31#include <signal.h>32#include <unistd.h>33}3435#include <cstdlib>3637#include <atf-c++.hpp>3839#include "utils/defs.hpp"40#include "utils/fs/path.hpp"41#include "utils/process/child.ipp"42#include "utils/process/status.hpp"43#include "utils/signals/exceptions.hpp"4445namespace fs = utils::fs;46namespace process = utils::process;47namespace signals = utils::signals;484950namespace {515253static void program_reset_raise(void) UTILS_NORETURN;545556/// Body of a subprocess that tests the signals::reset function.57///58/// This function programs a signal to be ignored, then uses signal::reset to59/// bring it back to its default handler and then delivers the signal to self.60/// The default behavior of the signal is for the process to die, so this61/// function should never return correctly (and thus the child process should62/// always die due to a signal if all goes well).63static void64program_reset_raise(void)65{66struct ::sigaction sa;67sa.sa_handler = SIG_IGN;68sigemptyset(&sa.sa_mask);69sa.sa_flags = 0;70if (::sigaction(SIGUSR1, &sa, NULL) == -1)71std::exit(EXIT_FAILURE);7273signals::reset(SIGUSR1);74::kill(::getpid(), SIGUSR1);7576// Should not be reached, but we do not assert this condition because we77// want to exit cleanly if the signal does not abort our execution to let78// the parent easily know what happened.79std::exit(EXIT_SUCCESS);80}818283/// Body of a subprocess that executes the signals::reset_all function.84///85/// The process exits with success if the function worked, or with a failure if86/// an error is reported. No signals are tested.87static void88run_reset_all(void)89{90const bool ok = signals::reset_all();91std::exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);92}939495} // anonymous namespace969798ATF_TEST_CASE_WITHOUT_HEAD(reset__ok);99ATF_TEST_CASE_BODY(reset__ok)100{101std::unique_ptr< process::child > child = process::child::fork_files(102program_reset_raise, fs::path("/dev/stdout"), fs::path("/dev/stderr"));103process::status status = child->wait();104ATF_REQUIRE(status.signaled());105ATF_REQUIRE_EQ(SIGUSR1, status.termsig());106}107108109ATF_TEST_CASE_WITHOUT_HEAD(reset__invalid);110ATF_TEST_CASE_BODY(reset__invalid)111{112ATF_REQUIRE_THROW(signals::system_error, signals::reset(-1));113}114115116ATF_TEST_CASE_WITHOUT_HEAD(reset_all);117ATF_TEST_CASE_BODY(reset_all)118{119std::unique_ptr< process::child > child = process::child::fork_files(120run_reset_all, fs::path("/dev/stdout"), fs::path("/dev/stderr"));121process::status status = child->wait();122ATF_REQUIRE(status.exited());123ATF_REQUIRE_EQ(EXIT_SUCCESS, status.exitstatus());124}125126127ATF_INIT_TEST_CASES(tcs)128{129ATF_ADD_TEST_CASE(tcs, reset__ok);130ATF_ADD_TEST_CASE(tcs, reset__invalid);131ATF_ADD_TEST_CASE(tcs, reset_all);132}133134135