aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver-test/process-tests.cpp
blob: 649f24f54ad49f554ee8502c8338e53542216343 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/zencore.h>

#if ZEN_WITH_TESTS

#	include "zenserver-test.h"

#	include <zencore/filesystem.h>
#	include <zencore/process.h>
#	include <zencore/testing.h>

#	if ZEN_PLATFORM_WINDOWS
#		include <zencore/windows.h>
#	else
#		include <unistd.h>
#	endif

namespace zen::tests {

using namespace std::literals;

static std::filesystem::path
GetAppStubPath()
{
	return TestEnv.ProgramBaseDir() / ("zentest-appstub" ZEN_EXE_SUFFIX_LITERAL);
}

// Read all available data from the read end of a StdoutPipeHandles.
// Must be called after CloseWriteEnd() so that the read will see EOF.
static std::string
ReadAllFromPipe(StdoutPipeHandles& Pipe)
{
	std::string Result;
	char		Buffer[4096];

#	if ZEN_PLATFORM_WINDOWS
	DWORD BytesRead = 0;
	while (::ReadFile(Pipe.ReadHandle, Buffer, sizeof(Buffer), &BytesRead, nullptr) && BytesRead > 0)
	{
		Result.append(Buffer, BytesRead);
	}
#	else
	ssize_t BytesRead = 0;
	while ((BytesRead = read(Pipe.ReadFd, Buffer, sizeof(Buffer))) > 0)
	{
		Result.append(Buffer, static_cast<size_t>(BytesRead));
	}
#	endif

	return Result;
}

TEST_SUITE_BEGIN("server.process");

//////////////////////////////////////////////////////////////////////////

TEST_CASE("pipe.capture_stdout")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	const std::string	  ExpectedOutput = "hello_from_pipe_test";
	std::filesystem::path AppStub		 = GetAppStubPath();
	std::string			  CommandLine	 = fmt::format("zentest-appstub -echo={}", ExpectedOutput);

	CreateProcOptions Options;
	Options.StdoutPipe = &Pipe;

	CreateProcResult ProcResult = CreateProc(AppStub, CommandLine, Options);

	ProcessHandle Process;
	Process.Initialize(ProcResult);

	// Close the write end, then drain before Wait() to avoid deadlock if output fills the pipe buffer.
	Pipe.CloseWriteEnd();

	std::string Output = ReadAllFromPipe(Pipe);

	Process.Wait();

	// The appstub also prints "[zentest] exiting with exit code: 0\n"
	CHECK(Output.find(ExpectedOutput) != std::string::npos);
	CHECK_EQ(Process.GetExitCode(), 0);
}

TEST_CASE("pipe.capture_multiline")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	std::filesystem::path AppStub	  = GetAppStubPath();
	std::string			  CommandLine = "zentest-appstub -echo=line1 -echo=line2 -echo=line3";

	CreateProcOptions Options;
	Options.StdoutPipe = &Pipe;

	CreateProcResult ProcResult = CreateProc(AppStub, CommandLine, Options);

	ProcessHandle Process;
	Process.Initialize(ProcResult);

	Pipe.CloseWriteEnd();

	std::string Output = ReadAllFromPipe(Pipe);

	Process.Wait();

	CHECK(Output.find("line1") != std::string::npos);
	CHECK(Output.find("line2") != std::string::npos);
	CHECK(Output.find("line3") != std::string::npos);
	CHECK_EQ(Process.GetExitCode(), 0);
}

TEST_CASE("pipe.raii_cleanup")
{
	// Verify that StdoutPipeHandles cleans up handles when it goes out of scope
	// (no leaked handles). We can't directly assert on handle counts, but we can
	// verify that creating and destroying many pipes doesn't fail.
	for (int i = 0; i < 100; ++i)
	{
		StdoutPipeHandles Pipe;
		REQUIRE(CreateStdoutPipe(Pipe));
		// Pipe goes out of scope here — destructor should close both ends
	}
}

TEST_CASE("pipe.move_semantics")
{
	StdoutPipeHandles Original;
	REQUIRE(CreateStdoutPipe(Original));

	// Move-construct a new pipe from Original
	StdoutPipeHandles Moved(std::move(Original));

#	if ZEN_PLATFORM_WINDOWS
	CHECK(Moved.ReadHandle != nullptr);
	CHECK(Moved.WriteHandle != nullptr);
	CHECK(Original.ReadHandle == nullptr);
	CHECK(Original.WriteHandle == nullptr);
#	else
	CHECK(Moved.ReadFd >= 0);
	CHECK(Moved.WriteFd >= 0);
	CHECK(Original.ReadFd == -1);
	CHECK(Original.WriteFd == -1);
#	endif

	// Move-assign
	StdoutPipeHandles Assigned;
	Assigned = std::move(Moved);

#	if ZEN_PLATFORM_WINDOWS
	CHECK(Assigned.ReadHandle != nullptr);
	CHECK(Assigned.WriteHandle != nullptr);
	CHECK(Moved.ReadHandle == nullptr);
	CHECK(Moved.WriteHandle == nullptr);
#	else
	CHECK(Assigned.ReadFd >= 0);
	CHECK(Assigned.WriteFd >= 0);
	CHECK(Moved.ReadFd == -1);
	CHECK(Moved.WriteFd == -1);
#	endif

	// Assigned goes out of scope — destructor closes handles
}

