forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathVariable.cpp
More file actions
159 lines (126 loc) · 5.2 KB
/
Copy pathPathVariable.cpp
File metadata and controls
159 lines (126 loc) · 5.2 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
#include "pch.h"
#include "TestCommon.h"
#include <AppInstallerRuntime.h>
#include <Resources.h>
#include <winget/PathVariable.h>
#include <winget/Filesystem.h>
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Registry::Environment;
TEST_CASE("PathVariable_EnforceReadOnly", "[pathVariable]")
{
auto pathVariable = PathVariable(ScopeEnum::User, true);
REQUIRE_THROWS_HR(pathVariable.Append("testString"), E_ACCESSDENIED);
REQUIRE_THROWS_HR(pathVariable.Remove("testString"), E_ACCESSDENIED);
}
TEST_CASE("PathVariable_Append_NoSemiColon", "[pathVariable]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
auto pathVariable = PathVariable(ScopeEnum::User);
std::filesystem::path testPath{ "testString" };
REQUIRE_FALSE(pathVariable.Contains(testPath));
REQUIRE(pathVariable.Append(testPath));
REQUIRE(pathVariable.Contains(testPath));
// Verify that the path value ends with a ';' and not include ";;"
std::string pathValue = pathVariable.GetPathValue();
REQUIRE(pathValue.back() == ';');
REQUIRE(pathValue.find(";;") == std::string::npos);
REQUIRE(pathVariable.Remove(testPath));
REQUIRE_FALSE(pathVariable.Contains(testPath));
}
TEST_CASE("PathVariable_Append_WithSemicolon", "[pathVariable]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
auto pathVariable = PathVariable(ScopeEnum::User);
std::filesystem::path testPath{ "testString;" };
REQUIRE_FALSE(pathVariable.Contains(testPath));
REQUIRE(pathVariable.Append(testPath));
REQUIRE(pathVariable.Contains(testPath));
// Verify that the path value ends with a ';' and does not include ";;"
std::string pathValue = pathVariable.GetPathValue();
REQUIRE(pathValue.back() == ';');
REQUIRE(pathValue.find(";;") == std::string::npos);
REQUIRE(pathVariable.Remove(testPath));
REQUIRE_FALSE(pathVariable.Contains(testPath));
}
std::wstring GetCurrentProcessPathVariable()
{
size_t requiredSize;
_wgetenv_s(&requiredSize, nullptr, 0, L"PATH");
if (requiredSize > 0)
{
auto buffer = std::make_unique<wchar_t[]>(requiredSize);
errno_t errorResult = _wgetenv_s(&requiredSize, buffer.get(), requiredSize, L"PATH");
if (errorResult == 0)
{
return std::wstring(buffer.get());
}
}
return {};
}
TEST_CASE("RefreshEnvironmentVariable_User", "[pathVariable]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
std::wstring testPathEntry = L"testUserPathEntry";
auto pathVariable = AppInstaller::Registry::Environment::PathVariable(ScopeEnum::User);
pathVariable.Append(testPathEntry);
std::wstring initialPathValue = GetCurrentProcessPathVariable();
bool firstCheck = initialPathValue.find(testPathEntry) != std::string::npos;
AppInstaller::Registry::Environment::RefreshPathVariableForCurrentProcess();
std::wstring updatedPathValue = GetCurrentProcessPathVariable();
bool secondCheck = updatedPathValue.find(testPathEntry) != std::string::npos;
pathVariable.Remove(testPathEntry);
REQUIRE_FALSE(firstCheck);
REQUIRE(secondCheck);
}
TEST_CASE("RefreshEnvironmentVariable_System", "[pathVariable]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
std::wstring testPathEntry = L"testSystemPathEntry";
auto pathVariable = AppInstaller::Registry::Environment::PathVariable(ScopeEnum::Machine);
pathVariable.Append(testPathEntry);
std::wstring initialPathValue = GetCurrentProcessPathVariable();
bool firstCheck = initialPathValue.find(testPathEntry) != std::string::npos;
AppInstaller::Registry::Environment::RefreshPathVariableForCurrentProcess();
std::wstring updatedPathValue = GetCurrentProcessPathVariable();
bool secondCheck = updatedPathValue.find(testPathEntry) != std::string::npos;
pathVariable.Remove(testPathEntry);
REQUIRE_FALSE(firstCheck);
REQUIRE(secondCheck);
}
TEST_CASE("VerifyPathRefreshExpandsValues", "[pathVariable]")
{
if (!AppInstaller::Runtime::IsRunningAsAdmin())
{
WARN("Test requires admin privilege. Skipped.");
return;
}
std::filesystem::path testEntry{ "%USERPROFILE%\\testPath" };
auto pathVariable = AppInstaller::Registry::Environment::PathVariable(ScopeEnum::User);
pathVariable.Append(testEntry);
std::wstring initialPathValue = GetCurrentProcessPathVariable();
bool firstCheck = initialPathValue.find(testEntry) != std::string::npos;
AppInstaller::Registry::Environment::RefreshPathVariableForCurrentProcess();
// %USERPROFILE% should be replaced with the actual path.
std::wstring updatedPathValue = GetCurrentProcessPathVariable();
std::wstring expandedTestPath = AppInstaller::Filesystem::GetExpandedPath(testEntry.u8string());
bool secondCheck = updatedPathValue.find(expandedTestPath) != std::string::npos;
pathVariable.Remove(testEntry);
REQUIRE_FALSE(firstCheck);
REQUIRE(secondCheck);
}