| 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 7) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetFilePointer function. |
| 10 | ** Test the FILE_END 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 | |
| 26 | const char* szTextFile = "text.txt" ; |
| 27 | |
| 28 | |
| 29 | int __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 | |
| 39 | if (0 != PAL_Initialize(argc,argv)) |
| 40 | { |
| 41 | return FAIL; |
| 42 | } |
| 43 | |
| 44 | /* create a test file */ |
| 45 | hFile = CreateFile(szTextFile, |
| 46 | GENERIC_READ | GENERIC_WRITE, |
| 47 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 48 | NULL, |
| 49 | CREATE_ALWAYS, |
| 50 | FILE_ATTRIBUTE_NORMAL, |
| 51 | NULL); |
| 52 | |
| 53 | if(hFile == INVALID_HANDLE_VALUE) |
| 54 | { |
| 55 | Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n" , |
| 56 | szTextFile); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* move -1 from beginning which should fail */ |
| 61 | dwHighOrder = -1; |
| 62 | dwOffset = 0; |
| 63 | dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_END); |
| 64 | if (dwRc != INVALID_SET_FILE_POINTER) |
| 65 | { |
| 66 | Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " |
| 67 | "before the beginning of the file.\n" ); |
| 68 | if (CloseHandle(hFile) != TRUE) |
| 69 | { |
| 70 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 71 | szTextFile); |
| 72 | } |
| 73 | if (!DeleteFileA(szTextFile)) |
| 74 | { |
| 75 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 76 | szTextFile); |
| 77 | } |
| 78 | PAL_TerminateEx(FAIL); |
| 79 | return FAIL; |
| 80 | } |
| 81 | |
| 82 | /* move the pointer ahead in the file and verify */ |
| 83 | dwHighOrder = 1; |
| 84 | dwOffset = 10; |
| 85 | dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_END); |
| 86 | if ((dwRc != 10) || (dwHighOrder != 1)) |
| 87 | { |
| 88 | Trace("SetFilePointer: ERROR -> Asked to move 4GB plus 10 bytes from " |
| 89 | "the beginning of the file but didn't.\n" ); |
| 90 | if (CloseHandle(hFile) != 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 | else |
| 104 | { |
| 105 | /* verify results */ |
| 106 | if (SetEndOfFile(hFile) != TRUE) |
| 107 | { |
| 108 | Trace("SetFilePointer: ERROR -> Call to SetEndOfFile failed with " |
| 109 | "error code: %d\n" , |
| 110 | GetLastError()); |
| 111 | if (CloseHandle(hFile) != 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 | dwReturnedOffset = GetFileSize(hFile, (DWORD*)&dwReturnedHighOrder); |
| 125 | if ((dwReturnedOffset != dwOffset) || (dwReturnedHighOrder != dwHighOrder)) |
| 126 | { |
| 127 | Trace("SetFilePointer: ERROR -> Asked to move far past the " |
| 128 | "end of the file. 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 \"%s\".\n" , |
| 135 | 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_END); |
| 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 | |