aboutsummaryrefslogtreecommitdiff
path: root/src/burn/test/BurnUnitTest/ExitCodeTest.cpp
blob: c742543ba096687f545d8053de6c5a0f960c073a (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// 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"

namespace Microsoft
{
namespace Tools
{
namespace WindowsInstallerXml
{
namespace Test
{
namespace Bootstrapper
{
    using namespace System;
    using namespace System::IO;
    using namespace System::Threading;
    using namespace Xunit;

struct EXIT_CODE_ITEM
{
    DWORD dwExitCode;
    HRESULT hrExpectedResult;
    BOOTSTRAPPER_APPLY_RESTART expectedRestart;
    LPCWSTR wzPackageId;
    HRESULT hrResultPerUser;
    BOOTSTRAPPER_APPLY_RESTART restartPerUser;
    HRESULT hrResultPerMachine;
    BOOTSTRAPPER_APPLY_RESTART restartPerMachine;
};

static BOOL STDAPICALLTYPE ExitCodeTest_ShellExecuteExW(
    __inout LPSHELLEXECUTEINFOW lpExecInfo
    );
static DWORD CALLBACK ExitCodeTest_ElevationThreadProc(
    __in LPVOID lpThreadParameter
    );
static BOOL STDAPICALLTYPE ExitCodeTest_CreateProcessW(
    __in_opt LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in BOOL bInheritHandles,
    __in DWORD dwCreationFlags,
    __in_opt LPVOID lpEnvironment,
    __in_opt LPCWSTR lpCurrentDirectory,
    __in LPSTARTUPINFOW lpStartupInfo,
    __out LPPROCESS_INFORMATION lpProcessInformation
    );
static DWORD CALLBACK ExitCodeTest_PackageThreadProc(
    __in LPVOID lpThreadParameter
    );
static int ExitCodeTest_GenericMessageHandler(
    __in GENERIC_EXECUTE_MESSAGE* pMessage,
    __in LPVOID pvContext
    );
static void LoadEngineState(
    __in BURN_ENGINE_STATE* pEngineState
    );

    public ref class ExitCodeTest : BurnUnitTest
    {
    public:
        ExitCodeTest(BurnTestFixture^ fixture) : BurnUnitTest(fixture)
        {
        }

        [Fact]
        void ExitCodeHandlingTest()
        {
            HRESULT hr = S_OK;
            BURN_ENGINE_STATE engineState = { };
            BURN_PIPE_CONNECTION* pConnection = &engineState.companionConnection;
            EXIT_CODE_ITEM rgExitCodeItems[] =
            {
                { 0, S_OK, BOOTSTRAPPER_APPLY_RESTART_NONE, L"Standard" },
                { 1, HRESULT_FROM_WIN32(1), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Standard" },
                { ERROR_SUCCESS_REBOOT_REQUIRED, S_OK, BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_REQUIRED), S_OK, BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { ERROR_SUCCESS_RESTART_REQUIRED, S_OK, BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_RESTART_REQUIRED), S_OK, BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { ERROR_SUCCESS_REBOOT_INITIATED, S_OK, BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Standard" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_INITIATED), S_OK, BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Standard" },
                { ERROR_FAIL_REBOOT_REQUIRED, HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Standard" },
                { ERROR_FAIL_REBOOT_INITIATED, HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Standard" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Standard" },
                { 0, E_FAIL, BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { 1, S_OK, BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { 3, S_OK, BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Custom" },
                { 4, S_OK, BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Custom" },
                { 5, HRESULT_FROM_WIN32(5), BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(5), HRESULT_FROM_WIN32(5), BOOTSTRAPPER_APPLY_RESTART_REQUIRED, L"Custom" },
                { 6, HRESULT_FROM_WIN32(6), BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(6), HRESULT_FROM_WIN32(6), BOOTSTRAPPER_APPLY_RESTART_INITIATED, L"Custom" },
                { ERROR_SUCCESS_REBOOT_REQUIRED, HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_REQUIRED), HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { ERROR_SUCCESS_RESTART_REQUIRED, HRESULT_FROM_WIN32(ERROR_SUCCESS_RESTART_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_RESTART_REQUIRED), HRESULT_FROM_WIN32(ERROR_SUCCESS_RESTART_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { ERROR_SUCCESS_REBOOT_INITIATED, HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_INITIATED), HRESULT_FROM_WIN32(ERROR_SUCCESS_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { ERROR_FAIL_REBOOT_REQUIRED, HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_REQUIRED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { ERROR_FAIL_REBOOT_INITIATED, HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
                { (DWORD)HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), HRESULT_FROM_WIN32(ERROR_FAIL_REBOOT_INITIATED), BOOTSTRAPPER_APPLY_RESTART_NONE, L"Custom" },
            };

            engineState.sczBundleEngineWorkingPath = L"tests\\ignore\\this\\path\\to\\burn.exe";

            try
            {
                ShelFunctionOverride(ExitCodeTest_ShellExecuteExW);
                CoreFunctionOverride(ExitCodeTest_CreateProcessW, ThrdWaitForCompletion);

                //
                // per-user side setup
                //
                LoadEngineState(&engineState);

                hr = ElevationElevate(&engineState, WM_BURN_ELEVATE, NULL);
                TestThrowOnFailure(hr, L"Failed to elevate.");

                for (DWORD i = 0; i < countof(rgExitCodeItems); ++i)
                {
                    // "run" the package both per-user and per-machine
                    ExecuteExePackage(&engineState, rgExitCodeItems + i);
                }

                //
                // initiate termination
                //
                hr = BurnPipeTerminateChildProcess(pConnection, 0, FALSE);
                TestThrowOnFailure(hr, L"Failed to terminate elevated process.");

                // check results
                for (DWORD i = 0; i < countof(rgExitCodeItems); ++i)
                {
                    EXIT_CODE_ITEM* pExitCode = rgExitCodeItems + i;
                    String^ packageId = gcnew String(pExitCode->wzPackageId);
                    String^ exitCodeString = ((UInt32)pExitCode->dwExitCode).ToString();

                    NativeAssert::SpecificReturnCode(pExitCode->hrExpectedResult, pExitCode->hrResultPerMachine, L"Per-machine package: {0}, exit code: {1}", packageId, exitCodeString);
                    Assert::True(pExitCode->expectedRestart == pExitCode->restartPerMachine, String::Format("Per-machine package: {0}, exit code: {1}, expected restart type '{2}' but got '{3}'", packageId, exitCodeString, gcnew String(LoggingRestartToString(pExitCode->expectedRestart)), gcnew String(LoggingRestartToString(pExitCode->restartPerMachine))));

                    NativeAssert::SpecificReturnCode(pExitCode->hrExpectedResult, pExitCode->hrResultPerUser, L"Per-user package: {0}, exit code: {1}", packageId, exitCodeString);
                    Assert::True(pExitCode->expectedRestart == pExitCode->restartPerUser, String::Format("Per-user package: {0}, exit code: {1}, expected restart type '{2}' but got '{3}'", packageId, exitCodeString, gcnew String(LoggingRestartToString(pExitCode->expectedRestart)), gcnew String(LoggingRestartToString(pExitCode->restartPerUser))));
                }
            }
            finally
            {
                VariablesUninitialize(&engineState.variables);
                BurnPipeConnectionUninitialize(pConnection);
            }
        }

    private:
        void ExecuteExePackage(
            __in BURN_ENGINE_STATE* pEngineState,
            __in EXIT_CODE_ITEM* pExitCode
            )
        {
            HRESULT hr = S_OK;
            LPWSTR sczExitCode = NULL;
            BURN_PACKAGE* pPackage = NULL;
            BURN_EXECUTE_ACTION executeAction = { };
            BURN_PIPE_CONNECTION* pConnection = &pEngineState->companionConnection;
            BOOL fRollback = FALSE;

            hr = PackageFindById(&pEngineState->packages, pExitCode->wzPackageId, &pPackage);
            TestThrowOnFailure(hr, L"Failed to find package.");

            executeAction.type = BURN_EXECUTE_ACTION_TYPE_EXE_PACKAGE;
            executeAction.exePackage.action = BOOTSTRAPPER_ACTION_STATE_INSTALL;
            executeAction.exePackage.pPackage = pPackage;

            try
            {
                hr = StrAllocFormatted(&sczExitCode, L"%u", pExitCode->dwExitCode);
                TestThrowOnFailure(hr, L"Failed to convert exit code to string.");

                hr = VariableSetString(&pEngineState->variables, L"ExeExitCode", sczExitCode, FALSE, FALSE);
                TestThrowOnFailure(hr, L"Failed to set variable.");

                pExitCode->hrResultPerMachine = ElevationExecuteExePackage(pConnection->hPipe, &executeAction, &pEngineState->variables, fRollback, ExitCodeTest_GenericMessageHandler, NULL, &pExitCode->restartPerMachine);

                pExitCode->hrResultPerUser = ExeEngineExecutePackage(&executeAction, &pEngineState->cache, &pEngineState->variables, fRollback, ExitCodeTest_GenericMessageHandler, NULL, &pExitCode->restartPerUser);
            }
            finally
            {
                ReleaseStr(sczExitCode);
            }
        }
    };


static BOOL STDAPICALLTYPE ExitCodeTest_ShellExecuteExW(
    __inout LPSHELLEXECUTEINFOW lpExecInfo
    )
{
    HRESULT hr = S_OK;
    LPWSTR scz = NULL;

    hr = StrAllocString(&scz, lpExecInfo->lpParameters, 0);
    ExitOnFailure(hr, "Failed to copy arguments.");

    // Pretend this thread is the elevated process.
    lpExecInfo->hProcess = ::CreateThread(NULL, 0, ExitCodeTest_ElevationThreadProc, scz, 0, NULL);
    ExitOnNullWithLastError(lpExecInfo->hProcess, hr, "Failed to create thread.");
    scz = NULL;

LExit:
    ReleaseStr(scz);

    return SUCCEEDED(hr);
}

static DWORD CALLBACK ExitCodeTest_ElevationThreadProc(
    __in LPVOID lpThreadParameter
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczArguments = (LPWSTR)lpThreadParameter;
    BURN_ENGINE_STATE engineState = { };
    BURN_PIPE_CONNECTION* pConnection = &engineState.companionConnection;
    HANDLE hLock = NULL;
    DWORD dwChildExitCode = 0;
    BOOL fRestart = FALSE;
    BOOL fApplying = FALSE;

    LoadEngineState(&engineState);

    StrAlloc(&pConnection->sczName, MAX_PATH);
    StrAlloc(&pConnection->sczSecret, MAX_PATH);

    // parse command line arguments
    if (3 != swscanf_s(sczArguments, L"-q -burn.elevated %s %s %u", pConnection->sczName, MAX_PATH, pConnection->sczSecret, MAX_PATH, &pConnection->dwProcessId))
    {
        hr = E_INVALIDARG;
        ExitOnFailure(hr, "Failed to parse argument string.");
    }

    // set up connection with per-user process
    hr = BurnPipeChildConnect(pConnection, TRUE);
    ExitOnFailure(hr, "Failed to connect to per-user process.");

    hr = ElevationChildPumpMessages(pConnection->hPipe, pConnection->hCachePipe, &engineState.approvedExes, &engineState.cache, &engineState.containers, &engineState.packages, &engineState.payloads, &engineState.variables, &engineState.registration, &engineState.userExperience, &hLock, &dwChildExitCode, &fRestart, &fApplying);
    ExitOnFailure(hr, "Failed while pumping messages in child 'process'.");

LExit:
    BurnPipeConnectionUninitialize(pConnection);
    VariablesUninitialize(&engineState.variables);
    ReleaseStr(sczArguments);

    return FAILED(hr) ? (DWORD)hr : dwChildExitCode;
}

static BOOL STDAPICALLTYPE ExitCodeTest_CreateProcessW(
    __in_opt LPCWSTR /*lpApplicationName*/,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt LPSECURITY_ATTRIBUTES /*lpProcessAttributes*/,
    __in_opt LPSECURITY_ATTRIBUTES /*lpThreadAttributes*/,
    __in BOOL /*bInheritHandles*/,
    __in DWORD /*dwCreationFlags*/,
    __in_opt LPVOID /*lpEnvironment*/,
    __in_opt LPCWSTR /*lpCurrentDirectory*/,
    __in LPSTARTUPINFOW /*lpStartupInfo*/,
    __out LPPROCESS_INFORMATION lpProcessInformation
    )
{
    HRESULT hr = S_OK;
    LPWSTR scz = NULL;
    LPCWSTR wzArgs = lpCommandLine;

    hr = StrAllocString(&scz, wzArgs, 0);
    ExitOnFailure(hr, "Failed to copy arguments.");

    // Pretend this thread is the package process.
    lpProcessInformation->hProcess = ::CreateThread(NULL, 0, ExitCodeTest_PackageThreadProc, scz, 0, NULL);
    ExitOnNullWithLastError(lpProcessInformation->hProcess, hr, "Failed to create thread.");

    scz = NULL;

LExit:
    ReleaseStr(scz);

    return SUCCEEDED(hr);
}

static DWORD CALLBACK ExitCodeTest_PackageThreadProc(
    __in LPVOID lpThreadParameter
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczArguments = (LPWSTR)lpThreadParameter;
    int argc = 0;
    LPWSTR* argv = NULL;
    DWORD dwResult = 0;

    hr = AppParseCommandLine(sczArguments, &argc, &argv);
    ExitOnFailure(hr, "Failed to parse command line: %ls", sczArguments);

    hr = StrStringToUInt32(argv[1], 0, reinterpret_cast<UINT*>(&dwResult));
    ExitOnFailure(hr, "Failed to convert %ls to DWORD.", argv[1]);

LExit:
    AppFreeCommandLineArgs(argv);
    ReleaseStr(sczArguments);

    return FAILED(hr) ? (DWORD)hr : dwResult;
}

static int ExitCodeTest_GenericMessageHandler(
    __in GENERIC_EXECUTE_MESSAGE* /*pMessage*/,
    __in LPVOID /*pvContext*/
    )
{
    return IDNOACTION;
}

static void LoadEngineState(
    __in BURN_ENGINE_STATE* pEngineState
    )
{
    HRESULT hr = S_OK;
    IXMLDOMElement* pixeBundle = NULL;

    LPCWSTR wzDocument =
        L"<BurnManifest>"
        L"    <Payload Id='test.exe' FilePath='test.exe' Packaging='external' SourcePath='test.exe' Hash='000000000000' FileSize='1' />"
        L"    <Chain>"
        L"        <ExePackage Id='Custom' Cache='remove' CacheId='test.exe' InstallSize='1' Size='1' PerMachine='no' Permanent='yes' Vital='yes' DetectCondition='' InstallArguments='[ExeExitCode]' UninstallArguments='' Uninstallable='no' RepairArguments='' Repairable='no' Protocol='none' DetectionType='condition'>"
        L"            <ExitCode Code='0' Type='2' />"
        L"            <ExitCode Code='3' Type='3' />"
        L"            <ExitCode Code='4' Type='4' />"
        L"            <ExitCode Code='5' Type='5' />"
        L"            <ExitCode Code='-2147024891' Type='5' />"
        L"            <ExitCode Code='6' Type='6' />"
        L"            <ExitCode Code='-2147024890' Type='6' />"
        L"            <ExitCode Code='3010' Type='2' />"
        L"            <ExitCode Code='-2147021886' Type='2' />"
        L"            <ExitCode Code='3011' Type='2' />"
        L"            <ExitCode Code='-2147021885' Type='2' />"
        L"            <ExitCode Code='1641' Type='2' />"
        L"            <ExitCode Code='-2147023255' Type='2' />"
        L"            <ExitCode Code='3017' Type='2' />"
        L"            <ExitCode Code='-2147021879' Type='2' />"
        L"            <ExitCode Code='3018' Type='2' />"
        L"            <ExitCode Code='-2147021878' Type='2' />"
        L"            <ExitCode Code='*' Type='1' />"
        L"            <PayloadRef Id='test.exe' />"
        L"        </ExePackage>"
        L"        <ExePackage Id='Standard' Cache='remove' CacheId='test.exe' InstallSize='1' Size='1' PerMachine='no' Permanent='yes' Vital='yes' DetectCondition='' InstallArguments='[ExeExitCode]' UninstallArguments='' Uninstallable='no' RepairArguments='' Repairable='no' Protocol='none' DetectionType='condition'>"
        L"            <PayloadRef Id='test.exe' />"
        L"        </ExePackage>"
        L"    </Chain>"
        L"</BurnManifest>";

    VariableInitialize(&pEngineState->variables);

    BurnPipeConnectionInitialize(&pEngineState->companionConnection);

    hr = CacheInitialize(&pEngineState->cache, &pEngineState->internalCommand);
    TestThrowOnFailure(hr, "CacheInitialize failed.");

    // load XML document
    LoadBundleXmlHelper(wzDocument, &pixeBundle);

    hr = PayloadsParseFromXml(&pEngineState->payloads, &pEngineState->containers, &pEngineState->layoutPayloads, pixeBundle);
    TestThrowOnFailure(hr, "Failed to parse payloads from manifest.");

    hr = PackagesParseFromXml(&pEngineState->packages, &pEngineState->payloads, pixeBundle);
    TestThrowOnFailure(hr, "Failed to parse packages from manifest.");

    ReleaseObject(pixeBundle);
}
}
}
}
}
}