| 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: test12.c (DuplicateHandle) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the DuplicateHandle function. |
| 10 | ** This test will create handle to file (to write) and close it, |
| 11 | ** then call duplicate handle to read what was written. |
| 12 | ** |
| 13 | ** |
| 14 | **===================================================================*/ |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | int __cdecl main(int argc, char **argv) |
| 18 | { |
| 19 | HANDLE hFile; |
| 20 | HANDLE hDupFile; |
| 21 | char buf[256]; |
| 22 | char teststr[] = "A uNiQuE tEsT sTrInG" ; |
| 23 | char lpFileName[] = "testfile.txt" ; |
| 24 | DWORD dwBytesWritten; |
| 25 | DWORD dwBytesRead; |
| 26 | BOOL bRetVal; |
| 27 | |
| 28 | /*Initalize the PAL*/ |
| 29 | if ((PAL_Initialize(argc,argv)) != 0) |
| 30 | { |
| 31 | return (FAIL); |
| 32 | } |
| 33 | |
| 34 | /*Create a file handle with CreateFile*/ |
| 35 | hFile = CreateFile(lpFileName, |
| 36 | GENERIC_WRITE|GENERIC_READ, |
| 37 | FILE_SHARE_WRITE|FILE_SHARE_READ, |
| 38 | NULL, |
| 39 | OPEN_ALWAYS, |
| 40 | FILE_ATTRIBUTE_NORMAL, |
| 41 | NULL); |
| 42 | if (hFile == INVALID_HANDLE_VALUE) |
| 43 | { |
| 44 | Fail("ERROR: %u :unable to create file \"%s\".\n" , |
| 45 | GetLastError(), |
| 46 | lpFileName); |
| 47 | } |
| 48 | |
| 49 | /*Write test string to the file.*/ |
| 50 | bRetVal = WriteFile(hFile, // handle to file |
| 51 | teststr, // data buffer |
| 52 | strlen(teststr), // number of bytes to write |
| 53 | &dwBytesWritten, // number of bytes written |
| 54 | NULL); // overlapped buffer |
| 55 | |
| 56 | if (bRetVal == FALSE) |
| 57 | { |
| 58 | Trace("ERROR: %u : unable to write to file handle " |
| 59 | "hFile=0x%lx\n" , |
| 60 | GetLastError(), |
| 61 | hFile); |
| 62 | CloseHandle(hFile); |
| 63 | Fail("" ); |
| 64 | } |
| 65 | |
| 66 | /*Create a duplicate handle with DuplicateHandle.*/ |
| 67 | if (!(DuplicateHandle( |
| 68 | GetCurrentProcess(), |
| 69 | hFile, |
| 70 | GetCurrentProcess(), |
| 71 | &hDupFile, |
| 72 | GENERIC_READ|GENERIC_WRITE, |
| 73 | FALSE, |
| 74 | DUPLICATE_SAME_ACCESS))) |
| 75 | { |
| 76 | Trace("ERROR: %u : Fail to create the duplicate handle" |
| 77 | " to hFile=0x%lx\n" , |
| 78 | GetLastError(), |
| 79 | hFile); |
| 80 | CloseHandle(hFile); |
| 81 | Fail("" ); |
| 82 | } |
| 83 | |
| 84 | if( !CloseHandle(hFile) ) |
| 85 | { |
| 86 | Fail("Duplicate Handle:Unable to close original file: Error[%u]\n" , GetLastError()); |
| 87 | } |
| 88 | |
| 89 | memset(buf, 0, 256); |
| 90 | |
| 91 | /*Read from the Duplicated handle.*/ |
| 92 | bRetVal = ReadFile(hDupFile, |
| 93 | buf, |
| 94 | 256, |
| 95 | &dwBytesRead, |
| 96 | NULL); |
| 97 | if (bRetVal == FALSE) |
| 98 | { |
| 99 | Trace("ERROR: %u :unable to read from file handle " |
| 100 | "hFile=0x%lx\n" , |
| 101 | GetLastError(), |
| 102 | hDupFile); |
| 103 | CloseHandle(hDupFile); |
| 104 | Fail("" ); |
| 105 | } |
| 106 | |
| 107 | /*Compare what was written to what was read.*/ |
| 108 | if (memcmp(teststr, buf, dwBytesRead) != 0) |
| 109 | { |
| 110 | Trace("ERROR: expected %s, got %s\n" , teststr, buf); |
| 111 | CloseHandle(hDupFile); |
| 112 | Fail("" ); |
| 113 | } |
| 114 | |
| 115 | /*Close the handles*/ |
| 116 | CloseHandle(hDupFile); |
| 117 | |
| 118 | bRetVal = DeleteFileA(lpFileName); |
| 119 | if (bRetVal != TRUE) |
| 120 | { |
| 121 | Trace("Error:%u: DuplicateHandle, DeleteFileA: Couldn't delete DeleteFileA's" |
| 122 | " %s\n" , GetLastError(), lpFileName); |
| 123 | Fail("" ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | PAL_Terminate(); |
| 128 | return (PASS); |
| 129 | } |
| 130 | |