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 5) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the SetFilePointer function. |
10 | ** Test the FILE_BEGIN option using the high word parameter |
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* szTextFile = "text.txt" ; |
27 | |
28 | int __cdecl main(int argc, char *argv[]) |
29 | { |
30 | HANDLE hFile = NULL; |
31 | DWORD dwOffset = 1; |
32 | LONG dwHighWord = 1; |
33 | DWORD dwReturnedOffset = 0; |
34 | DWORD dwReturnedHighWord = 0; |
35 | DWORD dwRc = 0; |
36 | DWORD dwError = 0; |
37 | BOOL bRc = FALSE; |
38 | |
39 | |
40 | if (0 != PAL_Initialize(argc,argv)) |
41 | { |
42 | return FAIL; |
43 | } |
44 | |
45 | /* create a test file */ |
46 | hFile = CreateFile(szTextFile, |
47 | GENERIC_READ | GENERIC_WRITE, |
48 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
49 | NULL, |
50 | CREATE_ALWAYS, |
51 | FILE_ATTRIBUTE_NORMAL, |
52 | NULL); |
53 | |
54 | if(hFile == INVALID_HANDLE_VALUE) |
55 | { |
56 | dwError = GetLastError(); |
57 | Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n with " |
58 | "error %ld" , |
59 | szTextFile, |
60 | GetLastError()); |
61 | } |
62 | |
63 | |
64 | |
65 | /* move -1 from beginning which should fail */ |
66 | dwRc = SetFilePointer(hFile, -1, &dwHighWord, FILE_BEGIN); |
67 | if (dwRc != INVALID_SET_FILE_POINTER) |
68 | { |
69 | Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " |
70 | "before the beginning of the file using the high word.\n" ); |
71 | bRc = CloseHandle(hFile); |
72 | if (bRc != TRUE) |
73 | { |
74 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
75 | szTextFile); |
76 | } |
77 | if (!DeleteFileA(szTextFile)) |
78 | { |
79 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
80 | szTextFile); |
81 | } |
82 | PAL_TerminateEx(FAIL); |
83 | return FAIL; |
84 | } |
85 | |
86 | /* set the pointer past the end of the file and verify */ |
87 | dwRc = SetFilePointer(hFile, dwOffset, &dwHighWord, FILE_BEGIN); |
88 | if ((dwRc == INVALID_SET_FILE_POINTER) && |
89 | ((dwError = GetLastError()) != ERROR_SUCCESS)) |
90 | { |
91 | Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n" ); |
92 | bRc = CloseHandle(hFile); |
93 | if (bRc != TRUE) |
94 | { |
95 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
96 | szTextFile); |
97 | } |
98 | if (!DeleteFileA(szTextFile)) |
99 | { |
100 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
101 | szTextFile); |
102 | } |
103 | PAL_TerminateEx(FAIL); |
104 | return FAIL; |
105 | } |
106 | else |
107 | { |
108 | /* verify */ |
109 | bRc = SetEndOfFile(hFile); |
110 | if (bRc != TRUE) |
111 | { |
112 | dwError = GetLastError(); |
113 | if (dwError == 112) |
114 | { |
115 | Trace("SetFilePointer: ERROR -> SetEndOfFile failed due to " |
116 | "lack of disk space\n" ); |
117 | } |
118 | else |
119 | { |
120 | Trace("SetFilePointer: ERROR -> SetEndOfFile call failed with " |
121 | "error %ld\n" , dwError); |
122 | } |
123 | bRc = CloseHandle(hFile); |
124 | if (bRc != TRUE) |
125 | { |
126 | Trace("SetFilePointer: ERROR -> Unable to close file" |
127 | " \"%s\".\n" , szTextFile); |
128 | } |
129 | if (!DeleteFileA(szTextFile)) |
130 | { |
131 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
132 | " \"%s\".\n" , szTextFile); |
133 | } |
134 | PAL_TerminateEx(FAIL); |
135 | return FAIL; |
136 | } |
137 | |
138 | dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighWord); |
139 | if ((dwOffset != dwReturnedOffset) || |
140 | (dwHighWord != dwReturnedHighWord)) |
141 | { |
142 | Trace("SetFilePointer: ERROR -> Failed to move pointer past" |
143 | " EOF.\n" ); |
144 | bRc = CloseHandle(hFile); |
145 | if (bRc != TRUE) |
146 | { |
147 | Trace("SetFilePointer: ERROR -> Unable to close file" |
148 | " \"%s\".\n" , szTextFile); |
149 | } |
150 | if (!DeleteFileA(szTextFile)) |
151 | { |
152 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
153 | " \"%s\".\n" , szTextFile); |
154 | } |
155 | PAL_TerminateEx(FAIL); |
156 | return FAIL; |
157 | } |
158 | } |
159 | |
160 | bRc = CloseHandle(hFile); |
161 | if (bRc != TRUE) |
162 | { |
163 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
164 | szTextFile); |
165 | if (!DeleteFileA(szTextFile)) |
166 | { |
167 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
168 | szTextFile); |
169 | } |
170 | PAL_TerminateEx(FAIL); |
171 | return FAIL; |
172 | } |
173 | |
174 | if (!DeleteFileA(szTextFile)) |
175 | { |
176 | Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
177 | szTextFile); |
178 | } |
179 | |
180 | PAL_Terminate(); |
181 | return PASS; |
182 | } |
183 | |