1 /*
2  * PROJECT:     ReactOS api tests
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Tests for SHLoadRegUIStringA/W
5  * COPYRIGHT:   Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6  */
7 
8 #include <apitest.h>
9 #include <shlwapi.h>
10 
11 typedef HRESULT (WINAPI *FN_SHLoadRegUIStringA)(HKEY hkey, LPCSTR value, LPSTR buf, DWORD size);
12 typedef HRESULT (WINAPI *FN_SHLoadRegUIStringW)(HKEY hkey, LPCWSTR value, LPWSTR buf, DWORD size);
13 
14 static FN_SHLoadRegUIStringA pSHLoadRegUIStringA = NULL;
15 static FN_SHLoadRegUIStringW pSHLoadRegUIStringW = NULL;
16 
17 static void test_SHLoadRegUIStringA(HKEY hKey)
18 {
19     HRESULT hr;
20     CHAR szBuff[MAX_PATH];
21 
22     hr = pSHLoadRegUIStringA(hKey, "TestValue1", szBuff, _countof(szBuff));
23     ok_long(hr, S_OK);
24     ok_str(szBuff, "%WINDIR%\\TEST");
25 
26     hr = pSHLoadRegUIStringA(hKey, "TestValue2", szBuff, _countof(szBuff));
27     ok_long(hr, S_OK);
28     ok_str(szBuff, "Test string one.");
29 }
30 
31 static void test_SHLoadRegUIStringW(HKEY hKey)
32 {
33     HRESULT hr;
34     WCHAR szBuff[MAX_PATH];
35 
36     hr = pSHLoadRegUIStringW(hKey, L"TestValue1", szBuff, _countof(szBuff));
37     ok_long(hr, S_OK);
38     ok_wstr(szBuff, L"%WINDIR%\\TEST");
39 
40     hr = pSHLoadRegUIStringW(hKey, L"TestValue2", szBuff, _countof(szBuff));
41     ok_long(hr, S_OK);
42     ok_wstr(szBuff, L"Test string one.");
43 }
44 
45 BOOL extract_resource(const WCHAR* Filename, LPCWSTR ResourceName);
46 
47 START_TEST(SHLoadRegUIString)
48 {
49     LONG error;
50     HKEY hKey;
51     DWORD cbValue;
52     static const WCHAR s_szTestValue1[] = L"%WINDIR%\\TEST";
53     static const WCHAR s_szTestValue2[] = L"@SHLoadRegUIString.dll%EmptyEnvVar%,-3";
54     HMODULE hSHLWAPI;
55 
56     SetEnvironmentVariableW(L"EmptyEnvVar", L"");
57 
58     /* Get procedures */
59     hSHLWAPI = GetModuleHandleW(L"shlwapi");
60     pSHLoadRegUIStringA = (FN_SHLoadRegUIStringA)GetProcAddress(hSHLWAPI, (LPCSTR)438);
61     pSHLoadRegUIStringW = (FN_SHLoadRegUIStringW)GetProcAddress(hSHLWAPI, (LPCSTR)439);
62     if (!pSHLoadRegUIStringA || !pSHLoadRegUIStringW)
63     {
64         skip("No procedure found\n");
65         return;
66     }
67 
68     if (!extract_resource(L"SHLoadRegUIString.dll", MAKEINTRESOURCEW(101)))
69     {
70         skip("File 'SHLoadRegUIString.dll' cannot be extracted\n");
71         return;
72     }
73 
74     /* Open registry key and write some test values */
75     error = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software", 0, KEY_READ | KEY_WRITE, &hKey);
76     ok_long(error, ERROR_SUCCESS);
77 
78     cbValue = (lstrlenW(s_szTestValue1) + 1) * sizeof(WCHAR);
79     error = RegSetValueExW(hKey, L"TestValue1", 0, REG_SZ, (LPBYTE)s_szTestValue1, cbValue);
80     ok_long(error, ERROR_SUCCESS);
81 
82     cbValue = (lstrlenW(s_szTestValue2) + 1) * sizeof(WCHAR);
83     error = RegSetValueExW(hKey, L"TestValue2", 0, REG_SZ, (LPBYTE)s_szTestValue2, cbValue);
84     ok_long(error, ERROR_SUCCESS);
85 
86     /* The main dish */
87     test_SHLoadRegUIStringA(hKey);
88     test_SHLoadRegUIStringW(hKey);
89 
90     /* Delete the test values and close the key */
91     RegDeleteValueW(hKey, L"TestValue1");
92     RegDeleteValueW(hKey, L"TestValue2");
93     RegCloseKey(hKey);
94 
95     DeleteFileW(L"SHLoadRegUIString.dll");
96 }
97