Path: blob/main/contrib/kyua/utils/process/status_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/process/status.hpp"2930extern "C" {31#include <sys/wait.h>3233#include <signal.h>34#include <unistd.h>35}3637#include <cstdlib>3839#include <atf-c++.hpp>4041#include "utils/test_utils.ipp"4243using utils::process::status;444546namespace {474849/// Body of a subprocess that exits with a particular exit status.50///51/// \tparam ExitStatus The status to exit with.52template< int ExitStatus >53void child_exit(void)54{55std::exit(ExitStatus);56}575859/// Body of a subprocess that sends a particular signal to itself.60///61/// \tparam Signo The signal to send to self.62template< int Signo >63void child_signal(void)64{65::kill(::getpid(), Signo);66}676869/// Spawns a process and waits for completion.70///71/// \param hook The function to run within the child. Should not return.72///73/// \return The termination status of the spawned subprocess.74status75fork_and_wait(void (*hook)(void))76{77pid_t pid = ::fork();78ATF_REQUIRE(pid != -1);79if (pid == 0) {80hook();81std::abort();82} else {83int stat_loc;84ATF_REQUIRE(::waitpid(pid, &stat_loc, 0) != -1);85const status s = status(pid, stat_loc);86ATF_REQUIRE_EQ(pid, s.dead_pid());87return s;88}89}909192} // anonymous namespace939495ATF_TEST_CASE_WITHOUT_HEAD(fake_exited)96ATF_TEST_CASE_BODY(fake_exited)97{98const status fake = status::fake_exited(123);99ATF_REQUIRE_EQ(-1, fake.dead_pid());100ATF_REQUIRE(fake.exited());101ATF_REQUIRE_EQ(123, fake.exitstatus());102ATF_REQUIRE(!fake.signaled());103}104105106ATF_TEST_CASE_WITHOUT_HEAD(fake_signaled)107ATF_TEST_CASE_BODY(fake_signaled)108{109const status fake = status::fake_signaled(567, true);110ATF_REQUIRE_EQ(-1, fake.dead_pid());111ATF_REQUIRE(!fake.exited());112ATF_REQUIRE(fake.signaled());113ATF_REQUIRE_EQ(567, fake.termsig());114ATF_REQUIRE(fake.coredump());115}116117118ATF_TEST_CASE_WITHOUT_HEAD(output__exitstatus);119ATF_TEST_CASE_BODY(output__exitstatus)120{121const status fake = status::fake_exited(123);122std::ostringstream str;123str << fake;124ATF_REQUIRE_EQ("status{exitstatus=123}", str.str());125}126127128ATF_TEST_CASE_WITHOUT_HEAD(output__signaled_without_core);129ATF_TEST_CASE_BODY(output__signaled_without_core)130{131const status fake = status::fake_signaled(8, false);132std::ostringstream str;133str << fake;134ATF_REQUIRE_EQ("status{termsig=8, coredump=false}", str.str());135}136137138ATF_TEST_CASE_WITHOUT_HEAD(output__signaled_with_core);139ATF_TEST_CASE_BODY(output__signaled_with_core)140{141const status fake = status::fake_signaled(9, true);142std::ostringstream str;143str << fake;144ATF_REQUIRE_EQ("status{termsig=9, coredump=true}", str.str());145}146147148ATF_TEST_CASE_WITHOUT_HEAD(integration__exited);149ATF_TEST_CASE_BODY(integration__exited)150{151const status exit_success = fork_and_wait(child_exit< EXIT_SUCCESS >);152ATF_REQUIRE(exit_success.exited());153ATF_REQUIRE_EQ(EXIT_SUCCESS, exit_success.exitstatus());154ATF_REQUIRE(!exit_success.signaled());155156const status exit_failure = fork_and_wait(child_exit< EXIT_FAILURE >);157ATF_REQUIRE(exit_failure.exited());158ATF_REQUIRE_EQ(EXIT_FAILURE, exit_failure.exitstatus());159ATF_REQUIRE(!exit_failure.signaled());160}161162163ATF_TEST_CASE_WITHOUT_HEAD(integration__signaled);164ATF_TEST_CASE_BODY(integration__signaled)165{166const status sigterm = fork_and_wait(child_signal< SIGTERM >);167ATF_REQUIRE(!sigterm.exited());168ATF_REQUIRE(sigterm.signaled());169ATF_REQUIRE_EQ(SIGTERM, sigterm.termsig());170ATF_REQUIRE(!sigterm.coredump());171172const status sigkill = fork_and_wait(child_signal< SIGKILL >);173ATF_REQUIRE(!sigkill.exited());174ATF_REQUIRE(sigkill.signaled());175ATF_REQUIRE_EQ(SIGKILL, sigkill.termsig());176ATF_REQUIRE(!sigkill.coredump());177}178179180ATF_TEST_CASE_WITHOUT_HEAD(integration__coredump);181ATF_TEST_CASE_BODY(integration__coredump)182{183utils::prepare_coredump_test(this);184185const status coredump = fork_and_wait(child_signal< SIGQUIT >);186ATF_REQUIRE(!coredump.exited());187ATF_REQUIRE(coredump.signaled());188ATF_REQUIRE_EQ(SIGQUIT, coredump.termsig());189#if !defined(WCOREDUMP)190expect_fail("Platform does not support checking for coredump");191#endif192ATF_REQUIRE(coredump.coredump());193}194195196ATF_INIT_TEST_CASES(tcs)197{198ATF_ADD_TEST_CASE(tcs, fake_exited);199ATF_ADD_TEST_CASE(tcs, fake_signaled);200201ATF_ADD_TEST_CASE(tcs, output__exitstatus);202ATF_ADD_TEST_CASE(tcs, output__signaled_without_core);203ATF_ADD_TEST_CASE(tcs, output__signaled_with_core);204205ATF_ADD_TEST_CASE(tcs, integration__exited);206ATF_ADD_TEST_CASE(tcs, integration__signaled);207ATF_ADD_TEST_CASE(tcs, integration__coredump);208}209210211