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 : test3.c
7**
8** Purpose: Test for SetEnvironmentVariableW() 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",
56 GetLastError());
57 }
58
59 /* Set the second environment Variable */
60 bRc = SetEnvironmentVariableW(SecondEnvironmentVariable,
61 SecondEnvironmentValue);
62
63 if(!bRc)
64 {
65 Fail("ERROR: SetEnvironmentVariable failed to set a "
66 "proper environment variable with error %u.\n",
67 GetLastError());
68 }
69
70
71 /* Normal case, PATH should fit into this buffer */
72 size = GetEnvironmentVariableW(FirstEnvironmentVariable,
73 pResultBuffer,
74 0);
75
76 /* Increase size to account for the null char at the end */
77 size = size + 1;
78
79 pResultBuffer = (WCHAR*)malloc(sizeof(WCHAR)*size);
80 if ( pResultBuffer == NULL )
81 {
82 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
83 }
84
85 /* Try to retrieve the value of the first environment variable */
86 GetEnvironmentVariableW(FirstEnvironmentVariable,
87 pResultBuffer,
88 size);
89
90 if ( pResultBuffer == NULL )
91 {
92 Fail("ERROR: GetEnvironmentVariable failed to return a value "
93 "from a proper environment variable with error %u.\n",
94 GetLastError());
95 }
96
97 /* Compare the strings to see that the correct variable was returned */
98 if(wcscmp(pResultBuffer,FirstEnvironmentValue) != 0)
99 {
100 Trace("ERROR: The value in the buffer should have been '%S' but "
101 "was really '%S'.\n",FirstEnvironmentValue, pResultBuffer);
102 free(pResultBuffer);
103 Fail("");
104 }
105
106 free(pResultBuffer);
107
108 /* Reallocate the memory for the string */
109 pResultBuffer = (WCHAR*)malloc(sizeof(WCHAR)*size);
110 if ( pResultBuffer == NULL )
111 {
112 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
113 }
114
115 /* Try retrieving the value of the first variable, even though the
116 second variable has the same spelling and only differs in case */
117 GetEnvironmentVariableW(SecondEnvironmentVariable,
118 pResultBuffer,
119 size);
120
121 if ( pResultBuffer == NULL )
122 {
123 Fail("ERROR: GetEnvironmentVariable failed to return a value "
124 "from a proper environment variable with error %u.\n",
125 GetLastError());
126 }
127
128 /* Compare the two strings to confirm that the right value is returned */
129 if(wcscmp(pResultBuffer,SecondEnvironmentValue) != 0)
130 {
131 Trace("ERROR: The value in the buffer should have been '%S' but "
132 "was really '%S'.\n",SecondEnvironmentValue,pResultBuffer);
133 free(pResultBuffer);
134 Fail("");
135 }
136
137 free(pResultBuffer);
138
139 PAL_Terminate();
140 return PASS;
141
142#endif
143}
144