ReactOS 0.4.16-dev-112-g52265ae
environ.c File Reference
#include <stdarg.h>
#include "wine/test.h"
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winnls.h"
Include dependency graph for environ.c:

Go to the source code of this file.

Macros

#define ok_w(res, format, szString)
 

Functions

static BOOL (WINAPI *pGetComputerNameExA)(COMPUTER_NAME_FORMAT
 
static void init_functionpointers (void)
 
static void test_Predefined (void)
 
static void test_GetSetEnvironmentVariableA (void)
 
static void test_GetSetEnvironmentVariableW (void)
 
static void test_ExpandEnvironmentStringsA (void)
 
static void test_GetComputerName (void)
 
static void test_GetComputerNameExA (void)
 
static void test_GetComputerNameExW (void)
 
 START_TEST (environ)
 

Variables

static CHAR string [MAX_PATH]
 
static LPSTR
 
static LPDWORD
 
static LPWSTR
 
static DWORD
 
static PHANDLE
 

Macro Definition Documentation

◆ ok_w

#define ok_w (   res,
  format,
  szString 
)
Value:
\
WideCharToMultiByte(CP_ACP, 0, szString, -1, string, MAX_PATH, NULL, NULL); \
ok(res, format, string);
#define NULL
Definition: types.h:112
#define CP_ACP
Definition: compat.h:109
#define MAX_PATH
Definition: compat.h:34
GLuint res
Definition: glext.h:9613
Definition: format.c:58

Definition at line 30 of file environ.c.

Function Documentation

◆ BOOL()

static BOOL ( WINAPI pGetComputerNameExA)
static

◆ init_functionpointers()

static void init_functionpointers ( void  )
static

Definition at line 40 of file environ.c.

41{
42 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
43 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
44 HMODULE huserenv = LoadLibraryA("userenv.dll");
45
46 pGetComputerNameExA = (void *)GetProcAddress(hkernel32, "GetComputerNameExA");
47 pGetComputerNameExW = (void *)GetProcAddress(hkernel32, "GetComputerNameExW");
48 pOpenProcessToken = (void *)GetProcAddress(hadvapi32, "OpenProcessToken");
49 pGetUserProfileDirectoryA = (void *)GetProcAddress(huserenv,
50 "GetUserProfileDirectoryA");
51}
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
static HINSTANCE hkernel32
Definition: process.c:66

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( environ  )

Definition at line 557 of file environ.c.

558{
560
568}
static void test_Predefined(void)
Definition: environ.c:53
static void test_GetComputerNameExA(void)
Definition: environ.c:420
static void test_ExpandEnvironmentStringsA(void)
Definition: environ.c:284
static void test_GetComputerNameExW(void)
Definition: environ.c:503
static void test_GetSetEnvironmentVariableW(void)
Definition: environ.c:182
static void init_functionpointers(void)
Definition: environ.c:40
static void test_GetComputerName(void)
Definition: environ.c:367
static void test_GetSetEnvironmentVariableA(void)
Definition: environ.c:92

◆ test_ExpandEnvironmentStringsA()

static void test_ExpandEnvironmentStringsA ( void  )
static

Definition at line 284 of file environ.c.

