File: fork_and_run_test.cpp

package info (click to toggle)
process-cpp 3.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 508 kB
  • sloc: cpp: 2,545; ansic: 228; makefile: 17
file content (160 lines) | stat: -rw-r--r-- 5,475 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/testing/fork_and_run.h>

#include <core/posix/signal.h>

#include <gtest/gtest.h>

namespace
{
struct TestingMacrosFixture : public ::testing::Test
{
    TestingMacrosFixture() = default;
};

::testing::AssertionResult ClientFailed(core::testing::ForkAndRunResult result)
{
    return
            (result & core::testing::ForkAndRunResult::client_failed) == core::testing::ForkAndRunResult::empty ?
              ::testing::AssertionFailure() :
              ::testing::AssertionSuccess();
}

::testing::AssertionResult ServiceFailed(core::testing::ForkAndRunResult result)
{
    return
            (result & core::testing::ForkAndRunResult::service_failed) == core::testing::ForkAndRunResult::empty ?
              ::testing::AssertionFailure() :
              ::testing::AssertionSuccess();
}
struct SigTermCatcher
{
    static void sig_term_handler(int)
    {
        std::cout << "Received sigterm." << std::endl;
    }

    SigTermCatcher()
    {
        signal(static_cast<int>(core::posix::Signal::sig_term), sig_term_handler);
    }
} sig_term_catcher;
}

TEST(ForkAndRun, succeeding_client_and_service_result_in_correct_return_value)
{
    auto service = [](){ return core::posix::exit::Status::success; };
    auto client = [](){ return core::posix::exit::Status::success; };

    auto result = core::testing::fork_and_run(service, client);

    ASSERT_FALSE(ClientFailed(result));
    ASSERT_FALSE(ServiceFailed(result));
}

TEST(ForkAndRun, succeeding_client_and_failing_service_result_in_correct_return_value)
{
    auto service = [](){ return core::posix::exit::Status::failure; };
    auto client = [](){ return core::posix::exit::Status::success; };

    auto result = core::testing::fork_and_run(service, client);

    EXPECT_FALSE(ClientFailed(result));
    EXPECT_TRUE(ServiceFailed(result));
}

TEST(ForkAndRun, failing_client_and_failing_service_result_in_correct_return_value)
{
    auto service = [](){ return core::posix::exit::Status::failure; };
    auto client = [](){ return core::posix::exit::Status::failure; };

    auto result = core::testing::fork_and_run(service, client);

    EXPECT_TRUE(ClientFailed(result));
    EXPECT_TRUE(ServiceFailed(result));
}

TEST(ForkAndRun, throwing_client_is_reported_as_failing)
{
    auto service = [](){ return core::posix::exit::Status::success; };
    auto client = [](){ throw std::runtime_error("failing client"); return core::posix::exit::Status::success; };

    auto result = core::testing::fork_and_run(service, client);

    EXPECT_TRUE(ClientFailed(result));
    EXPECT_FALSE(ServiceFailed(result));
}

TEST(ForkAndRun, exiting_with_failure_client_is_reported_as_failing)
{
    auto service = [](){ return core::posix::exit::Status::success; };
    auto client = [](){ exit(EXIT_FAILURE); return core::posix::exit::Status::success; };

    auto result = core::testing::fork_and_run(service, client);

    EXPECT_TRUE(ClientFailed(result));
    EXPECT_FALSE(ServiceFailed(result));
}

TEST(ForkAndRun, aborting_client_is_reported_as_failing)
{
    auto service = [](){ return core::posix::exit::Status::success; };
    auto client = [](){ abort(); return core::posix::exit::Status::success; };

    auto result = core::testing::fork_and_run(service, client);

    EXPECT_TRUE(ClientFailed(result));
    EXPECT_FALSE(ServiceFailed(result));
}

TESTP(TestingMacros, test_fp_macro_reports_success_for_passing_test,
    {
        return core::posix::exit::Status::success;
    })

TESTP_F(TestingMacrosFixture, test_fp_macro_reports_success_for_passing_test,
    {
        return core::posix::exit::Status::success;
    })

// The following two tests fail, and rightly so. However, translating this
// failing behavior to success is really difficult and we omit it for now.
TESTP(TestingMacros, DISABLED_test_fp_macro_reports_success_for_failing_test,
    {
        return core::posix::exit::Status::failure;
    })

TESTP_F(TestingMacrosFixture, DISABLED_test_fp_macro_reports_success_for_failing_test,
    {
        return core::posix::exit::Status::failure;
    })

#include <core/posix/backtrace.h>

TEST(BacktraceSymbolDemangling, demangling_a_cpp_symbol_works)
{
    const char* ref = "tests/fork_and_run_test(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc+0x4b) [0x4591f8]";
    const char* ref_demangled = "bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)";
    auto symbol = core::posix::backtrace::Frame::Symbol::for_testing_from_raw_symbol(ref);

    EXPECT_TRUE(symbol->is_cxx());
    EXPECT_EQ(ref, symbol->raw());
    EXPECT_EQ(ref_demangled, symbol->demangled());
}