summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/pipe/test/TestPipeCreateNamedPipe.c
blob: 8c8ead2c03b7640d73ccb3f4503db956bb9a9224 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/pipe.h>
#include <winpr/file.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
#include <winpr/print.h>
#include <winpr/synch.h>
#include <winpr/wlog.h>
#include <winpr/thread.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include "../pipe.h"

#define PIPE_BUFFER_SIZE 32

static HANDLE ReadyEvent;

static LPTSTR lpszPipeNameMt = _T("\\\\.\\pipe\\winpr_test_pipe_mt");
static LPTSTR lpszPipeNameSt = _T("\\\\.\\pipe\\winpr_test_pipe_st");

static BOOL testFailed = FALSE;

static DWORD WINAPI named_pipe_client_thread(LPVOID arg)
{
	HANDLE hNamedPipe = NULL;
	BYTE* lpReadBuffer = NULL;
	BYTE* lpWriteBuffer = NULL;
	BOOL fSuccess = FALSE;
	DWORD nNumberOfBytesToRead = 0;
	DWORD nNumberOfBytesToWrite = 0;
	DWORD lpNumberOfBytesRead = 0;
	DWORD lpNumberOfBytesWritten = 0;
	WaitForSingleObject(ReadyEvent, INFINITE);
	hNamedPipe =
	    CreateFile(lpszPipeNameMt, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	if (hNamedPipe == INVALID_HANDLE_VALUE)
	{
		printf("%s: Named Pipe CreateFile failure: INVALID_HANDLE_VALUE\n", __func__);
		goto out;
	}

	if (!(lpReadBuffer = (BYTE*)malloc(PIPE_BUFFER_SIZE)))
	{
		printf("%s: Error allocating read buffer\n", __func__);
		goto out;
	}

	if (!(lpWriteBuffer = (BYTE*)malloc(PIPE_BUFFER_SIZE)))
	{
		printf("%s: Error allocating write buffer\n", __func__);
		goto out;
	}

	lpNumberOfBytesWritten = 0;
	nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
	FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x59);

	if (!WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, &lpNumberOfBytesWritten,
	               NULL) ||
	    lpNumberOfBytesWritten != nNumberOfBytesToWrite)
	{
		printf("%s: Client NamedPipe WriteFile failure\n", __func__);
		goto out;
	}

	lpNumberOfBytesRead = 0;
	nNumberOfBytesToRead = PIPE_BUFFER_SIZE;
	ZeroMemory(lpReadBuffer, PIPE_BUFFER_SIZE);

	if (!ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL) ||
	    lpNumberOfBytesRead != nNumberOfBytesToRead)
	{
		printf("%s: Client NamedPipe ReadFile failure\n", __func__);
		goto out;
	}

	printf("Client ReadFile: %" PRIu32 " bytes\n", lpNumberOfBytesRead);
	winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, lpNumberOfBytesRead);
	fSuccess = TRUE;
out:
	free(lpReadBuffer);
	free(lpWriteBuffer);
	CloseHandle(hNamedPipe);

	if (!fSuccess)
		testFailed = TRUE;

	ExitThread(0);
	return 0;
}

