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 3) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the SetFilePointer function. |
10 | ** Test the FILE_CURRENT 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 | |
26 | const char* const szText = |
27 | "The quick brown fox jumped over the lazy dog's back." ; |
28 | const char* szTextFile = "text.txt" ; |
29 | |
30 | |
31 | int __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 | if (CloseHandle(hFile) != TRUE) |
67 | { |
68 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
69 | szTextFile); |
70 | } |
71 | if (!DeleteFileA(szTextFile)) |
72 | { |
73 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
74 | szTextFile); |
75 | } |
76 | PAL_TerminateEx(FAIL); |
77 | return FAIL; |
78 | } |
79 | |
80 | /* reset the pointer to the beginning */ |
81 | if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) |
82 | { |
83 | if (GetLastError() != ERROR_SUCCESS) |
84 | { |
85 | Trace("SetFilePointer: ERROR -> Unable to reset the pointer to the " |
86 | "beginning of the file" ); |
87 | if (CloseHandle(hFile) != TRUE) |
88 | { |
89 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
90 | szTextFile); |
91 | } |
92 | if (!DeleteFileA(szTextFile)) |
93 | { |
94 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
95 | " \"%s\".\n" , szTextFile); |
96 | } |
97 | PAL_TerminateEx(FAIL); |
98 | return FAIL; |
99 | } |
100 | } |
101 | |
102 | /* move -1 from beginning which should fail */ |
103 | dwRc = SetFilePointer(hFile, -1, NULL, FILE_CURRENT); |
104 | if ((dwRc != INVALID_SET_FILE_POINTER) || |
105 | (GetLastError() == ERROR_SUCCESS)) |
106 | { |
107 | Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " |
108 | "before the beginning of the file.\n" ); |
109 | if (CloseHandle(hFile) != TRUE) |
110 | { |
111 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
112 | szTextFile); |
113 | } |
114 | if (!DeleteFileA(szTextFile)) |
115 | { |
116 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
117 | szTextFile); |
118 | } |
119 | PAL_TerminateEx(FAIL); |
120 | return FAIL; |
121 | } |
122 | |
123 | |
124 | /* move the file pointer 0 bytes from the beginning and verify */ |
125 | dwRc = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); |
126 | if (dwRc != 0) |
127 | { |
128 | Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " |
129 | "beginning of the file but moved %ld bytes.\n" , dwRc); |
130 | if (CloseHandle(hFile) != TRUE) |
131 | { |
132 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
133 | szTextFile); |
134 | } |
135 | if (!DeleteFileA(szTextFile)) |
136 | { |
137 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
138 | szTextFile); |
139 | } |
140 | PAL_TerminateEx(FAIL); |
141 | return FAIL; |
142 | } |
143 | |
144 | /* move the pointer ahead in the file and verify */ |
145 | dwRc = SetFilePointer(hFile, 20, NULL, FILE_CURRENT); |
146 | if (dwRc != 20) |
147 | { |
148 | Trace("SetFilePointer: ERROR -> Asked to move 20 bytes from the " |
149 | "beginning of the file but moved %ld bytes.\n" , dwRc); |
150 | if (CloseHandle(hFile) != TRUE) |
151 | { |
152 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
153 | szTextFile); |
154 | } |
155 | if (!DeleteFileA(szTextFile)) |
156 | { |
157 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
158 | szTextFile); |
159 | } |
160 | PAL_TerminateEx(FAIL); |
161 | return FAIL; |
162 | } |
163 | else |
164 | { |
165 | /* verify results */ |
166 | memset(szBuffer, 0, 100); |
167 | bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText)-20, &dwByteCount, |
168 | NULL); |
169 | if ((bRc != TRUE) || (dwByteCount != strlen(szText)-20)) |
170 | { |
171 | Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly" ); |
172 | if (CloseHandle(hFile) != TRUE) |
173 | { |
174 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
175 | szTextFile); |
176 | } |
177 | if (!DeleteFileA(szTextFile)) |
178 | { |
179 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
180 | " \"%s\".\n" , szTextFile); |
181 | } |
182 | PAL_TerminateEx(FAIL); |
183 | return FAIL; |
184 | } |
185 | szPtr = szText + 20;; |
186 | if (strcmp(szPtr, szBuffer) != 0) |
187 | { |
188 | Trace("SetFilePointer: ERROR -> Apparently failed to move the" |
189 | " pointer properly\n" ); |
190 | if (CloseHandle(hFile) != TRUE) |
191 | { |
192 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
193 | szTextFile); |
194 | } |
195 | if (!DeleteFileA(szTextFile)) |
196 | { |
197 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
198 | " \"%s\".\n" , szTextFile); |
199 | } |
200 | PAL_TerminateEx(FAIL); |
201 | return FAIL; |
202 | } |
203 | } |
204 | |
205 | |
206 | /* get the current file pointer position (should be 52) */ |
207 | dwRc = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); |
208 | if (dwRc != 52) |
209 | { |
210 | Trace("SetFilePointer: ERROR -> Asked for current position." |
211 | " Should be 52 but was %ld.\n" , dwRc); |
212 | if (CloseHandle(hFile) != TRUE) |
213 | { |
214 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
215 | szTextFile); |
216 | } |
217 | if (!DeleteFileA(szTextFile)) |
218 | { |
219 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
220 | szTextFile); |
221 | } |
222 | PAL_TerminateEx(FAIL); |
223 | return FAIL; |
224 | } |
225 | |
226 | |
227 | /* move the pointer backwards in the file and verify */ |
228 | dwRc = SetFilePointer(hFile, -10, NULL, FILE_CURRENT); |
229 | if (dwRc != 42) |
230 | { |
231 | Trace("SetFilePointer: ERROR -> Asked to move back 10 bytes from the" |
232 | "end of the file but moved it to position %ld.\n" , dwRc); |
233 | if (CloseHandle(hFile) != TRUE) |
234 | { |
235 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
236 | szTextFile); |
237 | } |
238 | if (!DeleteFileA(szTextFile)) |
239 | { |
240 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
241 | szTextFile); |
242 | } |
243 | PAL_TerminateEx(FAIL); |
244 | return FAIL; |
245 | } |
246 | else |
247 | { |
248 | /* verify results */ |
249 | memset(szBuffer, 0, 100); |
250 | bRc = ReadFile(hFile, szBuffer, 10, &dwByteCount, NULL); |
251 | if ((bRc != TRUE) || (dwByteCount != 10)) |
252 | { |
253 | Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly" ); |
254 | if (CloseHandle(hFile) != TRUE) |
255 | { |
256 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
257 | szTextFile); |
258 | } |
259 | if (!DeleteFileA(szTextFile)) |
260 | { |
261 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
262 | " \"%s\".\n" , szTextFile); |
263 | } |
264 | PAL_TerminateEx(FAIL); |
265 | return FAIL; |
266 | } |
267 | szPtr = szText + 42; |
268 | if (strcmp(szPtr, szBuffer) != 0) |
269 | { |
270 | Trace("SetFilePointer: ERROR -> Apparently failed to move the" |
271 | " pointer properly\n" ); |
272 | if (CloseHandle(hFile) != TRUE) |
273 | { |
274 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
275 | szTextFile); |
276 | } |
277 | if (!DeleteFileA(szTextFile)) |
278 | { |
279 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
280 | " \"%s\".\n" , szTextFile); |
281 | } |
282 | PAL_TerminateEx(FAIL); |
283 | return FAIL; |
284 | } |
285 | } |
286 | |
287 | /* |
288 | * the file pointer is currently at the end of the file so... |
289 | * set the pointer past the end of the file and verify |
290 | */ |
291 | dwRc = SetFilePointer(hFile, 20, NULL, FILE_CURRENT); |
292 | if ((dwRc == INVALID_SET_FILE_POINTER) && (GetLastError() != ERROR_SUCCESS)) |
293 | { |
294 | Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n" ); |
295 | if (CloseHandle(hFile) != TRUE) |
296 | { |
297 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
298 | szTextFile); |
299 | } |
300 | if (!DeleteFileA(szTextFile)) |
301 | { |
302 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
303 | szTextFile); |
304 | } |
305 | PAL_TerminateEx(FAIL); |
306 | return FAIL; |
307 | } |
308 | else |
309 | { |
310 | if (SetFilePointer(hFile, 0, NULL, FILE_CURRENT) != strlen(szText)+20) |
311 | { |
312 | Trace("SetFilePointer: ERROR -> Failed to move pointer past" |
313 | " EOF.\n" ); |
314 | if (CloseHandle(hFile) != TRUE) |
315 | { |
316 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
317 | szTextFile); |
318 | } |
319 | if (!DeleteFileA(szTextFile)) |
320 | { |
321 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
322 | " \"%s\".\n" , szTextFile); |
323 | } |
324 | PAL_TerminateEx(FAIL); |
325 | return FAIL; |
326 | } |
327 | } |
328 | |
329 | if (CloseHandle(hFile) != TRUE) |
330 | { |
331 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
332 | szTextFile); |
333 | if (!DeleteFileA(szTextFile)) |
334 | { |
335 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
336 | szTextFile); |
337 | } |
338 | PAL_TerminateEx(FAIL); |
339 | return FAIL; |
340 | } |
341 | |
342 | if (!DeleteFileA(szTextFile)) |
343 | { |
344 | Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
345 | szTextFile); |
346 | } |
347 | |
348 | PAL_Terminate(); |
349 | return PASS; |
350 | } |
351 | |