• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

childprocess.test.h

Go to the documentation of this file.
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
00002    (c) 2007 Enrico Zini <enrico@enricozini.org> */
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     // Try running the child process and kill it
00056     Test kill() {
00057         EndlessChild child;
00058 
00059         // Start the child
00060         pid_t pid = child.fork();
00061 
00062         // We should get a nonzero pid
00063         assert(pid != 0);
00064 
00065         // Send SIGQUIT
00066         child.kill(2);
00067 
00068         // Wait for the child to terminate
00069         int res = child.wait();
00070 
00071         // Check that it was indeed terminated by signal 2
00072         assert(WIFSIGNALED(res));
00073         assert_eq(WTERMSIG(res), 2);
00074     }
00075 
00076     // Try getting the output of the child process
00077     Test output() {
00078         TestChild child;
00079         int out;
00080 
00081         // Fork the child redirecting its stdout
00082         pid_t pid = child.forkAndRedirect(0, &out, 0);
00083         assert(pid != 0);
00084 
00085         // Read the child output
00086         assert_eq(suckFd(out), "antani\n");
00087 
00088         // Wait for the child to terminate
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         // Fork the child redirecting its stdout
00098         pid_t pid = child.forkAndRedirect(0, &out, 0);
00099         assert(pid != 0);
00100 
00101         // Read the child output
00102         assert_eq(suckFd(out), "antani\n");
00103 
00104         // Wait for the child to terminate
00105         assert_eq(child.wait(), 0);
00106     }
00107 
00108     Test shellCommand() {
00109         ShellCommand child("A=antani; echo $A");
00110         int out;
00111     
00112         // Fork the child redirecting its stdout
00113         pid_t pid = child.forkAndRedirect(0, &out, 0);
00114         assert(pid != 0);
00115 
00116         // Read the child output
00117         assert_eq(suckFd(out), "antani\n");
00118 
00119         // Wait for the child to terminate
00120         assert_eq(child.wait(), 0);
00121     }
00122 
00123 };
00124 #endif
00125 // vim:set ts=4 sw=4:

Generated on Tue May 10 2011 16:51:50 for wibble by  doxygen 1.7.1