diff options
Diffstat (limited to 'src/ext/Bal/test/examples/TestEngine/TestEngine.cpp')
-rw-r--r-- | src/ext/Bal/test/examples/TestEngine/TestEngine.cpp | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/src/ext/Bal/test/examples/TestEngine/TestEngine.cpp b/src/ext/Bal/test/examples/TestEngine/TestEngine.cpp new file mode 100644 index 00000000..4c7ec1c3 --- /dev/null +++ b/src/ext/Bal/test/examples/TestEngine/TestEngine.cpp | |||
@@ -0,0 +1,256 @@ | |||
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 | BOOTSTRAPPER_CREATE_ARGS args = { }; | ||
30 | PFN_BOOTSTRAPPER_APPLICATION_CREATE pfnCreate = NULL; | ||
31 | |||
32 | if (m_pCreateResults || m_hBAModule) | ||
33 | { | ||
34 | ExitFunction1(hr = E_INVALIDSTATE); | ||
35 | } | ||
36 | |||
37 | m_pCreateResults = static_cast<BOOTSTRAPPER_CREATE_RESULTS*>(MemAlloc(sizeof(BOOTSTRAPPER_CREATE_RESULTS), TRUE)); | ||
38 | |||
39 | command.cbSize = sizeof(BOOTSTRAPPER_COMMAND); | ||
40 | |||
41 | hr = PathGetDirectory(wzBAFilePath, &command.wzBootstrapperWorkingFolder); | ||
42 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperWorkingFolder"); | ||
43 | |||
44 | hr = PathConcat(command.wzBootstrapperWorkingFolder, L"BootstrapperApplicationData.xml", &command.wzBootstrapperApplicationDataPath); | ||
45 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperApplicationDataPath"); | ||
46 | |||
47 | args.cbSize = sizeof(BOOTSTRAPPER_CREATE_ARGS); | ||
48 | args.pCommand = &command; | ||
49 | args.pfnBootstrapperEngineProc = TestEngine::EngineProc; | ||
50 | args.pvBootstrapperEngineProcContext = this; | ||
51 | args.qwEngineAPIVersion = MAKEQWORDVERSION(0, 0, 0, 1); | ||
52 | |||
53 | m_pCreateResults->cbSize = sizeof(BOOTSTRAPPER_CREATE_RESULTS); | ||
54 | |||
55 | m_hBAModule = ::LoadLibraryExW(wzBAFilePath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | ||
56 | ConsoleExitOnNullWithLastError(m_hBAModule, hr, CONSOLE_COLOR_RED, "Failed to load BA dll."); | ||
57 | |||
58 | pfnCreate = (PFN_BOOTSTRAPPER_APPLICATION_CREATE)::GetProcAddress(m_hBAModule, "BootstrapperApplicationCreate"); | ||
59 | ConsoleExitOnNull(pfnCreate, hr, E_OUTOFMEMORY, CONSOLE_COLOR_RED, "Failed to get address for BootstrapperApplicationCreate."); | ||
60 | |||
61 | hr = pfnCreate(&args, m_pCreateResults); | ||
62 | ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "BA returned failure on BootstrapperApplicationCreate."); | ||
63 | |||
64 | LExit: | ||
65 | ReleaseStr(command.wzBootstrapperApplicationDataPath); | ||
66 | ReleaseStr(command.wzBootstrapperWorkingFolder); | ||
67 | |||
68 | return hr; | ||
69 | } | ||
70 | |||
71 | HRESULT TestEngine::Log( | ||
72 | __in BOOTSTRAPPER_LOG_LEVEL level, | ||
73 | __in LPCWSTR wzMessage | ||
74 | ) | ||
75 | { | ||
76 | switch (level) | ||
77 | { | ||
78 | case BOOTSTRAPPER_LOG_LEVEL_NONE: | ||
79 | case BOOTSTRAPPER_LOG_LEVEL_DEBUG: | ||
80 | return S_OK; | ||
81 | default: | ||
82 | LogStringLine(REPORT_STANDARD, "%ls", wzMessage); | ||
83 | return ConsoleWriteLine(CONSOLE_COLOR_NORMAL, "%ls", wzMessage); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | HRESULT TestEngine::RunApplication() | ||
88 | { | ||
89 | HRESULT hr = S_OK; | ||
90 | MSG msg = { }; | ||
91 | BOOL fRet = FALSE; | ||
92 | |||
93 | // Enter the message pump. | ||
94 | while (0 != (fRet = ::GetMessageW(&msg, NULL, 0, 0))) | ||
95 | { | ||
96 | if (-1 == fRet) | ||
97 | { | ||
98 | ConsoleExitOnFailure(hr = E_UNEXPECTED, CONSOLE_COLOR_RED, "Unexpected return value from message pump."); | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | ProcessBAMessage(&msg); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | LExit: | ||
107 | return hr; | ||
108 | } | ||
109 | |||
110 | HRESULT TestEngine::SendShutdownEvent( | ||
111 | __in BOOTSTRAPPER_SHUTDOWN_ACTION defaultAction | ||
112 | ) | ||
113 | { | ||
114 | HRESULT hr = S_OK; | ||
115 | BA_ONSHUTDOWN_ARGS shutdownArgs = { }; | ||
116 | BA_ONSHUTDOWN_RESULTS shutdownResults = { }; | ||
117 | shutdownArgs.cbSize = sizeof(BA_ONSHUTDOWN_ARGS); | ||
118 | shutdownResults.action = defaultAction; | ||
119 | shutdownResults.cbSize = sizeof(BA_ONSHUTDOWN_RESULTS); | ||
120 | hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSHUTDOWN, &shutdownArgs, &shutdownResults, m_pCreateResults->pvBootstrapperApplicationProcContext); | ||
121 | return hr; | ||
122 | } | ||
123 | |||
124 | HRESULT TestEngine::SendStartupEvent() | ||
125 | { | ||
126 | HRESULT hr = S_OK; | ||
127 | BA_ONSTARTUP_ARGS startupArgs = { }; | ||
128 | BA_ONSTARTUP_RESULTS startupResults = { }; | ||
129 | startupArgs.cbSize = sizeof(BA_ONSTARTUP_ARGS); | ||
130 | startupResults.cbSize = sizeof(BA_ONSTARTUP_RESULTS); | ||
131 | hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSTARTUP, &startupArgs, &startupResults, m_pCreateResults->pvBootstrapperApplicationProcContext); | ||
132 | return hr; | ||
133 | } | ||
134 | |||
135 | HRESULT TestEngine::SimulateQuit( | ||
136 | __in DWORD dwExitCode | ||
137 | ) | ||
138 | { | ||
139 | BAENGINE_QUIT_ARGS args = { }; | ||
140 | BAENGINE_QUIT_RESULTS results = { }; | ||
141 | |||
142 | args.cbSize = sizeof(BAENGINE_QUIT_ARGS); | ||
143 | args.dwExitCode = dwExitCode; | ||
144 | |||
145 | results.cbSize = sizeof(BAENGINE_QUIT_RESULTS); | ||
146 | |||
147 | return BAEngineQuit(&args, &results); | ||
148 | } | ||
149 | |||
150 | void TestEngine::UnloadBA() | ||
151 | { | ||
152 | PFN_BOOTSTRAPPER_APPLICATION_DESTROY pfnDestroy = NULL; | ||
153 | BOOL fDisableUnloading = m_pCreateResults && m_pCreateResults->fDisableUnloading; | ||
154 | |||
155 | ReleaseNullMem(m_pCreateResults); | ||
156 | |||
157 | pfnDestroy = (PFN_BOOTSTRAPPER_APPLICATION_DESTROY)::GetProcAddress(m_hBAModule, "BootstrapperApplicationDestroy"); | ||
158 | |||
159 | if (pfnDestroy) | ||
160 | { | ||
161 | pfnDestroy(); | ||
162 | } | ||
163 | |||
164 | if (m_hBAModule) | ||
165 | { | ||
166 | if (!fDisableUnloading) | ||
167 | { | ||
168 | ::FreeLibrary(m_hBAModule); | ||
169 | } | ||
170 | |||
171 | m_hBAModule = NULL; | ||
172 | } | ||
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_pCreateResults = NULL; | ||
250 | m_dwThreadId = ::GetCurrentThreadId(); | ||
251 | } | ||
252 | |||
253 | TestEngine::~TestEngine() | ||
254 | { | ||
255 | ReleaseMem(m_pCreateResults); | ||
256 | } | ||