VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTPipe.cpp@ 96781

Last change on this file since 96781 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1/* $Id: tstRTPipe.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTPipe.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/pipe.h>
42
43#include <iprt/env.h>
44#include <iprt/err.h>
45#include <iprt/initterm.h>
46#include <iprt/mem.h>
47#include <iprt/message.h>
48#include <iprt/param.h>
49#include <iprt/process.h>
50#include <iprt/string.h>
51#include <iprt/test.h>
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57static const char g_szTest4Message[] = "This is test #4, everything is working fine.\n\r";
58static const char g_szTest5Message[] = "This is test #5, everything is working fine.\n\r";
59
60
61static RTEXITCODE tstRTPipe5Child(const char *pszPipe)
62{
63 int rc = RTR3InitExeNoArguments(0);
64 if (RT_FAILURE(rc))
65 return RTMsgInitFailure(rc);
66
67 int64_t iNative;
68 rc = RTStrToInt64Full(pszPipe, 10, &iNative);
69 if (RT_FAILURE(rc))
70 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrToUInt64Full(%s) -> %Rrc\n", pszPipe, rc);
71
72 RTPIPE hPipe;
73 rc = RTPipeFromNative(&hPipe, (RTHCINTPTR)iNative, RTPIPE_N_READ);
74 if (RT_FAILURE(rc))
75 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeFromNative(,%s,READ) -> %Rrc\n", pszPipe, rc);
76
77 ///
78 char szTmp[1024];
79 size_t cbRead = 0;
80 rc = RTPipeReadBlocking(hPipe, szTmp, sizeof(szTmp) - 1, &cbRead);
81 if (RT_FAILURE(rc))
82 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeReadBlocking() -> %Rrc\n", rc);
83 szTmp[cbRead] = '\0';
84
85 size_t cbRead2;
86 char szTmp2[4];
87 rc = RTPipeReadBlocking(hPipe, szTmp2, sizeof(szTmp2), &cbRead2);
88 if (rc != VERR_BROKEN_PIPE)
89 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeReadBlocking() -> %Rrc instead of VERR_BROKEN_PIPE\n", rc);
90
91 rc = RTPipeClose(hPipe);
92 if (RT_FAILURE(rc))
93 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeClose() -> %Rrc\n", rc);
94
95 if (memcmp(szTmp, g_szTest5Message, sizeof(g_szTest5Message)))
96 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Message mismatch.\n:Expected '%s'\nGot '%s'\n", g_szTest5Message, szTmp);
97
98 return RTEXITCODE_SUCCESS;
99}
100
101static void tstRTPipe5(void)
102{
103 RTTestISub("Inherit non-standard pipe handle, read end");
104
105 char szPathSelf[RTPATH_MAX];
106 RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szPathSelf, sizeof(szPathSelf)) == szPathSelf);
107
108 RTPIPE hPipeR;
109 RTPIPE hPipeW;
110 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ), VINF_SUCCESS);
111
112 RTHCINTPTR hNative = RTPipeToNative(hPipeR);
113 RTTESTI_CHECK_RETV(hNative != -1);
114
115 char szNative[64];
116 RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative);
117 const char *papszArgs[4] = { szPathSelf, "--child-5", szNative, NULL };
118
119 RTPROCESS hChild;
120 RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS);
121 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
122
123 RTTESTI_CHECK_RC(RTPipeWriteBlocking(hPipeW, g_szTest5Message, sizeof(g_szTest5Message) - 1, NULL), VINF_SUCCESS);
124 int rc;
125 RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeW), VINF_SUCCESS);
126 if (RT_FAILURE(rc))
127 RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS);
128
129 RTPROCSTATUS ProcStatus;
130 RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
131 if (RT_FAILURE(rc))
132 return;
133 RTTESTI_CHECK( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
134 && ProcStatus.iStatus == 0);
135}
136
137
138static RTEXITCODE tstRTPipe4Child(const char *pszPipe)
139{
140 int rc = RTR3InitExeNoArguments(0);
141 if (RT_FAILURE(rc))
142 return RTMsgInitFailure(rc);
143
144 int64_t iNative;
145 rc = RTStrToInt64Full(pszPipe, 10, &iNative);
146 if (RT_FAILURE(rc))
147 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTStrToUInt64Full(%s) -> %Rrc\n", pszPipe, rc);
148
149 RTPIPE hPipe;
150 rc = RTPipeFromNative(&hPipe, (RTHCINTPTR)iNative, RTPIPE_N_WRITE);
151 if (RT_FAILURE(rc))
152 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeFromNative(,%s,WRITE) -> %Rrc\n", pszPipe, rc);
153
154 rc = RTPipeWriteBlocking(hPipe, g_szTest4Message, sizeof(g_szTest4Message) - 1, NULL);
155 if (RT_FAILURE(rc))
156 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeWriteBlocking() -> %Rrc\n", rc);
157
158 rc = RTPipeClose(hPipe);
159 if (RT_FAILURE(rc))
160 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPipeClose() -> %Rrc\n", rc);
161 return RTEXITCODE_SUCCESS;
162}
163
164static void tstRTPipe4(void)
165{
166 RTTestISub("Inherit non-standard pipe handle, write end");
167
168 char szPathSelf[RTPATH_MAX];
169 RTTESTI_CHECK_RETV(RTProcGetExecutablePath(szPathSelf, sizeof(szPathSelf)) == szPathSelf);
170
171 RTPIPE hPipeR;
172 RTPIPE hPipeW;
173 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
174
175 RTHCINTPTR hNative = RTPipeToNative(hPipeW);
176 RTTESTI_CHECK_RETV(hNative != -1);
177
178 char szNative[64];
179 RTStrPrintf(szNative, sizeof(szNative), "%RHi", hNative);
180 const char *papszArgs[4] = { szPathSelf, "--child-4", szNative, NULL };
181
182 RTPROCESS hChild;
183 RTTESTI_CHECK_RC_RETV(RTProcCreate(szPathSelf, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hChild), VINF_SUCCESS);
184 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
185
186 char szTmp[1024];
187 size_t cbRead = 0;
188 int rc;
189 RTTESTI_CHECK_RC(rc = RTPipeReadBlocking(hPipeR, szTmp, sizeof(szTmp) - 1, &cbRead), VINF_SUCCESS);
190 if (RT_FAILURE(rc))
191 cbRead = 0;
192 RTTESTI_CHECK_RETV(cbRead < sizeof(szTmp));
193 szTmp[cbRead] = '\0';
194
195 size_t cbRead2;
196 char szTmp2[4];
197 RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeR, szTmp2, sizeof(szTmp2), &cbRead2), VERR_BROKEN_PIPE);
198 RTTESTI_CHECK_RC(rc = RTPipeClose(hPipeR), VINF_SUCCESS);
199 if (RT_FAILURE(rc))
200 RTTESTI_CHECK_RC(RTProcTerminate(hChild), VINF_SUCCESS);
201
202 RTPROCSTATUS ProcStatus;
203 RTTESTI_CHECK_RC(rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
204 if (RT_FAILURE(rc))
205 return;
206 RTTESTI_CHECK( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
207 && ProcStatus.iStatus == 0);
208 if (memcmp(szTmp, g_szTest4Message, sizeof(g_szTest4Message)))
209 RTTestIFailed("Message mismatch.\n:Expected '%s'\nGot '%s'\n", g_szTest4Message, szTmp);
210}
211
212
213static void tstRTPipe3(void)
214{
215 RTTestISub("Full write buffer");
216
217#ifdef RT_OS_WINDOWS
218 /* Kludge! The RTPipeWrite+RTPipeWriteBlocking code is able to write twice the size
219 of the pipe buffer, so the testcase won't behave right if we go lower
220 than 50% on the readback. */
221 for (unsigned uPct = 50; uPct < 100; uPct += 12)
222#else
223 for (unsigned uPct = 25; uPct < 100; uPct += 12)
224#endif
225 {
226 RTPIPE hPipeR = (RTPIPE)999999;
227 RTPIPE hPipeW = (RTPIPE)999999;
228 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
229 RTTestIErrContext("uPct=%d", uPct);
230
231 static char s_abBuf[_256K];
232 int rc = VINF_SUCCESS;
233 size_t cbTotal = 0;
234 memset(s_abBuf, 0xff, sizeof(s_abBuf));
235 for (;;)
236 {
237 RTTESTI_CHECK(cbTotal < _1G);
238 if (cbTotal > _1G)
239 break;
240
241 size_t cbWritten = _1G;
242 rc = RTPipeWrite(hPipeW, s_abBuf, sizeof(s_abBuf), &cbWritten);
243 RTTESTI_CHECK_MSG(rc == VINF_SUCCESS || rc == VINF_TRY_AGAIN, ("rc=%Rrc\n", rc));
244 if (rc != VINF_SUCCESS)
245 break;
246 cbTotal += cbWritten;
247 }
248
249 if (rc == VINF_TRY_AGAIN)
250 {
251 size_t const cbToRead = RT_MIN(sizeof(s_abBuf), cbTotal * uPct / 100);
252 RTTestIPrintf(RTTESTLVL_ALWAYS, "cbTotal=%zu (%#zx) cbToRead=%zu (%#zx)\n", cbTotal, cbTotal, cbToRead, cbToRead);
253 RTTESTI_CHECK_RC(RTPipeSelectOne(hPipeW, 0), VERR_TIMEOUT);
254 RTTESTI_CHECK_RC(RTPipeSelectOne(hPipeW, 1), VERR_TIMEOUT);
255 size_t cbRead;
256 RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeR, s_abBuf, cbToRead, &cbRead), VINF_SUCCESS);
257 RTTESTI_CHECK_RC(RTPipeSelectOne(hPipeW, 0), VINF_SUCCESS);
258 RTTESTI_CHECK_RC(RTPipeSelectOne(hPipeW, 1), VINF_SUCCESS);
259
260 size_t cbWritten = _1G;
261 rc = RTPipeWrite(hPipeW, s_abBuf, sizeof(s_abBuf), &cbWritten);
262 RTTESTI_CHECK(rc == VINF_SUCCESS);
263 }
264
265 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
266 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
267 }
268}
269
270static void tstRTPipe2(void)
271{
272 RTTestISub("Negative");
273
274 RTPIPE hPipeR = (RTPIPE)1;
275 RTPIPE hPipeW = (RTPIPE)1;
276 RTTESTI_CHECK_RC(RTPipeCreate(&hPipeR, &hPipeW, UINT32_MAX), VERR_INVALID_PARAMETER);
277 RTTESTI_CHECK_RC(RTPipeCreate(NULL, &hPipeW, 0), VERR_INVALID_POINTER);
278 RTTESTI_CHECK_RC(RTPipeCreate(&hPipeR, NULL, 0), VERR_INVALID_POINTER);
279 RTTESTI_CHECK(hPipeR == (RTPIPE)1);
280 RTTESTI_CHECK(hPipeW == (RTPIPE)1);
281
282
283 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
284
285 char abBuf[_4K];
286 size_t cbRead = ~(size_t)3;
287 RTTESTI_CHECK_RC(RTPipeRead(hPipeW, abBuf, 0, &cbRead), VERR_ACCESS_DENIED);
288 RTTESTI_CHECK_RC(RTPipeRead(hPipeW, abBuf, 1, &cbRead), VERR_ACCESS_DENIED);
289 RTTESTI_CHECK(cbRead == ~(size_t)3);
290 RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeW, abBuf, 0, NULL), VERR_ACCESS_DENIED);
291 RTTESTI_CHECK_RC(RTPipeReadBlocking(hPipeW, abBuf, 1, NULL), VERR_ACCESS_DENIED);
292
293 size_t cbWrite = ~(size_t)5;
294 RTTESTI_CHECK_RC(RTPipeWrite(hPipeR, "asdf", 0, &cbWrite), VERR_ACCESS_DENIED);
295 RTTESTI_CHECK_RC(RTPipeWrite(hPipeR, "asdf", 4, &cbWrite), VERR_ACCESS_DENIED);
296 RTTESTI_CHECK(cbWrite == ~(size_t)5);
297 RTTESTI_CHECK_RC(RTPipeWriteBlocking(hPipeR, "asdf", 0, NULL), VERR_ACCESS_DENIED);
298 RTTESTI_CHECK_RC(RTPipeWriteBlocking(hPipeR, "asdf", 4, NULL), VERR_ACCESS_DENIED);
299
300 RTTESTI_CHECK_RC(RTPipeFlush(hPipeR), VERR_ACCESS_DENIED);
301
302 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
303 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
304}
305
306
307static void tstRTPipe1(void)
308{
309 RTTestISub("Basics");
310
311 RTPIPE hPipeR = NIL_RTPIPE;
312 RTPIPE hPipeW = NIL_RTPIPE;
313 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
314 RTTESTI_CHECK_RETV(hPipeR != NIL_RTPIPE);
315 RTTESTI_CHECK_RETV(hPipeW != NIL_RTPIPE);
316 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
317 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
318 RTTESTI_CHECK_RC_RETV(RTPipeClose(NIL_RTPIPE), VINF_SUCCESS);
319
320 hPipeR = NIL_RTPIPE;
321 hPipeW = NIL_RTPIPE;
322 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ | RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
323 int rc = RTPipeFlush(hPipeW);
324 RTTESTI_CHECK_MSG(rc == VERR_NOT_SUPPORTED || rc == VINF_SUCCESS, ("%Rrc\n", rc));
325 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
326 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
327
328 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ), VINF_SUCCESS);
329 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeR, 0), VERR_TIMEOUT);
330 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeR, 1), VERR_TIMEOUT);
331 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeW, 0), VINF_SUCCESS);
332 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeW, 1), VINF_SUCCESS);
333 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
334 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
335
336 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
337 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
338 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
339
340 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_READ), VINF_SUCCESS);
341
342 size_t cbRead = ~(size_t)0;
343 char abBuf[_64K + _4K];
344 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, sizeof(abBuf), &cbRead), VINF_TRY_AGAIN);
345 RTTESTI_CHECK_RETV(cbRead == 0);
346
347 cbRead = ~(size_t)0;
348 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, 1, &cbRead), VINF_TRY_AGAIN);
349 RTTESTI_CHECK_RETV(cbRead == 0);
350
351 cbRead = ~(size_t)0;
352 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, 0, &cbRead), VINF_SUCCESS);
353 RTTESTI_CHECK_RETV(cbRead == 0);
354
355 size_t cbWritten = ~(size_t)2;
356 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, abBuf, 0, &cbWritten), VINF_SUCCESS);
357 RTTESTI_CHECK_RETV(cbWritten == 0);
358
359 /* We can write a number of bytes without blocking (see PIPE_BUF on
360 POSIX systems). */
361 cbWritten = ~(size_t)2;
362 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, "42", 2, &cbWritten), VINF_SUCCESS);
363 RTTESTI_CHECK_MSG_RETV(cbWritten == 2, ("cbWritten=%zu\n", cbWritten));
364 cbWritten = ~(size_t)2;
365 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, "!", 1, &cbWritten), VINF_SUCCESS);
366 RTTESTI_CHECK_RETV(cbWritten == 1);
367 cbRead = ~(size_t)0;
368 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, 3, &cbRead), VINF_SUCCESS);
369 RTTESTI_CHECK_RETV(cbRead == 3);
370 RTTESTI_CHECK_RETV(!memcmp(abBuf, "42!", 3));
371
372 cbWritten = ~(size_t)2;
373 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, "BigQ", 4, &cbWritten), VINF_SUCCESS);
374 RTTESTI_CHECK_RETV(cbWritten == 4);
375 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeR, 0), VINF_SUCCESS);
376 RTTESTI_CHECK_RC_RETV(RTPipeSelectOne(hPipeR, 1), VINF_SUCCESS);
377 cbRead = ~(size_t)0;
378 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
379 RTTESTI_CHECK_MSG(cbRead == cbWritten, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
380 cbRead = ~(size_t)0;
381 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, sizeof(abBuf), &cbRead), VINF_SUCCESS);
382 RTTESTI_CHECK_RETV(cbRead == 4);
383 RTTESTI_CHECK_RETV(!memcmp(abBuf, "BigQ", 4));
384
385 cbWritten = ~(size_t)2;
386 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, "H2G2", 4, &cbWritten), VINF_SUCCESS);
387 RTTESTI_CHECK_RETV(cbWritten == 4);
388 cbRead = ~(size_t)0;
389 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
390 RTTESTI_CHECK_MSG(cbRead == cbWritten, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
391 cbRead = ~(size_t)0;
392 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, &abBuf[0], 1, &cbRead), VINF_SUCCESS);
393 RTTESTI_CHECK_RETV(cbRead == 1);
394 cbRead = ~(size_t)0;
395 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
396 RTTESTI_CHECK_MSG(cbRead == cbWritten - 1, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
397 cbRead = ~(size_t)0;
398 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, &abBuf[1], 1, &cbRead), VINF_SUCCESS);
399 RTTESTI_CHECK_RETV(cbRead == 1);
400 cbRead = ~(size_t)0;
401 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
402 RTTESTI_CHECK_MSG(cbRead == cbWritten - 2, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
403 cbRead = ~(size_t)0;
404 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, &abBuf[2], 1, &cbRead), VINF_SUCCESS);
405 RTTESTI_CHECK_RETV(cbRead == 1);
406 cbRead = ~(size_t)0;
407 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
408 RTTESTI_CHECK_MSG(cbRead == cbWritten - 3, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
409 cbRead = ~(size_t)0;
410 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, &abBuf[3], 1, &cbRead), VINF_SUCCESS);
411 RTTESTI_CHECK_RETV(cbRead == 1);
412 RTTESTI_CHECK_RETV(!memcmp(abBuf, "H2G2", 4));
413 cbRead = ~(size_t)0;
414 RTTESTI_CHECK_RC_RETV(RTPipeQueryReadable(hPipeR, &cbRead), VINF_SUCCESS);
415 RTTESTI_CHECK_MSG(cbRead == cbWritten - 4, ("cbRead=%zu cbWritten=%zu\n", cbRead, cbWritten));
416
417 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
418 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
419
420
421 RTTestISub("VERR_BROKEN_PIPE");
422 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
423 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeR), VINF_SUCCESS);
424 cbWritten = ~(size_t)2;
425 RTTESTI_CHECK_RC(RTPipeWrite(hPipeW, "", 0, &cbWritten), VINF_SUCCESS);
426 RTTESTI_CHECK(cbWritten == 0);
427 cbWritten = ~(size_t)2;
428 RTTESTI_CHECK_RC(RTPipeWrite(hPipeW, "4", 1, &cbWritten), VERR_BROKEN_PIPE);
429 RTTESTI_CHECK(cbWritten == ~(size_t)2);
430 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
431
432 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
433 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
434 cbRead = ~(size_t)0;
435 RTTESTI_CHECK_RC(RTPipeRead(hPipeR, abBuf, 0, &cbRead), VINF_SUCCESS);
436 RTTESTI_CHECK(cbRead == 0);
437 cbRead = ~(size_t)3;
438 RTTESTI_CHECK_RC(RTPipeRead(hPipeR, abBuf, sizeof(abBuf), &cbRead), VERR_BROKEN_PIPE);
439 RTTESTI_CHECK(cbRead == ~(size_t)3);
440 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
441
442 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
443 cbWritten = ~(size_t)2;
444 RTTESTI_CHECK_RC(RTPipeWrite(hPipeW, "42", 2, &cbWritten), VINF_SUCCESS);
445 RTTESTI_CHECK(cbWritten == 2);
446 RTTESTI_CHECK_RC_RETV(RTPipeClose(hPipeW), VINF_SUCCESS);
447 cbRead = ~(size_t)0;
448 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, 0, &cbRead), VINF_SUCCESS);
449 RTTESTI_CHECK(cbRead == 0);
450 cbRead = ~(size_t)0;
451 RTTESTI_CHECK_RC(RTPipeRead(hPipeR, &abBuf[0], 1, &cbRead), VINF_SUCCESS);
452 RTTESTI_CHECK(cbRead == 1);
453 cbRead = ~(size_t)0;
454 RTTESTI_CHECK_RC(RTPipeRead(hPipeR, &abBuf[1], 1, &cbRead), VINF_SUCCESS);
455 RTTESTI_CHECK(cbRead == 1);
456 RTTESTI_CHECK(!memcmp(abBuf, "42", 2));
457 cbRead = ~(size_t)0;
458 RTTESTI_CHECK_RC(RTPipeRead(hPipeR, abBuf, 0, &cbRead), VINF_SUCCESS);
459 RTTESTI_CHECK(cbRead == 0);
460 cbRead = ~(size_t)3;
461 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, abBuf, sizeof(abBuf), &cbRead), VERR_BROKEN_PIPE);
462 RTTESTI_CHECK(cbRead == ~(size_t)3);
463 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
464
465 RTTestISub("Blocking");
466 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
467 RTTESTI_CHECK_RC_RETV(RTPipeWrite(hPipeW, "42!", 3, &cbWritten), VINF_SUCCESS);
468 RTTESTI_CHECK(cbWritten == 3);
469 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, abBuf, 3, NULL), VINF_SUCCESS);
470 RTTESTI_CHECK(!memcmp(abBuf, "42!", 3));
471 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
472 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[0], 0, NULL), VINF_SUCCESS);
473 cbRead = ~(size_t)42;
474 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[0], 0, &cbRead), VINF_SUCCESS);
475 RTTESTI_CHECK(cbRead == 0);
476 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[0], 1, NULL), VERR_BROKEN_PIPE);
477 cbRead = ~(size_t)42;
478 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[0], 1, &cbRead), VERR_BROKEN_PIPE);
479 RTTESTI_CHECK(cbRead == 0);
480 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
481
482 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, 0), VINF_SUCCESS);
483 RTTESTI_CHECK_RC_RETV(RTPipeWriteBlocking(hPipeW, "42!", 3, NULL), VINF_SUCCESS);
484 RTTESTI_CHECK(cbWritten == 3);
485 cbRead = ~(size_t)0;
486 RTTESTI_CHECK_RC_RETV(RTPipeRead(hPipeR, &abBuf[0], 1, &cbRead), VINF_SUCCESS);
487 RTTESTI_CHECK(cbRead == 1);
488 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[1], 1, NULL), VINF_SUCCESS);
489 RTTESTI_CHECK_RC_RETV(RTPipeReadBlocking(hPipeR, &abBuf[2], 1, NULL), VINF_SUCCESS);
490 RTTESTI_CHECK(!memcmp(abBuf, "42!", 3));
491 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
492 RTTESTI_CHECK_RC_RETV(RTPipeWriteBlocking(hPipeW, "", 0, NULL), VINF_SUCCESS);
493 cbWritten = ~(size_t)9;
494 RTTESTI_CHECK_RC_RETV(RTPipeWriteBlocking(hPipeW, "", 0, &cbWritten), VINF_SUCCESS);
495 RTTESTI_CHECK(cbWritten == 0);
496 RTTESTI_CHECK_RC_RETV(RTPipeWriteBlocking(hPipeW, "42", 2, NULL), VERR_BROKEN_PIPE);
497 cbWritten = ~(size_t)9;
498 RTTESTI_CHECK_RC_RETV(RTPipeWriteBlocking(hPipeW, "42", 2, &cbWritten), VERR_BROKEN_PIPE);
499 RTTESTI_CHECK(cbWritten == 0);
500 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
501}
502
503int main(int argc, char **argv)
504{
505 if (argc == 3 && !strcmp(argv[1], "--child-4"))
506 return tstRTPipe4Child(argv[2]);
507 if (argc == 3 && !strcmp(argv[1], "--child-5"))
508 return tstRTPipe5Child(argv[2]);
509
510 RTTEST hTest;
511 int rc = RTTestInitAndCreate("tstRTPipe", &hTest);
512 if (rc)
513 return rc;
514 RTTestBanner(hTest);
515
516 /*
517 * The tests.
518 */
519 tstRTPipe1();
520 if (RTTestErrorCount(hTest) == 0)
521 {
522 bool fMayPanic = RTAssertMayPanic();
523 bool fQuiet = RTAssertAreQuiet();
524 RTAssertSetMayPanic(false);
525 RTAssertSetQuiet(true);
526 tstRTPipe2();
527 RTAssertSetQuiet(fQuiet);
528 RTAssertSetMayPanic(fMayPanic);
529
530 tstRTPipe3();
531 tstRTPipe4();
532 tstRTPipe5();
533 }
534
535 /*
536 * Summary.
537 */
538 return RTTestSummaryAndDestroy(hTest);
539}
540
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette