| 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 2) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetEndOfFile function. |
| 10 | ** This test will attempt to truncate a file |
| 11 | ** |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | |
| 18 | const char* szStringTest = "The quick fox jumped over the lazy dog's back." ; |
| 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 dwBytesWritten; |
| 27 | BOOL bRc = FALSE; |
| 28 | char szBuffer[100]; |
| 29 | DWORD dwBytesRead = 0; |
| 30 | FILE *pFile = NULL; |
| 31 | |
| 32 | |
| 33 | if (0 != PAL_Initialize(argc,argv)) |
| 34 | { |
| 35 | return FAIL; |
| 36 | } |
| 37 | |
| 38 | // create a test file |
| 39 | hFile = CreateFile(szTextFile, |
| 40 | GENERIC_READ | GENERIC_WRITE, |
| 41 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 42 | NULL, |
| 43 | OPEN_ALWAYS, |
| 44 | FILE_ATTRIBUTE_NORMAL, |
| 45 | NULL); |
| 46 | |
| 47 | if(hFile == INVALID_HANDLE_VALUE) |
| 48 | { |
| 49 | Fail("SetEndOfFile: ERROR -> Unable to create file \"%s\".\n" , |
| 50 | szTextFile); |
| 51 | } |
| 52 | |
| 53 | bRc = WriteFile(hFile, szStringTest, 20, &dwBytesWritten, NULL); |
| 54 | if (bRc != TRUE) |
| 55 | { |
| 56 | Trace("SetEndOfFile: ERROR -> Uable to write to \"%s\".\n" , |
| 57 | szTextFile); |
| 58 | bRc = CloseHandle(hFile); |
| 59 | if (bRc != TRUE) |
| 60 | { |
| 61 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 62 | szTextFile); |
| 63 | } |
| 64 | PAL_TerminateEx(FAIL); |
| 65 | return FAIL; |
| 66 | } |
| 67 | |
| 68 | bRc = CloseHandle(hFile); |
| 69 | if (bRc != TRUE) |
| 70 | { |
| 71 | Fail("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 72 | szTextFile); |
| 73 | } |
| 74 | |
| 75 | // open the test file |
| 76 | hFile = CreateFile(szTextFile, |
| 77 | GENERIC_READ | GENERIC_WRITE, |
| 78 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 79 | NULL, |
| 80 | OPEN_EXISTING, |
| 81 | FILE_ATTRIBUTE_NORMAL, |
| 82 | NULL); |
| 83 | |
| 84 | if(hFile == INVALID_HANDLE_VALUE) |
| 85 | { |
| 86 | Fail("SetEndOfFile: ERROR -> Unable to open file \"%s\".\n" , |
| 87 | szTextFile); |
| 88 | } |
| 89 | |
| 90 | // read a bit of the file to move the file pointer |
| 91 | dwByteCount = 10; |
| 92 | bRc = ReadFile(hFile, szBuffer, dwByteCount, &dwBytesRead, NULL); |
| 93 | if (bRc != TRUE) |
| 94 | { |
| 95 | Trace("SetEndOfFile: ERROR -> Uable to read from \"%s\".\n" , |
| 96 | szTextFile); |
| 97 | bRc = CloseHandle(hFile); |
| 98 | if (bRc != TRUE) |
| 99 | { |
| 100 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 101 | szTextFile); |
| 102 | } |
| 103 | PAL_TerminateEx(FAIL); |
| 104 | return FAIL; |
| 105 | } |
| 106 | |
| 107 | bRc = SetEndOfFile(hFile); |
| 108 | if (bRc != TRUE) |
| 109 | { |
| 110 | Trace("SetEndOfFile: ERROR -> Uable to set end of file.\n" ); |
| 111 | bRc = CloseHandle(hFile); |
| 112 | if (bRc != TRUE) |
| 113 | { |
| 114 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 115 | szTextFile); |
| 116 | } |
| 117 | PAL_TerminateEx(FAIL); |
| 118 | return FAIL; |
| 119 | } |
| 120 | |
| 121 | bRc = CloseHandle(hFile); |
| 122 | if (bRc != TRUE) |
| 123 | { |
| 124 | Fail("SetEndOfFile: ERROR -> Unable to close file \"%s\".\n" , |
| 125 | szTextFile); |
| 126 | } |
| 127 | |
| 128 | // open and read the test file |
| 129 | pFile = fopen(szTextFile, "r" ); |
| 130 | if (pFile == NULL) |
| 131 | { |
| 132 | Fail("SetEndOfFile: ERROR -> fopen was unable to open file \"%s\".\n" , |
| 133 | szTextFile); |
| 134 | } |
| 135 | |
| 136 | // since we truncated the file at 10 characters, |
| 137 | // try reading 20 just to be safe |
| 138 | memset(szBuffer, 0, 100); |
| 139 | fgets(szBuffer, 20, pFile); |
| 140 | fclose(pFile); |
| 141 | if (strlen(szBuffer) != dwByteCount) |
| 142 | { |
| 143 | Fail("SetEndOfFile: ERROR -> file apparently not truncated at " |
| 144 | "correct position.\n" ); |
| 145 | } |
| 146 | if (strncmp(szBuffer, szStringTest, dwByteCount) != 0) |
| 147 | { |
| 148 | Fail("SetEndOfFile: ERROR -> truncated file contents doesn't " |
| 149 | "compare with what should be there\n" ); |
| 150 | } |
| 151 | |
| 152 | PAL_Terminate(); |
| 153 | return PASS; |
| 154 | } |
| 155 | |