File: childprocess.test.h

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (124 lines) | stat: -rw-r--r-- 2,648 bytes parent folder | download
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
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
   (c) 2007 Enrico Zini <enrico@enricozini.org> */

#include <wibble/sys/process.h>
#include <wibble/sys/childprocess.h>
#include <wibble/sys/exec.h>
#include <cstdlib>
#include <iostream>

#include <wibble/test.h>

using namespace std;
using namespace wibble::sys;

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() {
        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
        assert(WIFSIGNALED(res));
        assert_eq(WTERMSIG(res), 2);
    }

    // Try getting the output of the child process
    Test output() {
        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);
    }

    Test redirect() {
        Exec child("/bin/echo");
        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);
    }

};

// vim:set ts=4 sw=4: