| 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: GetStdHandle.c (test 1) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the GetStdHandle function. |
| 10 | ** |
| 11 | ** |
| 12 | **===================================================================*/ |
| 13 | |
| 14 | #include <palsuite.h> |
| 15 | |
| 16 | |
| 17 | int __cdecl main(int argc, char *argv[]) |
| 18 | { |
| 19 | HANDLE hFile = NULL; |
| 20 | DWORD dwBytesWritten = 0; |
| 21 | DWORD dwFileType; |
| 22 | BOOL bRc = FALSE; |
| 23 | const char* szText = "this is a test of GetStdHandle\n" ; |
| 24 | |
| 25 | |
| 26 | if (0 != PAL_Initialize(argc,argv)) |
| 27 | { |
| 28 | return FAIL; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * attempt to get an invalid handle |
| 33 | */ |
| 34 | hFile = GetStdHandle(-2); |
| 35 | if (hFile != INVALID_HANDLE_VALUE) |
| 36 | { |
| 37 | Fail("GetStdHandle: ERROR -> A request for the STD_INPUT_HANDLE " |
| 38 | "returned an invalid handle.\n" ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * test the STD_INPUT_HANDLE handle |
| 44 | */ |
| 45 | hFile = GetStdHandle(STD_INPUT_HANDLE); |
| 46 | if (hFile == INVALID_HANDLE_VALUE) |
| 47 | { |
| 48 | Fail("GetStdHandle: ERROR -> A request for the STD_INPUT_HANDLE " |
| 49 | "returned an invalid handle.\n" ); |
| 50 | } |
| 51 | |
| 52 | /* an attempt to write to the input handle should fail */ |
| 53 | /* I don't know how to automate a read from the input handle */ |
| 54 | bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwBytesWritten, NULL); |
| 55 | if (bRc != FALSE) |
| 56 | { |
| 57 | Fail("GetStdHandle: ERROR -> WriteFile was able to write to " |
| 58 | "STD_INPUT_HANDLE when it should have failed.\n" ); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* |
| 63 | * test the STD_OUTPUT_HANDLE handle |
| 64 | */ |
| 65 | hFile = GetStdHandle(STD_OUTPUT_HANDLE); |
| 66 | if (hFile == INVALID_HANDLE_VALUE) |
| 67 | { |
| 68 | Fail("GetStdHandle: ERROR -> A request for the STD_OUTPUT_HANDLE " |
| 69 | "returned an invalid handle.\n" ); |
| 70 | } |
| 71 | |
| 72 | /* try to write to the output handle */ |
| 73 | bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwBytesWritten, NULL); |
| 74 | if (bRc != TRUE) |
| 75 | { |
| 76 | Fail("GetStdHandle: ERROR -> WriteFile failed to write to " |
| 77 | "STD_OUTPUT_HANDLE with the error %ld\n" , |
| 78 | GetLastError()); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* test the STD_ERROR_HANDLE handle */ |
| 83 | hFile = GetStdHandle(STD_ERROR_HANDLE); |
| 84 | if (hFile == INVALID_HANDLE_VALUE) |
| 85 | { |
| 86 | Fail("GetStdHandle: ERROR -> A request for the STD_ERROR_HANDLE " |
| 87 | "returned an invalid handle.\n" ); |
| 88 | } |
| 89 | |
| 90 | /* try to write to the error handle */ |
| 91 | bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwBytesWritten, NULL); |
| 92 | if (bRc != TRUE) |
| 93 | { |
| 94 | Fail("GetStdHandle: ERROR -> WriteFile failed to write to " |
| 95 | "STD_ERROR_HANDLE with the error %ld\n" , |
| 96 | GetLastError()); |
| 97 | } |
| 98 | |
| 99 | /* check to see if we can CloseHandle works on the STD_ERROR_HANDLE */ |
| 100 | if (!CloseHandle(hFile)) |
| 101 | { |
| 102 | Fail("GetStdHandle: ERROR -> CloseHandle failed. GetLastError " |
| 103 | "returned %u.\n" , |
| 104 | GetLastError()); |
| 105 | } |
| 106 | |
| 107 | /* try to write to the closed error handle */ |
| 108 | bRc = WriteFile(hFile, |
| 109 | szText, |
| 110 | (DWORD)strlen(szText), |
| 111 | &dwBytesWritten, |
| 112 | NULL); |
| 113 | if (bRc) |
| 114 | { |
| 115 | Fail("GetStdHandle: ERROR -> WriteFile was able to write to the closed" |
| 116 | " STD_ERROR_HANDLE handle.\n" ); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | PAL_Terminate(); |
| 121 | return PASS; |
| 122 | } |
| 123 | |
| 124 | |