aboutsummaryrefslogtreecommitdiff
path: root/src/test/examples/TestEngine/TestEngine.cpp
blob: f0811e0abd19e17a0cf920aaf76e3dfc5941a2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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::Initialize(
    __in LPCWSTR wzBundleFilePath
    )
{
    HRESULT hr = S_OK;

    LogInitialize(::GetModuleHandleW(NULL));

    hr = LogOpen(NULL, PathFile(wzBundleFilePath), NULL, L"txt", FALSE, FALSE, NULL);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to open log.");

LExit:
    return hr;
}

HRESULT TestEngine::LoadBA(
    __in LPCWSTR wzBAFilePath
    )
{
    HRESULT hr = S_OK;
    BOOTSTRAPPER_COMMAND command = { };
    BOOTSTRAPPER_CREATE_ARGS args = { };
    PFN_BOOTSTRAPPER_APPLICATION_CREATE pfnCreate = NULL;

    if (m_pCreateResults || m_hBAModule)
    {
        ExitFunction1(hr = E_INVALIDSTATE);
    }

    m_pCreateResults = static_cast<BOOTSTRAPPER_CREATE_RESULTS*>(MemAlloc(sizeof(BOOTSTRAPPER_CREATE_RESULTS), TRUE));

    command.cbSize = sizeof(BOOTSTRAPPER_COMMAND);

    hr = PathGetDirectory(wzBAFilePath, &command.wzBootstrapperWorkingFolder);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperWorkingFolder");

    hr = PathConcat(command.wzBootstrapperWorkingFolder, L"BootstrapperApplicationData.xml", &command.wzBootstrapperApplicationDataPath);
    ConsoleExitOnFailure(hr, CONSOLE_COLOR_RED, "Failed to allocate wzBootstrapperApplicationDataPath");

    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);

    m_hBAModule = ::LoadLibraryExW(wzBAFilePath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
    ExitOnNullWithLastError(m_hBAModule, hr, "Failed to load BA dll.");

    pfnCreate = (PFN_BOOTSTRAPPER_APPLICATION_CREATE)::GetProcAddress(m_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:
    ReleaseStr(command.wzBootstrapperApplicationDataPath);
    ReleaseStr(command.wzBootstrapperWorkingFolder);

    return hr;
}

HRESULT TestEngine::Log(
    __in LPCWSTR wzMessage
    )
{
    LogStringLine(REPORT_STANDARD, "%ls", 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::SendStartupEvent()
{
    HRESULT hr = S_OK;
    BA_ONSTARTUP_ARGS startupArgs = { };
    BA_ONSTARTUP_RESULTS startupResults = { };
    startupArgs.cbSize = sizeof(BA_ONSTARTUP_ARGS);
    startupResults.cbSize = sizeof(BA_ONSTARTUP_RESULTS);
    hr = m_pCreateResults->pfnBootstrapperApplicationProc(BOOTSTRAPPER_APPLICATION_MESSAGE_ONSTARTUP, &startupArgs, &startupResults, m_pCreateResults->pvBootstrapperApplicationProcContext);
    return hr;
}

void TestEngine::UnloadBA()
{
    PFN_BOOTSTRAPPER_APPLICATION_DESTROY pfnDestroy = NULL;
    BOOL fDisableUnloading = m_pCreateResults && m_pCreateResults->fDisableUnloading;

    ReleaseNullMem(m_pCreateResults);

    pfnDestroy = (PFN_BOOTSTRAPPER_APPLICATION_DESTROY)::GetProcAddress(m_hBAModule, "BootstrapperApplicationDestroy");

    if (pfnDestroy)
    {
        pfnDestroy();
    }

    if (m_hBAModule)
    {
        if (!fDisableUnloading)
        {
            ::FreeLibrary(m_hBAModule);
        }

        m_hBAModule = NULL;
    }
}

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<BAENGINE_LOG_ARGS*>(pvArgs), reinterpret_cast<BAENGINE_LOG_RESULTS*>(pvResults));
        break;
    default:
        hr = E_NOTIMPL;
        break;
    }

LExit:
    return hr;
}

TestEngine::TestEngine()
{
    m_hBAModule = NULL;
    m_pCreateResults = NULL;
}

TestEngine::~TestEngine()
{
    ReleaseMem(m_pCreateResults);
}