From 4a176b759c47fa1970fcfd0d9e25c294bda82ef4 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Sun, 22 Dec 2019 13:51:35 +1100 Subject: Update the MbaHost test project to use an external exe to load the BA, which allows loading a different .NET than the one running the tests. This also allows writing the tests in C# instead of C++/CLI. --- .../examples/TestEngine/Example.TestEngine.vcxproj | 70 ++++++++++++ src/test/examples/TestEngine/ExampleTestEngine.cpp | 22 ++++ src/test/examples/TestEngine/ShutdownEngine.cpp | 23 ++++ src/test/examples/TestEngine/TestEngine.cpp | 117 +++++++++++++++++++++ src/test/examples/TestEngine/TestEngine.h | 42 ++++++++ src/test/examples/TestEngine/packages.config | 7 ++ src/test/examples/TestEngine/precomp.cpp | 3 + src/test/examples/TestEngine/precomp.h | 19 ++++ 8 files changed, 303 insertions(+) create mode 100644 src/test/examples/TestEngine/Example.TestEngine.vcxproj create mode 100644 src/test/examples/TestEngine/ExampleTestEngine.cpp create mode 100644 src/test/examples/TestEngine/ShutdownEngine.cpp create mode 100644 src/test/examples/TestEngine/TestEngine.cpp create mode 100644 src/test/examples/TestEngine/TestEngine.h create mode 100644 src/test/examples/TestEngine/packages.config create mode 100644 src/test/examples/TestEngine/precomp.cpp create mode 100644 src/test/examples/TestEngine/precomp.h (limited to 'src/test/examples/TestEngine') diff --git a/src/test/examples/TestEngine/Example.TestEngine.vcxproj b/src/test/examples/TestEngine/Example.TestEngine.vcxproj new file mode 100644 index 00000000..ab79dacc --- /dev/null +++ b/src/test/examples/TestEngine/Example.TestEngine.vcxproj @@ -0,0 +1,70 @@ + + + + + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {3D44B67D-A475-49BA-8310-E39F6C117CC9} + Application + Console + Example.TestEngine + v141 + Unicode + $([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0')) + + + + + + + + + + + + + + + Create + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/src/test/examples/TestEngine/ExampleTestEngine.cpp b/src/test/examples/TestEngine/ExampleTestEngine.cpp new file mode 100644 index 00000000..9f051875 --- /dev/null +++ b/src/test/examples/TestEngine/ExampleTestEngine.cpp @@ -0,0 +1,22 @@ +// 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" + +int __cdecl wmain(int argc, LPWSTR argv[]) +{ + HRESULT hr = E_INVALIDARG; + + ConsoleInitialize(); + + if (argc != 2) + { + ConsoleWriteError(hr, CONSOLE_COLOR_RED, "Usage: Example.TestEngine.exe BA.dll"); + } + else + { + hr = RunShutdownEngine(argv[1]); + } + + ConsoleUninitialize(); + return HRESULT_CODE(hr); +} diff --git a/src/test/examples/TestEngine/ShutdownEngine.cpp b/src/test/examples/TestEngine/ShutdownEngine.cpp new file mode 100644 index 00000000..69321d91 --- /dev/null +++ b/src/test/examples/TestEngine/ShutdownEngine.cpp @@ -0,0 +1,23 @@ +// 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" + +HRESULT RunShutdownEngine( + __in LPCWSTR wzBAFilePath + ) +{ + HRESULT hr = S_OK; + TestEngine* pTestEngine = NULL; + + pTestEngine = new TestEngine(); + ConsoleExitOnNull(pTestEngine, hr, E_OUTOFMEMORY, CONSOLE_COLOR_RED, "Failed to create new test engine."); + + hr = pTestEngine->LoadBA(wzBAFilePath); + ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to load BA."); + + hr = pTestEngine->SendShutdownEvent(BOOTSTRAPPER_SHUTDOWN_ACTION_RELOAD_BOOTSTRAPPER); + ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "BA returned failure for OnShutdown."); + +LExit: + return hr; +} diff --git a/src/test/examples/TestEngine/TestEngine.cpp b/src/test/examples/TestEngine/TestEngine.cpp new file mode 100644 index 00000000..c0a62eda --- /dev/null +++ b/src/test/examples/TestEngine/TestEngine.cpp @@ -0,0 +1,117 @@ +// 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" + +HRESULT TestEngine::LoadBA( + __in LPCWSTR wzBAFilePath + ) +{ + HRESULT hr = S_OK; + BOOTSTRAPPER_COMMAND command = { }; + BOOTSTRAPPER_CREATE_ARGS args = { }; + HMODULE hBAModule = NULL; + PFN_BOOTSTRAPPER_APPLICATION_CREATE pfnCreate = NULL; + + if (m_pCreateResults) + { + ExitFunction1(hr = E_INVALIDSTATE); + } + + LogInitialize(::GetModuleHandleW(NULL)); + + hr = LogOpen(NULL, L"ExampleTestEngine", NULL, L"txt", FALSE, FALSE, NULL); + ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to open log."); + + m_pCreateResults = static_cast(MemAlloc(sizeof(BOOTSTRAPPER_CREATE_RESULTS), TRUE)); + + command.cbSize = sizeof(BOOTSTRAPPER_COMMAND); + + args.cbSize = sizeof(BOOTSTRAPPER_CREATE_ARGS); + args.pCommand = &command; + args.pfnBootstrapperEngineProc = TestEngine::EngineProc; + args.pvBootstrapperEngineProcContext = this; + args.qwEngineAPIVersion = MAKEQWORDVERSION(0, 0, 0, 1); + + m_pCreateResults->cbSize = sizeof(BOOTSTRAPPER_CREATE_RESULTS); + + hBAModule = ::LoadLibraryExW(wzBAFilePath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + ExitOnNullWithLastError(hBAModule, hr, "Failed to load BA dll."); + + pfnCreate = (PFN_BOOTSTRAPPER_APPLICATION_CREATE)::GetProcAddress(hBAModule, "BootstrapperApplicationCreate"); + ConsoleExitOnNull(pfnCreate, hr, E_OUTOFMEMORY, CONSOLE_COLOR_RED, "Failed to get address for BootstrapperApplicationCreate."); + + hr = pfnCreate(&args, m_pCreateResults); + ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "BA returned failure on BootstrapperApplicationCreate."); + +LExit: + return hr; +} + +HRESULT TestEngine::Log( + __in LPCWSTR wzMessage + ) +{ + return ConsoleWriteLine(CONSOLE_COLOR_NORMAL, "%ls", wzMessage); +} + +HRESULT TestEngine::SendShutdownEvent( + __in BOOTSTRAPPER_SHUTDOWN_ACTION defaultAction + ) +{ + HRESULT hr = S_OK; + BA_ONSHUTDOWN_ARGS shutdownArgs = { }; + BA_ONSHUTDOWN_RESULTS shutdownResults = { }; + shutdownArgs.cbSize = sizeof(BA_ONSHUTDOWN_ARGS); + shutdownResults.action = defaultAction; + shutdownResults.cbSize = sizeof(BA_ONSHUTDOWN_RESULTS); + hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSHUTDOWN, &shutdownArgs, &shutdownResults, m_pCreateResults->pvBootstrapperApplicationProcContext); + return hr; +} + +HRESULT TestEngine::BAEngineLog( + __in TestEngine* pContext, + __in BAENGINE_LOG_ARGS* pArgs, + __in BAENGINE_LOG_RESULTS* /*pResults*/ + ) +{ + return pContext->Log(pArgs->wzMessage); +} + +HRESULT WINAPI TestEngine::EngineProc( + __in BOOTSTRAPPER_ENGINE_MESSAGE message, + __in const LPVOID pvArgs, + __inout LPVOID pvResults, + __in_opt LPVOID pvContext + ) +{ + HRESULT hr = S_OK; + TestEngine* pContext = (TestEngine*)pvContext; + + if (!pContext || !pvArgs || !pvResults) + { + ExitFunction1(hr = E_INVALIDARG); + } + + switch (message) + { + case BOOTSTRAPPER_ENGINE_MESSAGE_LOG: + hr = BAEngineLog(pContext, reinterpret_cast(pvArgs), reinterpret_cast(pvResults)); + break; + default: + hr = E_NOTIMPL; + break; + } + +LExit: + return hr; +} + +TestEngine::TestEngine() +{ + m_pCreateResults = NULL; +} + +TestEngine::~TestEngine() +{ + ReleaseMem(m_pCreateResults); +} \ No newline at end of file diff --git a/src/test/examples/TestEngine/TestEngine.h b/src/test/examples/TestEngine/TestEngine.h new file mode 100644 index 00000000..52872100 --- /dev/null +++ b/src/test/examples/TestEngine/TestEngine.h @@ -0,0 +1,42 @@ +#pragma once +// 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" + +class TestEngine +{ +public: + HRESULT LoadBA( + __in LPCWSTR wzBAFilePath + ); + + HRESULT Log( + __in LPCWSTR wzMessage + ); + + HRESULT SendShutdownEvent( + __in BOOTSTRAPPER_SHUTDOWN_ACTION defaultAction + ); + +private: + static HRESULT BAEngineLog( + __in TestEngine* pContext, + __in BAENGINE_LOG_ARGS* pArgs, + __in BAENGINE_LOG_RESULTS* /*pResults*/ + ); + + static HRESULT WINAPI EngineProc( + __in BOOTSTRAPPER_ENGINE_MESSAGE message, + __in const LPVOID pvArgs, + __inout LPVOID pvResults, + __in_opt LPVOID pvContext + ); + +public: + TestEngine(); + + ~TestEngine(); + +private: + BOOTSTRAPPER_CREATE_RESULTS* m_pCreateResults; +}; \ No newline at end of file diff --git a/src/test/examples/TestEngine/packages.config b/src/test/examples/TestEngine/packages.config new file mode 100644 index 00000000..f209d5fb --- /dev/null +++ b/src/test/examples/TestEngine/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/test/examples/TestEngine/precomp.cpp b/src/test/examples/TestEngine/precomp.cpp new file mode 100644 index 00000000..37664a1c --- /dev/null +++ b/src/test/examples/TestEngine/precomp.cpp @@ -0,0 +1,3 @@ +// 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" diff --git a/src/test/examples/TestEngine/precomp.h b/src/test/examples/TestEngine/precomp.h new file mode 100644 index 00000000..6e867e89 --- /dev/null +++ b/src/test/examples/TestEngine/precomp.h @@ -0,0 +1,19 @@ +#pragma once +// 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 +#include + +#include "dutil.h" +#include "conutil.h" +#include "logutil.h" +#include "memutil.h" + +#include "BootstrapperEngine.h" +#include "BootstrapperApplication.h" + +#include "TestEngine.h" + +HRESULT RunShutdownEngine( + __in LPCWSTR wzBAFilePath + ); -- cgit v1.2.3-55-g6feb