FreeRDP
TestPipeCreatePipe.c
1 
2 #include <stdio.h>
3 #include <winpr/crt.h>
4 #include <winpr/pipe.h>
5 #include <winpr/tchar.h>
6 #include <winpr/winpr.h>
7 
8 #define BUFFER_SIZE 16
9 
10 int TestPipeCreatePipe(int argc, char* argv[])
11 {
12  BOOL status = 0;
13  DWORD dwRead = 0;
14  DWORD dwWrite = 0;
15  HANDLE hReadPipe = NULL;
16  HANDLE hWritePipe = NULL;
17  BYTE readBuffer[BUFFER_SIZE] = { 0 };
18  BYTE writeBuffer[BUFFER_SIZE] = { 0 };
19  WINPR_UNUSED(argc);
20  WINPR_UNUSED(argv);
21  status = CreatePipe(&hReadPipe, &hWritePipe, NULL, BUFFER_SIZE * 2);
22 
23  if (!status)
24  {
25  _tprintf(_T("CreatePipe failed\n"));
26  return -1;
27  }
28 
29  FillMemory(writeBuffer, sizeof(writeBuffer), 0xAA);
30  status = WriteFile(hWritePipe, &writeBuffer, sizeof(writeBuffer), &dwWrite, NULL);
31 
32  if (!status)
33  {
34  _tprintf(_T("WriteFile failed\n"));
35  return -1;
36  }
37 
38  if (dwWrite != sizeof(writeBuffer))
39  {
40  _tprintf(_T("WriteFile: unexpected number of bytes written: Actual: %") _T(
41  PRIu32) _T(", Expected: %") _T(PRIuz) _T("\n"),
42  dwWrite, sizeof(writeBuffer));
43  return -1;
44  }
45 
46  status = ReadFile(hReadPipe, &readBuffer, sizeof(readBuffer), &dwRead, NULL);
47 
48  if (!status)
49  {
50  _tprintf(_T("ReadFile failed\n"));
51  return -1;
52  }
53 
54  if (dwRead != sizeof(readBuffer))
55  {
56  _tprintf(_T("ReadFile: unexpected number of bytes read: Actual: %") _T(
57  PRIu32) _T(", Expected: %") _T(PRIuz) _T("\n"),
58  dwWrite, sizeof(readBuffer));
59  return -1;
60  }
61 
62  if (memcmp(readBuffer, writeBuffer, BUFFER_SIZE) != 0)
63  {
64  _tprintf(_T("ReadFile: read buffer is different from write buffer\n"));
65  return -1;
66  }
67 
68  (void)CloseHandle(hReadPipe);
69  (void)CloseHandle(hWritePipe);
70 
71  return 0;
72 }