285{
286 const char* value="Long long value";
287 const char* not_an_env_var="%NotAnEnvVar%";
288 char buf[256], buf1[256], buf2[0x8000];
289 DWORD ret_size, ret_size1;
290
292
293 ret_size = ExpandEnvironmentStringsA(NULL, buf1, sizeof(buf1));
294 ok(ret_size == 1 || ret_size == 0 /* Win9x */ || ret_size == 2 /* NT4 */,
295 "ExpandEnvironmentStrings returned %d\n", ret_size);
296
297 /* Try to get the required buffer size 'the natural way' */
298 strcpy(buf, "%EnvVar%");
299 ret_size = ExpandEnvironmentStringsA(buf, NULL, 0);
300 ok(ret_size == strlen(value)+1 || /* win98 */
301 ret_size == (strlen(value)+1)*2 || /* NT4 */
302 ret_size == strlen(value)+2 || /* win2k, XP, win2k3 */
303 ret_size == 0 /* Win95 */,
304 "ExpandEnvironmentStrings returned %d instead of %d, %d or %d\n",
305 ret_size, lstrlenA(value)+1, lstrlenA(value)+2, 0);
306
307 /* Again, side-stepping the Win95 bug */
308 ret_size = ExpandEnvironmentStringsA(buf, buf1, 0);
309 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
310 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
311 ret_size == (strlen(value)+1)*2 /* NT4 */,
312 "ExpandEnvironmentStrings returned %d instead of %d\n",
313 ret_size, lstrlenA(value)+1);
314
315 /* Try with a buffer that's too small */
316 ret_size = ExpandEnvironmentStringsA(buf, buf1, 12);
317 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
318 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
319 ret_size == (strlen(value)+1)*2 /* NT4 */,
320 "ExpandEnvironmentStrings returned %d instead of %d\n",
321 ret_size, lstrlenA(value)+1);
322
323 /* Try with a buffer of just the right size */
324 /* v5.1.2600.2945 (XP SP2) needs and returns len + 2 here! */
325 ret_size = ExpandEnvironmentStringsA(buf, buf1, ret_size);
326 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
327 ret_size == (strlen(value)+1)*2 /* NT4 */,
328 "ExpandEnvironmentStrings returned %d instead of %d\n",
329 ret_size, lstrlenA(value)+1);
330 ok(!strcmp(buf1, value), "ExpandEnvironmentStrings returned [%s]\n", buf1);
331
332 /* Try with an unset environment variable */
333 strcpy(buf, not_an_env_var);
334 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
335 ok(ret_size == strlen(not_an_env_var)+1 ||
336 ret_size == (strlen(not_an_env_var)+1)*2 /* NT4 */,
337 "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlenA(not_an_env_var)+1);
338 ok(!strcmp(buf1, not_an_env_var), "ExpandEnvironmentStrings returned [%s]\n", buf1);
339
340 /* test a large destination size */
341 strcpy(buf, "12345");
342 ret_size = ExpandEnvironmentStringsA(buf, buf2, sizeof(buf2));
343 ok(!strcmp(buf, buf2), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf2, ret_size);
344
345 ret_size1 = GetWindowsDirectoryA(buf1,256);
346 ok ((ret_size1 >0) && (ret_size1<256), "GetWindowsDirectory Failed\n");
347 ret_size = ExpandEnvironmentStringsA("%SystemRoot%",buf,sizeof(buf));
349 {
350 ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf1, ret_size);
351 }
352
353 /* Try with a variable that references another */
354 SetEnvironmentVariableA("IndirectVar", "Foo%EnvVar%Bar");
355 strcpy(buf, "Indirect-%IndirectVar%-Indirect");
356 strcpy(buf2, "Indirect-Foo%EnvVar%Bar-Indirect");
357 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
358 ok(ret_size == strlen(buf2)+1 ||
359 ret_size == (strlen(buf2)+1)*2 /* NT4 */,
360 "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlenA(buf2)+1);
361 ok(!strcmp(buf1, buf2), "ExpandEnvironmentStrings returned [%s]\n", buf1);
362 SetEnvironmentVariableA("IndirectVar", NULL);
363
364 SetEnvironmentVariableA("EnvVar", NULL);
365}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ok(value,...)
Definition: atltest.h:57
DWORD WINAPI ExpandEnvironmentStringsA(IN LPCSTR lpSrc, IN LPSTR lpDst, IN DWORD nSize)
Definition: environ.c:399
BOOL WINAPI DECLSPEC_HOTPATCH SetEnvironmentVariableA(IN LPCSTR lpName, IN LPCSTR lpValue)
Definition: environ.c:218
UINT WINAPI GetWindowsDirectoryA(OUT LPSTR lpBuffer, IN UINT uSize)
Definition: path.c:2337
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
Definition: pdh_main.c:94
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_ENVVAR_NOT_FOUND
Definition: winerror.h:261

