1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: test1.c (CreatePipe) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the CreatePipe function. |
10 | ** This test will create two pipes, a read and a write. Once |
11 | ** the pipes have been created, they will be tested by writing |
12 | ** and then reading, then comparing the results. |
13 | ** |
14 | ** Depends: WriteFile |
15 | ** ReadFile |
16 | ** memcmp |
17 | ** CloseHandle |
18 | ** |
19 | ** |
20 | **===================================================================*/ |
21 | |
22 | #include <palsuite.h> |
23 | |
24 | const char* cTestString = "one fish, two fish, red fish, blue fish." ; |
25 | |
26 | int __cdecl main(int argc, char **argv) |
27 | { |
28 | HANDLE hReadPipe = NULL; |
29 | HANDLE hWritePipe = NULL; |
30 | BOOL bRetVal = FALSE; |
31 | DWORD dwBytesWritten; |
32 | DWORD dwBytesRead; |
33 | char buffer[256]; |
34 | |
35 | SECURITY_ATTRIBUTES lpPipeAttributes; |
36 | |
37 | /*Initialize the PAL*/ |
38 | if ((PAL_Initialize(argc, argv)) != 0) |
39 | { |
40 | return (FAIL); |
41 | } |
42 | |
43 | /*Setup SECURITY_ATTRIBUTES structure for CreatePipe*/ |
44 | lpPipeAttributes.nLength = sizeof(lpPipeAttributes); |
45 | lpPipeAttributes.lpSecurityDescriptor = NULL; |
46 | lpPipeAttributes.bInheritHandle = TRUE; |
47 | |
48 | /*Create a Pipe*/ |
49 | bRetVal = CreatePipe(&hReadPipe, /* read handle*/ |
50 | &hWritePipe, /* write handle */ |
51 | &lpPipeAttributes, /* security attributes*/ |
52 | 0); /* pipe size*/ |
53 | if (bRetVal == FALSE) |
54 | { |
55 | Fail("ERROR: %ld :Unable to create pipe\n" , GetLastError()); |
56 | } |
57 | |
58 | /*Write to the write pipe handle*/ |
59 | bRetVal = WriteFile(hWritePipe, /* handle to write pipe*/ |
60 | cTestString, /* buffer to write*/ |
61 | strlen(cTestString),/* number of bytes to write*/ |
62 | &dwBytesWritten, /* number of bytes written*/ |
63 | NULL); /* overlapped buffer*/ |
64 | if (bRetVal == FALSE) |
65 | { |
66 | Fail("ERROR: %ld :unable to write to write pipe handle " |
67 | "hWritePipe=0x%lx\n" , GetLastError(), hWritePipe); |
68 | } |
69 | |
70 | /*Read, 256 bytes, more bytes then actually written. |
71 | This will give allow us to use the value that ReadFile |
72 | returns for comparision.*/ |
73 | bRetVal = ReadFile(hReadPipe, /* handle to read pipe*/ |
74 | buffer, /* buffer to write to*/ |
75 | 256, /* number of bytes to read*/ |
76 | &dwBytesRead, /* number of bytes read*/ |
77 | NULL); /* overlapped buffer*/ |
78 | if (bRetVal == FALSE) |
79 | { |
80 | Fail("ERROR: %ld : unable read hWritePipe=0x%lx\n" , |
81 | GetLastError(), hWritePipe); |
82 | } |
83 | |
84 | /*Compare what was read with what was written.*/ |
85 | if ((memcmp(cTestString, buffer, dwBytesRead)) != 0) |
86 | { |
87 | Fail("ERROR: read \"%s\" expected \"%s\" \n" , buffer, cTestString); |
88 | } |
89 | |
90 | /*Compare values returned from WriteFile and ReadFile.*/ |
91 | if (dwBytesWritten != dwBytesRead) |
92 | { |
93 | Fail("ERROR: WriteFile wrote \"%d\", but ReadFile read \"%d\"," |
94 | " these should be the same\n" , buffer, cTestString); |
95 | } |
96 | |
97 | /*Close write pipe handle*/ |
98 | if (CloseHandle(hWritePipe) == 0) |
99 | { |
100 | Fail("ERROR: %ld : Unable to close write pipe handle " |
101 | "hWritePipe=0x%lx\n" ,GetLastError(), hWritePipe); |
102 | } |
103 | |
104 | /*Close Read pipe handle*/ |
105 | if (CloseHandle(hReadPipe) == 0) |
106 | { |
107 | Fail("ERROR: %ld : Unable to close read pipe handle " |
108 | "hReadPipe=0x%lx\n" , GetLastError(), hReadPipe); |
109 | } |
110 | |
111 | PAL_Terminate(); |
112 | return (PASS); |
113 | } |
114 | |