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