From 4e7b7c0059d76498d1c24f348dbf6d5799203fe0 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 10 Jan 2024 23:53:18 -0800 Subject: Add pipeutil to dutil Plus a couple small clean-ups in a couple of dutil files. --- src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp (limited to 'src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp') diff --git a/src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp b/src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp new file mode 100644 index 00000000..87a5e8f2 --- /dev/null +++ b/src/libs/dutil/test/DUtilUnitTest/PipeUtilTest.cpp @@ -0,0 +1,89 @@ +// 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. + +#include "precomp.h" + +using namespace System; +using namespace System::Security::Principal; +using namespace Xunit; +using namespace WixInternal::TestSupport; +using namespace WixInternal::TestSupport::XunitExtensions; + +static DWORD STDAPICALLTYPE _TestPipeClientThreadProc( + __in LPVOID lpThreadParameter +); + +namespace DutilTests +{ + public ref class PipeUtil + { + public: + [Fact] + void PipeConnectWriteAndRead() + { + HRESULT hr = S_OK; + HANDLE hServerPipe = INVALID_HANDLE_VALUE; + HANDLE hClientThread = NULL; + PIPE_MESSAGE msg = { }; + DWORD dwThread = 42; + DWORD dwTestMessageId = 987654; + + try + { + hr = PipeCreate(L"DutilTest", NULL, &hServerPipe); + NativeAssert::Succeeded(hr, "Failed to create server pipe."); + + hClientThread = ::CreateThread(NULL, 0, _TestPipeClientThreadProc, &dwTestMessageId, 0, NULL); + if (hClientThread == 0) + { + NativeAssert::Fail("Failed to create client thread."); + return; + } + + hr = PipeServerWaitForClientConnect(hServerPipe); + NativeAssert::Succeeded(hr, "Failed to wait for client to connect to pipe."); + + hr = PipeReadMessage(hServerPipe, &msg); + NativeAssert::Succeeded(hr, "Failed to read message from client."); + + NativeAssert::Equal(dwTestMessageId, msg.dwMessageType); + + AppWaitForSingleObject(hClientThread, INFINITE); + + ::GetExitCodeThread(hClientThread, &dwThread); + NativeAssert::Equal((DWORD)12, dwThread); + } + finally + { + ReleasePipeMessage(&msg); + ReleaseHandle(hClientThread); + ReleasePipeHandle(hServerPipe); + } + } + }; +} + + +static DWORD STDAPICALLTYPE _TestPipeClientThreadProc( + __in LPVOID lpThreadParameter +) +{ + HRESULT hr = S_OK; + HANDLE hClientPipe = INVALID_HANDLE_VALUE; + + hr = PipeClientConnect(L"DutilTest", &hClientPipe); + if (FAILED(hr)) + { + return hr; + } + + ::Sleep(200); + + hr = PipeWriteMessage(hClientPipe, *(LPDWORD)lpThreadParameter, NULL, 0); + if (FAILED(hr)) + { + return hr; + } + + ReleasePipeHandle(hClientPipe); + return 12; +} -- cgit v1.2.3-55-g6feb