wibble 1.1
childprocess.test.h
Go to the documentation of this file.
1/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2 (c) 2007 Enrico Zini <enrico@enricozini.org> */
4
6#include <wibble/sys/exec.h>
7#include <cstdlib>
8#include <unistd.h>
9#include <iostream>
10#include <unistd.h>
11
12#include <wibble/test.h>
13
14
15using namespace std;
16using namespace wibble::sys;
17
18#ifdef _WIN32
19#define sleep Sleep
20#endif
21
23{
24protected:
25 int main()
26 {
27 while (true)
28 sleep(60);
29 return 0;
30 }
31};
32
33class TestChild : public ChildProcess
34{
35protected:
36 int main()
37 {
38 cout << "antani" << endl;
39 return 0;
40 }
41};
42
43std::string suckFd(int fd)
44{
45 std::string res;
46 char c;
47 while (true)
48 {
49 int r = read(fd, &c, 1);
50 if (r == 0)
51 break;
52 if (r < 0)
53 throw wibble::exception::System("reading data from file descriptor");
54 res += c;
55 }
56 return res;
57}
58
60
61 // Try running the child process and kill it
63#ifdef POSIX
64 EndlessChild child;
65
66 // Start the child
67 pid_t pid = child.fork();
68
69 // We should get a nonzero pid
70 assert(pid != 0);
71
72 // Send SIGQUIT
73 child.kill(2);
74
75 // Wait for the child to terminate
76 int res = child.wait();
77
78 // Check that it was indeed terminated by signal 2
79#pragma GCC diagnostic push
80#pragma GCC diagnostic ignored "-Wold-style-cast"
81 assert(WIFSIGNALED(res));
82 assert_eq(WTERMSIG(res), 2);
83#pragma GCC diagnostic pop
84#endif
85 }
86
87 // Try getting the output of the child process
89#ifdef POSIX
90 TestChild child;
91 int out;
92
93 // Fork the child redirecting its stdout
94 pid_t pid = child.forkAndRedirect(0, &out, 0);
95 assert(pid != 0);
96
97 // Read the child output
98 assert_eq(suckFd(out), "antani\n");
99
100 // Wait for the child to terminate
101 assert_eq(child.wait(), 0);
102#endif
103 }
104
106 Exec child("echo");
107 child.searchInPath = true;
108 child.args.push_back("antani");
109 int out;
110
111 // Fork the child redirecting its stdout
112 pid_t pid = child.forkAndRedirect(0, &out, 0);
113 assert(pid != 0);
114
115 // Read the child output
116 assert_eq(suckFd(out), "antani\n");
117
118 // Wait for the child to terminate
119 assert_eq(child.wait(), 0);
120 }
121
123 ShellCommand child("A=antani; echo $A");
124 int out;
125
126 // Fork the child redirecting its stdout
127 pid_t pid = child.forkAndRedirect(0, &out, 0);
128 assert(pid != 0);
129
130 // Read the child output
131 assert_eq(suckFd(out), "antani\n");
132
133 // Wait for the child to terminate
134 assert_eq(child.wait(), 0);
135 }
136
138 Exec child("cat");
139 child.searchInPath = true;
140 int in, out;
141
142 // Fork the child redirecting its stdout
143 child.forkAndRedirect(&in, &out, 0);
144 // assert(pid != 0);
145 write(in, "hello\n", 6);
146 close(in);
147
148 // Read the child output
149 assert_eq(suckFd(out), "hello\n");
150
151 // Wait for the child to terminate
152 assert_eq(child.wait(), 0);
153 }
154
155};
156// vim:set ts=4 sw=4:
std::string suckFd(int fd)
Definition childprocess.test.h:43
Definition childprocess.test.h:23
int main()
Main function to be called in the child process after it has forked.
Definition childprocess.test.h:25
Definition childprocess.test.h:34
int main()
Main function to be called in the child process after it has forked.
Definition childprocess.test.h:36
Base class for system exceptions.
Definition exception.h:397
Fork a child process.
Definition childprocess.h:43
pid_t fork()
For a subprocess to run proc.
int wait(struct rusage *ru=0)
Wait for the child to finish, returning its exit status and optionally storing resource usage informa...
Definition childprocess.cpp:270
void kill(int signal)
Send the given signal to the process.
Definition childprocess.cpp:310
pid_t forkAndRedirect(int *stdinfd=0, int *stdoutfd=0, int *stderrfd=0)
Definition childprocess.h:107
Execute external commands, either forked as a ChildProcess or directly using exec().
Definition exec.h:34
bool searchInPath
Set to true if the file is to be searched in the current $PATH.
Definition exec.h:78
std::vector< std::string > args
Arguments for the process to execute.
Definition exec.h:59
Execute a shell command using /bin/sh -c.
Definition exec.h:98
Definition buffer.cpp:28
void sleep(int secs)
Portable version of sleep.
Definition thread.cpp:31
Definition childprocess.test.h:59
Test shellCommand()
Definition childprocess.test.h:122
Test redirect()
Definition childprocess.test.h:105
Test kill()
Definition childprocess.test.h:62
Test inout()
Definition childprocess.test.h:137
Test output()
Definition childprocess.test.h:88
Definition amorph.h:30
#define assert_eq(x, y)
Definition test.h:33
#define assert(x)
Definition test.h:30