| 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 1) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the SetFilePointer function. |
| 10 | ** Set the file pointer using a NULL handle and other invalid |
| 11 | ** options. |
| 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 | char buffer[100]; |
| 30 | |
| 31 | |
| 32 | if (0 != PAL_Initialize(argc,argv)) |
| 33 | { |
| 34 | return FAIL; |
| 35 | } |
| 36 | |
| 37 | /* set the file pointer on a NULL file handle */ |
| 38 | dwRc = SetFilePointer(NULL, dwOffset, NULL, FILE_BEGIN); |
| 39 | if (dwRc != INVALID_SET_FILE_POINTER) |
| 40 | { |
| 41 | Fail("SetFilePointer: ERROR -> Call to SetFilePointer succeeded " |
| 42 | "with a NULL pointer\n" ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /* create a test file without proper permission */ |
| 47 | hFile = CreateFile(szTextFile, |
| 48 | 0, |
| 49 | 0, |
| 50 | NULL, |
| 51 | CREATE_ALWAYS, |
| 52 | FILE_ATTRIBUTE_NORMAL, |
| 53 | NULL); |
| 54 | |
| 55 | if(hFile == INVALID_HANDLE_VALUE) |
| 56 | { |
| 57 | Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n" , |
| 58 | szTextFile); |
| 59 | } |
| 60 | |
| 61 | /* ReadFile fails as expected */ |
| 62 | bRc = ReadFile(hFile, buffer, 1, &dwByteCount, NULL); |
| 63 | if (bRc != FALSE) |
| 64 | { |
| 65 | Trace("SetFilePointer: ERROR -> ReadFile was successful when it was " |
| 66 | "expected to fail\n" ); |
| 67 | if (!CloseHandle(hFile)) |
| 68 | { |
| 69 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 70 | szTextFile); |
| 71 | } |
| 72 | if (!DeleteFileA(szTextFile)) |
| 73 | { |
| 74 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 75 | szTextFile); |
| 76 | } |
| 77 | PAL_TerminateEx(FAIL); |
| 78 | return FAIL; |
| 79 | } |
| 80 | |
| 81 | /* move the file pointer before the beginning of the file */ |
| 82 | dwRc = SetFilePointer(hFile, -1, NULL, FILE_BEGIN); |
| 83 | if (dwRc != INVALID_SET_FILE_POINTER) |
| 84 | { |
| 85 | Trace("SetFilePointer: ERROR -> Was able to move the pointer before " |
| 86 | "the beginning of the file.\n" ); |
| 87 | bRc = CloseHandle(hFile); |
| 88 | if (bRc != TRUE) |
| 89 | { |
| 90 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 91 | szTextFile); |
| 92 | } |
| 93 | if (!DeleteFileA(szTextFile)) |
| 94 | { |
| 95 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 96 | szTextFile); |
| 97 | } |
| 98 | PAL_TerminateEx(FAIL); |
| 99 | return FAIL; |
| 100 | } |
| 101 | |
| 102 | bRc = CloseHandle(hFile); |
| 103 | if (bRc != TRUE) |
| 104 | { |
| 105 | Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n" , |
| 106 | szTextFile); |
| 107 | if (!DeleteFileA(szTextFile)) |
| 108 | { |
| 109 | Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 110 | szTextFile); |
| 111 | } |
| 112 | PAL_TerminateEx(FAIL); |
| 113 | return FAIL; |
| 114 | } |
| 115 | |
| 116 | if (!DeleteFileA(szTextFile)) |
| 117 | { |
| 118 | Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n" , |
| 119 | szTextFile); |
| 120 | } |
| 121 | PAL_Terminate(); |
| 122 | return PASS; |
| 123 | } |
| 124 | |