00001
00002
00003 #include <wibble/sys/childprocess.h>
00004
00005 #ifdef POSIX
00006 #include <wibble/sys/process.h>
00007 #include <wibble/sys/exec.h>
00008 #include <cstdlib>
00009 #include <iostream>
00010
00011 #include <wibble/test.h>
00012
00013 using namespace std;
00014 using namespace wibble::sys;
00015
00016 class EndlessChild : public ChildProcess
00017 {
00018 protected:
00019 int main()
00020 {
00021 while (true)
00022 sleep(60);
00023 return 0;
00024 }
00025 };
00026
00027 class TestChild : public ChildProcess
00028 {
00029 protected:
00030 int main()
00031 {
00032 cout << "antani" << endl;
00033 return 0;
00034 }
00035 };
00036
00037 std::string suckFd(int fd)
00038 {
00039 std::string res;
00040 char c;
00041 while (true)
00042 {
00043 int r = read(fd, &c, 1);
00044 if (r == 0)
00045 break;
00046 if (r < 0)
00047 throw wibble::exception::System("reading data from file descriptor");
00048 res += c;
00049 }
00050 return res;
00051 }
00052
00053 struct TestChildprocess {
00054
00055
00056 Test kill() {
00057 EndlessChild child;
00058
00059
00060 pid_t pid = child.fork();
00061
00062
00063 assert(pid != 0);
00064
00065
00066 child.kill(2);
00067
00068
00069 int res = child.wait();
00070
00071
00072 assert(WIFSIGNALED(res));
00073 assert_eq(WTERMSIG(res), 2);
00074 }
00075
00076
00077 Test output() {
00078 TestChild child;
00079 int out;
00080
00081
00082 pid_t pid = child.forkAndRedirect(0, &out, 0);
00083 assert(pid != 0);
00084
00085
00086 assert_eq(suckFd(out), "antani\n");
00087
00088
00089 assert_eq(child.wait(), 0);
00090 }
00091
00092 Test redirect() {
00093 Exec child("/bin/echo");
00094 child.args.push_back("antani");
00095 int out;
00096
00097
00098 pid_t pid = child.forkAndRedirect(0, &out, 0);
00099 assert(pid != 0);
00100
00101
00102 assert_eq(suckFd(out), "antani\n");
00103
00104
00105 assert_eq(child.wait(), 0);
00106 }
00107
00108 Test shellCommand() {
00109 ShellCommand child("A=antani; echo $A");
00110 int out;
00111
00112
00113 pid_t pid = child.forkAndRedirect(0, &out, 0);
00114 assert(pid != 0);
00115
00116
00117 assert_eq(suckFd(out), "antani\n");
00118
00119
00120 assert_eq(child.wait(), 0);
00121 }
00122
00123 };
00124 #endif
00125