Referenced by START_TEST().

◆ test_GetComputerName()

static void test_GetComputerName ( void  )
static

Definition at line 367 of file environ.c.

368{
369 DWORD size;
370 BOOL ret;
371 LPSTR name;
373 DWORD error;
374 int name_len;
375
376 size = 0;
377 ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
379 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
380
381 /* Only Vista returns the computer name length as documented in the MSDN */
382 if (size != 0)
383 {
384 size++; /* nul terminating character */
385 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
386 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
388 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
390 }
391
393 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
394 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
396 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
397 trace("computer name is \"%s\"\n", name);
398 name_len = strlen(name);
399 ok(size == name_len, "size should be same as length, name_len=%d, size=%d\n", name_len, size);
401
402 size = 0;
403 SetLastError(0xdeadbeef);
404 ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size);
407 win_skip("GetComputerNameW is not implemented\n");
408 else
409 {
410 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
411 size++; /* nul terminating character */
412 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
413 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
415 ok(ret, "GetComputerNameW failed with error %d\n", GetLastError());
417 }
418}
#define trace
Definition: atltest.h:70
static const WCHAR nameW[]
Definition: main.c:49
BOOL WINAPI GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
Definition: compname.c:446
#define GetProcessHeap()
Definition: compat.h:736
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define SetLastError(x)
Definition: compat.h:752
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizeiptr size
Definition: glext.h:5919
#define error(str)
Definition: mkdosfs.c:1605
#define win_skip
Definition: test.h:163
BOOL WINAPI SHIM_OBJ_NAME() GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
Definition: shimtest.c:21
Definition: name.c:39
int ret
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:243
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:185
char * LPSTR
Definition: xmlstorage.h:182
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by START_TEST().

◆ test_GetComputerNameExA()

static void test_GetComputerNameExA ( void  )
static

Definition at line 420 of file environ.c.

