forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.cpp
More file actions
144 lines (119 loc) · 4.41 KB
/
Copy pathRuntime.cpp
File metadata and controls
144 lines (119 loc) · 4.41 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "TestHooks.h"
#include <AppInstallerRuntime.h>
#include <winget/Filesystem.h>
using namespace AppInstaller;
using namespace AppInstaller::Filesystem;
using namespace AppInstaller::Runtime;
using namespace TestCommon;
bool CanWriteToPath(const std::filesystem::path& directory, const std::filesystem::path& file = "test.txt")
{
std::ofstream out{ directory / file };
out << "Test";
return out.good();
}
void RequireAdminOwner(const std::filesystem::path& directory)
{
wil::unique_hlocal_security_descriptor securityDescriptor;
PSID ownerSID = nullptr;
THROW_IF_WIN32_ERROR(GetNamedSecurityInfoW(directory.c_str(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &ownerSID, nullptr, nullptr, nullptr, &securityDescriptor));
auto adminSID = wil::make_static_sid(SECURITY_NT_AUTHORITY, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
REQUIRE(EqualSid(adminSID.get(), ownerSID));
}
TEST_CASE("ApplyACL_CurrentUserOwner", "[runtime]")
{
TempDirectory directory("CurrentUserOwner");
PathDetails details;
details.Path = directory;
details.SetOwner(ACEPrincipal::CurrentUser);
details.ApplyACL();
REQUIRE(CanWriteToPath(directory));
}
TEST_CASE("ApplyACL_RemoveWriteForUser", "[runtime]")
{
TempDirectory directory("CurrentUserCantWrite");
PathDetails details;
details.Path = directory;
details.ACL[ACEPrincipal::CurrentUser] = ACEPermissions::ReadExecute;
details.ApplyACL();
REQUIRE(!CanWriteToPath(directory));
}
TEST_CASE("ApplyACL_AdminOwner", "[runtime]")
{
TempDirectory directory("AdminOwner");
PathDetails details;
details.Path = directory;
details.SetOwner(ACEPrincipal::Admins);
if (IsRunningAsAdmin())
{
details.ApplyACL();
RequireAdminOwner(directory);
REQUIRE(CanWriteToPath(directory));
}
else
{
// A non-admin token cannot set the owner to be the Admins group
REQUIRE_THROWS_HR(details.ApplyACL(), HRESULT_FROM_WIN32(ERROR_INVALID_OWNER));
}
}
TEST_CASE("ApplyACL_BothOwners", "[runtime]")
{
TempDirectory directory("AdminOwner");
PathDetails details;
details.Path = directory;
details.ACL[ACEPrincipal::CurrentUser] = ACEPermissions::ReadExecute;
details.ACL[ACEPrincipal::System] = ACEPermissions::All;
if (IsRunningAsSystem())
{
// Both cannot be owners
REQUIRE_THROWS_HR(details.ApplyACL(), HRESULT_FROM_WIN32(ERROR_INVALID_STATE));
}
else
{
REQUIRE_NOTHROW(details.ApplyACL());
}
}
TEST_CASE("ApplyACL_CurrentUserOwner_SystemAll", "[runtime]")
{
TempDirectory directory("UserAndSystem");
PathDetails details;
details.Path = directory;
details.SetOwner(ACEPrincipal::CurrentUser);
details.ACL[ACEPrincipal::System] = ACEPermissions::All;
details.ApplyACL();
REQUIRE(CanWriteToPath(directory));
}
TEST_CASE("VerifyDevModeEnabledCheck", "[runtime]")
{
if (!Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
bool initialState = IsDevModeEnabled();
EnableDevMode(!initialState);
bool modifiedState = IsDevModeEnabled();
// Revert to original state.
EnableDevMode(initialState);
bool revertedState = IsDevModeEnabled();
REQUIRE(modifiedState != initialState);
REQUIRE(revertedState == initialState);
}
TEST_CASE("EnsureUserProfileNotPresentInDisplayPaths", "[runtime]")
{
// Clear the overrides that we use when testing as they don't consider display purposes
Runtime::TestHook_ClearPathOverrides();
auto restorePaths = wil::scope_exit([]() { TestCommon::SetTestPathOverrides(); });
std::filesystem::path userProfilePath = Filesystem::GetKnownFolderPath(FOLDERID_Profile);
std::string userProfileString = userProfilePath.u8string();
for (auto i = ToIntegral(ToEnum<Runtime::PathName>(0)); i < ToIntegral(Runtime::PathName::Max); ++i)
{
std::filesystem::path displayPath = GetPathTo(ToEnum<Runtime::PathName>(i), true);
std::string displayPathString = displayPath.u8string();
INFO(i << " = " << displayPathString);
REQUIRE(displayPathString.find(userProfileString) == std::string::npos);
}
}