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 (DuplicateHandle)
8**
9** Purpose: Tests the PAL implementation of the DuplicateHandle function.
10** This test will create two handles to file, one to write and
11** one to read what was written. Test on a closed handle and a
12** NULL handle, both should fail.
13**
14**
15**===================================================================*/
16#include <palsuite.h>
17
18int __cdecl main(int argc, char **argv)
19{
20 HANDLE hFile;
21 HANDLE hDupFile;
22 char buf[256];
23 char teststr[] = "A uNiQuE tEsT sTrInG";
24 char lpFileName[] = "testfile.txt";
25 DWORD dwBytesWritten;
26 DWORD dwBytesRead;
27 BOOL bRetVal;
28
29 /*Initalize the PAL*/
30 if ((PAL_Initialize(argc,argv)) != 0)
31 {
32 return (FAIL);
33 }
34
35 /*Create a file handle with CreateFile*/
36 hFile = CreateFile(lpFileName,
37 GENERIC_WRITE|GENERIC_READ,
38 FILE_SHARE_WRITE|FILE_SHARE_READ,
39 NULL,
40 OPEN_ALWAYS,
41 FILE_ATTRIBUTE_NORMAL,
42 NULL);
43 if (hFile == INVALID_HANDLE_VALUE)
44 {
45 Fail("ERROR: %u :unable to create file \"%s\".\n",
46 GetLastError(),
47 lpFileName);
48 }
49
50 /*Write test string to the file.*/
51 bRetVal = WriteFile(hFile, // handle to file
52 teststr, // data buffer
53 strlen(teststr), // number of bytes to write
54 &dwBytesWritten, // number of bytes written
55 NULL); // overlapped buffer
56
57 if (bRetVal == FALSE)
58 {
59 Trace("ERROR: %u : unable to write to file handle "
60 "hFile=0x%lx\n",
61 GetLastError(),
62 hFile);
63 CloseHandle(hFile);
64 Fail("");
65 }
66
67 /*Create a duplicate handle with DuplicateHandle.*/
68 if (!(DuplicateHandle(
69 GetCurrentProcess(),
70 hFile,
71 GetCurrentProcess(),
72 &hDupFile,
73 GENERIC_READ|GENERIC_WRITE,
74 FALSE,
75 DUPLICATE_SAME_ACCESS)))
76 {
77 Trace("ERROR: %u : Fail to create the duplicate handle"
78 " to hFile=0x%lx\n",
79 GetLastError(),
80 hFile);
81 CloseHandle(hFile);
82 Fail("");
83 }
84
85 memset(buf, 0, 256);
86
87 /*Read from the Duplicated handle.*/
88 bRetVal = ReadFile(hDupFile,
89 buf,
90 256,
91 &dwBytesRead,
92 NULL);
93 if (bRetVal == FALSE)
94 {
95 Trace("ERROR: %u :unable to read from file handle "
96 "hFile=0x%lx\n",
97 GetLastError(),
98 hFile);
99 CloseHandle(hFile);
100 CloseHandle(hDupFile);
101 Fail("");
102 }
103
104 /*Compare what was written to what was read.*/
105 if (memcmp(teststr, buf, dwBytesRead) != 0)
106 {
107 Trace("ERROR: expected %s, got %s\n", teststr, buf);
108 CloseHandle(hFile);
109 CloseHandle(hDupFile);
110 Fail("");
111 }
112
113 /*Close the handles*/
114 CloseHandle(hFile);
115 CloseHandle(hDupFile);
116
117 /*Failure test: Create DuplicateHandle to a closed handle*/
118 if ((DuplicateHandle(
119 GetCurrentProcess(),
120 hFile,
121 GetCurrentProcess(),
122 &hDupFile,
123 GENERIC_READ|GENERIC_WRITE,
124 FALSE,
125 DUPLICATE_SAME_ACCESS)))
126 {
127 Fail("ERROR: %u :Created a duplicate handle to"
128 " a closed handle hFile=0x%lx\n",
129 GetLastError(),
130 hFile);
131 }
132
133 /*Failure test: Create DuplicateHandle to a NULL handle*/
134 hFile = NULL;
135 if ((DuplicateHandle(
136 GetCurrentProcess(),
137 hFile,
138 GetCurrentProcess(),
139 &hDupFile,
140 GENERIC_READ|GENERIC_WRITE,
141 FALSE,
142 DUPLICATE_SAME_ACCESS)))
143 {
144 Fail("ERROR: %u :Created a duplicate handle to "
145 " a NULL handle hFile=0x%lx\n",
146 GetLastError(),
147 hFile);
148 }
149
150 PAL_Terminate();
151 return (PASS);
152}
153