summaryrefslogtreecommitdiff
path: root/src/burn/test/BurnUnitTest/TestRegistryFixture.cpp
blob: fe82c4afb5a27a0a31c36d1daf9ec2fdf5a7a40b (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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.

#include "precomp.h"

#define TEST_REGISTRY_FIXTURE_ROOT_PATH L"SOFTWARE\\WiX_Burn_UnitTest"
#define TEST_REGISTRY_FIXTURE_HKLM_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKLM"
#define TEST_REGISTRY_FIXTURE_HKLM32_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\Wow6432Node\\HKLM"
#define TEST_REGISTRY_FIXTURE_HKCU_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKCU"
#define TEST_REGISTRY_FIXTURE_HKCU32_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\Wow6432Node\\HKCU"

static REG_KEY_BITNESS GetDesiredBitness(
    __in REGSAM samDesired
    )
{
    REG_KEY_BITNESS desiredBitness = REG_KEY_DEFAULT;

    switch (KEY_WOW64_RES & samDesired)
    {
    case KEY_WOW64_32KEY:
        desiredBitness = REG_KEY_32BIT;
        break;
    case KEY_WOW64_64KEY:
        desiredBitness = REG_KEY_64BIT;
        break;
    default:
#if defined(_WIN64)
        desiredBitness = REG_KEY_64BIT;
#else
        desiredBitness = REG_KEY_32BIT;
#endif
        break;
    }

    return desiredBitness;
}

static LSTATUS GetRootKey(
    __in HKEY hKey,
    __in REGSAM samDesired,
    __in ACCESS_MASK accessDesired,
    __inout HKEY* phkRoot)
{
    LSTATUS ls = ERROR_SUCCESS;
    LPCWSTR wzRoot = NULL;

    if (HKEY_LOCAL_MACHINE == hKey)
    {
        if (REG_KEY_32BIT == GetDesiredBitness(samDesired))
        {
            wzRoot = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
        }
        else
        {
            wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH;
        }
    }
    else if (HKEY_CURRENT_USER == hKey)
    {
        if (REG_KEY_32BIT == GetDesiredBitness(samDesired))
        {
            wzRoot = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
        }
        else
        {
            wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH;
        }
    }

    if (wzRoot)
    {
        ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE | accessDesired, phkRoot);
    }
    else
    {
        *phkRoot = hKey;
    }

    return ls;
}

static LSTATUS APIENTRY TestRegistryFixture_RegCreateKeyExW(
    __in HKEY hKey,
    __in LPCWSTR lpSubKey,
    __reserved DWORD Reserved,
    __in_opt LPWSTR lpClass,
    __in DWORD dwOptions,
    __in REGSAM samDesired,
    __in_opt CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    __out PHKEY phkResult,
    __out_opt LPDWORD lpdwDisposition
    )
{
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hkRoot = NULL;

    ls = GetRootKey(hKey, samDesired, 0, &hkRoot);
    if (ERROR_SUCCESS != ls)
    {
        ExitFunction();
    }

    ls = ::RegCreateKeyExW(hkRoot, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);

LExit:
    if (hkRoot != hKey)
    {
        ReleaseRegKey(hkRoot);
    }

    return ls;
}

static LSTATUS APIENTRY TestRegistryFixture_RegOpenKeyExW(
    __in HKEY hKey,
    __in_opt LPCWSTR lpSubKey,
    __reserved DWORD ulOptions,
    __in REGSAM samDesired,
    __out PHKEY phkResult
    )
{
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hkRoot = NULL;

    ls = GetRootKey(hKey, samDesired, 0, &hkRoot);
    if (ERROR_SUCCESS != ls)
    {
        ExitFunction();
    }

    ls = ::RegOpenKeyExW(hkRoot, lpSubKey, ulOptions, samDesired, phkResult);

LExit:
    if (hkRoot != hKey)
    {
        ReleaseRegKey(hkRoot);
    }

    return ls;
}

static LSTATUS APIENTRY TestRegistryFixture_RegDeleteKeyExW(
    __in HKEY hKey,
    __in LPCWSTR lpSubKey,
    __in REGSAM samDesired,
    __reserved DWORD Reserved
    )
{
    LSTATUS ls = ERROR_SUCCESS;
    HKEY hkRoot = NULL;

    ls = GetRootKey(hKey, samDesired, samDesired, &hkRoot);
    if (ERROR_SUCCESS != ls)
    {
        ExitFunction();
    }

    ls = ::RegDeleteKeyExW(hkRoot, lpSubKey, samDesired, Reserved);

LExit:
    if (hkRoot != hKey)
    {
        ReleaseRegKey(hkRoot);
    }

    return ls;
}

namespace WixInternal
{
    namespace TestSupport
    {
        using namespace System;
        using namespace System::IO;
        using namespace Microsoft::Win32;

        TestRegistryFixture::TestRegistryFixture()
        {
            this->rootPath = gcnew String(TEST_REGISTRY_FIXTURE_ROOT_PATH);
        }

        TestRegistryFixture::~TestRegistryFixture()
        {
            this->TearDown();
        }

        void TestRegistryFixture::SetUp()
        {
            // set mock API's
            RegFunctionOverride(TestRegistryFixture_RegCreateKeyExW, TestRegistryFixture_RegOpenKeyExW, TestRegistryFixture_RegDeleteKeyExW, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

            Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKCU_PATH);
            Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKCU32_PATH);
            Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKLM_PATH);
            Registry::CurrentUser->CreateSubKey(TEST_REGISTRY_FIXTURE_HKLM32_PATH);
        }

        void TestRegistryFixture::TearDown()
        {
            Registry::CurrentUser->DeleteSubKeyTree(this->rootPath, false);

            RegFunctionOverride(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
        }

        String^ TestRegistryFixture::GetDirectHkcuPath(REG_KEY_BITNESS bitness, ... array<String^>^ paths)
        {
            String^ hkcuPath;

            switch (bitness)
            {
            case REG_KEY_32BIT:
                hkcuPath = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
                break;
            case REG_KEY_64BIT:
                hkcuPath = TEST_REGISTRY_FIXTURE_HKCU_PATH;
                break;
            default:
#if defined(_WIN64)
                hkcuPath = TEST_REGISTRY_FIXTURE_HKCU_PATH;
#else
                hkcuPath = TEST_REGISTRY_FIXTURE_HKCU32_PATH;
#endif
                break;
            }

            return Path::Combine(Registry::CurrentUser->Name, hkcuPath, Path::Combine(paths));
        }

        String^ TestRegistryFixture::GetDirectHklmPath(REG_KEY_BITNESS bitness, ... array<String^>^ paths)
        {
            String^ hklmPath;

            switch (bitness)
            {
            case REG_KEY_32BIT:
                hklmPath = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
                break;
            case REG_KEY_64BIT:
                hklmPath = TEST_REGISTRY_FIXTURE_HKLM_PATH;
                break;
            default:
#if defined(_WIN64)
                hklmPath = TEST_REGISTRY_FIXTURE_HKLM_PATH;
#else
                hklmPath = TEST_REGISTRY_FIXTURE_HKLM32_PATH;
#endif
                break;
            }

            return Path::Combine(Registry::CurrentUser->Name, hklmPath, Path::Combine(paths));
        }
    }
}