/usr/include/wibble/sys/childprocess.test.h is in libwibble-dev 0.1.28-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
(c) 2007 Enrico Zini <enrico@enricozini.org> */
#include <wibble/sys/childprocess.h>
#ifdef POSIX
#include <wibble/sys/process.h>
#include <wibble/sys/exec.h>
#include <cstdlib>
#include <iostream>
#include <wibble/test.h>
using namespace std;
using namespace wibble::sys;
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() {
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
assert(WIFSIGNALED(res));
assert_eq(WTERMSIG(res), 2);
}
// Try getting the output of the child process
Test output() {
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);
}
Test redirect() {
Exec child("/bin/echo");
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);
}
};
#endif
// vim:set ts=4 sw=4:
|