From 7e61f48f6be0849cb0c0da796ec77603c14532e5 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Mon, 28 Feb 2022 18:38:46 -0600 Subject: Factor out TestRegistryFixture so other tests can mock regutil API's. --- src/burn/test/BurnUnitTest/TestRegistryFixture.cpp | 184 +++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/burn/test/BurnUnitTest/TestRegistryFixture.cpp (limited to 'src/burn/test/BurnUnitTest/TestRegistryFixture.cpp') 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 @@ +// 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_HKCU_PATH TEST_REGISTRY_FIXTURE_ROOT_PATH L"\\HKCU" + +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; + LPCWSTR wzRoot = NULL; + HKEY hkRoot = NULL; + + if (HKEY_LOCAL_MACHINE == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; + } + else if (HKEY_CURRENT_USER == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; + } + else + { + hkRoot = hKey; + } + + if (wzRoot) + { + ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE, &hkRoot); + if (ERROR_SUCCESS != ls) + { + ExitFunction(); + } + } + + ls = ::RegCreateKeyExW(hkRoot, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition); + +LExit: + 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; + LPCWSTR wzRoot = NULL; + HKEY hkRoot = NULL; + + if (HKEY_LOCAL_MACHINE == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; + } + else if (HKEY_CURRENT_USER == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; + } + else + { + hkRoot = hKey; + } + + if (wzRoot) + { + ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE, &hkRoot); + if (ERROR_SUCCESS != ls) + { + ExitFunction(); + } + } + + ls = ::RegOpenKeyExW(hkRoot, lpSubKey, ulOptions, samDesired, phkResult); + +LExit: + 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; + LPCWSTR wzRoot = NULL; + HKEY hkRoot = NULL; + + if (HKEY_LOCAL_MACHINE == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKLM_PATH; + } + else if (HKEY_CURRENT_USER == hKey) + { + wzRoot = TEST_REGISTRY_FIXTURE_HKCU_PATH; + } + else + { + hkRoot = hKey; + } + + if (wzRoot) + { + ls = ::RegOpenKeyExW(HKEY_CURRENT_USER, wzRoot, 0, KEY_WRITE | samDesired, &hkRoot); + if (ERROR_SUCCESS != ls) + { + ExitFunction(); + } + } + + ls = ::RegDeleteKeyExW(hkRoot, lpSubKey, samDesired, Reserved); + +LExit: + ReleaseRegKey(hkRoot); + + return ls; +} + +namespace WixBuildTools +{ + namespace TestSupport + { + using namespace System; + using namespace System::IO; + using namespace Microsoft::Win32; + + TestRegistryFixture::TestRegistryFixture() + { + this->rootPath = gcnew String(TEST_REGISTRY_FIXTURE_ROOT_PATH); + this->hkcuPath = gcnew String(TEST_REGISTRY_FIXTURE_HKCU_PATH); + this->hklmPath = gcnew String(TEST_REGISTRY_FIXTURE_HKLM_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); + + Registry::CurrentUser->CreateSubKey(this->hkcuPath); + Registry::CurrentUser->CreateSubKey(this->hklmPath); + } + + void TestRegistryFixture::TearDown() + { + Registry::CurrentUser->DeleteSubKeyTree(this->rootPath, false); + + RegFunctionOverride(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + } + + String^ TestRegistryFixture::GetDirectHkcuPath(... array^ paths) + { + return Path::Combine(Registry::CurrentUser->Name, this->hkcuPath, Path::Combine(paths)); + } + + String^ TestRegistryFixture::GetDirectHklmPath(... array^ paths) + { + return Path::Combine(Registry::CurrentUser->Name, this->hklmPath, Path::Combine(paths)); + } + } +} -- cgit v1.2.3-55-g6feb