421{
422 DWORD size;
423 BOOL ret;
424 LPSTR name;
425 DWORD error;
426
427 static const int MAX_COMP_NAME = 32767;
428
429 if (!pGetComputerNameExA)
430 {
431 win_skip("GetComputerNameExA function not implemented\n");
432 return;
433 }
434
435 size = 0;
436 ret = pGetComputerNameExA(ComputerNameDnsDomain, (LPSTR)0xdeadbeef, &size);
438 ok(ret == 0, "Expected 0, got %d\n", ret);
439 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
440
441 /* size is not set in win2k */
442 if (size == 0)
443 {
444 win_skip("Win2k doesn't set the size\n");
445 size = MAX_COMP_NAME;
446 }
447 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
448 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
449 ret = pGetComputerNameExA(ComputerNameDnsDomain, name, &size);
450 ok(ret, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
451 trace("domain name is \"%s\"\n", name);
453
454 size = 0;
455 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, (LPSTR)0xdeadbeef, &size);
457 ok(ret == 0, "Expected 0, got %d\n", ret);
458 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
459
460 /* size is not set in win2k */
461 if (size == 0)
462 size = MAX_COMP_NAME;
463 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
464 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
465 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, name, &size);
466 ok(ret, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
467 trace("fully qualified hostname is \"%s\"\n", name);
469
470 size = 0;
471 ret = pGetComputerNameExA(ComputerNameDnsHostname, (LPSTR)0xdeadbeef, &size);
473 ok(ret == 0, "Expected 0, got %d\n", ret);
474 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
475
476 /* size is not set in win2k */
477 if (size == 0)
478 size = MAX_COMP_NAME;
479 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
480 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
481 ret = pGetComputerNameExA(ComputerNameDnsHostname, name, &size);
482 ok(ret, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
483 trace("hostname is \"%s\"\n", name);
485
486 size = 0;
487 ret = pGetComputerNameExA(ComputerNameNetBIOS, (LPSTR)0xdeadbeef, &size);
489 ok(ret == 0, "Expected 0, got %d\n", ret);
490 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
491
492 /* size is not set in win2k */
493 if (size == 0)
494 size = MAX_COMP_NAME;
495 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
496 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
497 ret = pGetComputerNameExA(ComputerNameNetBIOS, name, &size);
498 ok(ret, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
499 trace("NetBIOS name is \"%s\"\n", name);
501}
#define ERROR_MORE_DATA
Definition: dderror.h:13

Referenced by START_TEST().

◆ test_GetComputerNameExW()

static void test_GetComputerNameExW ( void  )
static

Definition at line 503 of file environ.c.

504{
505 DWORD size;
506 BOOL ret;
508 DWORD error;
509
510 if (!pGetComputerNameExW)
511 {
512 win_skip("GetComputerNameExW function not implemented\n");
513 return;
514 }
515
516 size = 0;
517 ret = pGetComputerNameExW(ComputerNameDnsDomain, (LPWSTR)0xdeadbeef, &size);
519 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
520 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
521 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
522 ret = pGetComputerNameExW(ComputerNameDnsDomain, nameW, &size);
523 ok(ret, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
525
526 size = 0;
527 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, (LPWSTR)0xdeadbeef, &size);
529 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
530 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
531 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
532 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, nameW, &size);
533 ok(ret, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
535
536 size = 0;
537 ret = pGetComputerNameExW(ComputerNameDnsHostname, (LPWSTR)0xdeadbeef, &size);
539 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
540 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
541 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
542 ret = pGetComputerNameExW(ComputerNameDnsHostname, nameW, &size);
543 ok(ret, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
545
546 size = 0;
547 ret = pGetComputerNameExW(ComputerNameNetBIOS, (LPWSTR)0xdeadbeef, &size);
549 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
550 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
551 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
552 ret = pGetComputerNameExW(ComputerNameNetBIOS, nameW, &size);
553 ok(ret, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
555}

Referenced by START_TEST().

◆ test_GetSetEnvironmentVariableA()

static void test_GetSetEnvironmentVariableA ( void  )
static

Definition at line 92 of file environ.c.

93{
94 char buf[256];
95 BOOL ret;
96 DWORD ret_size;
97 static const char name[] = "SomeWildName";
98 static const char name_cased[] = "sOMEwILDnAME";
99 static const char value[] = "SomeWildValue";
100
102 ok(ret == TRUE,
103 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
104 GetLastError());
105
106 /* Try to retrieve the environment variable we just set */
107 ret_size = GetEnvironmentVariableA(name, NULL, 0);
108 ok(ret_size == strlen(value) + 1,
109 "should return length with terminating 0 ret_size=%d\n", ret_size);
110
111 lstrcpyA(buf, "foo");
113 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
114 ok(ret_size == strlen(value) + 1,
115 "should return length with terminating 0 ret_size=%d\n", ret_size);
116
117 lstrcpyA(buf, "foo");
118 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
119 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
120 ok(ret_size == strlen(value),
121 "should return length without terminating 0 ret_size=%d\n", ret_size);
122
123 lstrcpyA(buf, "foo");
124 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
125 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
126 ok(ret_size == strlen(value),
127 "should return length without terminating 0 ret_size=%d\n", ret_size);
128
129 /* Remove that environment variable */
130 ret = SetEnvironmentVariableA(name_cased, NULL);
131 ok(ret == TRUE, "should erase existing variable\n");
132
133 lstrcpyA(buf, "foo");
134 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
135 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
136 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
137 "should not find variable but ret_size=%d GetLastError=%d\n",
138 ret_size, GetLastError());
139
140 /* Check behavior of SetEnvironmentVariableA(name, "") */
142 ok(ret == TRUE,
143 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
144 GetLastError());
145
146 lstrcpyA(buf, "foo");
147 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
148 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
149 ok(ret_size == strlen(value),
150 "should return length without terminating 0 ret_size=%d\n", ret_size);
151
152 ret = SetEnvironmentVariableA(name_cased, "");
153 ok(ret == TRUE,
154 "should not fail with empty value but GetLastError=%d\n", GetLastError());
155
156 lstrcpyA(buf, "foo");
157 SetLastError(0);
158 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
159 ok(ret_size == 0 &&
160 ((GetLastError() == 0 && lstrcmpA(buf, "") == 0) ||
162 "%s should be set to \"\" (NT) or removed (Win9x) but ret_size=%d GetLastError=%d and buf=%s\n",
163 name, ret_size, GetLastError(), buf);
164
165 /* Test the limits */
166 ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
168 "should not find variable but ret_size=%d GetLastError=%d\n",
169 ret_size, GetLastError());
170
171 ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
173 "should not find variable but ret_size=%d GetLastError=%d\n",
174 ret_size, GetLastError());
175
176 ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
177 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
178 "should not find variable but ret_size=%d GetLastError=%d\n",
179 ret_size, GetLastError());
180}
#define TRUE
Definition: types.h:120
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define GetEnvironmentVariableA(x, y, z)
Definition: compat.h:754
int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
Definition: locale.c:4194
LPSTR WINAPI lstrcpyA(LPSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:100

Referenced by START_TEST().

◆ test_GetSetEnvironmentVariableW()

static void test_GetSetEnvironmentVariableW ( void  )
static

Definition at line 182 of file environ.c.

183{
184 WCHAR buf[256];
185 BOOL ret;
186 DWORD ret_size;
187 static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
188 static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
189 static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
190 static const WCHAR empty_strW[] = { 0 };
191 static const WCHAR fooW[] = {'f','o','o',0};
192
195 {
196 /* Must be Win9x which doesn't support the Unicode functions */
197 win_skip("SetEnvironmentVariableW is not implemented\n");
198 return;
199 }
200 ok(ret == TRUE,
201 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
202 GetLastError());
203
204 /* Try to retrieve the environment variable we just set */
205 ret_size = GetEnvironmentVariableW(name, NULL, 0);
206 ok(ret_size == lstrlenW(value) + 1,
207 "should return length with terminating 0 ret_size=%d\n",
208 ret_size);
209
210 lstrcpyW(buf, fooW);
212 ok_w(lstrcmpW(buf, fooW) == 0 ||
213 lstrlenW(buf) == 0, /* Vista */
214 "Expected untouched or empty buffer, got \"%s\"\n", buf);
215
216 ok(ret_size == lstrlenW(value) + 1,
217 "should return length with terminating 0 ret_size=%d\n", ret_size);
218
219 lstrcpyW(buf, fooW);
220 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
221 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
222 ok(ret_size == lstrlenW(value),
223 "should return length without terminating 0 ret_size=%d\n", ret_size);
224
225 lstrcpyW(buf, fooW);
226 ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
227 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
228 ok(ret_size == lstrlenW(value),
229 "should return length without terminating 0 ret_size=%d\n", ret_size);
230
231 /* Remove that environment variable */
232 ret = SetEnvironmentVariableW(name_cased, NULL);
233 ok(ret == TRUE, "should erase existing variable\n");
234
235 lstrcpyW(buf, fooW);
236 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
237 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
238 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
239 "should not find variable but ret_size=%d GetLastError=%d\n",
240 ret_size, GetLastError());
241
242 /* Check behavior of SetEnvironmentVariableW(name, "") */
244 ok(ret == TRUE,
245 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
246 GetLastError());
247
248 lstrcpyW(buf, fooW);
249 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
250 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
251 ok(ret_size == lstrlenW(value),
252 "should return length without terminating 0 ret_size=%d\n", ret_size);
253
254 ret = SetEnvironmentVariableW(name_cased, empty_strW);
255 ok(ret == TRUE, "should not fail with empty value but GetLastError=%d\n", GetLastError());
256
257 lstrcpyW(buf, fooW);
258 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
259 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
260 "should not find variable but ret_size=%d GetLastError=%d\n",
261 ret_size, GetLastError());
262 ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string\n");
263
264 /* Test the limits */
265 ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
266 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
267 "should not find variable but ret_size=%d GetLastError=%d\n",
268 ret_size, GetLastError());
269
270 if (0) /* Both tests crash on Vista */
271 {
272 ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
273 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
274 "should not find variable but ret_size=%d GetLastError=%d\n",
275 ret_size, GetLastError());
276
279 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
280 ret, GetLastError());
281 }
282}
#define FALSE
Definition: types.h:117
#define GetEnvironmentVariableW(x, y, z)
Definition: compat.h:755
#define lstrcpyW
Definition: compat.h:749
#define lstrlenW
Definition: compat.h:750
BOOL WINAPI DECLSPEC_HOTPATCH SetEnvironmentVariableW(IN LPCWSTR lpName, IN LPCWSTR lpValue)
Definition: environ.c:259
int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4242
#define ok_w(res, format, szString)
Definition: environ.c:30
static const WCHAR fooW[]
Definition: locale.c:44
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by START_TEST().

