2 * Copyright © 2012-2013 Canonical Ltd.
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.
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.
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/>.
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
19 #include <core/testing/fork_and_run.h>
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/wait.h>
25 core::testing::ForkAndRunResult core::testing::operator|(
26 core::testing::ForkAndRunResult lhs,
27 core::testing::ForkAndRunResult rhs)
29 return static_cast<core::testing::ForkAndRunResult>(
30 static_cast<unsigned int> (lhs) | static_cast<unsigned int>(rhs));
33 core::testing::ForkAndRunResult core::testing::operator&(
34 core::testing::ForkAndRunResult lhs,
35 core::testing::ForkAndRunResult rhs)
37 return static_cast<core::testing::ForkAndRunResult>(
38 static_cast<unsigned int> (lhs) & static_cast<unsigned int>(rhs));
41 core::testing::ForkAndRunResult core::testing::fork_and_run(
42 const std::function<core::posix::exit::Status()>& service,
43 const std::function<core::posix::exit::Status()>& client)
45 core::testing::ForkAndRunResult result = core::testing::ForkAndRunResult::empty;
47 auto service_process = core::posix::fork(service, core::posix::StandardStream::empty);
48 auto client_process = core::posix::fork(client, core::posix::StandardStream::empty);
50 auto client_result = client_process.wait_for(core::posix::wait::Flags::untraced);
52 switch (client_result.status)
54 case core::posix::wait::Result::Status::exited:
55 if (client_result.detail.if_exited.status == core::posix::exit::Status::failure)
56 result = result | core::testing::ForkAndRunResult::client_failed;
59 result = result | core::testing::ForkAndRunResult::client_failed;
63 service_process.send_signal_or_throw(core::posix::Signal::sig_term);
64 auto service_result = service_process.wait_for(core::posix::wait::Flags::untraced);
66 switch (service_result.status)
68 case core::posix::wait::Result::Status::exited:
69 if (service_result.detail.if_exited.status == core::posix::exit::Status::failure)
70 result = result | core::testing::ForkAndRunResult::service_failed;
73 result = result | core::testing::ForkAndRunResult::service_failed;