main
cpp 252 lines 6.53 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 #include "precomp.h"
4
5 #define TEST_REGISTRY_FIXTURE_ROOT_PATH L"SOFTWARE\\WiX_Burn_UnitTest"
6 #define TEST_REGISTRY_FIXTURE_HKLM_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKLM"
7 #define TEST_REGISTRY_FIXTURE_HKLM32_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\Wow6432Node\\HKLM"
8 #define TEST_REGISTRY_FIXTURE_HKCU_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKCU"
9 #define TEST_REGISTRY_FIXTURE_HKCU32_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\Wow6432Node\\HKCU"
10
11 static REG_KEY_BITNESS GetDesiredBitness(
12 __in REGSAM samDesired
13 )
14 {
15 REG_KEY_BITNESS desiredBitness = REG_KEY_DEFAULT;
16
17 switch (KEY_WOW64_RES & samDesired)
18 {
19 case KEY_WOW64_32KEY:
20 desiredBitness = REG_KEY_32BIT;
21 break;
22 case KEY_WOW64_64KEY:
23 desiredBitness = REG_KEY_64BIT;
24 break;
25 default:
26 #if defined(_WIN64)
27 desiredBitness = REG_KEY_64BIT;
28 #else
29 desiredBitness = REG_KEY_32BIT;
30 #endif
31 break;
32 }
33
34 return desiredBitness;
35 }
36
37 static LSTATUS GetRootKey(
38 __in HKEY hKey,
39 __in REGSAM samDesired,
40 __in ACCESS_MASK accessDesired,
41 __inout HKEY* phkRoot)
42 {
43 LSTATUS ls = ERROR_SUCCESS;
44 LPCWSTR wzRoot = NULL;
45
46 if (HKEY_LOCAL_MACHINE == hKey)
47 {
48 if (REG_KEY_32BIT == GetDesiredBitness(samDesired))
49 {
50 wzRoot = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
51 }
52 else
53 {
54 wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH;
55 }
56 }
57 else if (HKEY_CURRENT_USER == hKey)
58 {
59 if (REG_KEY_32BIT == GetDesiredBitness(samDesired))
60 {
61 wzRoot = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
62 }
63 else
64 {
65 wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH;
66 }
67 }
68
69 if (wzRoot)
70 {
71 ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE | accessDesired, phkRoot);
72 }
73 else
74 {
75 *phkRoot = hKey;
76 }
77
78 return ls;
79 }
80
81 static LSTATUS APIENTRY TestRegistryFixture_RegCreateKeyExW(
82 __in HKEY hKey,
83 __in LPCWSTR lpSubKey,
84 __reserved DWORD Reserved,
85 __in_opt LPWSTR lpClass,
86 __in DWORD dwOptions,
87 __in REGSAM samDesired,
88 __in_opt CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes,
89 __out PHKEY phkResult,
90 __out_opt LPDWORD lpdwDisposition
91 )
92 {
93 LSTATUS ls = ERROR_SUCCESS;
94 HKEY hkRoot = NULL;
95
96 ls = GetRootKey(hKey, samDesired, 0, &hkRoot);
97 if (ERROR_SUCCESS != ls)
98 {
99 ExitFunction();
100 }
101
102 ls = ::RegCreateKeyExW(hkRoot, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
103
104 LExit:
105 if (hkRoot != hKey)
106 {
107 ReleaseRegKey(hkRoot);
108 }
109
110 return ls;
111 }
112
113 static LSTATUS APIENTRY TestRegistryFixture_RegOpenKeyExW(
114 __in HKEY hKey,
115 __in_opt LPCWSTR lpSubKey,
116 __reserved DWORD ulOptions,
117 __in REGSAM samDesired,
118 __out PHKEY phkResult
119 )
120 {
121 LSTATUS ls = ERROR_SUCCESS;
122 HKEY hkRoot = NULL;
123
124 ls = GetRootKey(hKey, samDesired, 0, &hkRoot);
125 if (ERROR_SUCCESS != ls)
126 {
127 ExitFunction();
128 }
129
130 ls = ::RegOpenKeyExW(hkRoot, lpSubKey, ulOptions, samDesired, phkResult);
131
132 LExit:
133 if (hkRoot != hKey)
134 {
135 ReleaseRegKey(hkRoot);
136 }
137
138 return ls;
139 }
140
141 static LSTATUS APIENTRY TestRegistryFixture_RegDeleteKeyExW(
142 __in HKEY hKey,
143 __in LPCWSTR lpSubKey,
144 __in REGSAM samDesired,
145 __reserved DWORD Reserved
146 )
147 {
148 LSTATUS ls = ERROR_SUCCESS;
149 HKEY hkRoot = NULL;
150
151 ls = GetRootKey(hKey, samDesired, samDesired, &hkRoot);
152 if (ERROR_SUCCESS != ls)
153 {
154 ExitFunction();
155 }
156
157 ls = ::RegDeleteKeyExW(hkRoot, lpSubKey, samDesired, Reserved);
158
159 LExit:
160 if (hkRoot != hKey)
161 {
162 ReleaseRegKey(hkRoot);
163 }
164
165 return ls;
166 }
167
168 namespace WixInternal
169 {
170 namespace TestSupport
171 {
172 using namespace System;
173 using namespace System::IO;
174 using namespace Microsoft::Win32;
175
176 TestRegistryFixture::TestRegistryFixture()
177 {
178 this->rootPath = gcnew String(TEST_REGISTRY_FIXTURE_ROOT_PATH);
179 }
180
181 TestRegistryFixture::~TestRegistryFixture()
182 {
183 this->TearDown();
184 }
185
186 void TestRegistryFixture::SetUp()
187 {
188 // set mock API's
189 RegFunctionOverride(TestRegistryFixture_RegCreateKeyExW, TestRegistryFixture_RegOpenKeyExW, TestRegistryFixture_RegDeleteKeyExW, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
190
191 Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKCU_PATH);
192 Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKCU32_PATH);
193 Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKLM_PATH);
194 Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKLM32_PATH);
195 }
196
197 void TestRegistryFixture::TearDown()
198 {
199 Registry::CurrentUser->DeleteSubKeyTree(this->rootPath, false);
200
201 RegFunctionOverride(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
202 }
203
204 String^ TestRegistryFixture::GetDirectHkcuPath(REG_KEY_BITNESS bitness, ... array<String^>^ paths)
205 {
206 String^ hkcuPath;
207
208 switch (bitness)
209 {
210 case REG_KEY_32BIT:
211 hkcuPath = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
212 break;
213 case REG_KEY_64BIT:
214 hkcuPath = TEST_REGISTRY_FIXTURE_HKCU_PATH;
215 break;
216 default:
217 #if defined(_WIN64)
218 hkcuPath = TEST_REGISTRY_FIXTURE_HKCU_PATH;
219 #else
220 hkcuPath = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
221 #endif
222 break;
223 }
224
225 return Path::Combine(Registry::CurrentUser->Name, hkcuPath, Path::Combine(paths));
226 }
227
228 String^ TestRegistryFixture::GetDirectHklmPath(REG_KEY_BITNESS bitness, ... array<String^>^ paths)
229 {
230 String^ hklmPath;
231
232 switch (bitness)
233 {
234 case REG_KEY_32BIT:
235 hklmPath = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
236 break;
237 case REG_KEY_64BIT:
238 hklmPath = TEST_REGISTRY_FIXTURE_HKLM_PATH;
239 break;
240 default:
241 #if defined(_WIN64)
242 hklmPath = TEST_REGISTRY_FIXTURE_HKLM_PATH;
243 #else
244 hklmPath = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
245 #endif
246 break;
247 }
248
249 return Path::Combine(Registry::CurrentUser->Name, hklmPath, Path::Combine(paths));
250 }
251 }
252 }