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: test6.c (DuplicateHandle)
8**
9** Purpose: Tests the PAL implementation of the DuplicateHandle function,
10** with CreatePipe. This test will create a pipe, then duplicate
11** the write handle, write to the handle, and use the read to
12** verify.
13**
14** Depends: WriteFile
15** ReadFile
16** memcmp
17** CloseHandle
18**
19**
20**===================================================================*/
21
22#include <palsuite.h>
23
24const char* cTestString = "one fish, two fish, red fish, blue fish.";
25
26int __cdecl main(int argc, char **argv)
27{
28 HANDLE hReadPipe = NULL;
29 HANDLE hWritePipe = NULL;
30 HANDLE hDupPipe = NULL;
31 BOOL bRetVal = FALSE;
32 DWORD dwBytesWritten;
33 DWORD dwBytesRead;
34 char buffer[256];
35
36 SECURITY_ATTRIBUTES lpPipeAttributes;
37
38 /*Initialize the PAL*/
39 if ((PAL_Initialize(argc, argv)) != 0)
40 {
41 return (FAIL);
42 }
43
44 /*Setup SECURITY_ATTRIBUTES structure for CreatePipe*/
45 lpPipeAttributes.nLength = sizeof(lpPipeAttributes);
46 lpPipeAttributes.lpSecurityDescriptor = NULL;
47 lpPipeAttributes.bInheritHandle = TRUE;
48
49 /*Create a Pipe*/
50 bRetVal = CreatePipe(&hReadPipe, /* read handle*/
51 &hWritePipe, /* write handle */
52 &lpPipeAttributes,/* security attributes*/
53 0); /* pipe size*/
54 if (bRetVal == FALSE)
55 {
56 Fail("ERROR: %ld :Unable to create pipe\n", GetLastError());
57 }
58
59 /*Duplicate the pipe handle*/
60 if (!(DuplicateHandle(GetCurrentProcess(), /* source handle process*/
61 hWritePipe, /* handle to duplicate*/
62 GetCurrentProcess(), /* target process handle*/
63 &hDupPipe, /* duplicate handle*/
64 GENERIC_READ|GENERIC_WRITE,/* requested access*/
65 FALSE, /* handle inheritance*/
66 DUPLICATE_SAME_ACCESS))) /* optional actions*/
67 {
68 Trace("ERROR: %ld :Fail to create the duplicate handle"
69 " to hWritePipe=0x%lx",
70 GetLastError(),
71 hWritePipe);
72 CloseHandle(hReadPipe);
73 CloseHandle(hWritePipe);
74 Fail("");
75 }
76
77 /*Write to the duplicate write pipe handle*/
78 bRetVal = WriteFile(hDupPipe, /* handle to write pipe*/
79 cTestString, /* buffer to write*/
80 strlen(cTestString),/* number of bytes to write*/
81 &dwBytesWritten, /* number of bytes written*/
82 NULL); /* overlapped buffer*/
83 if (bRetVal == FALSE)
84 {
85 Trace("ERROR: %ld :unable to write to duplicate write pipe handle "
86 "hDupPipe=0x%lx\n",
87 GetLastError(),
88 hDupPipe);
89 CloseHandle(hReadPipe);
90 CloseHandle(hWritePipe);
91 CloseHandle(hDupPipe);
92 Fail("");
93 }
94
95 /*Read from the read handle, 256 bytes, more bytes
96 then actually written. This will give allow us to use
97 the value that ReadFile returns for comparision.*/
98 bRetVal = ReadFile(hReadPipe, /* handle to read pipe*/
99 buffer, /* buffer to write to*/
100 256, /* number of bytes to read*/
101 &dwBytesRead, /* number of bytes read*/
102 NULL); /* overlapped buffer*/
103 if (bRetVal == FALSE)
104 {
105 Trace("ERROR: %ld : unable read hReadPipe=0x%lx\n",
106 GetLastError(), hReadPipe);
107 CloseHandle(hReadPipe);
108 CloseHandle(hWritePipe);
109 CloseHandle(hDupPipe);
110 Fail("");
111 }
112
113 /*Compare what was read with what was written.*/
114 if ((memcmp(cTestString, buffer, dwBytesRead)) != 0)
115 {
116 Trace("ERROR: read \"%s\" expected \"%s\" \n",
117 buffer,
118 cTestString);
119 CloseHandle(hReadPipe);
120 CloseHandle(hWritePipe);
121 CloseHandle(hDupPipe);
122 Fail("");
123 }
124
125 /*Compare values returned from WriteFile and ReadFile.*/
126 if (dwBytesWritten != dwBytesRead)
127 {
128 Trace("ERROR: WriteFile wrote \"%s\", but ReadFile read \"%s\","
129 " these should be the same\n",
130 buffer,
131 cTestString);
132 CloseHandle(hReadPipe);
133 CloseHandle(hWritePipe);
134 CloseHandle(hDupPipe);
135 Fail("");
136 }
137
138 /*Cleanup.*/
139 CloseHandle(hReadPipe);
140 CloseHandle(hWritePipe);
141 CloseHandle(hDupPipe);
142
143
144 PAL_Terminate();
145 return (PASS);
146}
147