| 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 | ** |
| 11 | ** |
| 12 | **=========================================================*/ |
| 13 | |
| 14 | #define UNICODE |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | int __cdecl main(int argc, char *argv[]) |
| 18 | { |
| 19 | |
| 20 | /* Define some buffers needed for the function */ |
| 21 | WCHAR * 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 = |
| 38 | GetEnvironmentVariable(convert("PATH" ), |
| 39 | pResultBuffer, |
| 40 | 0); |
| 41 | |
| 42 | pResultBuffer = (WCHAR*)malloc(size*sizeof(WCHAR)); |
| 43 | if ( pResultBuffer == NULL ) |
| 44 | { |
| 45 | Fail("ERROR: Failed to allocate memory for pResultBuffer pointer. " |
| 46 | "Can't properly exec test case without this.\n" ); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /* Normal case, PATH should fit into this buffer */ |
| 51 | ReturnValueForLargeBuffer = GetEnvironmentVariable(convert("PATH" ), |
| 52 | pResultBuffer, |
| 53 | size); |
| 54 | free(pResultBuffer); |
| 55 | |
| 56 | /* Ensure that it returned a positive value */ |
| 57 | if(ReturnValueForLargeBuffer <= 0) |
| 58 | { |
| 59 | Fail("The return was %d, which indicates that the function failed.\n" , |
| 60 | ReturnValueForLargeBuffer); |
| 61 | } |
| 62 | |
| 63 | /* Ensure that it succeeded and copied the correct number of characters. |
| 64 | If this is true, then the return value should be one less of the |
| 65 | size of the buffer. (Doesn't include that NULL byte) |
| 66 | */ |
| 67 | if(ReturnValueForLargeBuffer != size-1) |
| 68 | { |
| 69 | Fail("The value returned was %d when it should have been %d. This " |
| 70 | "should be the number of characters copied, " |
| 71 | "minus the NULL byte.\n" , ReturnValueForLargeBuffer, size-1); |
| 72 | } |
| 73 | |
| 74 | PAL_Terminate(); |
| 75 | return PASS; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |