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: test5.c
8**
9** Purpose: Tests the PAL implementation of the SetEndOfFile function.
10** Test attempts to read the number of characters up to
11** the EOF pointer, which was specified.
12**
13**
14**===================================================================*/
15
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[])
19{
20 HANDLE hFile = NULL;
21 DWORD dwBytesWritten;
22 DWORD retCode;
23 BOOL bRc = FALSE;
24 char szBuffer[256];
25 DWORD dwBytesRead = 0;
26 char testFile[] = "testfile.tmp";
27 char testString[] = "watch what happens";
28 LONG shiftAmount = 10;
29
30 /* Initialize the PAL.
31 */
32 if (0 != PAL_Initialize(argc,argv))
33 {
34 return FAIL;
35 }
36
37 /* Initialize the buffer.
38 */
39 memset(szBuffer, 0, 256);
40
41 /* Create a file to test with.
42 */
43 hFile = CreateFile(testFile,
44 GENERIC_WRITE|GENERIC_READ,
45 FILE_SHARE_WRITE|FILE_SHARE_READ,
46 NULL,
47 CREATE_ALWAYS,
48 FILE_ATTRIBUTE_NORMAL,
49 NULL);
50
51 if(hFile == INVALID_HANDLE_VALUE)
52 {
53 Fail("ERROR:%u: Unable to create file \"%s\".\n",
54 GetLastError(),
55 testFile);
56 }
57
58 /* Write to the File handle.
59 */
60 bRc = WriteFile(hFile,
61 testString,
62 strlen(testString),
63 &dwBytesWritten,
64 NULL);
65
66 if (bRc == FALSE)
67 {
68 Trace("ERROR:%u: Unable to write to file handle "
69 "hFile=0x%lx\n",
70 GetLastError(),
71 hFile);
72 if (!CloseHandle(hFile))
73 {
74 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
75 GetLastError(),
76 hFile);
77 }
78 Fail("");
79 }
80
81 /* Set the file pointer to shiftAmount bytes from the front of the file
82 */
83 retCode = SetFilePointer(hFile, shiftAmount, NULL, FILE_BEGIN);
84 if(retCode == INVALID_SET_FILE_POINTER)
85 {
86 Trace("ERROR:%u: Unable to set the file pointer to %d\n",
87 GetLastError(),
88 shiftAmount);
89 if (!CloseHandle(hFile))
90 {
91 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
92 GetLastError(),
93 hFile);
94 }
95 Fail("");
96 }
97
98 /* set the end of file pointer to 'shiftAmount' */
99 bRc = SetEndOfFile(hFile);
100 if (bRc == FALSE)
101 {
102 Trace("ERROR:%u: Unable to set file pointer of file handle 0x%lx.\n",
103 GetLastError(),
104 hFile);
105 if (!CloseHandle(hFile))
106 {
107 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
108 GetLastError(),
109 hFile);
110 }
111 Fail("");
112 }
113
114 /* Set the file pointer to 10 bytes from the front of the file
115 */
116 retCode = SetFilePointer(hFile, (LONG)NULL, NULL, FILE_BEGIN);
117 if(retCode == INVALID_SET_FILE_POINTER)
118 {
119 Trace("ERROR:%u: Unable to set the file pointer to %d\n",
120 GetLastError(),
121 FILE_BEGIN);
122 if (!CloseHandle(hFile))
123 {
124 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
125 GetLastError(),
126 hFile);
127 }
128 Fail("");
129 }
130
131 /* Attempt to read the entire string, 'testString' from a file
132 * that has it's end of pointer set at shiftAmount;
133 */
134 bRc = ReadFile(hFile,
135 szBuffer,
136 strlen(testString),
137 &dwBytesRead,
138 NULL);
139
140 if (bRc == FALSE)
141 {
142 Trace("ERROR:%u: Unable to read from file handle 0x%lx.\n",
143 GetLastError(),
144 hFile);
145 if (!CloseHandle(hFile))
146 {
147 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
148 GetLastError(),
149 hFile);
150 }
151 Fail("");
152 }
153
154 /* Confirm the number of bytes read with that requested.
155 */
156 if (dwBytesRead != shiftAmount)
157 {
158 Trace("ERROR: The number of bytes read \"%d\" is not equal to the "
159 "number that should have been written \"%d\".\n",
160 dwBytesRead,
161 shiftAmount);
162 if (!CloseHandle(hFile))
163 {
164 Fail("ERROR:%u%: Unable to close handle 0x%lx.\n",
165 GetLastError(),
166 hFile);
167 }
168 Fail("");
169 }
170
171 bRc = CloseHandle(hFile);
172 if(!bRc)
173 {
174 Fail("ERROR:%u CloseHandle failed to close the handle\n",
175 GetLastError());
176 }
177
178 /* Terminate the PAL.
179 */
180 PAL_Terminate();
181 return PASS;
182}
183
184