/usr/include/wibble/sys/childprocess.test.h is in libwibble-dev 1.1-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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /* -*- 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:
|