◆ test_Predefined()

static void test_Predefined ( void  )
static

Definition at line 53 of file environ.c.

54{
55 char Data[1024];
57 char Env[sizeof(Data)];
60 BOOL NoErr;
61
62 /*
63 * Check value of %USERPROFILE%, should be same as GetUserProfileDirectory()
64 * If this fails, your test environment is probably not set up
65 */
66 if (pOpenProcessToken == NULL || pGetUserProfileDirectoryA == NULL)
67 {
68 skip("Skipping USERPROFILE check\n");
69 return;
70 }
71 NoErr = pOpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token);
72 ok(NoErr, "Failed to open token, error %u\n", GetLastError());
73 DataSize = sizeof(Data);
74 NoErr = pGetUserProfileDirectoryA(Token, Data, &DataSize);
75 ok(NoErr, "Failed to get user profile dir, error %u\n", GetLastError());
76 if (NoErr)
77 {
78 EnvSize = GetEnvironmentVariableA("USERPROFILE", Env, sizeof(Env));
79 ok(EnvSize != 0 && EnvSize <= sizeof(Env),
80 "Failed to retrieve environment variable USERPROFILE, error %u\n",
81 GetLastError());
82 ok(strcmp(Data, Env) == 0,
83 "USERPROFILE env var %s doesn't match GetUserProfileDirectory %s\n",
84 Env, Data);
85 }
86 else
87 skip("Skipping USERPROFILE check, can't get user profile dir\n");
88 NoErr = CloseHandle(Token);
89 ok(NoErr, "Failed to close token, error %u\n", GetLastError());
90}
#define skip(...)
Definition: atltest.h:64
static PVOID Env
Definition: dem.c:259
static ULONG EnvSize
Definition: dem.c:258
#define CloseHandle
Definition: compat.h:739
#define GetCurrentProcess()
Definition: compat.h:759
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4755
#define TOKEN_QUERY
Definition: setypes.h:928

Referenced by START_TEST().

Variable Documentation

◆ DWORD

Definition at line 37 of file environ.c.

◆ LPDWORD

Definition at line 35 of file environ.c.

◆ LPSTR

Definition at line 35 of file environ.c.

◆ LPWSTR

Definition at line 36 of file environ.c.

◆ PHANDLE

Definition at line 37 of file environ.c.

◆ string

CHAR string[MAX_PATH]
static

Definition at line 29 of file environ.c.