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 SetEnvironmentVariableA() 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",
56 GetLastError());
57 }
58
59 /* Set the second environment Variable */
60 bRc = SetEnvironmentVariableA(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 = GetEnvironmentVariableA(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 = (char*)malloc(sizeof(char)*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 GetEnvironmentVariable(FirstEnvironmentVariable,
87 pResultBuffer,
88 size);
89
90 if ( pResultBuffer == NULL )
91 {
92 free(pResultBuffer);
93 Fail("ERROR: GetEnvironmentVariable failed to return a value "
94 "from a proper environment variable with error %u.\n",
95 GetLastError());
96 }
97
98 /* Compare the strings to see that the correct variable was returned */
99 if(strcmp(pResultBuffer,FirstEnvironmentValue) != 0)
100 {
101 Trace("ERROR: The value in the buffer should have been '%s' but "
102 "was really '%s'.\n",FirstEnvironmentValue, pResultBuffer);
103 free(pResultBuffer);
104 Fail("");
105 }
106
107 free(pResultBuffer);
108
109 /* Reallocate the memory for the string */
110 pResultBuffer = (char*)malloc(sizeof(char)*size);
111 if ( pResultBuffer == NULL )
112 {
113 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
114 }
115
116 /* Try retrieving the value of the first variable, even though the
117 second variable has the same spelling and only differs in case */
118 GetEnvironmentVariable(SecondEnvironmentVariable,
119 pResultBuffer,
120 size);
121
122 if ( pResultBuffer == NULL )
123 {
124 Fail("ERROR: GetEnvironmentVariable failed to return a value "
125 "from a proper environment variable with error %u.\n",
126 GetLastError());
127 }
128
129 /* Compare the two strings to confirm that the right value is returned */
130 if(strcmp(pResultBuffer,SecondEnvironmentValue) != 0)
131 {
132 Trace("ERROR: The value in the buffer should have been '%s' but "
133 "was really '%s'.\n",SecondEnvironmentValue,pResultBuffer);
134 free(pResultBuffer);
135 Fail("");
136 }
137
138 free(pResultBuffer);
139
140 PAL_Terminate();
141 return PASS;
142
143#endif
144}
145