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 6)
8**
9** Purpose: Tests the PAL implementation of the SetFilePointer function.
10** Test the FILE_CURRENT option with high order support
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* szTextFile = "text.txt";
27
28
29int __cdecl main(int argc, char *argv[])
30{
31 HANDLE hFile = NULL;
32 DWORD dwOffset = 0;
33 LONG dwHighOrder = 0;
34 DWORD dwReturnedOffset = 0;
35 LONG dwReturnedHighOrder = 0;
36 DWORD dwRc = 0;
37
38 if (0 != PAL_Initialize(argc,argv))
39 {
40 return FAIL;
41 }
42
43 /* create a test file */
44 hFile = CreateFile(szTextFile,
45 GENERIC_READ | GENERIC_WRITE,
46 FILE_SHARE_READ | FILE_SHARE_WRITE,
47 NULL,
48 CREATE_ALWAYS,
49 FILE_ATTRIBUTE_NORMAL,
50 NULL);
51
52 if(hFile == INVALID_HANDLE_VALUE)
53 {
54 Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n",
55 szTextFile);
56 }
57
58
59 /* move waaaay before the beginning which should fail */
60 dwHighOrder = -1;
61 dwOffset = 0;
62 dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT);
63 if (dwRc != INVALID_SET_FILE_POINTER)
64 {
65 Trace("SetFilePointer: ERROR -> Succeeded to move the pointer "
66 "before the beginning of the file.\n");
67 if (CloseHandle(hFile) != 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 /* move the pointer ahead in the file and verify */
82 dwHighOrder = 1;
83 dwOffset = 10;
84 dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT);
85 if ((dwRc != 10) || (dwHighOrder != 1))
86 {
87 Trace("SetFilePointer: ERROR -> Asked to move 2GB plus 10 bytes from "
88 "the beginning of the file but didn't.\n");
89 if (CloseHandle(hFile) != TRUE)
90 {
91 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
92 szTextFile);
93 }
94 if (!DeleteFileA(szTextFile))
95 {
96 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
97 szTextFile);
98 }
99 PAL_TerminateEx(FAIL);
100 return FAIL;
101 }
102 else
103 {
104 /* verify results */
105 if (SetEndOfFile(hFile) != TRUE)
106 {
107 Trace("SetFilePointer: ERROR -> Call to SetEndOfFile failed with "
108 "error code: %d\n", GetLastError());
109 if (CloseHandle(hFile) != TRUE)
110 {
111 Trace("SetFilePointer: ERROR -> Unable to close file"
112 " \"%s\".\n", szTextFile);
113 }
114 if (!DeleteFileA(szTextFile))
115 {
116 Trace("SetFilePointer: ERROR -> Unable to delete file"
117 " \"%s\".\n", szTextFile);
118 }
119 PAL_TerminateEx(FAIL);
120 return FAIL;
121 }
122 dwReturnedOffset = GetFileSize(hFile, (DWORD*)&dwReturnedHighOrder);
123 if ((dwReturnedOffset != dwOffset) ||
124 (dwReturnedHighOrder != dwHighOrder))
125 {
126 Trace("SetFilePointer: ERROR -> Asked to move far past the "
127 "current file pointer. "
128 "low order sent: %ld low order returned: %ld "
129 "high order sent: %ld high order returned: %ld",
130 dwOffset, dwReturnedOffset,
131 dwHighOrder, dwReturnedHighOrder);
132 if (!DeleteFileA(szTextFile))
133 {
134 Trace("SetFilePointer: ERROR -> Unable to delete file"
135 " \"%s\".\n", szTextFile);
136 }
137 PAL_TerminateEx(FAIL);
138 return FAIL;
139 }
140 }
141
142
143 /*
144 * move the pointer backwards in the file and verify
145 */
146 dwOffset = 0;
147 dwHighOrder = -1;
148 dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT);
149 if (dwRc != 10)
150 {
151 Trace("SetFilePointer: ERROR -> Asked to move back to 10 bytes from the"
152 "beginning of the file but moved it to position %ld.\n", dwRc);
153 if (CloseHandle(hFile) != TRUE)
154 {
155 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
156 szTextFile);
157 }
158 if (!DeleteFileA(szTextFile))
159 {
160 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
161 szTextFile);
162 }
163 PAL_TerminateEx(FAIL);
164 return FAIL;
165 }
166 else
167 {
168 /* verify results */
169 dwReturnedHighOrder = 0;
170 dwRc = SetFilePointer(hFile, 0, &dwReturnedHighOrder, FILE_CURRENT);
171 if (dwRc != 10)
172 {
173 Trace("SetFilePointer: ERROR -> Asked for current position. "
174 "Should be 10 but was %ld.\n", dwRc);
175 if (CloseHandle(hFile) != TRUE)
176 {
177 Trace("SetFilePointer: ERROR -> Unable to close file"
178 " \"%s\".\n", szTextFile);
179 }
180 if (!DeleteFileA(szTextFile))
181 {
182 Trace("SetFilePointer: ERROR -> Unable to delete file"
183 " \"%s\".\n", szTextFile);
184 }
185 PAL_TerminateEx(FAIL);
186 return FAIL;
187 }
188 }
189
190
191 /* clean up, clean up, everybody do their share... */
192 if (CloseHandle(hFile) != TRUE)
193 {
194 Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n",
195 szTextFile);
196 if (!DeleteFileA(szTextFile))
197 {
198 Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
199 szTextFile);
200 }
201 PAL_TerminateEx(FAIL);
202 return FAIL;
203 }
204
205 if (!DeleteFileA(szTextFile))
206 {
207 Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n",
208 szTextFile);
209 }
210
211 PAL_Terminate();
212 return PASS;
213}
214