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: SetFilePointer.c (test 2)
8**
9** Purpose: Tests the PAL implementation of the SetFilePointer function.
10** Test the FILE_BEGIN option
11**
12** Assumes Successful:
13** CreateFile
14** ReadFile
15** WriteFile
16** strlen
17** CloseHandle
18** strcmp
19** GetFileSize
20**
21**
22**===================================================================*/
23
24#include <palsuite.h>
25
26const char * const szText =
27 "The quick brown fox jumped over the lazy dog's back.";
28const char* szTextFile = "text.txt";
29
30
31int __cdecl main(int argc, char *argv[])
32{
33 HANDLE hFile = NULL;
34 DWORD dwByteCount = 0;
35 DWORD dwRc = 0;
36 BOOL bRc = FALSE;
37 char szBuffer[100];
38 const char *szPtr;
39
40
41 if (0 != PAL_Initialize(argc,argv))
42 {
43 return FAIL;
44 }
45
46 /* create a test file */
47 hFile = CreateFile(szTextFile,
48 GENERIC_READ | GENERIC_WRITE,
49 FILE_SHARE_READ | FILE_SHARE_WRITE,
50 NULL,
51 CREATE_ALWAYS,
52 FILE_ATTRIBUTE_NORMAL,
53 NULL);
54
55 if(hFile == INVALID_HANDLE_VALUE)
56 {
57 Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n",
58 szTextFile);
59 }
60
61 bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwByteCount, NULL);
62 if (bRc == FALSE)
63 {
64 Trace("SetFilePointer: ERROR -> Unable to write to file \"%s\".\n",
65 szTextFile);
66 bRc = CloseHandle(hFile);
67 if (bRc != TRUE)
68 {
69 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
70 szTextFile);
71 }
72 if (!DeleteFileA(szTextFile))
73 {
74 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
75 szTextFile);
76 }
77 PAL_TerminateEx(FAIL);
78 return FAIL;
79 }
80
81
82 /* move -1 from beginning which should fail */
83 dwRc = SetFilePointer(hFile, -1, NULL, FILE_BEGIN);
84 if ((dwRc != INVALID_SET_FILE_POINTER) ||
85 (GetLastError() == ERROR_SUCCESS))
86 {
87 Trace("SetFilePointer: ERROR -> Succeeded to move the pointer "
88 "before the beginning of the file.\n");
89 bRc = CloseHandle(hFile);
90 if (bRc != TRUE)
91 {
92 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
93 szTextFile);
94 }
95 if (!DeleteFileA(szTextFile))
96 {
97 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
98 szTextFile);
99 }
100 PAL_TerminateEx(FAIL);
101 return FAIL;
102 }
103
104 /* move the file pointer 0 bytes from the beginning and verify */
105 dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
106 if (dwRc != 0)
107 {
108 Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the "
109 "beginning of the file but moved %ld bytes.\n", dwRc);
110 bRc = CloseHandle(hFile);
111 if (bRc != TRUE)
112 {
113 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
114 szTextFile);
115 }
116 if (!DeleteFileA(szTextFile))
117 {
118 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
119 szTextFile);
120 }
121 PAL_TerminateEx(FAIL);
122 return FAIL;
123 }
124
125 /* move the pointer ahead in the file and verify */
126 dwRc = SetFilePointer(hFile, 20, NULL, FILE_BEGIN);
127 if (dwRc != 20)
128 {
129 Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the "
130 "beginning of the file but moved %ld bytes.\n", dwRc);
131 bRc = CloseHandle(hFile);
132 if (bRc != TRUE)
133 {
134 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
135 szTextFile);
136 }
137 if (!DeleteFileA(szTextFile))
138 {
139 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
140 szTextFile);
141 }
142 PAL_TerminateEx(FAIL);
143 return FAIL;
144 }
145 else
146 {
147 /* verify results */
148 memset(szBuffer, 0, 100);
149 bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText)-20, &dwByteCount,
150 NULL);
151 if ((bRc != TRUE) || (dwByteCount != strlen(szText)-20))
152 {
153 Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly");
154 bRc = CloseHandle(hFile);
155 if (bRc != TRUE)
156 {
157 Trace("SetFilePointer: ERROR -> Unable to close file"
158 "\"%s\".\n", szTextFile);
159 }
160 if (!DeleteFileA(szTextFile))
161 {
162 Trace("SetFilePointer: ERROR -> Unable to delete file"
163 "\"%s\".\n", szTextFile);
164 }
165 PAL_TerminateEx(FAIL);
166 return FAIL;
167 }
168 szPtr = szText + 20;
169 if (strcmp(szPtr, szBuffer) != 0)
170 {
171 Trace("SetFilePointer: ERROR -> Apparently failed to move the "
172 "pointer properly\n");
173 bRc = CloseHandle(hFile);
174 if (bRc != TRUE)
175 {
176 Trace("SetFilePointer: ERROR -> Unable to close file"
177 " \"%s\".\n", szTextFile);
178 }
179 if (!DeleteFileA(szTextFile))
180 {
181 Trace("SetFilePointer: ERROR -> Unable to delete file"
182 " \"%s\".\n", szTextFile);
183 }
184 PAL_TerminateEx(FAIL);
185 return FAIL;
186 }
187 }
188
189 /* move the file pointer back to the beginning and verify */
190 dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
191 if (dwRc != 0)
192 {
193 Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the "
194 "beginning of the file but moved %ld bytes.\n", dwRc);
195 bRc = CloseHandle(hFile);
196 if (bRc != TRUE)
197 {
198 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
199 szTextFile);
200 }
201 if (!DeleteFileA(szTextFile))
202 {
203 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
204 szTextFile);
205 }
206 PAL_TerminateEx(FAIL);
207 return FAIL;
208 }
209 else
210 {
211 /* verify results */
212 memset(szBuffer, 0, 100);
213 bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText), &dwByteCount,
214 NULL);
215 if ((bRc != TRUE) || (dwByteCount != strlen(szText)))
216 {
217 Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly");
218 bRc = CloseHandle(hFile);
219 if (bRc != TRUE)
220 {
221 Trace("SetFilePointer: ERROR -> Unable to close file"
222 " \"%s\".\n", szTextFile);
223 }
224 if (!DeleteFileA(szTextFile))
225 {
226 Trace("SetFilePointer: ERROR -> Unable to delete file"
227 " \"%s\".\n", szTextFile);
228 }
229 PAL_TerminateEx(FAIL);
230 return FAIL;
231 }
232 if (strcmp(szText, szBuffer) != 0)
233 {
234 Trace("SetFilePointer: ERROR -> Failed to return the pointer "
235 "properly to the beginning of the file\n");
236 bRc = CloseHandle(hFile);
237 if (bRc != TRUE)
238 {
239 Trace("SetFilePointer: ERROR -> Unable to close file"
240 " \"%s\".\n", szTextFile);
241 }
242 if (!DeleteFileA(szTextFile))
243 {
244 Trace("SetFilePointer: ERROR -> Unable to delete file"
245 " \"%s\".\n", szTextFile);
246 }
247 PAL_TerminateEx(FAIL);
248 return FAIL;
249 }
250 }
251
252 /* return the pointer to the beginning of the file */
253 dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
254 if (dwRc != 0)
255 {
256 Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the "
257 "beginning of the file but moved %ld bytes.\n", dwRc);
258 bRc = CloseHandle(hFile);
259 if (bRc != TRUE)
260 {
261 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
262 szTextFile);
263 }
264 if (!DeleteFileA(szTextFile))
265 {
266 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
267 szTextFile);
268 }
269 PAL_TerminateEx(FAIL);
270 return FAIL;
271 }
272
273 /* set the pointer past the end of the file and verify */
274 dwRc = SetFilePointer(hFile, (DWORD)strlen(szText)+20, NULL, FILE_BEGIN);
275 if ((dwRc == INVALID_SET_FILE_POINTER) && (GetLastError() != ERROR_SUCCESS))
276 {
277 Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n");
278 bRc = CloseHandle(hFile);
279 if (bRc != TRUE)
280 {
281 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
282 szTextFile);
283 }
284 if (!DeleteFileA(szTextFile))
285 {
286 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
287 szTextFile);
288 }
289 PAL_TerminateEx(FAIL);
290 return FAIL;
291 }
292 else
293 {
294 /* verify */
295 bRc = SetEndOfFile(hFile);
296 if (bRc != TRUE)
297 {
298 bRc = CloseHandle(hFile);
299 if (bRc != TRUE)
300 {
301 Trace("SetFilePointer: ERROR -> Unable to close file"
302 " \"%s\".\n", szTextFile);
303 }
304 if (!DeleteFileA(szTextFile))
305 {
306 Trace("SetFilePointer: ERROR -> Unable to delete file"
307 " \"%s\".\n", szTextFile);
308 }
309 PAL_TerminateEx(FAIL);
310 return FAIL;
311 }
312
313 if (GetFileSize(hFile, NULL) != strlen(szText)+20)
314 {
315 Trace("SetFilePointer: ERROR -> Failed to move pointer past"
316 " EOF.\n");
317 bRc = CloseHandle(hFile);
318 if (bRc != TRUE)
319 {
320 Trace("SetFilePointer: ERROR -> Unable to close file"
321 " \"%s\".\n", szTextFile);
322 }
323 if (!DeleteFileA(szTextFile))
324 {
325 Trace("SetFilePointer: ERROR -> Unable to delete file"
326 " \"%s\".\n", szTextFile);
327 }
328 PAL_TerminateEx(FAIL);
329 return FAIL;
330 }
331 }
332
333
334
335 bRc = CloseHandle(hFile);
336 if (bRc != TRUE)
337 {
338 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
339 szTextFile);
340 if (!DeleteFileA(szTextFile))
341 {
342 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
343 szTextFile);
344 }
345 PAL_TerminateEx(FAIL);
346 return FAIL;
347 }
348
349 if (!DeleteFileA(szTextFile))
350 {
351 Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
352 szTextFile);
353 }
354 PAL_Terminate();
355 return PASS;
356}
357