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**
7** Source: test1.c
8**
9** Purpose: Test for SetEnvironmentVariableA() function. Set an
10** environment variable and check to ensure success was returned. Then
11** get the environment variable and compare to the correct value. Also, check
12** that calling the function again, resets the variable properly. And that
13** calling with NULL deletes the variable.
14**
15** Depends:
16** GetEnvironmentVariable
17** memcmp
18** memset
19** strlen
20**
21**
22**=========================================================*/
23
24#include <palsuite.h>
25#define BUF_SIZE 128
26
27int __cdecl main(int argc, char *argv[])
28{
29
30 /* Define some buffers needed for the function */
31 char* VariableBuffer = "PALTEST";
32 char* ValueBuffer = "Testing";
33 char* SecondValueBuffer = "SecondTest";
34 char NewValue[BUF_SIZE];
35 int SetResult = 0;
36
37 /*
38 * Initialize the PAL and return FAILURE if this fails
39 */
40
41 if(0 != (PAL_Initialize(argc, argv)))
42 {
43 return FAIL;
44 }
45
46
47
48 /*
49 Test #1
50 =======
51 */
52
53 SetResult = SetEnvironmentVariable(VariableBuffer,
54 ValueBuffer);
55
56 /* If result is 0, the SetEnviron function failed */
57 if(SetResult == 0)
58 {
59 Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that "
60 "it failed, even though it should have succeeded in setting the "
61 "variable PALTEST.\n");
62 }
63
64 memset(NewValue,0,BUF_SIZE);
65
66 /* Grab the Environment variable we just set */
67 if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) <= 0)
68 {
69 Fail("ERROR: GetEnvironmentVariable returned 0 or less, which "
70 "indicates that no value was read in from the given variable.");
71 }
72
73 /* Make sure that the value put into NewValue was indeed the environment
74 variable we set.
75 */
76
77 if(memcmp(NewValue,ValueBuffer,strlen(ValueBuffer)+1) != 0)
78 {
79 Fail("ERROR: When retrieving the variable that was just set, a "
80 "difference was found. Instead of the value being '%s' it "
81 "was instead '%s'.\n",ValueBuffer,NewValue);
82 }
83
84
85
86 /*
87 Test #2
88 =======
89 */
90
91 /* If we set the same environment variable with a different value, the
92 old value should be replaced.
93 */
94
95 SetResult = SetEnvironmentVariable(VariableBuffer,
96 SecondValueBuffer);
97
98 /* If result is 0, the SetEnviron function failed */
99 if(SetResult == 0)
100 {
101 Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that "
102 "it failed, even though it should have succeeded in re-setting "
103 "the variable PALTEST.\n");
104 }
105
106 memset(NewValue,0,BUF_SIZE);
107
108 /* Grab the Environment variable we just set */
109 if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) <= 0)
110 {
111 Fail("ERROR: GetEnvironmentVariable returned 0 or less, which "
112 "indicates that no value was read in from the given variable.");
113 }
114
115 /* Make sure that the value put into NewValue was indeed the environment
116 variable we set.
117 */
118
119 if(memcmp(NewValue,SecondValueBuffer,strlen(SecondValueBuffer)+1) != 0)
120 {
121 Fail("ERROR: When retrieving the variable that was just set, a "
122 "difference was found. Instead of the value being '%s' it "
123 "was instead '%s'.\n",SecondValueBuffer,NewValue);
124 }
125
126
127
128 /*
129 Test #3
130 =======
131 */
132
133 /* Finally, set this variable with NULL, which should delete it from the
134 current environment.
135 */
136
137 SetResult = SetEnvironmentVariable(VariableBuffer, NULL);
138
139 /* If result is 0, the SetEnviron function failed */
140 if(SetResult == 0)
141 {
142 Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that "
143 "it failed, even though it should have succeeded in deleting "
144 "the variable PALTEST.\n");
145 }
146
147 memset(NewValue,0,BUF_SIZE);
148
149 /* Grab the Environment variable we just set, ensure that it's
150 empty now.
151 */
152 if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) != 0)
153 {
154 Fail("ERROR: GetEnvironmentVariable returned a non-zero value, "
155 "even though the environment variable which was checked should "
156 "have been empty.");
157 }
158
159
160 /*
161 Clean Up
162 */
163 PAL_Terminate();
164 return PASS;
165}
166
167
168
169