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 : test4.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 /* Define some buffers needed for the function */
24 WCHAR * pResultBuffer = NULL;
25
26 WCHAR FirstEnvironmentVariable[] = {'P','A','L','T','E','S','T','\0'};
27 WCHAR FirstEnvironmentValue[] = {'F','I','R','S','T','\0'};
28
29 WCHAR ModifiedEnvironmentVariable[] = {'p','a','l','t','e','s','t','\0'};
30
31 DWORD size = 0;
32 BOOL bRc = TRUE;
33
34 /*
35 * Initialize the PAL and return FAILURE if this fails
36 */
37
38 if(0 != (PAL_Initialize(argc, argv)))
39 {
40 return FAIL;
41 }
42
43 /* Set the first environment variable */
44 bRc = SetEnvironmentVariableW(FirstEnvironmentVariable,
45 FirstEnvironmentValue);
46
47 if(!bRc)
48 {
49 Fail("ERROR: SetEnvironmentVariable failed to set a "
50 "proper environment variable with error %u.\n",
51 GetLastError());
52 }
53
54 /* Normal case, PATH should fit into this buffer */
55 size = GetEnvironmentVariableW(ModifiedEnvironmentVariable,
56 pResultBuffer,
57 0);
58
59 /* To account for the nul character at the end of the string */
60 size = size + 1;
61
62 pResultBuffer = (WCHAR*)malloc(sizeof(WCHAR)*size);
63 if ( pResultBuffer == NULL )
64 {
65 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer.\n");
66 }
67
68 /* Try to retrieve the value of the first environment variable */
69 GetEnvironmentVariableW(ModifiedEnvironmentVariable,
70 pResultBuffer,
71 size);
72
73 if ( pResultBuffer == NULL )
74 {
75 Fail("ERROR: GetEnvironmentVariable failed to return a value "
76 "from a proper environment variable with error %u.\n",
77 GetLastError());
78 }
79
80 /* Compare the strings to see that the correct variable was returned */
81 if(wcsncmp(pResultBuffer,FirstEnvironmentValue,wcslen(pResultBuffer)) != 0)
82 {
83 Trace("ERROR: The value in the buffer should have been '%S' but "
84 "was really '%S'.\n",FirstEnvironmentValue, pResultBuffer);
85 free(pResultBuffer);
86 Fail("");
87 }
88
89 free(pResultBuffer);
90
91 PAL_Terminate();
92 return PASS;
93
94
95#else
96
97 return PASS;
98#endif
99}
100