2
2
// / \author Diffblue Ltd.
3
3
// / Unit tests for checking the piped process communication mechanism.
4
4
5
- #ifdef _WIN32
6
- // No unit tests yet!
7
- #else
8
-
9
5
# include < testing-utils/use_catch.h>
10
6
# include < util/optional.h>
11
7
# include < util/piped_process.h>
@@ -18,8 +14,12 @@ TEST_CASE(
18
14
const std::string to_be_echoed = " The Jabberwocky" ;
19
15
// Need to give path to avoid shell built-in invocation
20
16
std::vector<std::string> commands;
17
+ #ifdef _WIN32
18
+ commands.push_back (" cmd /c echo The Jabberwocky" );
19
+ #else
21
20
commands.push_back (" /bin/echo" );
22
21
commands.push_back (to_be_echoed);
22
+ #endif
23
23
piped_processt process = piped_processt (commands);
24
24
25
25
// This is an indirect way to detect when the pipe has something. This
@@ -35,9 +35,14 @@ TEST_CASE(
35
35
" Creating a sub process with a binary that doesn't exist." ,
36
36
" [core][util][piped_process]" )
37
37
{
38
- const std::string expected_error (" Launching abcde failed" );
39
38
std::vector<std::string> commands;
39
+ #ifdef _WIN32
40
+ const std::string expected_error (" 'abcde' is not recogni" );
41
+ commands.push_back (" cmd /c abcde" );
42
+ #else
43
+ const std::string expected_error (" Launching abcde failed" );
40
44
commands.push_back (" abcde" );
45
+ #endif
41
46
piped_processt process = piped_processt (commands);
42
47
43
48
// This is an indirect way to detect when the pipe has something. This
@@ -61,6 +66,39 @@ TEST_CASE(
61
66
REQUIRE (response.find (expected_error) < response.length () - 5 );
62
67
}
63
68
69
+ // This is a test of child termination, it's not perfect and could go wrong
70
+ // if run at midnight, but it's sufficient for a basic check for now.
71
+ TEST_CASE (
72
+ " Creating a sub process and terminate it." ,
73
+ " [core][util][piped_process]" )
74
+ {
75
+ std::vector<std::string> commands;
76
+ #ifdef _WIN32
77
+ commands.push_back (" cmd /c ping 127.0.0.1 -n 6 > nul" );
78
+ SYSTEMTIME st;
79
+ GetSystemTime (&st);
80
+ WORD calc = 3600 * st.wHour + 60 * st.wMinute + st.wSecond ;
81
+ piped_processt process = piped_processt (commands);
82
+ process.~piped_processt ();
83
+ GetSystemTime (&st);
84
+ // New time minus old time, could go wrong at midnight
85
+ calc = 3600 * st.wHour + 60 * st.wMinute + st.wSecond - calc;
86
+ #else
87
+ // Currently not working under Linxu/MacOS?!
88
+ // commands.push_back("sleep 6");
89
+ // time_t calc = time(NULL);
90
+ // piped_processt process = piped_processt(commands);
91
+ // process.~piped_processt();
92
+ // calc = time(NULL) - calc;
93
+ size_t calc = 0 ;
94
+ #endif
95
+ // Command should take >5 seconds, check we called destructor and
96
+ // moved on in less than 2 seconds.
97
+ REQUIRE (calc < 2 );
98
+ }
99
+
100
+ #ifndef _WIN32
101
+ // No Windows tests for z3 due to path and dependency issues.
64
102
TEST_CASE (
65
103
" Creating a sub process of z3 and read a response from an echo command." ,
66
104
" [core][util][piped_process][.z3]" )
@@ -266,5 +304,4 @@ TEST_CASE(
266
304
REQUIRE (
267
305
process.send (" (exit)\n " ) == piped_processt::send_responset::SUCCEEDED);
268
306
}
269
-
270
307
#endif
0 commit comments