| 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 4) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetFilePointer function. |
| 10 | ** Test the FILE_END 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* szText = "The quick brown fox jumped over the lazy dog's back." ; |
| 27 | const char* szTextFile = "text.txt" ; |
| 28 | |
| 29 | |
| 30 | int __cdecl main(int argc, char *argv[]) |
| 31 | { |
| 32 | HANDLE hFile = NULL; |
| 33 | DWORD dwByteCount = 0; |
| 34 | DWORD dwOffset = 0; |
| 35 | DWORD dwRc = 0; |
| 36 | BOOL bRc = FALSE; |
| 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 | bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwByteCount, NULL); |
| 59 | if (bRc == FALSE) |
| 60 | { |
| 61 | Trace("SetFilePointer: ERROR -> Unable to write to file \"%s\".\n" , |
| 62 | szTextFile); |
| 63 | if (CloseHandle(hFile) != TRUE) |
| 64 | { |
| 65 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 66 | szTextFile); |
| 67 | } |
| 68 | if (!DeleteFileA(szTextFile)) |
| 69 | { |
| 70 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 71 | szTextFile); |
| 72 | } |
| 73 | PAL_TerminateEx(FAIL); |
| 74 | return FAIL; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * move -1 from the end |
| 80 | */ |
| 81 | dwRc = SetFilePointer(hFile, -1, NULL, FILE_END); |
| 82 | if (dwRc == INVALID_SET_FILE_POINTER) |
| 83 | { |
| 84 | if (GetLastError() != ERROR_SUCCESS) |
| 85 | { |
| 86 | Trace("SetFilePointer: ERROR -> Failed to move the pointer " |
| 87 | "back one character from EOF.\n" ); |
| 88 | if (CloseHandle(hFile) != TRUE) |
| 89 | { |
| 90 | Trace("SetFilePointer: ERROR -> Unable to close file" |
| 91 | " \"%s\".\n" , szTextFile); |
| 92 | } |
| 93 | if (!DeleteFileA(szTextFile)) |
| 94 | { |
| 95 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
| 96 | " \"%s\".\n" , szTextFile); |
| 97 | } |
| 98 | PAL_TerminateEx(FAIL); |
| 99 | return FAIL; |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | /* verify */ |
| 105 | if ((dwRc != strlen(szText)-1)) |
| 106 | { |
| 107 | Trace("SetFilePointer: ERROR -> Failed to move the pointer" |
| 108 | " -1 bytes from EOF\n" ); |
| 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 | } |
| 123 | |
| 124 | /* |
| 125 | * move the file pointer 0 bytes from the end and verify |
| 126 | */ |
| 127 | dwRc = SetFilePointer(hFile, 0, NULL, FILE_END); |
| 128 | if (dwRc != strlen(szText)) |
| 129 | { |
| 130 | Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " |
| 131 | "end of the file. Function returned %ld instead of 52.\n" , dwRc); |
| 132 | if (CloseHandle(hFile) != 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 | |
| 146 | /* |
| 147 | * move the pointer past the end of the file and verify |
| 148 | */ |
| 149 | dwRc = SetFilePointer(hFile, 20, NULL, FILE_END); |
| 150 | if (dwRc != strlen(szText)+20) |
| 151 | { |
| 152 | Trace("SetFilePointer: ERROR -> Asked to move 20 bytes past the " |
| 153 | "end of the file. Function returned %ld instead of %d.\n" , |
| 154 | dwRc, |
| 155 | strlen(szText)+20); |
| 156 | if (CloseHandle(hFile) != TRUE) |
| 157 | { |
| 158 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 159 | szTextFile); |
| 160 | } |
| 161 | if (!DeleteFileA(szTextFile)) |
| 162 | { |
| 163 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 164 | szTextFile); |
| 165 | } |
| 166 | PAL_TerminateEx(FAIL); |
| 167 | return FAIL; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | /* verify results */ |
| 172 | bRc = SetEndOfFile(hFile); |
| 173 | if ((dwRc = GetFileSize(hFile, NULL)) != strlen(szText)+20) |
| 174 | { |
| 175 | Trace("SetFilePointer: ERROR -> Asked to move back 20 bytes past" |
| 176 | " theend of the file. GetFileSize returned %ld whereas it " |
| 177 | "should have been %d.\n" , |
| 178 | dwRc, |
| 179 | strlen(szText)+20); |
| 180 | if (CloseHandle(hFile) != TRUE) |
| 181 | { |
| 182 | Trace("SetFilePointer: ERROR -> Unable to close file" |
| 183 | " \"%s\".\n" , szTextFile); |
| 184 | } |
| 185 | if (!DeleteFileA(szTextFile)) |
| 186 | { |
| 187 | Trace("SetFilePointer: ERROR -> Unable to delete file" |
| 188 | " \"%s\".\n" , szTextFile); |
| 189 | } |
| 190 | PAL_TerminateEx(FAIL); |
| 191 | return FAIL; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* |
| 197 | * move the pointer backwards to before the start of the file and verify |
| 198 | */ |
| 199 | |
| 200 | dwOffset = (dwRc + 20) * -1; |
| 201 | dwRc = SetFilePointer(hFile, dwOffset, NULL, FILE_END); |
| 202 | if ((dwRc != INVALID_SET_FILE_POINTER) || |
| 203 | (GetLastError() == ERROR_SUCCESS)) |
| 204 | { |
| 205 | Trace("SetFilePointer: ERROR -> Was able to move the pointer " |
| 206 | "to before the beginning of the file.\n" ); |
| 207 | if (CloseHandle(hFile) != TRUE) |
| 208 | { |
| 209 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 210 | szTextFile); |
| 211 | } |
| 212 | if (!DeleteFileA(szTextFile)) |
| 213 | { |
| 214 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 215 | szTextFile); |
| 216 | } |
| 217 | PAL_TerminateEx(FAIL); |
| 218 | return FAIL; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | if (CloseHandle(hFile) != TRUE) |
| 223 | { |
| 224 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 225 | szTextFile); |
| 226 | if (!DeleteFileA(szTextFile)) |
| 227 | { |
| 228 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 229 | szTextFile); |
| 230 | } |
| 231 | PAL_TerminateEx(FAIL); |
| 232 | return FAIL; |
| 233 | } |
| 234 | |
| 235 | if (!DeleteFileA(szTextFile)) |
| 236 | { |
| 237 | Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 238 | szTextFile); |
| 239 | } |
| 240 | PAL_Terminate(); |
| 241 | return PASS; |
| 242 | } |
| 243 | |