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 : test.c
8**
9** Purpose: Test for GetEnvironmentVariable() function
10** Assign a properly sized buffer and get an environment
11** variable, check to ensure it returns the correct values.
12**
13**
14**=========================================================*/
15
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[]) {
19
20 /* Define some buffers needed for the function */
21 char * pResultBuffer = NULL;
22 int size = 0;
23
24 /* A place to stash the returned values */
25 int ReturnValueForLargeBuffer = 0;
26
27 /*
28 * Initialize the PAL and return FAILURE if this fails
29 */
30
31 if(0 != (PAL_Initialize(argc, argv)))
32 {
33 return FAIL;
34 }
35
36 /* Recieve and allocate the correct amount of memory for the buffer */
37 size = ReturnValueForLargeBuffer = GetEnvironmentVariable("PATH",
38 pResultBuffer,
39 0);
40 pResultBuffer = (char*)malloc(size);
41 if ( pResultBuffer == NULL )
42 {
43 Fail("ERROR: Failed to allocate memory for pResultBuffer pointer. "
44 "Can't properly exec test case without this.\n");
45 }
46
47
48 /* Normal case, PATH should fit into this buffer */
49 ReturnValueForLargeBuffer = GetEnvironmentVariable("PATH",
50 pResultBuffer,
51 size);
52
53 /* Ensure that it returned a positive value */
54 if(ReturnValueForLargeBuffer <= 0)
55 {
56 free(pResultBuffer);
57
58 Fail("The return was %d, which indicates that the function failed.\n",
59 ReturnValueForLargeBuffer);
60 }
61
62 /* Ensure that it succeeded and copied the correct number of characters.
63 If this is true, then the return value should be one less of the size of
64 the buffer. (Doesn't include that NULL byte)
65 */
66
67 if(ReturnValueForLargeBuffer != size-1)
68 {
69 free(pResultBuffer);
70
71 Fail("The value returned was %d when it should have been %d. "
72 "This should be the number of characters copied, minus the "
73 "NULL byte.\n",ReturnValueForLargeBuffer, size-1);
74 }
75
76
77 free(pResultBuffer);
78
79 PAL_Terminate();
80 return PASS;
81}
82
83
84
85