TEST_CASE("pipe.close_is_idempotent")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	Pipe.Close();
	// Calling Close again should be safe (no double-close)
	Pipe.Close();

#	if ZEN_PLATFORM_WINDOWS
	CHECK(Pipe.ReadHandle == nullptr);
	CHECK(Pipe.WriteHandle == nullptr);
#	else
	CHECK(Pipe.ReadFd == -1);
	CHECK(Pipe.WriteFd == -1);
#	endif
}

TEST_CASE("pipe.close_write_end_only")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	Pipe.CloseWriteEnd();

#	if ZEN_PLATFORM_WINDOWS
	CHECK(Pipe.ReadHandle != nullptr);
	CHECK(Pipe.WriteHandle == nullptr);
#	else
	CHECK(Pipe.ReadFd >= 0);
	CHECK(Pipe.WriteFd == -1);
#	endif

	// Remaining read handle cleaned up by destructor
}

TEST_CASE("pipe.capture_with_nonzero_exit")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	std::filesystem::path AppStub	  = GetAppStubPath();
	std::string			  CommandLine = "zentest-appstub -echo=before_exit -f=42";

	CreateProcOptions Options;
	Options.StdoutPipe = &Pipe;

	CreateProcResult ProcResult = CreateProc(AppStub, CommandLine, Options);

	ProcessHandle Process;
	Process.Initialize(ProcResult);

	Pipe.CloseWriteEnd();

	std::string Output = ReadAllFromPipe(Pipe);

	Process.Wait();

	CHECK(Output.find("before_exit") != std::string::npos);
	CHECK_EQ(Process.GetExitCode(), 42);
}

TEST_CASE("pipe.stderr_on_shared_pipe")
{
	StdoutPipeHandles Pipe;
	REQUIRE(CreateStdoutPipe(Pipe));

	std::filesystem::path AppStub	  = GetAppStubPath();
	std::string			  CommandLine = "zentest-appstub -echo=from_stdout -echoerr=from_stderr";

	CreateProcOptions Options;
	Options.StdoutPipe = &Pipe;

	CreateProcResult ProcResult = CreateProc(AppStub, CommandLine, Options);

	ProcessHandle Process;
	Process.Initialize(ProcResult);

	Pipe.CloseWriteEnd();

	std::string Output = ReadAllFromPipe(Pipe);

	Process.Wait();

	// Both stdout and stderr content should appear on the shared pipe
	CHECK(Output.find("from_stdout") != std::string::npos);
	CHECK(Output.find("from_stderr") != std::string::npos);
	CHECK_EQ(Process.GetExitCode(), 0);
}

TEST_CASE("pipe.separate_stderr")
{
	StdoutPipeHandles StdoutPipe;
	StdoutPipeHandles StderrPipe;
	REQUIRE(CreateStdoutPipe(StdoutPipe));
	REQUIRE(CreateStdoutPipe(StderrPipe));

	std::filesystem::path AppStub	  = GetAppStubPath();
	std::string			  CommandLine = "zentest-appstub -echo=on_stdout -echoerr=on_stderr";

	CreateProcOptions Options;
	Options.StdoutPipe = &StdoutPipe;
	Options.StderrPipe = &StderrPipe;

	CreateProcResult ProcResult = CreateProc(AppStub, CommandLine, Options);

	ProcessHandle Process;
	Process.Initialize(ProcResult);

	StdoutPipe.CloseWriteEnd();
	StderrPipe.CloseWriteEnd();

	std::string StdoutOutput = ReadAllFromPipe(StdoutPipe);
	std::string StderrOutput = ReadAllFromPipe(StderrPipe);

	Process.Wait();

	CHECK(StdoutOutput.find("on_stdout") != std::string::npos);
	CHECK(StderrOutput.find("on_stderr") != std::string::npos);
	// Verify separation: stderr content should NOT appear in stdout pipe
	CHECK(StdoutOutput.find("on_stderr") == std::string::npos);
	CHECK(StderrOutput.find("on_stdout") == std::string::npos);
	CHECK_EQ(Process.GetExitCode(), 0);
}

//////////////////////////////////////////////////////////////////////////

TEST_SUITE_END();

}  // namespace zen::tests

#endif