24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
33 ::testing::AssertionResult is_error(
const std::error_code& ec)
35 return ec ? ::testing::AssertionResult{
true} : ::testing::AssertionResult{
false};
38 struct ForkedSpinningProcess :
public ::testing::Test
66 std::shared_ptr<core::posix::SignalTrap> signal_trap;
67 std::unique_ptr<core::posix::ChildProcess::DeathObserver> death_observer;
72 TEST(PosixProcess, ctor_throws_for_invalid_pid)
74 pid_t invalid_pid{-1};
78 TEST(PosixProcess, this_process_instance_reports_correct_pid)
83 TEST(PosixProcess, this_process_instance_reports_correct_parent)
88 TEST(PosixProcess, throwing_access_to_process_group_id_of_this_process_works)
93 TEST(PosixProcess, non_throwing_access_to_process_group_id_of_this_process_works)
97 EXPECT_FALSE(is_error(se));
98 EXPECT_EQ(getpgrp(), pg.id());
101 TEST(PosixProcess, trying_to_access_process_group_of_invalid_process_throws)
106 TEST(PosixProcess, trying_to_access_process_group_of_invalid_process_reports_error)
110 EXPECT_TRUE(is_error(se));
113 TEST_F(ForkedSpinningProcess, throwing_access_to_process_group_id_of_a_forked_process_works)
115 auto pg = child.process_group_or_throw();
116 EXPECT_EQ(getpgrp(), pg.id());
119 TEST_F(ForkedSpinningProcess, non_throwing_access_to_process_group_id_of_a_forked_process_works)
122 auto pg = child.process_group(se);
125 EXPECT_EQ(getpgrp(), pg.id());
128 TEST(PosixProcess, accessing_streams_of_this_process_works)
131 std::stringstream ss;
134 EXPECT_EQ(ss.str(),
"core::posix::this_process::instance().cout()\n");
139 std::stringstream ss;
142 EXPECT_EQ(ss.str(),
"core::posix::this_process::instance().cerr()\n");
147 TEST(Self, non_mutable_access_to_the_environment_returns_correct_results)
149 static const char* home =
"HOME";
150 static const char* totally_not_existent =
"totally_not_existent_42";
155 TEST(Self, mutable_access_to_the_environment_alters_the_environment)
157 static const char* totally_not_existent =
"totally_not_existent_42";
158 static const char* totally_not_existent_value =
"42";
162 totally_not_existent,
163 totally_not_existent_value));
164 EXPECT_EQ(totally_not_existent_value,
169 totally_not_existent));
174 TEST(Self, getting_env_var_for_empty_key_does_not_throw)
179 TEST(Self, setting_env_var_for_empty_key_throws)
186 TEST(ChildProcess, fork_returns_process_object_with_valid_pid_and_wait_for_returns_correct_result)
191 EXPECT_TRUE(child.
pid() > 0);
197 result.detail.if_exited.status);
202 EXPECT_TRUE(child.
pid() > 0);
208 result.detail.if_exited.status);
211 TEST_F(ForkedSpinningProcess, signalling_a_forked_child_makes_wait_for_return_correct_result)
218 result.detail.if_signaled.signal);
223 EXPECT_TRUE(child.pid() > 0);
230 result.detail.if_signaled.signal);
233 TEST(ChildProcess, stopping_a_forked_child_makes_wait_for_return_correct_result)
247 EXPECT_TRUE(child.
pid() > 0);
249 const std::string echo_value{
"42"};
250 child.
cin() << echo_value << std::endl;
251 std::string line; child.
cout() >> line;
253 EXPECT_EQ(echo_value, line);
260 result.detail.if_stopped.signal);
267 result.detail.if_signaled.signal);
270 TEST(ChildProcess, exec_returns_process_object_with_valid_pid_and_wait_for_returns_correct_result)
272 const std::string program{
"/usr/bin/sleep"};
273 const std::vector<std::string> argv = {
"10"};
274 std::map<std::string, std::string> env;
277 env.insert(std::make_pair(key, value));
284 EXPECT_TRUE(child.pid() > 0);
290 result.detail.if_signaled.signal);
293 TEST(ChildProcess, signalling_an_execd_child_makes_wait_for_return_correct_result)
295 const std::string program{
"/usr/bin/env"};
296 const std::vector<std::string> argv = {};
297 std::map<std::string, std::string> env;
300 env.insert(std::make_pair(key, value));
309 EXPECT_TRUE(child.pid() > 0);
316 result.detail.if_signaled.signal);
322 EXPECT_TRUE(child.pid() > 0);
329 result.detail.if_signaled.signal);
332 TEST(ChildProcess, stopping_an_execd_child_makes_wait_for_return_correct_result)
334 const std::string program{
"/usr/bin/sleep"};
335 const std::vector<std::string> argv = {
"10"};
336 std::map<std::string, std::string> env;
339 env.insert(std::make_pair(key, value));
346 EXPECT_TRUE(child.pid() > 0);
353 result.detail.if_signaled.signal);
359 result.detail.if_signaled.signal);
364 struct ChildDeathObserverEventCollector
370 TEST_F(ForkedSpinningProcess, observing_child_processes_for_death_works_if_child_is_signalled_with_sigkill)
372 using namespace ::testing;
374 ChildDeathObserverEventCollector event_collector;
376 core::ScopedConnection sc
380 event_collector.on_child_died(cp);
384 EXPECT_TRUE(init.death_observer->add(child));
385 EXPECT_CALL(event_collector, on_child_died(_))
389 init.signal_trap.get(),
392 std::thread worker{[]() { init.signal_trap->run(); }};
396 if (worker.joinable())
400 TEST_F(ForkedSpinningProcess, observing_child_processes_for_death_works_if_child_is_signalled_with_sigterm)
402 using namespace ::testing;
404 ChildDeathObserverEventCollector signal_trap;
406 EXPECT_TRUE(init.death_observer->add(child));
408 core::ScopedConnection sc
412 signal_trap.on_child_died(child);
416 EXPECT_CALL(signal_trap, on_child_died(_))
420 init.signal_trap.get(),
423 std::thread worker{[]() { init.signal_trap->run(); }};
427 if (worker.joinable())
431 TEST(ChildProcess, ensure_that_forked_children_are_cleaned_up)
433 static const unsigned int child_process_count = 100;
434 unsigned int counter = 1;
436 core::ScopedConnection sc
442 if (counter == child_process_count)
444 init.signal_trap->stop();
449 std::thread t([]() {init.signal_trap->run();});
451 for (
unsigned int i = 0; i < child_process_count; i++)
456 init.death_observer->add(child);
459 std::this_thread::sleep_for(std::chrono::milliseconds{5});
465 EXPECT_EQ(child_process_count, counter);
468 TEST(StreamRedirect, redirecting_stdin_stdout_stderr_works)
484 ASSERT_TRUE(child.
pid() > 0);
486 const std::string echo_value{
"42"};
487 child.
cin() << echo_value << std::endl;
489 EXPECT_NO_THROW(child.
cout() >> line);
490 EXPECT_EQ(echo_value, line);
491 EXPECT_NO_THROW(child.
cerr() >> line);
492 EXPECT_EQ(echo_value, line);
497 TEST(Environment, iterating_the_environment_does_not_throw)
500 [](
const std::string& key,
const std::string& value)
502 std::cout << key <<
" -> " << value << std::endl;
506 TEST(Environment, specifying_default_value_for_get_returns_correct_result)
508 const std::string expected_value{
"42"};
509 EXPECT_EQ(expected_value,
513 TEST(Environment, for_each_returns_correct_results)
515 std::array<std::string, 3> env_keys = {
"totally_non_existant_key_in_env_blubb0",
516 "totally_non_existant_key_in_env_blubb1",
517 "totally_non_existant_key_in_env_blubb2"};
518 std::array<std::string, 3> env_vars = {env_keys[0] +
"=" +
"hello",
519 env_keys[1] +
"=" +
"",
520 env_keys[2] +
"=" +
"string=hello"};
521 for(
auto const& env_var : env_vars )
523 ::putenv(const_cast<char*>(env_var.c_str()));
528 if (key == env_keys[0])
530 EXPECT_EQ(
"hello", value);
532 else if (key == env_keys[1])
534 EXPECT_EQ(
"", value);
536 else if (key == env_keys[2])
538 EXPECT_EQ(
"string=hello", value);
static ChildProcess invalid()
Creates an invalid ChildProcess.
static Process invalid()
Returns an invalid instance for testing purposes.
std::istream & cout()
Access this process's stdout.
The process was signalled and stopped.
std::ostream & cin()
Access this process's stdin.
The Process class models a process and possible operations on it.
The process exited normally.
static std::unique_ptr< DeathObserver > create_once_with_signal_trap(std::shared_ptr< SignalTrap > trap)
Creates the unique instance of class DeathObserver.
The process was signalled and terminated.
CORE_POSIX_DLL_PUBLIC std::istream & cin() noexcept(true)
Access this process's stdin.
EXPECT_ANY_THROW(auto death_observer=core::posix::ChildProcess::DeathObserver::create_once_with_signal_trap(trap))
CORE_POSIX_DLL_PUBLIC void for_each(const std::function< void(const std::string &, const std::string &)> &functor) noexcept(true)
for_each invokes a functor for every key-value pair in the environment.
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process...
CORE_POSIX_DLL_PUBLIC std::ostream & cerr() noexcept(true)
Access this process's stderr.
CORE_POSIX_DLL_PUBLIC void set_or_throw(const std::string &key, const std::string &value)
set_or_throw will adjust the contents of the variable identified by key to the provided value...
TEST_F(ForkedSpinningProcess, throwing_access_to_process_group_id_of_a_forked_process_works)
CORE_POSIX_DLL_PUBLIC ChildProcess exec(const std::string &fn, const std::vector< std::string > &argv, const std::map< std::string, std::string > &env, const StandardStream &flags)
exec execve's the executable with the provided arguments and environment.
virtual pid_t pid() const
Query the pid of the process.
virtual void stop()=0
Stops execution of the signal trap.
virtual ProcessGroup process_group(std::error_code &se) const noexcept(true)
Queries the id of the process group this process belongs to.
std::istream & cerr()
Access this process's stderr.
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
CORE_POSIX_DLL_PUBLIC void unset_or_throw(const std::string &key)
unset_or_throw removes the variable with name key from the environment.
wait::Result wait_for(const wait::Flags &flags)
Wait for the child process to change state.
The Process class models a child process of this process.
CORE_POSIX_DLL_PUBLIC std::shared_ptr< SignalTrap > trap_signals_for_all_subsequent_threads(std::initializer_list< core::posix::Signal > blocked_signals)
Traps the specified signals for the current thread, and inherits the respective signal mask to all ch...
CORE_POSIX_DLL_PUBLIC std::ostream & cout() noexcept(true)
Access this process's stdout.
CORE_POSIX_DLL_PUBLIC std::string get(const std::string &key, const std::string &default_value=std::string()) noexcept(true)
get queries the value of an environment variable.
Also wait for state changes in untraced children.
CORE_POSIX_DLL_PUBLIC Process parent() noexcept(true)
Query the parent of the process.
TEST(PosixProcess, ctor_throws_for_invalid_pid)
virtual void send_signal_or_throw(Signal signal)
Sends a signal to this signalable object.