ReactOS 0.4.17-dev-1133-gc25e4fd
RegenerateUserEnvironment.cpp
Go to the documentation of this file.
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 RegenerateUserEnvironment
5 * PROGRAMMER: Alex Mendoza
6 */
7
8#include "shelltest.h"
9#include <userenv.h>
10
11#define NDEBUG
12#include <debug.h>
13
14typedef BOOL (WINAPI *PFN_REGENERATE_USER_ENVIRONMENT)(PVOID *pPrevEnv, BOOL bSetCurrentEnv);
16
18{
19 LPWCH p = (LPWCH)pEnv;
20
21 if (!p || *p == L'\0')
22 return FALSE;
23
24 /* Walk the block but bail out early if it looks corrupted */
25 while (*p != L'\0')
26 {
27 SIZE_T len = wcslen(p);
28 if (len == 0)
29 return FALSE;
30 p += len + 1;
31 }
32
33 return TRUE;
34}
35
36static void Test_BasicCall(void)
37{
38 PVOID pEnv = NULL;
39 BOOL ret;
40
42 ok(ret != FALSE,
43 "RegenerateUserEnvironment(&pEnv, TRUE) failed with last error %lu\n",
44 GetLastError());
45
46 pEnv = NULL;
48 ok(ret != FALSE,
49 "RegenerateUserEnvironment(&pEnv, FALSE) failed with last error %lu\n",
50 GetLastError());
51 if (pEnv)
53}
54
56{
57 PVOID pPrev = NULL;
58 BOOL ret;
59
61 ok(ret != FALSE,
62 "RegenerateUserEnvironment(&pPrev, FALSE) failed with last error %lu\n",
63 GetLastError());
64
65 if (!ret)
66 {
67 skip("RegenerateUserEnvironment failed; skipping pPrev checks.\n");
68 return;
69 }
70
71 ok(pPrev != NULL,
72 "pPrev must not be NULL when bSetCurrentEnv is FALSE\n");
73
74 if (!pPrev)
75 return;
76
77 ok(IsValidEnvBlock(pPrev),
78 "pPrev does not point to a valid environment block\n");
79
81}
82
84{
85 static const WCHAR szKey[] = L"Environment";
86 static const WCHAR szVarName[] = L"__REGEN_TEST_VAR__";
87 static const WCHAR szVarValue[]= L"ReactOS_RegenerateTest";
88
89 HKEY hKey;
90 LONG lRet;
91 BOOL ret;
92 WCHAR szBuf[64];
93
95 if (lRet != ERROR_SUCCESS)
96 {
97 skip("Cannot open HKCU\\Environment (error %ld); skipping registry test.\n", lRet);
98 return;
99 }
100
101 /* Write canary value */
102 lRet = RegSetValueExW(hKey, szVarName, 0, REG_SZ,
103 (const BYTE *)szVarValue,
104 (DWORD)((wcslen(szVarValue) + 1) * sizeof(WCHAR)));
105 ok(lRet == ERROR_SUCCESS,
106 "RegSetValueExW failed: %ld\n", lRet);
107
108 if (lRet != ERROR_SUCCESS)
109 {
111 return;
112 }
113
114 /* Regenerate and apply to current process */
115 PVOID pEnv = NULL;
117 ok(ret != FALSE,
118 "RegenerateUserEnvironment failed: %lu\n", GetLastError());
119
120 /* The canary should be visible with GetEnvironmentVariableW */
121 szBuf[0] = L'\0';
122 GetEnvironmentVariableW(szVarName, szBuf, _countof(szBuf));
123 ok(wcscmp(szBuf, szVarValue) == 0,
124 "Expected env var '%ls' = '%ls', got '%ls'\n",
125 szVarName, szVarValue, szBuf);
126
127 /* Remove canary from registry */
128 RegDeleteValueW(hKey, szVarName);
130
131 PVOID pEnv2 = NULL;
133}
134
136{
137 HMODULE hShell32 = GetModuleHandleW(L"shell32.dll");
138
139 if (!hShell32)
140 hShell32 = LoadLibraryW(L"shell32.dll");
141
143 (PFN_REGENERATE_USER_ENVIRONMENT)GetProcAddress(hShell32, "RegenerateUserEnvironment");
144
146 {
147 skip("RegenerateUserEnvironment not found in shell32.dll\n");
148 return;
149 }
150
154}
static BOOL IsValidEnvBlock(PVOID pEnv)
static PFN_REGENERATE_USER_ENVIRONMENT pRegenerateUserEnvironment
BOOL(WINAPI * PFN_REGENERATE_USER_ENVIRONMENT)(PVOID *pPrevEnv, BOOL bSetCurrentEnv)
static void Test_PreviousEnvironmentReturned(void)
static void Test_BasicCall(void)
static void Test_RegistryVariablePickedUp(void)
#define REG_SZ
Definition: layer.c:22
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4882
LONG WINAPI RegDeleteValueW(HKEY hKey, LPCWSTR lpValueName)
Definition: reg.c:2330
#define GetEnvironmentVariableW(x, y, z)
Definition: compat.h:755
#define GetProcAddress(x, y)
Definition: compat.h:753
#define LoadLibraryW(x)
Definition: compat.h:747
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2988
_ACRTIMP int __cdecl wcscmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:1977
BOOL WINAPI DestroyEnvironmentBlock(IN LPVOID lpEnvironment)
Definition: environment.c:725
return ret
Definition: mutex.c:147
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
static HMODULE hShell32
Definition: string.c:34
#define BOOL
Definition: nt_native.h:43
#define KEY_SET_VALUE
Definition: nt_native.h:1020
WCHAR * LPWCH
Definition: ntbasedef.h:422
short WCHAR
Definition: pedump.c:58
long LONG
Definition: pedump.c:60
EXTERN_C BOOL WINAPI RegenerateUserEnvironment(LPVOID *lpEnvironment, BOOL bUpdateSelf)
Definition: shell32.cpp:91
#define _countof(array)
Definition: sndvol32.h:70
ULONG_PTR SIZE_T
Definition: typedefs.h:80
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
#define HKEY_CURRENT_USER
Definition: winreg.h:11
unsigned char BYTE
Definition: xxhash.c:193