1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================
6** Source : test5.c
7**
8** Purpose: Test for GetEnvironmentVariableW() function
9** Create environment variables that differ only
10** in case and verify that they return the appropriate
11** value on the BSD environment.
12**
13**
14===========================================================*/
15
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[])
19{
20
21#if WIN32
22
23 return PASS;
24
25#else
26
27 /* Define some buffers needed for the function */
28 WCHAR * pResultBuffer = NULL;
29
30 WCHAR FirstEnvironmentVariable[] = {'P','A','L','T','E','S','T','\0'};
31 WCHAR FirstEnvironmentValue[] = {'F','I','R','S','T','\0'};
32
33 WCHAR SecondEnvironmentVariable[] = {'p','a','l','t','e','s','t','\0'};
34 WCHAR SecondEnvironmentValue[] = {'S','E','C','O','N','D','\0'};
35
36 DWORD size = 0;
37 BOOL bRc = TRUE;
38
39 /*
40 * Initialize the PAL and return FAILURE if this fails
41 */
42
43 if(0 != (PAL_Initialize(argc, argv)))
44 {
45 return FAIL;
46 }
47
48 /* Set the first environment variable */
49 bRc = SetEnvironmentVariableW(FirstEnvironmentVariable,
50 FirstEnvironmentValue);
51
52 if(!bRc)
53 {
54 Fail("ERROR: SetEnvironmentVariable failed to set a "
55 "proper environment variable with error %u.\n", GetLastError());
56 }
57
58 /* Normal case, PATH should fit into this buffer */
59 size = GetEnvironmentVariableW(FirstEnvironmentVariable,
60 pResultBuffer,
61 0);
62
63 /* To account for the nul character at the end of the string */
64 size = size + 1;
65
66 pResultBuffer = (WCHAR*)malloc(sizeof(WCHAR)*size);
67 if ( pResultBuffer == NULL )
68 {
69 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
70 }
71
72 /* Try to retrieve the value of the first environment variable */
73 GetEnvironmentVariableW(FirstEnvironmentVariable,
74 pResultBuffer,
75 size);
76
77 if ( pResultBuffer == NULL )
78 {
79 free(pResultBuffer);
80 Fail("ERROR: GetEnvironmentVariable failed to return a value "
81 "from a proper environment variable with error %u.\n",
82 GetLastError());
83 }
84
85 /* Compare the strings to see that the correct variable was returned */
86 if(wcsncmp(pResultBuffer,FirstEnvironmentValue,wcslen(pResultBuffer)) != 0)
87 {
88 free(pResultBuffer);
89 Fail("ERROR: The value in the buffer should have been '%S' but "
90 "was really '%S'.\n",FirstEnvironmentValue, pResultBuffer);
91 }
92
93 free(pResultBuffer);
94
95 /* Set the second environment Variable */
96 bRc = SetEnvironmentVariableW(SecondEnvironmentVariable,
97 SecondEnvironmentValue);
98
99 if(!bRc)
100 {
101 Fail("ERROR: SetEnvironmentVariable failed to set a "
102 "proper environment variable with error %u.\n",
103 GetLastError());
104 }
105
106 /* Reallocate the memory for the string */
107 pResultBuffer = (WCHAR*)malloc(sizeof(WCHAR)*size);
108 if ( pResultBuffer == NULL )
109 {
110 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
111 }
112
113 /* Try retrieving the value of the first variable, even though the
114 second variable has the same spelling and only differs in case */
115 GetEnvironmentVariableW(FirstEnvironmentVariable,
116 pResultBuffer,
117 size);
118
119 if ( pResultBuffer == NULL )
120 {
121 free(pResultBuffer);
122 Fail("ERROR: GetEnvironmentVariable failed to return a value "
123 "from a proper environment variable with error %u.\n",
124 GetLastError());
125 }
126
127 /* Compare the two strings to confirm that the right value is returned */
128 if(wcsncmp(pResultBuffer,FirstEnvironmentValue,wcslen(pResultBuffer)) != 0)
129 {
130 free(pResultBuffer);
131 Fail("ERROR: The value in the buffer should have been '%S' but "
132 "was really '%S'.\n",FirstEnvironmentValue,pResultBuffer);
133 }
134
135 free(pResultBuffer);
136
137 PAL_Terminate();
138 return PASS;
139
140#endif
141}
142
143
144
145