diff options
| author | Sean Hall <r.sean.hall@gmail.com> | 2022-02-28 18:38:46 -0600 |
|---|---|---|
| committer | Sean Hall <r.sean.hall@gmail.com> | 2022-03-01 11:37:00 -0600 |
| commit | 7e61f48f6be0849cb0c0da796ec77603c14532e5 (patch) | |
| tree | 154019fc14c82f62a2480023c92775ccf2995a76 /src/burn/test/BurnUnitTest/TestRegistryFixture.cpp | |
| parent | 815b407c1f370dd71efc21248b49e63519a57cb4 (diff) | |
| download | wix-7e61f48f6be0849cb0c0da796ec77603c14532e5.tar.gz wix-7e61f48f6be0849cb0c0da796ec77603c14532e5.tar.bz2 wix-7e61f48f6be0849cb0c0da796ec77603c14532e5.zip | |
Factor out TestRegistryFixture so other tests can mock regutil API's.
Diffstat (limited to 'src/burn/test/BurnUnitTest/TestRegistryFixture.cpp')
| -rw-r--r-- | src/burn/test/BurnUnitTest/TestRegistryFixture.cpp | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/src/burn/test/BurnUnitTest/TestRegistryFixture.cpp b/src/burn/test/BurnUnitTest/TestRegistryFixture.cpp new file mode 100644 index 00000000..3e78c6f5 --- /dev/null +++ b/src/burn/test/BurnUnitTest/TestRegistryFixture.cpp | |||
| @@ -0,0 +1,184 @@ | |||
| 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_HKCU_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKCU" | ||
| 8 | |||
| 9 | static LSTATUS APIENTRY TestRegistryFixture_RegCreateKeyExW( | ||
| 10 | __in HKEY hKey, | ||
| 11 | __in LPCWSTR lpSubKey, | ||
| 12 | __reserved DWORD Reserved, | ||
| 13 | __in_opt LPWSTR lpClass, | ||
| 14 | __in DWORD dwOptions, | ||
| 15 | __in REGSAM samDesired, | ||
| 16 | __in_opt CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, | ||
| 17 | __out PHKEY phkResult, | ||
| 18 | __out_opt LPDWORD lpdwDisposition | ||
| 19 | ) | ||
| 20 | { | ||
| 21 | LSTATUS ls = ERROR_SUCCESS; | ||
| 22 | LPCWSTR wzRoot = NULL; | ||
| 23 | HKEY hkRoot = NULL; | ||
| 24 | |||
| 25 | if (HKEY_LOCAL_MACHINE == hKey) | ||
| 26 | { | ||
| 27 | wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; | ||
| 28 | } | ||
| 29 | else if (HKEY_CURRENT_USER == hKey) | ||
| 30 | { | ||
| 31 | wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 | hkRoot = hKey; | ||
| 36 | } | ||
| 37 | |||
| 38 | if (wzRoot) | ||
| 39 | { | ||
| 40 | ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE, &hkRoot); | ||
| 41 | if (ERROR_SUCCESS != ls) | ||
| 42 | { | ||
| 43 | ExitFunction(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | ls = ::RegCreateKeyExW(hkRoot, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition); | ||
| 48 | |||
| 49 | LExit: | ||
| 50 | ReleaseRegKey(hkRoot); | ||
| 51 | |||
| 52 | return ls; | ||
| 53 | } | ||
| 54 | |||
| 55 | static LSTATUS APIENTRY TestRegistryFixture_RegOpenKeyExW( | ||
| 56 | __in HKEY hKey, | ||
| 57 | __in_opt LPCWSTR lpSubKey, | ||
| 58 | __reserved DWORD ulOptions, | ||
| 59 | __in REGSAM samDesired, | ||
| 60 | __out PHKEY phkResult | ||
| 61 | ) | ||
| 62 | { | ||
| 63 | LSTATUS ls = ERROR_SUCCESS; | ||
| 64 | LPCWSTR wzRoot = NULL; | ||
| 65 | HKEY hkRoot = NULL; | ||
| 66 | |||
| 67 | if (HKEY_LOCAL_MACHINE == hKey) | ||
| 68 | { | ||
| 69 | wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; | ||
| 70 | } | ||
| 71 | else if (HKEY_CURRENT_USER == hKey) | ||
| 72 | { | ||
| 73 | wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; | ||
| 74 | } | ||
| 75 | else | ||
| 76 | { | ||
| 77 | hkRoot = hKey; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (wzRoot) | ||
| 81 | { | ||
| 82 | ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE, &hkRoot); | ||
| 83 | if (ERROR_SUCCESS != ls) | ||
| 84 | { | ||
| 85 | ExitFunction(); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | ls = ::RegOpenKeyExW(hkRoot, lpSubKey, ulOptions, samDesired, phkResult); | ||
| 90 | |||
| 91 | LExit: | ||
| 92 | ReleaseRegKey(hkRoot); | ||
| 93 | |||
| 94 | return ls; | ||
| 95 | } | ||
| 96 | |||
| 97 | static LSTATUS APIENTRY TestRegistryFixture_RegDeleteKeyExW( | ||
| 98 | __in HKEY hKey, | ||
| 99 | __in LPCWSTR lpSubKey, | ||
| 100 | __in REGSAM samDesired, | ||
| 101 | __reserved DWORD Reserved | ||
| 102 | ) | ||
| 103 | { | ||
| 104 | LSTATUS ls = ERROR_SUCCESS; | ||
| 105 | LPCWSTR wzRoot = NULL; | ||
| 106 | HKEY hkRoot = NULL; | ||
| 107 | |||
| 108 | if (HKEY_LOCAL_MACHINE == hKey) | ||
| 109 | { | ||
| 110 | wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; | ||
| 111 | } | ||
| 112 | else if (HKEY_CURRENT_USER == hKey) | ||
| 113 | { | ||
| 114 | wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; | ||
| 115 | } | ||
| 116 | else | ||
| 117 | { | ||
| 118 | hkRoot = hKey; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (wzRoot) | ||
| 122 | { | ||
| 123 | ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE | samDesired, &hkRoot); | ||
| 124 | if (ERROR_SUCCESS != ls) | ||
| 125 | { | ||
| 126 | ExitFunction(); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | ls = ::RegDeleteKeyExW(hkRoot, lpSubKey, samDesired, Reserved); | ||
| 131 | |||
| 132 | LExit: | ||
| 133 | ReleaseRegKey(hkRoot); | ||
| 134 | |||
| 135 | return ls; | ||
| 136 | } | ||
| 137 | |||
| 138 | namespace WixBuildTools | ||
| 139 | { | ||
| 140 | namespace TestSupport | ||
| 141 | { | ||
| 142 | using namespace System; | ||
| 143 | using namespace System::IO; | ||
| 144 | using namespace Microsoft::Win32; | ||
| 145 | |||
| 146 | TestRegistryFixture::TestRegistryFixture() | ||
| 147 | { | ||
| 148 | this->rootPath = gcnew String(TEST_REGISTRY_FIXTURE_ROOT_PATH); | ||
| 149 | this->hkcuPath = gcnew String(TEST_REGISTRY_FIXTURE_HKCU_PATH); | ||
| 150 | this->hklmPath = gcnew String(TEST_REGISTRY_FIXTURE_HKLM_PATH); | ||
| 151 | } | ||
| 152 | |||
| 153 | TestRegistryFixture::~TestRegistryFixture() | ||
| 154 | { | ||
| 155 | this->TearDown(); | ||
| 156 | } | ||
| 157 | |||
| 158 | void TestRegistryFixture::SetUp() | ||
| 159 | { | ||
| 160 | // set mock API's | ||
| 161 | RegFunctionOverride(TestRegistryFixture_RegCreateKeyExW, TestRegistryFixture_RegOpenKeyExW, TestRegistryFixture_RegDeleteKeyExW, NULL, NULL, NULL, NULL, NULL, NULL); | ||
| 162 | |||
| 163 | Registry::CurrentUser->CreateSubKey(this->hkcuPath); | ||
| 164 | Registry::CurrentUser->CreateSubKey(this->hklmPath); | ||
| 165 | } | ||
| 166 | |||
| 167 | void TestRegistryFixture::TearDown() | ||
| 168 | { | ||
| 169 | Registry::CurrentUser->DeleteSubKeyTree(this->rootPath, false); | ||
| 170 | |||
| 171 | RegFunctionOverride(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | ||
| 172 | } | ||
| 173 | |||
| 174 | String^ TestRegistryFixture::GetDirectHkcuPath(... array<String^>^ paths) | ||
| 175 | { | ||
| 176 | return Path::Combine(Registry::CurrentUser->Name, this->hkcuPath, Path::Combine(paths)); | ||
| 177 | } | ||
| 178 | |||
| 179 | String^ TestRegistryFixture::GetDirectHklmPath(... array<String^>^ paths) | ||
| 180 | { | ||
| 181 | return Path::Combine(Registry::CurrentUser->Name, this->hklmPath, Path::Combine(paths)); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
