113 lines · cpp
1//===-- Implementation of libc death test executors -----------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "LibcTest.h"10 11#include "src/__support/macros/config.h"12#include "test/UnitTest/ExecuteFunction.h"13#include "test/UnitTest/TestLogger.h"14 15#include <assert.h>16 17namespace {18constexpr unsigned TIMEOUT_MS = 10000;19} // Anonymous namespace20 21namespace LIBC_NAMESPACE_DECL {22namespace testing {23 24bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,25 const char *LHSStr,26 [[maybe_unused]] const char *RHSStr,27 internal::Location Loc) {28 testutils::ProcessStatus Result =29 testutils::invoke_in_subprocess(Func, TIMEOUT_MS);30 31 if (const char *error = Result.get_error()) {32 Ctx->markFail();33 tlog << Loc;34 tlog << error << '\n';35 return false;36 }37 38 if (Result.timed_out()) {39 Ctx->markFail();40 tlog << Loc;41 tlog << "Process timed out after " << TIMEOUT_MS << " milliseconds.\n";42 return false;43 }44 45 if (Result.exited_normally()) {46 Ctx->markFail();47 tlog << Loc;48 tlog << "Expected " << LHSStr49 << " to be killed by a signal\nBut it exited normally!\n";50 return false;51 }52 53 int KilledBy = Result.get_fatal_signal();54 assert(KilledBy != 0 && "Not killed by any signal");55 if (Signal == -1 || KilledBy == Signal)56 return true;57 58 using testutils::signal_as_string;59 Ctx->markFail();60 tlog << Loc;61 tlog << " Expected: " << LHSStr << '\n'62 << "To be killed by signal: " << Signal << '\n'63 << " Which is: " << signal_as_string(Signal) << '\n'64 << " But it was killed by: " << KilledBy << '\n'65 << " Which is: " << signal_as_string(KilledBy) << '\n';66 return false;67}68 69bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,70 const char *LHSStr, const char *RHSStr,71 internal::Location Loc) {72 testutils::ProcessStatus Result =73 testutils::invoke_in_subprocess(Func, TIMEOUT_MS);74 75 if (const char *error = Result.get_error()) {76 Ctx->markFail();77 tlog << Loc;78 tlog << error << '\n';79 return false;80 }81 82 if (Result.timed_out()) {83 Ctx->markFail();84 tlog << Loc;85 tlog << "Process timed out after " << TIMEOUT_MS << " milliseconds.\n";86 return false;87 }88 89 if (!Result.exited_normally()) {90 Ctx->markFail();91 tlog << Loc;92 tlog << "Expected " << LHSStr << '\n'93 << "to exit with exit code " << ExitCode << '\n'94 << "But it exited abnormally!\n";95 return false;96 }97 98 int ActualExit = Result.get_exit_code();99 if (ActualExit == ExitCode)100 return true;101 102 Ctx->markFail();103 tlog << Loc;104 tlog << "Expected exit code of: " << LHSStr << '\n'105 << " Which is: " << ActualExit << '\n'106 << " To be equal to: " << RHSStr << '\n'107 << " Which is: " << ExitCode << '\n';108 return false;109}110 111} // namespace testing112} // namespace LIBC_NAMESPACE_DECL113