| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | using namespace System; |
| 6 | using namespace System::Security::Principal; |
| 7 | using namespace Xunit; |
| 8 | using namespace WixInternal::TestSupport; |
| 9 | using namespace WixInternal::TestSupport::XunitExtensions; |
| 10 | |
| 11 | static DWORD STDAPICALLTYPE _TestPipeClientThreadProc( |
| 12 | __in LPVOID lpThreadParameter |
| 13 | ); |
| 14 | |
| 15 | namespace DutilTests |
| 16 | { |
| 17 | public ref class PipeUtil |
| 18 | { |
| 19 | public: |
| 20 | [Fact] |
| 21 | void PipeConnectWriteAndRead() |
| 22 | { |
| 23 | HRESULT hr = S_OK; |
| 24 | HANDLE hServerPipe = INVALID_HANDLE_VALUE; |
| 25 | HANDLE hClientThread = NULL; |
| 26 | PIPE_RPC_HANDLE hRpc = { INVALID_HANDLE_VALUE }; |
| 27 | PIPE_MESSAGE msg = { }; |
| 28 | DWORD dwThread = 42; |
| 29 | DWORD dwTestMessageId = 987654; |
| 30 | |
| 31 | try |
| 32 | { |
| 33 | hr = PipeCreate(L"DutilTest", NULL, &hServerPipe); |
| 34 | NativeAssert::Succeeded(hr, "Failed to create server pipe."); |
| 35 | |
| 36 | PipeRpcInitialize(&hRpc, hServerPipe, FALSE); |
| 37 | NativeAssert::Equal((DWORD_PTR)hServerPipe, (DWORD_PTR)hRpc.hPipe); |
| 38 | |
| 39 | BOOL fInitialized = PipeRpcInitialized(&hRpc); |
| 40 | NativeAssert::True(fInitialized); |
| 41 | |
| 42 | hClientThread = ::CreateThread(NULL, 0, _TestPipeClientThreadProc, &dwTestMessageId, 0, NULL); |
| 43 | if (hClientThread == 0) |
| 44 | { |
| 45 | NativeAssert::Fail("Failed to create client thread."); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | hr = PipeServerWaitForClientConnect(hClientThread, hServerPipe); |
| 50 | NativeAssert::Succeeded(hr, "Failed to wait for client to connect to pipe."); |
| 51 | |
| 52 | hr = PipeRpcReadMessage(&hRpc, &msg); |
| 53 | NativeAssert::Succeeded(hr, "Failed to read message from client."); |
| 54 | |
| 55 | NativeAssert::Equal(dwTestMessageId, msg.dwMessageType); |
| 56 | |
| 57 | AppWaitForSingleObject(hClientThread, INFINITE); |
| 58 | |
| 59 | ::GetExitCodeThread(hClientThread, &dwThread); |
| 60 | NativeAssert::Equal((DWORD)12, dwThread); |
| 61 | } |
| 62 | finally |
| 63 | { |
| 64 | ReleasePipeMessage(&msg); |
| 65 | ReleaseHandle(hClientThread); |
| 66 | ReleasePipeHandle(hServerPipe); |
| 67 | |
| 68 | PipeRpcUninitiailize(&hRpc); |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static DWORD STDAPICALLTYPE _TestPipeClientThreadProc( |
| 76 | __in LPVOID lpThreadParameter |
| 77 | ) |
| 78 | { |
| 79 | HRESULT hr = S_OK; |
| 80 | HANDLE hClientPipe = INVALID_HANDLE_VALUE; |
| 81 | PIPE_RPC_HANDLE hRpc = { INVALID_HANDLE_VALUE }; |
| 82 | |
| 83 | hr = PipeClientConnect(L"DutilTest", &hClientPipe); |
| 84 | if (FAILED(hr)) |
| 85 | { |
| 86 | return hr; |
| 87 | } |
| 88 | |
| 89 | PipeRpcInitialize(&hRpc, hClientPipe, TRUE); |
| 90 | |
| 91 | ::Sleep(200); |
| 92 | |
| 93 | hr = PipeRpcWriteMessage(&hRpc, *(LPDWORD)lpThreadParameter, NULL, 0); |
| 94 | if (FAILED(hr)) |
| 95 | { |
| 96 | return hr; |
| 97 | } |
| 98 | |
| 99 | PipeRpcUninitiailize(&hRpc); |
| 100 | |
| 101 | return 12; |
| 102 | } |