ReactOS 0.4.17-dev-116-ga4b6fe9
GetEnvironmentVariable.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Tests for GetEnvironmentVariable(A/W)
5 * COPYRIGHT: Copyright 2026 Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7
8#include "precomp.h"
9
11{
13 ULONG ActualLength, Length;
14
15 /* Get the COMSPEC variable */
16 memset(Buffer, 0xAB, sizeof(Buffer));
18 Buffer[MAX_PATH - 1] = '\0';
19 ActualLength = (ULONG)strlen(Buffer);
20 ok(ActualLength < ARRAYSIZE(Buffer) - 1, "ActualLength == ARRAYSIZE(Buffer) - 1. Not null-terminated?\n");
21 ok_eq_ulong(Length, ActualLength);
22 ok_eq_char(Buffer[ActualLength + 1], '\xAB');
23
24 /* Get the length only (return value is including NULL char) */
25 Length = GetEnvironmentVariableA("COMSPEC", NULL, 0);
26 ok_eq_ulong(Length, ActualLength + 1);
27
28 /* Test a buffer size that is too small */
29 memset(Buffer, 0xAB, sizeof(Buffer));
30 Length = GetEnvironmentVariableA("COMSPEC", Buffer, 5);
31 ok_eq_ulong(Length, ActualLength + 1);
32 ok_eq_char(Buffer[0], '\xAB');
33
34 /* Test a buffer size that doesn't fit the terminating NULL char */
35 memset(Buffer, 0xAB, sizeof(Buffer));
36 Length = GetEnvironmentVariableA("COMSPEC", Buffer, ActualLength);
37 ok_eq_ulong(Length, ActualLength + 1);
38 ok_eq_char(Buffer[0], '\xAB');
39
40 /* Test a buffer size that is just large enough */
41 memset(Buffer, 0xAB, sizeof(Buffer));
42 Length = GetEnvironmentVariableA("COMSPEC", Buffer, ActualLength + 1);
43 ok_eq_ulong(Length, ActualLength);
44 ok_eq_wchar(Buffer[ActualLength], '\0');
45 ok_eq_wchar(Buffer[ActualLength + 1], '\xAB');
46
47 /* Test non-existant variable name */
48 memset(Buffer, 0xAB, sizeof(Buffer));
49 Length = GetEnvironmentVariableA("ThisVariableDoesNotExit", Buffer, ARRAYSIZE(Buffer));
51 ok_eq_wchar(Buffer[0], '\xAB');
52
53 /* Test NULL variable name */
56
57 /* Test NULL buffer with non-null size */
58 StartSeh()
61}
62
64{
66 ULONG ActualLength, Length;
67
68 /* Get the COMSPEC variable */
69 memset(Buffer, 0xAB, sizeof(Buffer));
71 Buffer[MAX_PATH - 1] = L'\0';
72 ActualLength = (ULONG)wcslen(Buffer);
73 ok(ActualLength < ARRAYSIZE(Buffer) - 1, "ActualLength == ARRAYSIZE(Buffer) - 1. Not null-terminated?\n");
74 ok_eq_ulong(Length, ActualLength);
75 ok_eq_wchar(Buffer[ActualLength + 1], 0xABAB);
76
77 /* Get the length only (return value is including NULL char) */
78 Length = GetEnvironmentVariableW(L"COMSPEC", NULL, 0);
79 ok_eq_ulong(Length, ActualLength + 1);
80
81 /* Test a buffer size that is too small */
82 memset(Buffer, 0xAB, sizeof(Buffer));
83 Length = GetEnvironmentVariableW(L"COMSPEC", Buffer, 2);
84 ok_eq_ulong(Length, ActualLength + 1);
85 ok(Buffer[0] == UNICODE_NULL || broken(/* Windows 2003 */ Buffer[0] == 0xABAB), "Buffer[0] = 0x%x\n", Buffer[0]);
86 ok_eq_wchar(Buffer[1], 0xABAB);
87
88 /* Test a buffer size that doesn't fit the terminating NULL char */
89 memset(Buffer, 0xAB, sizeof(Buffer));
90 Length = GetEnvironmentVariableW(L"COMSPEC", Buffer, ActualLength);
91 ok_eq_ulong(Length, ActualLength + 1);
92 ok(Buffer[0] == UNICODE_NULL || broken(/* Windows 2003 */ Buffer[0] == 0xABAB), "Buffer[0] = 0x%x\n", Buffer[0]);
93 ok_eq_wchar(Buffer[1], 0xABAB);
94
95 /* Test a buffer size that is just large enough */
96 memset(Buffer, 0xAB, sizeof(Buffer));
97 Length = GetEnvironmentVariableW(L"COMSPEC", Buffer, ActualLength + 1);
98 ok_eq_ulong(Length, ActualLength);
99 ok_eq_wchar(Buffer[ActualLength], L'\0');
100 ok_eq_wchar(Buffer[ActualLength + 1], 0xABAB);
101
102 /* Test non-existant variable name */
103 memset(Buffer, 0xAB, sizeof(Buffer));
104 Length = GetEnvironmentVariableW(L"ThisVariableDoesNotExit", Buffer, ARRAYSIZE(Buffer));
106 ok_eq_wchar(Buffer[0], 0xABAB);
107
108 /* Test NULL variable name */
109 StartSeh()
113
114 /* Test NULL buffer with non-null size */
115 StartSeh()
118 ok((Length == ActualLength + 1) || broken(/* Windows 2003 */ Length == 0), "Length == %lu, ActualLength == %lu\n", Length, ActualLength);
119}
120
122{
125}
static void Test_GetEnvironmentVariableA(void)
static void Test_GetEnvironmentVariableW(void)
#define ok_eq_char(value, expected)
Definition: apitest.h:123
#define ok_eq_ulong(value, expected)
Definition: apitest.h:120
#define StartSeh()
Definition: apitest.h:93
#define ok_eq_wchar(value, expected)
Definition: apitest.h:124
#define EndSeh(ExpectedStatus)
Definition: apitest.h:99
#define ok(value,...)
Definition: atltest.h:57
#define broken(x)
Definition: atltest.h:178
#define START_TEST(x)
Definition: atltest.h:75
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define GetEnvironmentVariableW(x, y, z)
Definition: compat.h:755
#define MAX_PATH
Definition: compat.h:34
#define GetEnvironmentVariableA(x, y, z)
Definition: compat.h:754
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
#define L(x)
Definition: resources.c:13
#define STATUS_ACCESS_VIOLATION
#define UNICODE_NULL
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
short WCHAR
Definition: pedump.c:58
char CHAR
Definition: pedump.c:57
#define memset(x, y, z)
Definition: compat.h:39
#define STATUS_SUCCESS
Definition: shellext.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define GetEnvironmentVariable
Definition: winbase.h:3563