-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathhelpers.cpp
More file actions
191 lines (166 loc) · 7.11 KB
/
helpers.cpp
File metadata and controls
191 lines (166 loc) · 7.11 KB
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
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#include "pch.h"
#include "constants.h"
#include "helpers.h"
using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Management::Deployment;
using namespace winrt::Windows::System;
namespace WindowsAppRuntimeInstallerTests
{
wil::unique_handle Execute(const std::wstring& command, const std::wstring& args)
{
SHELLEXECUTEINFO ei{};
ei.cbSize = sizeof(SHELLEXECUTEINFO);
ei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_DOENVSUBST;
ei.lpFile = command.c_str();
ei.lpParameters = args.c_str();
if (!ShellExecuteEx(&ei))
{
auto lastError{ GetLastError() };
VERIFY_ARE_EQUAL(S_OK, HRESULT_FROM_WIN32(lastError));
}
wil::unique_handle process{ ei.hProcess };
return process;
}
HRESULT RunInstaller(const std::wstring& args)
{
const std::wstring installerPath{ GetInstallerPath().c_str() };
Log::Comment(WEX::Common::String().Format(L"Running installer at: %ws", installerPath.c_str()));
Log::Comment(WEX::Common::String().Format(L"Arguments: %ws", args.c_str()));
auto process{ Execute(installerPath, args) };
auto waitResult{ WaitForSingleObject(process.get(), c_phaseTimeout) };
if (waitResult != WAIT_OBJECT_0)
{
auto lastError{ GetLastError() };
VERIFY_ARE_NOT_EQUAL(S_OK, HRESULT_FROM_WIN32(lastError));
}
DWORD exitCode{};
THROW_IF_WIN32_BOOL_FALSE(GetExitCodeProcess(process.get(), &exitCode));
Log::Comment(WEX::Common::String().Format(L"Installer exit code: 0x%0X", HRESULT_FROM_WIN32(exitCode)));
return HRESULT_FROM_WIN32(exitCode);
}
void RemovePackage(const std::wstring& packageName, bool ignoreFailures)
{
Log::Comment(WEX::Common::String().Format(L"Removing package: %ws", packageName.c_str()));
PackageManager manager;
for (int attempt = 1; attempt <= c_packageRemovalRetryCount; ++attempt)
{
auto result{ manager.RemovePackageAsync(packageName).get() };
const auto hr{ result.ExtendedErrorCode().value };
Log::Comment(WEX::Common::String().Format(L"Removal result: 0x%0X (attempt %d/%d)", hr, attempt, c_packageRemovalRetryCount));
if (SUCCEEDED(hr))
{
return;
}
// If the package is not found, treat as success — nothing to remove.
if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
{
Log::Comment(WEX::Common::String().Format(L"Package not found (already removed): %ws", packageName.c_str()));
return;
}
// Retry on transient errors (package in use, etc.) unless this is the last attempt.
if (attempt < c_packageRemovalRetryCount)
{
Log::Warning(WEX::Common::String().Format(L"Retrying removal of package %ws after %dms (error: 0x%0X)", packageName.c_str(), c_packageRemovalRetryDelayMs, hr));
Sleep(c_packageRemovalRetryDelayMs);
}
else if (!ignoreFailures)
{
winrt::check_hresult(result.ExtendedErrorCode());
}
else
{
Log::Warning(WEX::Common::String().Format(L"Failed to remove package %ws after %d attempts (error: 0x%0X)", packageName.c_str(), c_packageRemovalRetryCount, hr));
}
}
}
// Removing provisioned packages requires the test to run elevated with a user that is an administrator.
// This is best-effort at cleanup, though the program is idempotent and should succeed regardless of existing
// provisioning state in situations where it is not run elevated.
void TryRemoveProvisionedPackage(const std::wstring& packageFamilyName)
{
Log::Comment(WEX::Common::String().Format(L"Trying to remove provisioned package: %ws", packageFamilyName.c_str()));
PackageManager manager;
auto result{ manager.DeprovisionPackageForAllUsersAsync(packageFamilyName).get() };
Log::Comment(WEX::Common::String().Format(L"Provision removal result: 0x%0X", result.ExtendedErrorCode().value));
}
void RemoveAllPackages(bool ignoreFailures)
{
for (const auto& packageName : c_packages)
{
RemovePackage(packageName, ignoreFailures);
}
for (const auto& packageFamilyName : c_mainPackageFamilies)
{
TryRemoveProvisionedPackage(packageFamilyName);
}
}
bool VerifyAllPackagesRemoved()
{
bool allRemoved{ true };
for (const auto& packageName : c_packages)
{
if (IsPackageRegistered(packageName))
{
Log::Warning(WEX::Common::String().Format(L"Setup error: package still registered after removal: %ws", packageName.c_str()));
allRemoved = false;
}
}
return allRemoved;
}
bool IsPackageRegistered(const std::wstring& packageFullName)
{
PackageManager manager;
auto result{ manager.FindPackageForUser(L"", packageFullName) };
Log::Comment(WEX::Common::String().Format(L"Package %ws is %wsregistered", packageFullName.c_str(), result?L"":L"not "));
return result != nullptr;
}
ProcessorArchitecture GetSystemArchitecture()
{
USHORT processMachine{ IMAGE_FILE_MACHINE_UNKNOWN };
USHORT nativeMachine{ IMAGE_FILE_MACHINE_UNKNOWN };
THROW_IF_WIN32_BOOL_FALSE(::IsWow64Process2(::GetCurrentProcess(), &processMachine, &nativeMachine));
switch (nativeMachine)
{
case IMAGE_FILE_MACHINE_I386:
return ProcessorArchitecture::X86;
case IMAGE_FILE_MACHINE_AMD64:
return ProcessorArchitecture::X64;
case IMAGE_FILE_MACHINE_ARM64:
return ProcessorArchitecture::Arm64;
default:
THROW_HR_MSG(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), "nativeMachine=%hu", nativeMachine);
}
}
std::filesystem::path GetModulePath(HMODULE hmodule)
{
if (hmodule == NULL)
{
hmodule = GetModuleHandle(L"InstallerFunctionalTests.dll");
}
auto path{ GetModuleFileName(hmodule) };
return path.remove_filename();
}
std::filesystem::path GetModuleFileName(HMODULE hmodule)
{
auto moduleFileName{ wil::GetModuleFileNameW(hmodule) };
return std::filesystem::path(moduleFileName.get());
}
std::filesystem::path GetCommonRootPath()
{
auto path = GetModulePath();
// TAEF runs as a package under the installer, so we have to go way up the parent root in order to
// get to the common project root and then get to the build output.
return path.parent_path().parent_path();
}
std::filesystem::path GetInstallerPath()
{
auto path{ GetCommonRootPath() };
return path /= INSTALLER_EXE_PATH;
}
}