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