Skip to content

Commit f86ea58

Browse files
committed
Add unit tests for Windows piped process IPC
Adds unit tests for Windows piped process communication. These duplicate the Linux/MacOS ones in most cases. There is also a termination one that does not have a working Linux/MacOS version. (Also z3 is not tested on Windows).
1 parent fde3f48 commit f86ea58

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

unit/util/piped_process.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
/// \author Diffblue Ltd.
33
/// Unit tests for checking the piped process communication mechanism.
44

5-
#ifdef _WIN32
6-
// No unit tests yet!
7-
#else
8-
95
# include <testing-utils/use_catch.h>
106
# include <util/optional.h>
117
# include <util/piped_process.h>
@@ -18,8 +14,12 @@ TEST_CASE(
1814
const std::string to_be_echoed = "The Jabberwocky";
1915
// Need to give path to avoid shell built-in invocation
2016
std::vector<std::string> commands;
17+
#ifdef _WIN32
18+
commands.push_back("cmd /c echo The Jabberwocky");
19+
#else
2120
commands.push_back("/bin/echo");
2221
commands.push_back(to_be_echoed);
22+
#endif
2323
piped_processt process = piped_processt(commands);
2424

2525
// This is an indirect way to detect when the pipe has something. This
@@ -35,9 +35,14 @@ TEST_CASE(
3535
"Creating a sub process with a binary that doesn't exist.",
3636
"[core][util][piped_process]")
3737
{
38-
const std::string expected_error("Launching abcde failed");
3938
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");
4044
commands.push_back("abcde");
45+
#endif
4146
piped_processt process = piped_processt(commands);
4247

4348
// This is an indirect way to detect when the pipe has something. This
@@ -61,6 +66,39 @@ TEST_CASE(
6166
REQUIRE(response.find(expected_error) < response.length() - 5);
6267
}
6368

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.
64102
TEST_CASE(
65103
"Creating a sub process of z3 and read a response from an echo command.",
66104
"[core][util][piped_process][.z3]")
@@ -266,5 +304,4 @@ TEST_CASE(
266304
REQUIRE(
267305
process.send("(exit)\n") == piped_processt::send_responset::SUCCEEDED);
268306
}
269-
270307
#endif

0 commit comments

Comments
 (0)