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
|
// 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"
const DWORD TEST_CHILD_SENT_MESSAGE_ID = 0xFFFE;
const DWORD TEST_PARENT_SENT_MESSAGE_ID = 0xFFFF;
const HRESULT S_TEST_SUCCEEDED = 0x3133;
const char TEST_MESSAGE_DATA[] = "{94949868-7EAE-4ac5-BEAC-AFCA2821DE01}";
static BOOL STDAPICALLTYPE ElevateTest_ShellExecuteExW(
__inout LPSHELLEXECUTEINFOW lpExecInfo
);
static DWORD CALLBACK ElevateTest_ThreadProc(
__in LPVOID lpThreadParameter
);
static HRESULT ProcessParentMessages(
__in BURN_PIPE_MESSAGE* pMsg,
__in_opt LPVOID pvContext,
__out DWORD* pdwResult
);
static HRESULT ProcessChildMessages(
__in BURN_PIPE_MESSAGE* pMsg,
__in_opt LPVOID pvContext,
__out DWORD* pdwResult
);
namespace Microsoft
{
namespace Tools
{
namespace WindowsInstallerXml
{
namespace Test
{
namespace Bootstrapper
{
using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace Xunit;
public ref class ElevationTest : BurnUnitTest
{
public:
ElevationTest(BurnTestFixture^ fixture) : BurnUnitTest(fixture)
{
}
[Fact]
void ElevateTest()
{
HRESULT hr = S_OK;
BURN_ENGINE_STATE engineState = { };
BURN_PIPE_CONNECTION* pConnection = &engineState.companionConnection;
DWORD dwResult = S_OK;
engineState.sczBundleEngineWorkingPath = L"tests\\ignore\\this\\path\\to\\burn.exe";
try
{
ShelFunctionOverride(ElevateTest_ShellExecuteExW);
CoreFunctionOverride(NULL, ThrdWaitForCompletion);
PipeConnectionInitialize(pConnection);
//
// per-user side setup
//
hr = ElevationElevate(&engineState, WM_BURN_ELEVATE, NULL);
TestThrowOnFailure(hr, L"Failed to elevate.");
// post execute message
hr = PipeSendMessage(pConnection->hPipe, TEST_PARENT_SENT_MESSAGE_ID, NULL, 0, ProcessParentMessages, NULL, &dwResult);
TestThrowOnFailure(hr, "Failed to post execute message to per-machine process.");
//
// initiate termination
//
hr = PipeTerminateChildProcess(pConnection, 666, FALSE);
TestThrowOnFailure(hr, L"Failed to terminate elevated process.");
// check flags
Assert::Equal(S_TEST_SUCCEEDED, (HRESULT)dwResult);
}
finally
{
PipeConnectionUninitialize(pConnection);
}
}
};
}
}
}
}
}
static BOOL STDAPICALLTYPE ElevateTest_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, ElevateTest_ThreadProc, scz, 0, NULL);
ExitOnNullWithLastError(lpExecInfo->hProcess, hr, "Failed to create thread.");
scz = NULL;
LExit:
ReleaseStr(scz);
return SUCCEEDED(hr);
}
static DWORD CALLBACK ElevateTest_ThreadProc(
__in LPVOID lpThreadParameter
)
{
HRESULT hr = S_OK;
LPWSTR sczArguments = (LPWSTR)lpThreadParameter;
BURN_PIPE_CONNECTION connection = { };
BURN_PIPE_RESULT result = { };
PipeConnectionInitialize(&connection);
StrAlloc(&connection.sczName, MAX_PATH);
StrAlloc(&connection.sczSecret, MAX_PATH);
// parse command line arguments
if (3 != swscanf_s(sczArguments, L"-q -burn.elevated %s %s %u", connection.sczName, MAX_PATH, connection.sczSecret, MAX_PATH, &connection.dwProcessId))
{
hr = E_INVALIDARG;
ExitOnFailure(hr, "Failed to parse argument string.");
}
// set up connection with per-user process
hr = PipeChildConnect(&connection, TRUE);
ExitOnFailure(hr, "Failed to connect to per-user process.");
// pump messages
hr = PipePumpMessages(connection.hPipe, ProcessChildMessages, static_cast<LPVOID>(connection.hPipe), &result);
ExitOnFailure(hr, "Failed while pumping messages in child 'process'.");
LExit:
PipeConnectionUninitialize(&connection);
ReleaseStr(sczArguments);
return FAILED(hr) ? (DWORD)hr : result.dwResult;
}
static HRESULT ProcessParentMessages(
__in BURN_PIPE_MESSAGE* pMsg,
__in_opt LPVOID /*pvContext*/,
__out DWORD* pdwResult
)
{
HRESULT hr = S_OK;
HRESULT hrResult = E_INVALIDDATA;
// Process the message.
switch (pMsg->dwMessage)
{
case TEST_CHILD_SENT_MESSAGE_ID:
if (sizeof(TEST_MESSAGE_DATA) == pMsg->cbData && 0 == memcmp(TEST_MESSAGE_DATA, pMsg->pvData, sizeof(TEST_MESSAGE_DATA)))
{
hrResult = S_TEST_SUCCEEDED;
}
break;
default:
hr = E_INVALIDARG;
ExitOnRootFailure(hr, "Unexpected elevated message sent to parent process, msg: %u", pMsg->dwMessage);
}
*pdwResult = static_cast<DWORD>(hrResult);
LExit:
return hr;
}
static HRESULT ProcessChildMessages(
__in BURN_PIPE_MESSAGE* pMsg,
__in_opt LPVOID pvContext,
__out DWORD* pdwResult
)
{
HRESULT hr = S_OK;
HANDLE hPipe = static_cast<HANDLE>(pvContext);
DWORD dwResult = 0;
// Process the message.
switch (pMsg->dwMessage)
{
case TEST_PARENT_SENT_MESSAGE_ID:
// send test message
hr = PipeSendMessage(hPipe, TEST_CHILD_SENT_MESSAGE_ID, (LPVOID)TEST_MESSAGE_DATA, sizeof(TEST_MESSAGE_DATA), NULL, NULL, &dwResult);
ExitOnFailure(hr, "Failed to send message to per-machine process.");
break;
default:
hr = E_INVALIDARG;
ExitOnRootFailure(hr, "Unexpected elevated message sent to child process, msg: %u", pMsg->dwMessage);
}
*pdwResult = dwResult;
LExit:
return hr;
}
|