xref: /freebsd/contrib/kyua/utils/test_utils.ipp (revision e0c4386e)
1// Copyright 2016 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29/// \file utils/test_utils.ipp
30/// Provides test-only convenience utilities.
31
32#if defined(UTILS_TEST_UTILS_IPP)
33#   error "utils/test_utils.hpp can only be included once"
34#endif
35#define UTILS_TEST_UTILS_IPP
36
37extern "C" {
38#include <sys/resource.h>
39}
40
41#include <cstdlib>
42#include <iostream>
43
44#include <atf-c++.hpp>
45
46#include "utils/defs.hpp"
47#include "utils/stacktrace.hpp"
48#include "utils/text/operations.ipp"
49
50namespace utils {
51
52
53/// Tries to prevent dumping core if we do not need one on a crash.
54///
55/// This is a best-effort operation provided so that tests that will cause
56/// a crash do not collect an unnecessary core dump, which can be slow on
57/// some systems (e.g. on macOS).
58inline void
59avoid_coredump_on_crash(void)
60{
61    struct ::rlimit rl;
62    rl.rlim_cur = 0;
63    rl.rlim_max = 0;
64    if (::setrlimit(RLIMIT_CORE, &rl) == -1) {
65        std::cerr << "Failed to zero core size limit; may dump core\n";
66    }
67}
68
69
70inline void abort_without_coredump(void) UTILS_NORETURN;
71
72
73/// Aborts execution and tries to not dump core.
74///
75/// The coredump avoidance is a best-effort operation provided so that tests
76/// that will cause a crash do not collect an unnecessary core dump, which can
77/// be slow on some systems (e.g. on macOS).
78inline void
79abort_without_coredump(void)
80{
81    avoid_coredump_on_crash();
82    std::abort();
83}
84
85
86/// Skips the test if coredump tests have been disabled by the user.
87///
88/// \param tc The calling test.
89inline void
90require_run_coredump_tests(const atf::tests::tc* tc)
91{
92    if (tc->has_config_var("run_coredump_tests") &&
93        !text::to_type< bool >(tc->get_config_var("run_coredump_tests"))) {
94        tc->skip("run_coredump_tests=false; not running test");
95    }
96}
97
98
99/// Prepares the test so that it can dump core, or skips it otherwise.
100///
101/// \param tc The calling test.
102inline void
103prepare_coredump_test(const atf::tests::tc* tc)
104{
105    require_run_coredump_tests(tc);
106
107    if (!unlimit_core_size()) {
108        tc->skip("Cannot unlimit the core file size; check limits manually");
109    }
110}
111
112
113}  // namespace utils
114