childprocess.test.h
Go to the documentation of this file.
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
(c) 2007 Enrico Zini <enrico@enricozini.org> */
#include <wibble/sys/childprocess.h>
#include <wibble/sys/process.h>
#include <wibble/sys/exec.h>
#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <wibble/test.h>
using namespace std;
using namespace wibble::sys;
#ifdef _WIN32
#define sleep Sleep
#endif
class EndlessChild : public ChildProcess
{
protected:
int main()
{
while (true)
sleep(60);
return 0;
}
};
class TestChild : public ChildProcess
{
protected:
int main()
{
cout << "antani" << endl;
return 0;
}
};
std::string suckFd(int fd)
{
std::string res;
char c;
while (true)
{
int r = read(fd, &c, 1);
if (r == 0)
break;
if (r < 0)
throw wibble::exception::System("reading data from file descriptor");
res += c;
}
return res;
}
struct TestChildprocess {
// Try running the child process and kill it
Test kill() {
#ifdef POSIX
EndlessChild child;
// Start the child
pid_t pid = child.fork();
// We should get a nonzero pid
assert(pid != 0);
// Send SIGQUIT
child.kill(2);
// Wait for the child to terminate
int res = child.wait();
// Check that it was indeed terminated by signal 2
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
assert(WIFSIGNALED(res));
assert_eq(WTERMSIG(res), 2);
#pragma GCC diagnostic pop
#endif
}
// Try getting the output of the child process
Test output() {
#ifdef POSIX
TestChild child;
int out;
// Fork the child redirecting its stdout
pid_t pid = child.forkAndRedirect(0, &out, 0);
assert(pid != 0);
// Read the child output
assert_eq(suckFd(out), "antani\n");
// Wait for the child to terminate
assert_eq(child.wait(), 0);
#endif
}
Test redirect() {
Exec child("echo");
child.searchInPath = true;
child.args.push_back("antani");
int out;
// Fork the child redirecting its stdout
pid_t pid = child.forkAndRedirect(0, &out, 0);
assert(pid != 0);
// Read the child output
assert_eq(suckFd(out), "antani\n");
// Wait for the child to terminate
assert_eq(child.wait(), 0);
}
Test shellCommand() {
ShellCommand child("A=antani; echo $A");
int out;
// Fork the child redirecting its stdout
pid_t pid = child.forkAndRedirect(0, &out, 0);
assert(pid != 0);
// Read the child output
assert_eq(suckFd(out), "antani\n");
// Wait for the child to terminate
assert_eq(child.wait(), 0);
}
Test inout() {
Exec child("cat");
child.searchInPath = true;
int in, out;
// Fork the child redirecting its stdout
child.forkAndRedirect(&in, &out, 0);
// assert(pid != 0);
write(in, "hello\n", 6);
close(in);
// Read the child output
assert_eq(suckFd(out), "hello\n");
// Wait for the child to terminate
assert_eq(child.wait(), 0);
}
};
// vim:set ts=4 sw=4:
TestChild::main
int main()
Main function to be called in the child process after it has forked.
Definition: childprocess.test.h:36
wibble::sys::ChildProcess
Fork a child process.
Definition: childprocess.h:42
Test
void Test
Definition: test.h:178
TestChildprocess::output
Test output()
Definition: childprocess.test.h:88
test.h
std
TestChildprocess::shellCommand
Test shellCommand()
Definition: childprocess.test.h:122
wibble::sys
Definition: buffer.cpp:28
wibble::sys::Exec::searchInPath
bool searchInPath
Set to true if the file is to be searched in the current $PATH.
Definition: exec.h:78
EndlessChild
Definition: childprocess.test.h:22
wibble::sys::sleep
void sleep(int secs)
Portable version of sleep.
Definition: thread.cpp:31
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
wibble::sys::ChildProcess::wait
int wait(struct rusage *ru=0)
Wait for the child to finish, returning its exit status and optionally storing resource usage informa...
Definition: childprocess.cpp:270
assert
#define assert(x)
Definition: test.h:30
EndlessChild::main
int main()
Main function to be called in the child process after it has forked.
Definition: childprocess.test.h:25
TestChildprocess::kill
Test kill()
Definition: childprocess.test.h:62
wibble::sys::ChildProcess::forkAndRedirect
pid_t forkAndRedirect(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition: childprocess.h:107
wibble::sys::ChildProcess::kill
void kill(int signal)
Send the given signal to the process.
Definition: childprocess.cpp:310
TestChildprocess::redirect
Test redirect()
Definition: childprocess.test.h:105
process.h
suckFd
std::string suckFd(int fd)
Definition: childprocess.test.h:43
wibble::sys::ChildProcess::fork
pid_t fork()
For a subprocess to run proc.
exec.h
TestChild
Definition: childprocess.test.h:33
TestChildprocess
Definition: childprocess.test.h:59
TestChildprocess::inout
Test inout()
Definition: childprocess.test.h:137
wibble::sys::Exec::args
std::vector< std::string > args
Arguments for the process to execute.
Definition: exec.h:59
childprocess.h
wibble::exception::System
Base class for system exceptions.
Definition: exception.h:396
wibble::sys::ShellCommand
Execute a shell command using /bin/sh -c.
Definition: exec.h:97
wibble::sys::Exec
Execute external commands, either forked as a ChildProcess or directly using exec().
Definition: exec.h:33