| 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: SetEndOfFile.c (test 3) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetEndOfFile function. |
| 10 | ** This test will attempt to expand a file. Assumes successful |
| 11 | ** SetFilePointer and GetFileSize tests. |
| 12 | ** |
| 13 | ** |
| 14 | **===================================================================*/ |
| 15 | |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | |
| 19 | const char* szTextFile = "text.txt" ; |
| 20 | |
| 21 | |
| 22 | int __cdecl main(int argc, char *argv[]) |
| 23 | { |
| 24 | HANDLE hFile = NULL; |
| 25 | DWORD dwByteCount = 0; |
| 26 | DWORD dwOffset = 25; |
| 27 | DWORD dwRc = 0; |
| 28 | BOOL bRc = FALSE; |
| 29 | |
| 30 | |
| 31 | if (0 != PAL_Initialize(argc,argv)) |
| 32 | { |
| 33 | return FAIL; |
| 34 | } |
| 35 | |
| 36 | /* create a test file */ |
| 37 | hFile = CreateFile(szTextFile, |
| 38 | GENERIC_READ | GENERIC_WRITE, |
| 39 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 40 | NULL, |
| 41 | CREATE_ALWAYS, |
| 42 | FILE_ATTRIBUTE_NORMAL, |
| 43 | NULL); |
| 44 | |
| 45 | if(hFile == INVALID_HANDLE_VALUE) |
| 46 | { |
| 47 | Fail("SetEndOfFile: ERROR -> Unable to create file \"%s\".\n" , |
| 48 | szTextFile); |
| 49 | } |
| 50 | |
| 51 | /* move the file pointer */ |
| 52 | /* assumes a successful SetFilePointer test */ |
| 53 | dwRc = SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN); |
| 54 | if (dwRc == INVALID_SET_FILE_POINTER) |
| 55 | { |
| 56 | Trace("SetEndOfFile: ERROR -> Call to SetFilePointer failed\n" ); |
| 57 | bRc = CloseHandle(hFile); |
| 58 | if (bRc != TRUE) |
| 59 | { |
| 60 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 61 | szTextFile); |
| 62 | } |
| 63 | PAL_Terminate(); |
| 64 | return FAIL; |
| 65 | } |
| 66 | |
| 67 | bRc = SetEndOfFile(hFile); |
| 68 | if (bRc != TRUE) |
| 69 | { |
| 70 | Trace("SetEndOfFile: ERROR -> Uable to set end of file.\n" ); |
| 71 | bRc = CloseHandle(hFile); |
| 72 | if (bRc != TRUE) |
| 73 | { |
| 74 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 75 | szTextFile); |
| 76 | } |
| 77 | PAL_TerminateEx(FAIL); |
| 78 | return FAIL; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* call GetFileSize to verify pointer position */ |
| 83 | /* assumes a successful GetFileSize test */ |
| 84 | |
| 85 | dwByteCount = GetFileSize(hFile, NULL); |
| 86 | if (dwByteCount != dwOffset) |
| 87 | { |
| 88 | Trace("SetEndOfFile: ERROR -> file apparently not expanded to the" |
| 89 | " correct size.\n" ); |
| 90 | bRc = CloseHandle(hFile); |
| 91 | if (bRc != TRUE) |
| 92 | { |
| 93 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 94 | szTextFile); |
| 95 | } |
| 96 | PAL_TerminateEx(FAIL); |
| 97 | return FAIL; |
| 98 | } |
| 99 | |
| 100 | bRc = CloseHandle(hFile); |
| 101 | if (bRc != TRUE) |
| 102 | { |
| 103 | Fail("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 104 | szTextFile); |
| 105 | } |
| 106 | |
| 107 | PAL_Terminate(); |
| 108 | return PASS; |
| 109 | } |
| 110 | |