7bd5dec4471c6aee353fc31a1f40131edbeae7ea
[iec.git] / src / type3_AndroidCloud / anbox-master / external / process-cpp-minimal / include / core / testing / fork_and_run.h
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef CORE_TESTING_FORK_AND_RUN_H_
19 #define CORE_TESTING_FORK_AND_RUN_H_
20
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/visibility.h>
24
25 #include <functional>
26
27 namespace core
28 {
29 namespace testing
30 {
31 /**
32  * @brief The ForkAndRunResult enum models the different failure modes of fork_and_run.
33  */
34 enum class ForkAndRunResult
35 {
36     empty = 0, ///< Special value indicating no bit being set.
37     client_failed = 1 << 0, ///< The client failed.
38     service_failed = 1 << 1 ///< The service failed.
39 };
40
41 CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs);
42 CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs);
43
44 /**
45  * @brief Forks two processes for both the service and the client.
46  *
47  * The function does the following:
48  *   - Forks a process for the service and runs the respective closure.
49  *   - Forks a process for the client and runs the respective closure.
50  *   - After the client has finished, the service is signalled with sigterm.
51  *
52  * @throw std::system_error if an error occured during process interaction.
53  * @throw std::runtime_error for signalling all other error conditions.
54  * @param [in] service The service to be executed in a child process.
55  * @param [in] client The client to be executed in a child process.
56  * @return ForkAndRunResult indicating if either of service or client failed.
57  */
58 CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(
59         const std::function<core::posix::exit::Status()>& service,
60         const std::function<core::posix::exit::Status()>& client);
61 }
62 }
63
64 /**
65  * Test definition macro which runs a TEST in a forked process.
66  * Note that you can only use EXPECT_*, not
67  * ASSERT_*!
68  *
69  * Usage:
70  * TESTP(test_suite, test_name, {
71  *      test code ...
72  *      EXPECT_* ...
73  * })
74  */
75 #define TESTP(test_suite, test_name, CODE)                                             \
76     TEST(test_suite, test_name) {                                                       \
77         auto test = [&]() {                                                             \
78             CODE                                                                        \
79             return HasFailure() ? core::posix::exit::Status::failure                    \
80                             : core::posix::exit::Status::success;                       \
81         };                                                                              \
82         auto child = core::posix::fork(                                                 \
83             test,                                                                       \
84             core::posix::StandardStream::empty);                                        \
85         auto result = child.wait_for(core::posix::wait::Flags::untraced);               \
86         EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status);            \
87         EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status);  \
88     } \
89
90 /**
91  * Test definition macro which runs a TEST_F in a forked process.
92  * Note that you can only use EXPECT_*, not ASSERT_*!
93  *
94  * Usage:
95  * TESTP_F(FixtureName, TestName, {
96  *    ... test code ...
97  *    EXPECT_* ...
98  *  })
99  */
100 #define TESTP_F(test_fixture, test_name, CODE)                                          \
101     TEST_F(test_fixture, test_name) {                                                   \
102         auto test = [&]() {                                                             \
103             CODE                                                                        \
104             return HasFailure() ? core::posix::exit::Status::failure                    \
105                             : core::posix::exit::Status::success;                       \
106         };                                                                              \
107         auto child = core::posix::fork(                                                 \
108             test,                                                                       \
109             core::posix::StandardStream::empty);                                        \
110         auto result = child.wait_for(core::posix::wait::Flags::untraced);               \
111         EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status);            \
112         EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status);  \
113     } \
114
115 #endif // CORE_TESTING_FORK_AND_RUN_H_