]> git.localhorst.tv Git - blank.git/blob - tst/app/ProcessTest.cpp
db181aafc3c1e433ae97189ccea594affda33201
[blank.git] / tst / app / ProcessTest.cpp
1 #include "ProcessTest.hpp"
2
3 #include "app/Process.hpp"
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::ProcessTest);
6
7 using namespace std;
8
9
10 namespace blank {
11 namespace test {
12
13 void ProcessTest::setUp() {
14
15 }
16
17 void ProcessTest::tearDown() {
18
19 }
20
21
22 void ProcessTest::testExit() {
23 #ifdef __WIN32
24 #  error "TODO: implement Process tests for windows"
25 #else
26
27         {
28                 Process proc("/usr/bin/env", { "env", "true" }, { });
29                 int status = proc.Join();
30                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
31                         "exit status of true assumed 0",
32                         0, status);
33         }
34
35         {
36                 Process proc("/usr/bin/env", { "env", "false" }, { });
37                 int status = proc.Join();
38                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
39                         "exit status of false assumed 1",
40                         1, status);
41         }
42
43 #endif
44 }
45
46 void ProcessTest::testStream() {
47 #ifdef __WIN32
48 #  error "TODO: implemente Process tests for windows"
49 #else
50
51         {
52                 const string test_input("hello, world");
53                 const string expected_output("hello, world\n");
54                 Process proc("/usr/bin/env", { "env", "echo", test_input.c_str() }, { });
55                 char buffer[expected_output.length() + 1];
56                 size_t len = proc.ReadOut(buffer, sizeof(buffer));
57                 const string output(buffer, len);
58                 int status = proc.Join();
59                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
60                         "exit status of echo assumed 0",
61                         0, status);
62                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
63                         "unexpected length of echo output",
64                         expected_output.size(), len);
65                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
66                         "unexpected output of echo",
67                         expected_output, output);
68         }
69
70         {
71                 const string test_input("hello, error");
72                 const string expected_output("hello, error\n");
73                 Process proc("/usr/bin/env", { "env", "sh", "-c", "echo $1 >&2", "echo", test_input.c_str() }, { });
74                 char buffer[expected_output.length() + 1];
75                 size_t len = proc.ReadErr(buffer, sizeof(buffer));
76                 const string output(buffer, len);
77                 int status = proc.Join();
78                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
79                         "exit status of echo assumed 0",
80                         0, status);
81                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
82                         "unexpected length of echo output",
83                         expected_output.size(), len);
84                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
85                         "unexpected error output of echo",
86                         expected_output, output);
87         }
88
89
90         {
91                 const string test_input("dog");
92                 const string expected_output("dog");
93                 Process proc("/usr/bin/env", { "env", "cat" }, { });
94                 size_t len = proc.WriteIn(test_input.c_str(), test_input.size());
95                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
96                         "unexpected length of input to cat",
97                         test_input.size(), len);
98
99                 char buffer[expected_output.length() + 1];
100                 len = proc.ReadOut(buffer, sizeof(buffer));
101                 const string output(buffer, len);
102                 int status = proc.Join();
103                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
104                         "exit status of cat assumed 0",
105                         0, status);
106                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
107                         "unexpected length of cat output",
108                         expected_output.size(), len);
109                 CPPUNIT_ASSERT_EQUAL_MESSAGE(
110                         "unexpected output of cat",
111                         expected_output, output);
112         }
113
114 #endif
115 }
116
117 }
118 }