| 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 | HRESULT TestEngine::Initialize( |
| 6 | __in LPCWSTR wzBundleFilePath |
| 7 | ) |
| 8 | { |
| 9 | HRESULT hr = S_OK; |
| 10 | MSG msg = { }; |
| 11 | |
| 12 | LogInitialize(::GetModuleHandleW(NULL)); |
| 13 | |
| 14 | hr = LogOpen(NULL, PathFile(wzBundleFilePath), NULL, L"txt", FALSE, FALSE, NULL); |
| 15 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to open log."); |
| 16 | |
| 17 | ::PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); |
| 18 | |
| 19 | LExit: |
| 20 | return hr; |
| 21 | } |
| 22 | |
| 23 | HRESULT TestEngine::LoadBA( |
| 24 | __in LPCWSTR wzBAFilePath |
| 25 | ) |
| 26 | { |
| 27 | HRESULT hr = S_OK; |
| 28 | BOOTSTRAPPER_COMMAND command = { }; |
| 29 | |
| 30 | if (m_hBAModule) |
| 31 | { |
| 32 | ExitFunction1(hr = E_INVALIDSTATE); |
| 33 | } |
| 34 | |
| 35 | command.cbSize = sizeof(BOOTSTRAPPER_COMMAND); |
| 36 | |
| 37 | hr = PathGetDirectory(wzBAFilePath, &command.wzBootstrapperWorkingFolder); |
| 38 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperWorkingFolder"); |
| 39 | |
| 40 | hr = PathConcat(command.wzBootstrapperWorkingFolder, L"BootstrapperApplicationData.xml", &command.wzBootstrapperApplicationDataPath); |
| 41 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperApplicationDataPath"); |
| 42 | |
| 43 | #ifdef TODO_DELETE |
| 44 | m_hBAModule = ::LoadLibraryExW(wzBAFilePath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 45 | ConsoleExitOnNullWithLastError(m_hBAModule, hr, CONSOLE_COLOR_RED, "Failed to load BA dll."); |
| 46 | |
| 47 | pfnCreate = (PFN_BOOTSTRAPPER_APPLICATION_CREATE)::GetProcAddress(m_hBAModule, "BootstrapperApplicationCreate"); |
| 48 | ConsoleExitOnNull(pfnCreate, hr, E_OUTOFMEMORY, CONSOLE_COLOR_RED, "Failed to get address for BootstrapperApplicationCreate."); |
| 49 | |
| 50 | hr = pfnCreate(&args, m_pCreateResults); |
| 51 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "BA returned failure on BootstrapperApplicationCreate."); |
| 52 | #endif |
| 53 | |
| 54 | LExit: |
| 55 | ReleaseStr(command.wzBootstrapperApplicationDataPath); |
| 56 | ReleaseStr(command.wzBootstrapperWorkingFolder); |
| 57 | |
| 58 | return hr; |
| 59 | } |
| 60 | |
| 61 | HRESULT TestEngine::Log( |
| 62 | __in BOOTSTRAPPER_LOG_LEVEL level, |
| 63 | __in LPCWSTR wzMessage |
| 64 | ) |
| 65 | { |
| 66 | switch (level) |
| 67 | { |
| 68 | case BOOTSTRAPPER_LOG_LEVEL_NONE: |
| 69 | case BOOTSTRAPPER_LOG_LEVEL_DEBUG: |
| 70 | return S_OK; |
| 71 | default: |
| 72 | LogStringLine(REPORT_STANDARD, "%ls", wzMessage); |
| 73 | return ConsoleWriteLine(CONSOLE_COLOR_NORMAL, "%ls", wzMessage); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | HRESULT TestEngine::RunApplication() |
| 78 | { |
| 79 | HRESULT hr = S_OK; |
| 80 | MSG msg = { }; |
| 81 | BOOL fRet = FALSE; |
| 82 | |
| 83 | // Enter the message pump. |
| 84 | while (0 != (fRet = ::GetMessageW(&msg, NULL, 0, 0))) |
| 85 | { |
| 86 | if (-1 == fRet) |
| 87 | { |
| 88 | ConsoleExitOnFailure(hr = E_UNEXPECTED, CONSOLE_COLOR_RED, "Unexpected return value from message pump."); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | ProcessBAMessage(&msg); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | LExit: |
| 97 | return hr; |
| 98 | } |
| 99 | |
| 100 | HRESULT TestEngine::SendShutdownEvent( |
| 101 | __in BOOTSTRAPPER_SHUTDOWN_ACTION defaultAction |
| 102 | ) |
| 103 | { |
| 104 | HRESULT hr = S_OK; |
| 105 | BA_ONSHUTDOWN_ARGS shutdownArgs = { }; |
| 106 | BA_ONSHUTDOWN_RESULTS shutdownResults = { }; |
| 107 | shutdownArgs.cbSize = sizeof(BA_ONSHUTDOWN_ARGS); |
| 108 | shutdownResults.action = defaultAction; |
| 109 | shutdownResults.cbSize = sizeof(BA_ONSHUTDOWN_RESULTS); |
| 110 | // hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSHUTDOWN, &shutdownArgs, &shutdownResults, m_pCreateResults->pvBootstrapperApplicationProcContext); |
| 111 | return hr; |
| 112 | } |
| 113 | |
| 114 | HRESULT TestEngine::SendStartupEvent() |
| 115 | { |
| 116 | HRESULT hr = S_OK; |
| 117 | BA_ONSTARTUP_ARGS startupArgs = { }; |
| 118 | BA_ONSTARTUP_RESULTS startupResults = { }; |
| 119 | startupArgs.cbSize = sizeof(BA_ONSTARTUP_ARGS); |
| 120 | startupResults.cbSize = sizeof(BA_ONSTARTUP_RESULTS); |
| 121 | // hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSTARTUP, &startupArgs, &startupResults, m_pCreateResults->pvBootstrapperApplicationProcContext); |
| 122 | return hr; |
| 123 | } |
| 124 | |
| 125 | HRESULT TestEngine::SimulateQuit( |
| 126 | __in DWORD dwExitCode |
| 127 | ) |
| 128 | { |
| 129 | BAENGINE_QUIT_ARGS args = { }; |
| 130 | BAENGINE_QUIT_RESULTS results = { }; |
| 131 | |
| 132 | args.cbSize = sizeof(BAENGINE_QUIT_ARGS); |
| 133 | args.dwExitCode = dwExitCode; |
| 134 | |
| 135 | results.cbSize = sizeof(BAENGINE_QUIT_RESULTS); |
| 136 | |
| 137 | return BAEngineQuit(&args, &results); |
| 138 | } |
| 139 | |
| 140 | void TestEngine::UnloadBA( |
| 141 | __in BOOL fReload |
| 142 | ) |
| 143 | { |
| 144 | #ifdef TODO_DELETE |
| 145 | PFN_BOOTSTRAPPER_APPLICATION_DESTROY pfnDestroy = NULL; |
| 146 | BOOTSTRAPPER_DESTROY_ARGS args = { }; |
| 147 | BOOTSTRAPPER_DESTROY_RESULTS results = { }; |
| 148 | |
| 149 | args.cbSize = sizeof(args); |
| 150 | args.fReload = fReload; |
| 151 | |
| 152 | results.cbSize = sizeof(results); |
| 153 | |
| 154 | ReleaseNullMem(m_pCreateResults); |
| 155 | |
| 156 | pfnDestroy = (PFN_BOOTSTRAPPER_APPLICATION_DESTROY)::GetProcAddress(m_hBAModule, "BootstrapperApplicationDestroy"); |
| 157 | |
| 158 | if (pfnDestroy) |
| 159 | { |
| 160 | pfnDestroy(&args, &results); |
| 161 | } |
| 162 | |
| 163 | if (m_hBAModule) |
| 164 | { |
| 165 | if (!results.fDisableUnloading) |
| 166 | { |
| 167 | ::FreeLibrary(m_hBAModule); |
| 168 | } |
| 169 | |
| 170 | m_hBAModule = NULL; |
| 171 | } |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | HRESULT TestEngine::BAEngineLog( |
| 176 | __in BAENGINE_LOG_ARGS* pArgs, |
| 177 | __in BAENGINE_LOG_RESULTS* /*pResults*/ |
| 178 | ) |
| 179 | { |
| 180 | return Log(pArgs->level, pArgs->wzMessage); |
| 181 | } |
| 182 | |
| 183 | HRESULT TestEngine::BAEngineQuit( |
| 184 | __in BAENGINE_QUIT_ARGS* pArgs, |
| 185 | __in BAENGINE_QUIT_RESULTS* /*pResults*/ |
| 186 | ) |
| 187 | { |
| 188 | HRESULT hr = S_OK; |
| 189 | |
| 190 | if (!::PostThreadMessageW(m_dwThreadId, WM_TESTENG_QUIT, static_cast<WPARAM>(pArgs->dwExitCode), 0)) |
| 191 | { |
| 192 | ConsoleExitWithLastError(hr, CONSOLE_COLOR_RED, "Failed to post shutdown message."); |
| 193 | } |
| 194 | |
| 195 | LExit: |
| 196 | return hr; |
| 197 | } |
| 198 | |
| 199 | HRESULT WINAPI TestEngine::EngineProc( |
| 200 | __in BOOTSTRAPPER_ENGINE_MESSAGE message, |
| 201 | __in const LPVOID pvArgs, |
| 202 | __inout LPVOID pvResults, |
| 203 | __in_opt LPVOID pvContext |
| 204 | ) |
| 205 | { |
| 206 | HRESULT hr = S_OK; |
| 207 | TestEngine* pContext = (TestEngine*)pvContext; |
| 208 | |
| 209 | if (!pContext || !pvArgs || !pvResults) |
| 210 | { |
| 211 | ExitFunction1(hr = E_INVALIDARG); |
| 212 | } |
| 213 | |
| 214 | switch (message) |
| 215 | { |
| 216 | case BOOTSTRAPPER_ENGINE_MESSAGE_LOG: |
| 217 | hr = pContext->BAEngineLog(reinterpret_cast<BAENGINE_LOG_ARGS*>(pvArgs), reinterpret_cast<BAENGINE_LOG_RESULTS*>(pvResults)); |
| 218 | break; |
| 219 | case BOOTSTRAPPER_ENGINE_MESSAGE_QUIT: |
| 220 | hr = pContext->BAEngineQuit(reinterpret_cast<BAENGINE_QUIT_ARGS*>(pvArgs), reinterpret_cast<BAENGINE_QUIT_RESULTS*>(pvResults)); |
| 221 | default: |
| 222 | hr = E_NOTIMPL; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | LExit: |
| 227 | return hr; |
| 228 | } |
| 229 | |
| 230 | HRESULT TestEngine::ProcessBAMessage( |
| 231 | __in const MSG* pmsg |
| 232 | ) |
| 233 | { |
| 234 | HRESULT hr = S_OK; |
| 235 | |
| 236 | switch (pmsg->message) |
| 237 | { |
| 238 | case WM_TESTENG_QUIT: |
| 239 | ::PostQuitMessage(static_cast<int>(pmsg->wParam)); // go bye-bye. |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | return hr; |
| 244 | } |
| 245 | |
| 246 | TestEngine::TestEngine() |
| 247 | { |
| 248 | m_hBAModule = NULL; |
| 249 | m_dwThreadId = ::GetCurrentThreadId(); |
| 250 | } |
| 251 | |
| 252 | TestEngine::~TestEngine() |
| 253 | { |
| 254 | } |