static DWORD WINAPI named_pipe_server_thread(LPVOID arg)
{
	HANDLE hNamedPipe = NULL;
	BYTE* lpReadBuffer = NULL;
	BYTE* lpWriteBuffer = NULL;
	BOOL fSuccess = FALSE;
	BOOL fConnected = FALSE;
	DWORD nNumberOfBytesToRead = 0;
	DWORD nNumberOfBytesToWrite = 0;
	DWORD lpNumberOfBytesRead = 0;
	DWORD lpNumberOfBytesWritten = 0;
	hNamedPipe = CreateNamedPipe(
	    lpszPipeNameMt, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
	    PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 0, NULL);

	if (!hNamedPipe)
	{
		printf("%s: CreateNamedPipe failure: NULL handle\n", __func__);
		goto out;
	}

	if (hNamedPipe == INVALID_HANDLE_VALUE)
	{
		printf("%s: CreateNamedPipe failure: INVALID_HANDLE_VALUE\n", __func__);
		goto out;
	}

	SetEvent(ReadyEvent);

	/**
	 * Note:
	 * If a client connects before ConnectNamedPipe is called, the function returns zero and
	 * GetLastError returns ERROR_PIPE_CONNECTED. This can happen if a client connects in the
	 * interval between the call to CreateNamedPipe and the call to ConnectNamedPipe.
	 * In this situation, there is a good connection between client and server, even though
	 * the function returns zero.
	 */
	fConnected =
	    ConnectNamedPipe(hNamedPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

	if (!fConnected)
	{
		printf("%s: ConnectNamedPipe failure\n", __func__);
		goto out;
	}

	if (!(lpReadBuffer = (BYTE*)calloc(1, PIPE_BUFFER_SIZE)))
	{
		printf("%s: Error allocating read buffer\n", __func__);
		goto out;
	}

	if (!(lpWriteBuffer = (BYTE*)malloc(PIPE_BUFFER_SIZE)))
	{
		printf("%s: Error allocating write buffer\n", __func__);
		goto out;
	}

	lpNumberOfBytesRead = 0;
	nNumberOfBytesToRead = PIPE_BUFFER_SIZE;

	if (!ReadFile(hNamedPipe, lpReadBuffer, nNumberOfBytesToRead, &lpNumberOfBytesRead, NULL) ||
	    lpNumberOfBytesRead != nNumberOfBytesToRead)
	{
		printf("%s: Server NamedPipe ReadFile failure\n", __func__);
		goto out;
	}

	printf("Server ReadFile: %" PRIu32 " bytes\n", lpNumberOfBytesRead);
	winpr_HexDump("pipe.test", WLOG_DEBUG, lpReadBuffer, lpNumberOfBytesRead);
	lpNumberOfBytesWritten = 0;
	nNumberOfBytesToWrite = PIPE_BUFFER_SIZE;
	FillMemory(lpWriteBuffer, PIPE_BUFFER_SIZE, 0x45);

	if (!WriteFile(hNamedPipe, lpWriteBuffer, nNumberOfBytesToWrite, &lpNumberOfBytesWritten,
	               NULL) ||
	    lpNumberOfBytesWritten != nNumberOfBytesToWrite)
	{
		printf("%s: Server NamedPipe WriteFile failure\n", __func__);
		goto out;
	}

	fSuccess = TRUE;
out:
	free(lpReadBuffer);
	free(lpWriteBuffer);
	CloseHandle(hNamedPipe);

	if (!fSuccess)
		testFailed = TRUE;

	ExitThread(0);
	return 0;
}

#define TESTNUMPIPESST 16
static DWORD WINAPI named_pipe_single_thread(LPVOID arg)
{
	HANDLE servers[TESTNUMPIPESST] = { 0 };
	HANDLE clients[TESTNUMPIPESST] = { 0 };
	DWORD dwRead = 0;
	DWORD dwWritten = 0;
	int numPipes = 0;
	BOOL bSuccess = FALSE;
	numPipes = TESTNUMPIPESST;
	WaitForSingleObject(ReadyEvent, INFINITE);

	for (int i = 0; i < numPipes; i++)
	{
		if (!(servers[i] = CreateNamedPipe(lpszPipeNameSt, PIPE_ACCESS_DUPLEX,
		                                   PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
		                                   PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE,
		                                   PIPE_BUFFER_SIZE, 0, NULL)))
		{
			printf("%s: CreateNamedPipe #%d failed\n", __func__, i);
			goto out;
		}
	}

#ifndef _WIN32

	for (int i = 0; i < numPipes; i++)
	{
		WINPR_NAMED_PIPE* p = (WINPR_NAMED_PIPE*)servers[i];

		if (strcmp(lpszPipeNameSt, p->name))
		{
			printf("%s: Pipe name mismatch for pipe #%d ([%s] instead of [%s])\n", __func__, i,
			       p->name, lpszPipeNameSt);
			goto out;
		}

		if (p->clientfd != -1)
		{
			printf("%s: Unexpected client fd value for pipe #%d (%d instead of -1)\n", __func__, i,
			       p->clientfd);
			goto out;
		}

		if (p->serverfd < 1)
		{
			printf("%s: Unexpected server fd value for pipe #%d (%d is not > 0)\n", __func__, i,
			       p->serverfd);
			goto out;
		}

		if (p->ServerMode == FALSE)
		{
			printf("%s: Unexpected ServerMode value for pipe #%d (0 instead of 1)\n", __func__, i);
			goto out;
		}
	}

#endif

	for (int i = 0; i < numPipes; i++)
	{
		BOOL fConnected = 0;
		if ((clients[i] = CreateFile(lpszPipeNameSt, GENERIC_READ | GENERIC_WRITE, 0, NULL,
		                             OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
		{
			printf("%s: CreateFile #%d failed\n", __func__, i);
			goto out;
		}

		/**
		 * Note:
		 * If a client connects before ConnectNamedPipe is called, the function returns zero and
		 * GetLastError returns ERROR_PIPE_CONNECTED. This can happen if a client connects in the
		 * interval between the call to CreateNamedPipe and the call to ConnectNamedPipe.
		 * In this situation, there is a good connection between client and server, even though
		 * the function returns zero.
		 */
		fConnected =
		    ConnectNamedPipe(servers[i], NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

		if (!fConnected)
		{
			printf("%s: ConnectNamedPipe #%d failed. (%" PRIu32 ")\n", __func__, i, GetLastError());
			goto out;
		}
	}

#ifndef _WIN32

	for (int i = 0; i < numPipes; i++)
	{
		WINPR_NAMED_PIPE* p = servers[i];

		if (p->clientfd < 1)
		{
			printf("%s: Unexpected client fd value for pipe #%d (%d is not > 0)\n", __func__, i,
			       p->clientfd);
			goto out;
		}

		if (p->ServerMode)
		{
			printf("%s: Unexpected ServerMode value for pipe #%d (1 instead of 0)\n", __func__, i);
			goto out;
		}
	}

	for (int i = 0; i < numPipes; i++)
	{
		{
			char sndbuf[PIPE_BUFFER_SIZE] = { 0 };
			char rcvbuf[PIPE_BUFFER_SIZE] = { 0 };
			/* Test writing from clients to servers */
			sprintf_s(sndbuf, sizeof(sndbuf), "CLIENT->SERVER ON PIPE #%05d", i);

			if (!WriteFile(clients[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL) ||
			    dwWritten != sizeof(sndbuf))
			{
				printf("%s: Error writing to client end of pipe #%d\n", __func__, i);
				goto out;
			}

			if (!ReadFile(servers[i], rcvbuf, dwWritten, &dwRead, NULL) || dwRead != dwWritten)
			{
				printf("%s: Error reading on server end of pipe #%d\n", __func__, i);
				goto out;
			}

			if (memcmp(sndbuf, rcvbuf, sizeof(sndbuf)))
			{
				printf("%s: Error data read on server end of pipe #%d is corrupted\n", __func__, i);
				goto out;
			}
		}
		{

			char sndbuf[PIPE_BUFFER_SIZE] = { 0 };
			char rcvbuf[PIPE_BUFFER_SIZE] = { 0 };
			/* Test writing from servers to clients */

			sprintf_s(sndbuf, sizeof(sndbuf), "SERVER->CLIENT ON PIPE #%05d", i);

			if (!WriteFile(servers[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL) ||
			    dwWritten != sizeof(sndbuf))
			{
				printf("%s: Error writing to server end of pipe #%d\n", __func__, i);
				goto out;
			}

			if (!ReadFile(clients[i], rcvbuf, dwWritten, &dwRead, NULL) || dwRead != dwWritten)
			{
				printf("%s: Error reading on client end of pipe #%d\n", __func__, i);
				goto out;
			}

			if (memcmp(sndbuf, rcvbuf, sizeof(sndbuf)))
			{
				printf("%s: Error data read on client end of pipe #%d is corrupted\n", __func__, i);
				goto out;
			}
		}
	}

#endif
	/**
	 * After DisconnectNamedPipe on server end
	 * ReadFile/WriteFile must fail on client end
	 */
	int i = numPipes - 1;
	DisconnectNamedPipe(servers[i]);
	{
		char sndbuf[PIPE_BUFFER_SIZE] = { 0 };
		char rcvbuf[PIPE_BUFFER_SIZE] = { 0 };
		if (ReadFile(clients[i], rcvbuf, sizeof(rcvbuf), &dwRead, NULL))
		{
			printf("%s: Error ReadFile on client should have failed after DisconnectNamedPipe on "
			       "server\n",
			       __func__);
			goto out;
		}

		if (WriteFile(clients[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL))
		{
			printf(
			    "%s: Error WriteFile on client end should have failed after DisconnectNamedPipe on "
			    "server\n",
			    __func__);
			goto out;
		}
	}
	CloseHandle(servers[i]);
	CloseHandle(clients[i]);
	numPipes--;
	/**
	 * After CloseHandle (without calling DisconnectNamedPipe first) on server end
	 * ReadFile/WriteFile must fail on client end
	 */
	i = numPipes - 1;
	CloseHandle(servers[i]);

	{
		char sndbuf[PIPE_BUFFER_SIZE] = { 0 };
		char rcvbuf[PIPE_BUFFER_SIZE] = { 0 };

		if (ReadFile(clients[i], rcvbuf, sizeof(rcvbuf), &dwRead, NULL))
		{
			printf(
			    "%s: Error ReadFile on client end should have failed after CloseHandle on server\n",
			    __func__);
			goto out;
		}

		if (WriteFile(clients[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL))
		{
			printf("%s: Error WriteFile on client end should have failed after CloseHandle on "
			       "server\n",
			       __func__);
			goto out;
		}
	}
	CloseHandle(clients[i]);
	numPipes--;
	/**
	 * After CloseHandle on client end
	 * ReadFile/WriteFile must fail on server end
	 */
	i = numPipes - 1;
	CloseHandle(clients[i]);

	{
		char sndbuf[PIPE_BUFFER_SIZE] = { 0 };
		char rcvbuf[PIPE_BUFFER_SIZE] = { 0 };

		if (ReadFile(servers[i], rcvbuf, sizeof(rcvbuf), &dwRead, NULL))
		{
			printf(
			    "%s: Error ReadFile on server end should have failed after CloseHandle on client\n",
			    __func__);
			goto out;
		}

		if (WriteFile(servers[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL))
		{
			printf("%s: Error WriteFile on server end should have failed after CloseHandle on "
			       "client\n",
			       __func__);
			goto out;
		}
	}

	DisconnectNamedPipe(servers[i]);
	CloseHandle(servers[i]);
	numPipes--;

	/* Close all remaining pipes */
	for (int i = 0; i < numPipes; i++)
	{
		DisconnectNamedPipe(servers[i]);
		CloseHandle(servers[i]);
		CloseHandle(clients[i]);
	}

	bSuccess = TRUE;
out:

	if (!bSuccess)
		testFailed = TRUE;

	return 0;
}

int TestPipeCreateNamedPipe(int argc, char* argv[])
{
	HANDLE SingleThread = NULL;
	HANDLE ClientThread = NULL;
	HANDLE ServerThread = NULL;
	HANDLE hPipe = NULL;
	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);
	/* Verify that CreateNamedPipe returns INVALID_HANDLE_VALUE on failure */
	hPipe = CreateNamedPipeA(NULL, 0, 0, 0, 0, 0, 0, NULL);
	if (hPipe != INVALID_HANDLE_VALUE)
	{
		printf("CreateNamedPipe unexpectedly returned %p instead of INVALID_HANDLE_VALUE (%p)\n",
		       hPipe, INVALID_HANDLE_VALUE);
		return -1;
	}

#ifndef _WIN32
	signal(SIGPIPE, SIG_IGN);
#endif
	if (!(ReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
	{
		printf("CreateEvent failure: (%" PRIu32 ")\n", GetLastError());
		return -1;
	}
	if (!(SingleThread = CreateThread(NULL, 0, named_pipe_single_thread, NULL, 0, NULL)))
	{
		printf("CreateThread (SingleThread) failure: (%" PRIu32 ")\n", GetLastError());
		return -1;
	}
	if (!(ClientThread = CreateThread(NULL, 0, named_pipe_client_thread, NULL, 0, NULL)))
	{
		printf("CreateThread (ClientThread) failure: (%" PRIu32 ")\n", GetLastError());
		return -1;
	}
	if (!(ServerThread = CreateThread(NULL, 0, named_pipe_server_thread, NULL, 0, NULL)))
	{
		printf("CreateThread (ServerThread) failure: (%" PRIu32 ")\n", GetLastError());
		return -1;
	}
	WaitForSingleObject(SingleThread, INFINITE);
	WaitForSingleObject(ClientThread, INFINITE);
	WaitForSingleObject(ServerThread, INFINITE);
	CloseHandle(SingleThread);
	CloseHandle(ClientThread);
	CloseHandle(ServerThread);
	return testFailed;
}