forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.cpp
More file actions
171 lines (133 loc) · 4.89 KB
/
Copy pathRegistry.cpp
File metadata and controls
171 lines (133 loc) · 4.89 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <AppInstallerStrings.h>
#include <winget/Registry.h>
using namespace std::string_literals;
using namespace std::string_view_literals;
using namespace AppInstaller::Registry;
using namespace AppInstaller::Utility;
using namespace TestCommon;
TEST_CASE("EmptyKey", "[registry]")
{
Key key;
REQUIRE(!key);
}
TEST_CASE("Constructor_NotFound", "[registry]")
{
Key key;
REQUIRE_THROWS_HR(key = Key(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Foo\\Bar\\Does\\Not\\Exist"), HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
}
TEST_CASE("OpenIfExists_NotFound", "[registry]")
{
Key key = Key::OpenIfExists(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Foo\\Bar\\Does\\Not\\Exist");
REQUIRE(!key);
}
TEST_CASE("EnumerateKeys", "[registry]")
{
wil::unique_hkey root = RegCreateVolatileTestRoot();
std::vector<std::wstring> subKeyNames = { L"A", L"BEE", L"SEE", L"deigh" };
for (const auto& name : subKeyNames)
{
RegCreateVolatileSubKey(root.get(), name);
}
Key key{ root.get(), L"" };
for (const auto& subkey : key)
{
INFO(subkey.Name());
std::wstring nameUtf16 = ConvertToUTF16(subkey.Name());
auto itr = std::find(subKeyNames.begin(), subKeyNames.end(), nameUtf16);
if (itr == subKeyNames.end())
{
FAIL();
}
else
{
subKeyNames.erase(itr);
}
Key sk = subkey.Open();
REQUIRE(sk);
}
REQUIRE(subKeyNames.empty());
}
TEST_CASE("Values_String", "[registry]")
{
std::wstring valueName = L"TestValueName";
std::wstring valueValue = L"TestValueValue";
wil::unique_hkey root = RegCreateVolatileTestRoot();
SetRegistryValue(root.get(), valueName, valueValue);
Key key{ root.get(), L"" };
auto value = key[valueName];
REQUIRE(value);
REQUIRE(value->GetType() == Value::Type::String);
REQUIRE(value->GetValue<Value::Type::String>() == ConvertToUTF8(valueValue));
}
TEST_CASE("Values_WideStringWithNarrowNull", "[registry]")
{
std::wstring valueName = L"TestValueName";
std::wstring valueValue = L"TestValueValue";
wil::unique_hkey root = RegCreateVolatileTestRoot();
// Copy the bytes from the string value into a byte vector
std::vector<BYTE> valueBytes;
valueBytes.resize((valueValue.length() + 1) * sizeof(wchar_t));
memcpy_s(valueBytes.data(), valueBytes.size(), valueValue.c_str(), (valueValue.length() + 1) * sizeof(wchar_t));
// Remove the last byte to make a narrow null
valueBytes.resize(valueBytes.size() - 1);
SetRegistryValue(root.get(), valueName, valueBytes, REG_SZ);
Key key{ root.get(), L"" };
auto value = key[valueName];
REQUIRE(value);
REQUIRE(value->GetType() == Value::Type::String);
REQUIRE(value->GetValue<Value::Type::String>() == ConvertToUTF8(valueValue));
}
TEST_CASE("Values_ExpandString", "[registry]")
{
std::wstring valueName = L"TestValueName";
std::wstring valueValue = L"%TEMP%";
wil::unique_hkey root = RegCreateVolatileTestRoot();
SetRegistryValue(root.get(), valueName, valueValue, REG_EXPAND_SZ);
Key key{ root.get(), L"" };
auto value = key[valueName];
REQUIRE(value);
REQUIRE(value->GetType() == Value::Type::ExpandString);
REQUIRE(value->GetValue<Value::Type::String>() == ConvertToUTF8(valueValue));
wchar_t buffer[MAX_PATH];
GetTempPathW(ARRAYSIZE(buffer), buffer);
std::string tempPath = ConvertToUTF8(buffer);
if (!tempPath.empty() && tempPath.back() == '\\')
{
tempPath.resize(tempPath.size() - 1);
}
REQUIRE(value->GetValue<Value::Type::ExpandString>() == tempPath);
}
TEST_CASE("Values_Binary", "[registry]")
{
std::wstring valueName = L"TestValueName";
std::vector<BYTE> valueValue = { 2, 7, 3, 14, 42 };
wil::unique_hkey root = RegCreateVolatileTestRoot();
SetRegistryValue(root.get(), valueName, valueValue);
Key key{ root.get(), L"" };
auto value = key[valueName];
REQUIRE(value);
REQUIRE(value->GetType() == Value::Type::Binary);
auto result = value->GetValue<Value::Type::Binary>();
REQUIRE(result.size() == valueValue.size());
for (size_t i = 0; i < result.size(); ++i)
{
INFO(i);
REQUIRE(result[i] == valueValue[i]);
}
}
TEST_CASE("Values_DWORD", "[registry]")
{
std::wstring valueName = L"TestValueName";
DWORD valueValue = 42;
wil::unique_hkey root = RegCreateVolatileTestRoot();
SetRegistryValue(root.get(), valueName, valueValue);
Key key{ root.get(), L"" };
auto value = key[valueName];
REQUIRE(value);
REQUIRE(value->GetType() == Value::Type::DWord);
REQUIRE(value->GetValue<Value::Type::DWord>() == valueValue);
}