1 /*
2  * PROJECT:     ReactOS API tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test for SHRestricted
5  * COPYRIGHT:   Copyright 2024 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  */
7 
8 #include "shelltest.h"
9 #include <versionhelpers.h>
10 
11 #define REGKEY_POLICIES L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies"
12 #define REGKEY_POLICIES_EXPLORER REGKEY_POLICIES L"\\Explorer"
13 
14 typedef DWORD (WINAPI *FN_SHRestricted)(RESTRICTIONS rest);
15 typedef BOOL (WINAPI *FN_SHSettingsChanged)(LPCVOID unused, LPCVOID inpRegKey);
16 
17 #define DELETE_VALUE(hBaseKey) \
18     SHDeleteValueW((hBaseKey), REGKEY_POLICIES_EXPLORER, L"NoRun")
19 
20 #define SET_VALUE(hBaseKey, value) do { \
21     dwValue = (value); \
22     SHSetValueW((hBaseKey), REGKEY_POLICIES_EXPLORER, L"NoRun", \
23                 REG_DWORD, &dwValue, sizeof(dwValue)); \
24 } while (0)
25 
26 static VOID
27 TEST_SHRestricted(FN_SHRestricted fnGetValue, FN_SHSettingsChanged fnRefresh)
28 {
29     DWORD dwValue;
30 
31     DELETE_VALUE(HKEY_CURRENT_USER);
32     DELETE_VALUE(HKEY_LOCAL_MACHINE);
33 
34     fnRefresh(NULL, NULL);
35     ok_long(fnGetValue(REST_NORUN), 0);
36 
37     SET_VALUE(HKEY_CURRENT_USER, 0);
38     DELETE_VALUE(HKEY_LOCAL_MACHINE);
39 
40     ok_long(fnGetValue(REST_NORUN), 0);
41     fnRefresh(NULL, NULL);
42     ok_long(fnGetValue(REST_NORUN), 0);
43 
44     SET_VALUE(HKEY_CURRENT_USER, 1);
45     DELETE_VALUE(HKEY_LOCAL_MACHINE);
46 
47     ok_long(fnGetValue(REST_NORUN), 0);
48     fnRefresh(NULL, NULL);
49     ok_long(fnGetValue(REST_NORUN), 1);
50 
51     DELETE_VALUE(HKEY_CURRENT_USER);
52     SET_VALUE(HKEY_LOCAL_MACHINE, 0);
53 
54     ok_long(fnGetValue(REST_NORUN), 1);
55     fnRefresh(NULL, NULL);
56     ok_long(fnGetValue(REST_NORUN), 0);
57 
58     DELETE_VALUE(HKEY_CURRENT_USER);
59     SET_VALUE(HKEY_LOCAL_MACHINE, 1);
60 
61     ok_long(fnGetValue(REST_NORUN), 0);
62     fnRefresh(NULL, NULL);
63     ok_long(fnGetValue(REST_NORUN), 1);
64 
65     SET_VALUE(HKEY_CURRENT_USER, 2);
66     SET_VALUE(HKEY_LOCAL_MACHINE, 1);
67 
68     ok_long(fnGetValue(REST_NORUN), 1);
69     fnRefresh(NULL, NULL);
70     ok_long(fnGetValue(REST_NORUN), 1);
71 
72     DELETE_VALUE(HKEY_CURRENT_USER);
73     DELETE_VALUE(HKEY_LOCAL_MACHINE);
74 
75     ok_long(fnGetValue(REST_NORUN), 1);
76     fnRefresh(NULL, NULL);
77     ok_long(fnGetValue(REST_NORUN), 0);
78 }
79 
80 START_TEST(SHRestricted)
81 {
82     if (IsWindowsVistaOrGreater())
83     {
84         skip("Vista+");
85         return;
86     }
87 
88     HMODULE hSHELL32 = LoadLibraryW(L"shell32.dll");
89     FN_SHRestricted fnGetValue;
90     FN_SHSettingsChanged fnRefresh;
91 
92     fnGetValue = (FN_SHRestricted)GetProcAddress(hSHELL32, MAKEINTRESOURCEA(100));
93     fnRefresh = (FN_SHSettingsChanged)GetProcAddress(hSHELL32, MAKEINTRESOURCEA(244));
94 
95     if (fnGetValue && fnRefresh)
96     {
97         TEST_SHRestricted(fnGetValue, fnRefresh);
98     }
99     else
100     {
101         if (!fnGetValue)
102             skip("SHRestricted not found\n");
103         if (!fnRefresh)
104             skip("SHSettingsChanged not found\n");
105     }
106 
107     FreeLibrary(hSHELL32);
108 }
109