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
|
// 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"
static HRESULT CALLBACK CacheTestEventRoutine(
__in BURN_CACHE_MESSAGE* pMessage,
__in LPVOID pvContext
);
static DWORD CALLBACK CacheTestProgressRoutine(
__in LARGE_INTEGER TotalFileSize,
__in LARGE_INTEGER TotalBytesTransferred,
__in LARGE_INTEGER StreamSize,
__in LARGE_INTEGER StreamBytesTransferred,
__in DWORD dwStreamNumber,
__in DWORD dwCallbackReason,
__in HANDLE hSourceFile,
__in HANDLE hDestinationFile,
__in_opt LPVOID lpData
);
typedef struct _CACHE_TEST_CONTEXT
{
} CACHE_TEST_CONTEXT;
namespace Microsoft
{
namespace Tools
{
namespace WindowsInstallerXml
{
namespace Test
{
namespace Bootstrapper
{
using namespace System;
using namespace System::IO;
using namespace Xunit;
public ref class CacheTest : BurnUnitTest, IClassFixture<TestRegistryFixture^>
{
private:
TestRegistryFixture^ testRegistry;
public:
CacheTest(BurnTestFixture^ fixture, TestRegistryFixture^ registryFixture) : BurnUnitTest(fixture)
{
this->testRegistry = registryFixture;
}
[Fact]
void CacheElevatedTempFallbacksTest()
{
HRESULT hr = S_OK;
BURN_CACHE cache = { };
BURN_ENGINE_COMMAND internalCommand = { };
HKEY hkSystemEnvironment = NULL;
HKEY hkBurnPolicy = NULL;
internalCommand.fInitiallyElevated = TRUE;
try
{
this->testRegistry->SetUp();
// No registry keys, so should fallback to %windir%\TEMP.
hr = CacheInitialize(&cache, &internalCommand);
NativeAssert::Succeeded(hr, "Failed to initialize cache.");
Assert::NotEqual<DWORD>(0, cache.cPotentialBaseWorkingFolders);
VerifyBaseWorkingFolder(L"%windir%\\TEMP\\", cache.rgsczPotentialBaseWorkingFolders[0]);
CacheUninitialize(&cache);
hr = RegCreate(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Session Manager\\Environment", GENERIC_WRITE, &hkSystemEnvironment);
NativeAssert::Succeeded(hr, "Failed to create system environment key.");
// Third fallback is system-level %TEMP%.
hr = RegWriteExpandString(hkSystemEnvironment, L"TEMP", L"A:\\TEST\\TEMP");
NativeAssert::Succeeded(hr, "Failed to write TEMP system environment value.");
hr = CacheInitialize(&cache, &internalCommand);
NativeAssert::Succeeded(hr, "Failed to initialize cache.");
Assert::NotEqual<DWORD>(0, cache.cPotentialBaseWorkingFolders);
VerifyBaseWorkingFolder(L"A:\\TEST\\TEMP\\", cache.rgsczPotentialBaseWorkingFolders[0]);
CacheUninitialize(&cache);
// Second fallback is system-level %TMP%.
hr = RegWriteString(hkSystemEnvironment, L"TMP", L"B:\\TEST\\TMP\\");
NativeAssert::Succeeded(hr, "Failed to write TEMP system environment value.");
hr = CacheInitialize(&cache, &internalCommand);
NativeAssert::Succeeded(hr, "Failed to initialize cache.");
Assert::NotEqual<DWORD>(0, cache.cPotentialBaseWorkingFolders);
VerifyBaseWorkingFolder(L"B:\\TEST\\TMP\\", cache.rgsczPotentialBaseWorkingFolders[0]);
CacheUninitialize(&cache);
// First fallback is impractical to mock out - %windir%\SystemTemp on Win11 when running as SYSTEM.
hr = RegCreate(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Policies\\WiX\\Burn", GENERIC_WRITE, &hkBurnPolicy);
NativeAssert::Succeeded(hr, "Failed to create Burn policy key.");
// Default source is Burn policy.
hr = RegWriteExpandString(hkBurnPolicy, L"EngineWorkingDirectory", L"D:\\TEST\\POLICY\\");
NativeAssert::Succeeded(hr, "Failed to write EngineWorkingDirectory Burn policy value.");
hr = CacheInitialize(&cache, &internalCommand);
NativeAssert::Succeeded(hr, "Failed to initialize cache.");
Assert::NotEqual<DWORD>(0, cache.cPotentialBaseWorkingFolders);
VerifyBaseWorkingFolder(L"D:\\TEST\\POLICY\\", cache.rgsczPotentialBaseWorkingFolders[0]);
CacheUninitialize(&cache);
// Command line parameter overrides everything else.
hr = StrAllocString(&internalCommand.sczEngineWorkingDirectory, L"E:\\TEST\\COMMANDLINE\\", 0);
NativeAssert::Succeeded(hr, "Failed to copy command line working directory.");
hr = CacheInitialize(&cache, &internalCommand);
NativeAssert::Succeeded(hr, "Failed to initialize cache.");
Assert::NotEqual<DWORD>(0, cache.cPotentialBaseWorkingFolders);
VerifyBaseWorkingFolder(L"E:\\TEST\\COMMANDLINE\\", cache.rgsczPotentialBaseWorkingFolders[0]);
CacheUninitialize(&cache);
}
finally
{
ReleaseRegKey(hkBurnPolicy);
ReleaseRegKey(hkSystemEnvironment);
ReleaseStr(internalCommand.sczEngineWorkingDirectory);
CacheUninitialize(&cache);
this->testRegistry->TearDown();
}
}
void VerifyBaseWorkingFolder(LPCWSTR wzExpectedUnexpanded, LPCWSTR wzActual)
{
String^ expected = Environment::ExpandEnvironmentVariables(gcnew String(wzExpectedUnexpanded));
WixAssert::StringEqual(expected, gcnew String(wzActual), true);
}
[Fact]
void CacheSignatureTest()
{
HRESULT hr = S_OK;
BURN_CACHE cache = { };
BURN_ENGINE_COMMAND internalCommand = { };
BURN_PACKAGE package = { };
BURN_PAYLOAD payload = { };
LPWSTR sczPayloadPath = NULL;
BYTE* pb = NULL;
DWORD cb = NULL;
CACHE_TEST_CONTEXT context = { };
try
{
pin_ptr<const wchar_t> dataDirectory = PtrToStringChars(this->TestContext->TestDirectory);
hr = PathConcat(dataDirectory, L"TestData\\CacheTest\\CacheSignatureTest.File", &sczPayloadPath);
Assert::True(S_OK == hr, "Failed to get path to test file.");
Assert::True(FileExistsEx(sczPayloadPath, NULL), "Test file does not exist.");
hr = StrAllocHexDecode(L"25e61cd83485062b70713aebddd3fe4992826cb121466fddc8de3eacb1e42f39d4bdd8455d95eec8c9529ced4c0296ab861931fe2c86df2f2b4e8d259a6d9223", &pb, &cb);
Assert::Equal(S_OK, hr);
package.fPerMachine = FALSE;
package.sczCacheId = L"Bootstrapper.CacheTest.CacheSignatureTest";
payload.sczKey = L"CacheSignatureTest.PayloadKey";
payload.sczFilePath = L"CacheSignatureTest.File";
payload.pbHash = pb;
payload.cbHash = cb;
payload.qwFileSize = 27;
payload.verification = BURN_PAYLOAD_VERIFICATION_HASH;
hr = CacheInitialize(&cache, &internalCommand);
TestThrowOnFailure(hr, L"Failed initialize cache.");
hr = CacheCompletePayload(&cache, package.fPerMachine, &payload, package.sczCacheId, sczPayloadPath, FALSE, CacheTestEventRoutine, CacheTestProgressRoutine, &context);
Assert::Equal(S_OK, hr);
}
finally
{
ReleaseMem(pb);
ReleaseStr(sczPayloadPath);
String^ filePath = Path::Combine(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "Package Cache\\Bootstrapper.CacheTest.CacheSignatureTest\\CacheSignatureTest.File");
if (File::Exists(filePath))
{
File::SetAttributes(filePath, FileAttributes::Normal);
File::Delete(filePath);
}
CacheUninitialize(&cache);
}
}
};
}
}
}
}
}
static HRESULT CALLBACK CacheTestEventRoutine(
__in BURN_CACHE_MESSAGE* /*pMessage*/,
__in LPVOID /*pvContext*/
)
{
return S_OK;
}
static DWORD CALLBACK CacheTestProgressRoutine(
__in LARGE_INTEGER /*TotalFileSize*/,
__in LARGE_INTEGER /*TotalBytesTransferred*/,
__in LARGE_INTEGER /*StreamSize*/,
__in LARGE_INTEGER /*StreamBytesTransferred*/,
__in DWORD /*dwStreamNumber*/,
__in DWORD /*dwCallbackReason*/,
__in HANDLE /*hSourceFile*/,
__in HANDLE /*hDestinationFile*/,
__in_opt LPVOID /*lpData*/
)
{
return PROGRESS_QUIET;
}
|