forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResumeFlow.cpp
More file actions
277 lines (217 loc) · 11.9 KB
/
Copy pathResumeFlow.cpp
File metadata and controls
277 lines (217 loc) · 11.9 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WorkflowCommon.h"
#include "TestHooks.h"
#include <Commands/InstallCommand.h>
#include <Commands/ResumeCommand.h>
#include <AppInstallerRuntime.h>
#include <AppInstallerStrings.h>
#include <AppInstallerVersions.h>
#include <CheckpointManager.h>
#include <Workflows/ShellExecuteInstallerHandler.h>
using namespace std::string_literals;
using namespace AppInstaller::CLI;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::Settings;
using namespace AppInstaller::Runtime;
using namespace TestCommon;
using namespace AppInstaller::Checkpoints;
constexpr std::string_view s_AutomaticCheckpoint = "automatic"sv;
constexpr std::string_view s_CheckpointsFileName = "checkpoints.db"sv;
TEST_CASE("ResumeFlow_IndexNotFound", "[Resume]")
{
std::string tempGuidString = "{ec3a098c-a815-4d52-8866-946c03093a37}";
std::ostringstream resumeOutput;
TestContext context{ resumeOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::ResumeId, tempGuidString);
ResumeCommand resume({});
context.SetExecutingCommand(&resume);
resume.Execute(context);
INFO(resumeOutput.str());
REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_RESUME_ID_NOT_FOUND);
auto expectedMessage = Resource::String::ResumeIdNotFoundError(AppInstaller::Utility::LocIndString(tempGuidString));
REQUIRE(resumeOutput.str().find(Resource::LocString(expectedMessage).get()) != std::string::npos);
}
TEST_CASE("ResumeFlow_InvalidClientVersion", "[Resume]")
{
TestCommon::TempDirectory tempCheckpointRecordDirectory("TempCheckpointRecordDirectory", true);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointRecordDirectory.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
// Create temp guid and populate with invalid client version.
std::string tempGuidString = "{615339e9-3ac5-4e86-a5ab-c246657aca25}";
auto tempRecordPath = tempCheckpointRecordDirectoryPath / tempGuidString / s_CheckpointsFileName;
std::string_view invalidClientVersion = "1.2.3.4"sv;
INFO("Using temporary file named: " << tempRecordPath);
{
// Manually set invalid client version
std::filesystem::create_directories(tempRecordPath.parent_path());
std::shared_ptr<CheckpointDatabase> checkpointRecord = CheckpointDatabase::CreateNew(tempRecordPath.u8string());
CheckpointDatabase::IdType checkpointId = checkpointRecord->AddCheckpoint(s_AutomaticCheckpoint);
checkpointRecord->SetDataValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}, { "1.2.3.4" });
}
std::ostringstream resumeOutput;
TestContext context{ resumeOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::ResumeId, tempGuidString);
ResumeCommand resume({});
context.SetExecutingCommand(&resume);
resume.Execute(context);
INFO(resumeOutput.str());
REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_CLIENT_VERSION_MISMATCH);
auto expectedMessage = Resource::String::ClientVersionMismatchError(AppInstaller::Utility::LocIndString(invalidClientVersion));
REQUIRE(resumeOutput.str().find(Resource::LocString(expectedMessage).get()) != std::string::npos);
}
TEST_CASE("ResumeFlow_EmptyIndex", "[Resume]")
{
TestCommon::TempDirectory tempCheckpointRecordDirectory("TempCheckpointRecordDirectory", true);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointRecordDirectory.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
std::string tempGuidString = "{43ca664c-3eae-4f73-99ee-18cf83912c02}";
auto tempRecordPath = tempCheckpointRecordDirectoryPath / tempGuidString / s_CheckpointsFileName;
INFO("Using temporary file named: " << tempRecordPath);
{
std::filesystem::create_directories(tempRecordPath.parent_path());
CheckpointDatabase::CreateNew(tempRecordPath.u8string());
}
std::ostringstream resumeOutput;
TestContext context{ resumeOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::ResumeId, tempGuidString);
ResumeCommand resume({});
context.SetExecutingCommand(&resume);
resume.Execute(context);
INFO(resumeOutput.str());
REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_INVALID_RESUME_STATE);
REQUIRE(resumeOutput.str().find(Resource::LocString(Resource::String::ResumeStateDataNotFoundError).get()) != std::string::npos);
}
TEST_CASE("ResumeFlow_InstallSuccess", "[Resume]")
{
TestCommon::TempDirectory tempCheckpointRecordDirectory("TempCheckpointRecordDirectory", false);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointRecordDirectory.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::EFResume>(true);
TestCommon::TempFile installResultPath("TestExeInstalled.txt");
{
std::ostringstream installOutput;
TestContext context{ installOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
OverrideForShellExecute(context);
const auto& testManifestPath = TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string();
context.Args.AddArg(Execution::Args::Type::Manifest, testManifestPath);
InstallCommand install({});
context.SetExecutingCommand(&install);
install.Execute(context);
INFO(installOutput.str());
}
// Verify Installer is called and parameters are passed in.
REQUIRE(std::filesystem::exists(installResultPath.GetPath()));
std::ifstream installResultFile(installResultPath.GetPath());
REQUIRE(installResultFile.is_open());
std::string installResultStr;
std::getline(installResultFile, installResultStr);
REQUIRE(installResultStr.find("/custom") != std::string::npos);
REQUIRE(installResultStr.find("/silentwithprogress") != std::string::npos);
// The checkpoint index should not exist if the context succeeded.
std::vector<std::filesystem::path> checkpointFiles;
for (const auto& entry : std::filesystem::directory_iterator(tempCheckpointRecordDirectoryPath))
{
checkpointFiles.emplace_back(entry.path());
}
REQUIRE(checkpointFiles.size() == 0);
}
TEST_CASE("ResumeFlow_InstallFailure", "[Resume]")
{
TestCommon::TempDirectory tempCheckpointRecordDirectory("TempCheckpointRecordDirectory", false);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointRecordDirectory.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::EFResume>(true);
{
std::ostringstream installOutput;
TestContext context{ installOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
const auto& testManifestPath = TestDataFile("InstallFlowTest_UnsupportedArguments.yaml").GetPath().u8string();
context.Args.AddArg(Execution::Args::Type::Manifest, testManifestPath);
context.Args.AddArg(Execution::Args::Type::InstallLocation, "installLocation"sv);
InstallCommand install({});
context.SetExecutingCommand(&install);
install.Execute(context);
INFO(installOutput.str());
// Verify unsupported arguments error message is shown
REQUIRE(context.GetTerminationHR() == APPINSTALLER_CLI_ERROR_UNSUPPORTED_ARGUMENT);
}
// Checkpoint file should be cleaned up if the hr is not reboot related.
REQUIRE(std::filesystem::is_empty(tempCheckpointRecordDirectoryPath));
}
TEST_CASE("ResumeFlow_WriteToRunOnceRegistry", "[Reboot][Resume][windowsFeature]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
TestCommon::TempDirectory tempCheckpointRecordDirectory("TempCheckpointRecordDirectory", false);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointRecordDirectory.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::EFResume>(true);
std::ostringstream installOutput;
TestContext context{ installOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
OverrideOpenDependencySource(context);
OverrideRegisterStartupAfterReboot(context);
// Override with reboot required HRESULT.
auto doesFeatureExistOverride = TestHook::SetDoesWindowsFeatureExistResult_Override(ERROR_SUCCESS);
auto setEnableFeatureOverride = TestHook::SetEnableWindowsFeatureResult_Override(ERROR_SUCCESS_REBOOT_REQUIRED);
TestHook::SetRegisterForRestartResult_Override registerForRestartResultOverride(false);
TestHook::SetInitiateRebootResult_Override initiateRebootResultOverride(false);
const auto& testManifestPath = TestDataFile("InstallFlowTest_WindowsFeatures.yaml").GetPath().u8string();
context.Args.AddArg(Execution::Args::Type::Manifest, testManifestPath);
context.Args.AddArg(Execution::Args::Type::AllowReboot);
InstallCommand install({});
install.Execute(context);
INFO(installOutput.str());
REQUIRE(context.GetTerminationHR() == APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_REQUIRED_FOR_INSTALL);
}
TEST_CASE("ResumeFlow_ResumeLimitExceeded", "[Resume]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
TestCommon::TempDirectory tempCheckpointsLocationDir("TempCheckpointsLocationDir", true);
const auto& tempCheckpointRecordDirectoryPath = tempCheckpointsLocationDir.GetPath();
TestHook_SetPathOverride(PathName::CheckpointsLocation, tempCheckpointRecordDirectoryPath);
TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::EFResume>(true);
testSettings.Set<Setting::MaxResumes>(1);
std::filesystem::path testResumeId = L"testResumeId";
std::filesystem::path tempResumeDir = tempCheckpointRecordDirectoryPath / testResumeId;
std::filesystem::path tempCheckpointDatabasePath = tempResumeDir / L"checkpoints.db";
std::filesystem::create_directory(tempResumeDir);
{
std::shared_ptr<CheckpointDatabase> database = CheckpointDatabase::CreateNew(tempCheckpointDatabasePath.u8string(), {1, 0});
std::string_view testCheckpointName = "testCheckpoint"sv;
CheckpointDatabase::IdType checkpointId = database->AddCheckpoint(testCheckpointName);
database->SetDataValue(checkpointId, AutomaticCheckpointData::Command, {}, { "install" });
database->SetDataValue(checkpointId, AutomaticCheckpointData::ClientVersion, {}, { GetClientVersion()});
database->SetDataValue(checkpointId, AutomaticCheckpointData::ResumeCount, {}, {"1"});
}
{
std::ostringstream resumeOutput;
TestContext resumeContext{ resumeOutput, std::cin };
auto previousThreadGlobals = resumeContext.SetForCurrentThread();
resumeContext.Args.AddArg(Execution::Args::Type::ResumeId, testResumeId.u8string());
ResumeCommand resume({});
resume.Execute(resumeContext);
INFO(resumeOutput.str());
REQUIRE(resumeContext.IsTerminated());
REQUIRE(resumeContext.GetTerminationHR() == APPINSTALLER_CLI_ERROR_RESUME_LIMIT_EXCEEDED);
REQUIRE(resumeOutput.str().find(Resource::LocString(Resource::String::ResumeLimitExceeded('1')).get()) != std::string::npos);
REQUIRE(resumeOutput.str().find("winget resume -g " + testResumeId.u8string() + " --ignore-resume-limit") != std::string::npos);
}
}