Repository navigation
Expand file tree
/
Copy pathWorkFlow.cpp
More file actions
239 lines (209 loc) · 9.32 KB
/
Copy pathWorkFlow.cpp
File metadata and controls
239 lines (209 loc) · 9.32 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WorkflowCommon.h"
#include "TestSettings.h"
#include <AppInstallerDownloader.h>
#include <winget/Settings.h>
#include <Workflows/DownloadFlow.h>
#include <Commands/InstallCommand.h>
#include <Commands/SettingsCommand.h>
#include <Commands/ValidateCommand.h>
using namespace TestCommon;
using namespace AppInstaller::CLI;
using namespace AppInstaller::CLI::Execution;
using namespace AppInstaller::CLI::Workflow;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Repository;
using namespace AppInstaller::Settings;
using namespace AppInstaller::Utility;
void VerifyMotw(const std::filesystem::path& testFile, DWORD zone)
{
std::filesystem::path motwFile(testFile);
motwFile += ":Zone.Identifier:$data";
std::ifstream motwStream(motwFile);
std::stringstream motwContent;
motwContent << motwStream.rdbuf();
std::string motwContentStr = motwContent.str();
motwStream.close();
REQUIRE(motwContentStr.find("ZoneId=" + std::to_string(zone)) != std::string::npos);
}
TEST_CASE("VerifyInstallerTrustLevelAndUpdateInstallerFileMotw", "[DownloadInstaller][workflow]")
{
TestCommon::TempFile testInstallerPath("TestInstaller.txt");
std::ofstream ofile(testInstallerPath, std::ofstream::out);
ofile << "test";
ofile.close();
ApplyMotwIfApplicable(testInstallerPath, URLZONE_INTERNET);
VerifyMotw(testInstallerPath, 3);
std::ostringstream updateMotwOutput;
TestContext context{ updateMotwOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
auto installerHash = SHA256::ComputeHashFromFile(testInstallerPath);
context.Add<Data::DownloadHashInfo>({ installerHash, DownloadResult{ installerHash } });
context.Add<Data::InstallerPath>(testInstallerPath);
auto packageVersion = std::make_shared<TestPackageVersion>(Manifest{});
auto testSource = std::make_shared<TestSource>();
testSource->Details.TrustLevel = SourceTrustLevel::Trusted;
packageVersion->Source = testSource;
context.Add<Data::PackageVersion>(packageVersion);
ManifestInstaller installer;
installer.Url = "http://NotTrusted.com";
context.Add<Data::Installer>(std::move(installer));
context << VerifyInstallerHash << UpdateInstallerFileMotwIfApplicable;
REQUIRE(WI_IsFlagSet(context.GetFlags(), ContextFlag::InstallerTrusted));
VerifyMotw(testInstallerPath, 2);
testSource->Details.TrustLevel = SourceTrustLevel::None;
context.ClearFlags(ContextFlag::InstallerTrusted);
context << VerifyInstallerHash << UpdateInstallerFileMotwIfApplicable;
REQUIRE_FALSE(WI_IsFlagSet(context.GetFlags(), ContextFlag::InstallerTrusted));
VerifyMotw(testInstallerPath, 3);
INFO(updateMotwOutput.str());
}
TEST_CASE("VerifyInstallerHash_RejectsMissingOrMismatchedHash", "[DownloadInstaller][workflow]")
{
auto expectedHash = SHA256::ComputeHash("test"sv);
DownloadResult downloadResult{ expectedHash };
downloadResult.SizeInBytes = 4;
SECTION("Missing expected hash")
{
expectedHash.clear();
}
SECTION("Truncated expected hash")
{
expectedHash.pop_back();
}
SECTION("Overlong expected hash")
{
expectedHash.push_back(0);
}
SECTION("Mismatched hash")
{
expectedHash[0] ^= 0xFF;
}
SECTION("Missing downloaded hash")
{
downloadResult.Sha256Hash.clear();
}
SECTION("Truncated downloaded hash")
{
downloadResult.Sha256Hash.pop_back();
}
SECTION("Both hashes missing")
{
expectedHash.clear();
downloadResult.Sha256Hash.clear();
}
std::ostringstream output;
TestContext context{ output, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Add<Data::Manifest>(Manifest{});
context.Add<Data::DownloadHashInfo>({ expectedHash, downloadResult });
context << VerifyInstallerHash;
REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_INSTALLER_HASH_MISMATCH);
REQUIRE_FALSE(WI_IsFlagSet(context.GetFlags(), ContextFlag::InstallerHashMatched));
REQUIRE_FALSE(WI_IsFlagSet(context.GetFlags(), ContextFlag::InstallerTrusted));
}
TEST_CASE("ValidateCommand_Dependencies", "[workflow][dependencies]")
{
std::ostringstream validateOutput;
TestContext context{ validateOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Args::Type::ValidateManifest, TestDataFile("Manifest-Good-AllDependencyTypes.yaml").GetPath().u8string());
ValidateCommand validate({});
validate.Execute(context);
INFO(validateOutput.str());
// Verify all types of dependencies are printed
REQUIRE(validateOutput.str().find(Resource::LocString(Resource::String::ValidateCommandReportDependencies).get()) != std::string::npos);
REQUIRE(validateOutput.str().find("WindowsFeaturesDep") != std::string::npos);
REQUIRE(validateOutput.str().find("WindowsLibrariesDep") != std::string::npos);
// PackageDep1 has minimum version (1.0), PackageDep2 doesn't (shouldn't show [>=...])
REQUIRE(validateOutput.str().find("Package.Dep1-x64 [>= 1.0]") != std::string::npos);
REQUIRE(validateOutput.str().find("Package.Dep2-x64") != std::string::npos);
REQUIRE(validateOutput.str().find("Package.Dep2-x64 [") == std::string::npos);
REQUIRE(validateOutput.str().find("ExternalDep") != std::string::npos);
}
TEST_CASE("AdminSetting_LocalManifestFiles", "[LocalManifests][workflow]")
{
RemoveSetting(Stream::AdminSettings);
{
// If there's no admin setting, using local manifest should fail.
Execution::Args args;
args.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
InstallCommand installCommand({});
REQUIRE_THROWS(installCommand.ValidateArguments(args));
}
{
// Using settings command to enable local manifests
std::ostringstream settingsOutput;
TestContext context{ settingsOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::AdminSettingEnable, "LocalManifestFiles"sv);
context.Override({ EnsureRunningAsAdmin, [](TestContext&) {} });
SettingsCommand settings({});
settings.Execute(context);
INFO(settingsOutput.str());
}
{
// Now using local manifests should succeed
Execution::Args args2;
args2.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
InstallCommand installCommand2({});
REQUIRE_NOTHROW(installCommand2.ValidateArguments(args2));
}
{
// Using settings command to disable local manifests
std::ostringstream settingsOutput2;
TestContext context2{ settingsOutput2, std::cin };
auto previousThreadGlobals = context2.SetForCurrentThread();
context2.Args.AddArg(Execution::Args::Type::AdminSettingDisable, "LocalManifestFiles"sv);
context2.Override({ EnsureRunningAsAdmin, [](TestContext&) {} });
SettingsCommand settings2({});
settings2.Execute(context2);
INFO(settingsOutput2.str());
}
{
// Now using local manifests should fail
Execution::Args args3;
args3.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
InstallCommand installCommand3({});
REQUIRE_THROWS(installCommand3.ValidateArguments(args3));
}
}
TEST_CASE("Export_Settings", "[Settings][workflow]")
{
RemoveSetting(Stream::AdminSettings);
{
// No admin settings, local manifest should be false.
std::ostringstream exportOutput;
TestContext context{ exportOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
SettingsExportCommand settingsExportCommand({});
settingsExportCommand.Execute(context);
auto json = ConvertToJson(exportOutput.str());
REQUIRE(!json.isNull());
REQUIRE_FALSE(json["adminSettings"]["LocalManifestFiles"].asBool());
auto userSettingsFileValue = std::string(json["userSettingsFile"].asCString());
REQUIRE(userSettingsFileValue.find("settings.json") != std::string::npos);
}
{
// Enable local manifest and verify export works.
std::ostringstream settingsOutput;
TestContext context{ settingsOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::AdminSettingEnable, "LocalManifestFiles"sv);
context.Override({ EnsureRunningAsAdmin, [](TestContext&) {} });
SettingsCommand settings({});
settings.Execute(context);
std::ostringstream exportOutput;
TestContext context2{ exportOutput, std::cin };
auto previousThreadGlobals2 = context2.SetForCurrentThread();
SettingsExportCommand settingsExportCommand({});
settingsExportCommand.Execute(context2);
auto json = ConvertToJson(exportOutput.str());
REQUIRE(!json.isNull());
REQUIRE(json["adminSettings"]["LocalManifestFiles"].asBool());
auto userSettingsFileValue = std::string(json["userSettingsFile"].asCString());
REQUIRE(userSettingsFileValue.find("settings.json") != std::string::npos);
}
}