| 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: |
| 10 | ** Set an 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 | ** |
| 16 | **=========================================================*/ |
| 17 | |
| 18 | #define UNICODE |
| 19 | #define BUF_SIZE 128 |
| 20 | |
| 21 | #include <palsuite.h> |
| 22 | |
| 23 | /* Depends on GetEnvironmentVariable */ |
| 24 | |
| 25 | int __cdecl main(int argc, char *argv[]) |
| 26 | { |
| 27 | |
| 28 | /* Define some buffers needed for the function */ |
| 29 | WCHAR VariableBuffer[] = {'P','A','L','T','E','S','T','\0'}; |
| 30 | WCHAR ValueBuffer[] = {'T','e','s','t','i','n','g','\0'}; |
| 31 | WCHAR SecondValueBuffer[] = {'S','e','c','o','n','d','T','e','s','t','\0'}; |
| 32 | WCHAR NewValue[BUF_SIZE]; |
| 33 | int SetResult = 0; |
| 34 | |
| 35 | /* |
| 36 | * Initialize the PAL and return FAILURE if this fails |
| 37 | */ |
| 38 | |
| 39 | if(0 != (PAL_Initialize(argc, argv))) |
| 40 | { |
| 41 | return FAIL; |
| 42 | } |
| 43 | |
| 44 | SetResult = SetEnvironmentVariable(VariableBuffer, |
| 45 | ValueBuffer); |
| 46 | |
| 47 | /* If result is 0, the SetEnviron function failed */ |
| 48 | if(SetResult == 0) |
| 49 | { |
| 50 | Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that " |
| 51 | "it failed, even though it should have succeeded in setting the " |
| 52 | "variable PALTEST.\n" ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Grab the Environment variable we just set */ |
| 57 | if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) <= 0) |
| 58 | { |
| 59 | Fail("ERROR: GetEnvironmentVariable returned 0 or less, which " |
| 60 | "indicates that no value was read in from the given variable." ); |
| 61 | } |
| 62 | |
| 63 | /* Make sure that the value put into NewValue was indeed the environment |
| 64 | variable we set. |
| 65 | */ |
| 66 | |
| 67 | if(memcmp(NewValue,ValueBuffer,wcslen(ValueBuffer)*sizeof(WCHAR)+2) != 0) |
| 68 | { |
| 69 | Fail("ERROR: When retrieving the variable that was just set, a " |
| 70 | "difference was found. Instead of the value being '%s' it " |
| 71 | "was instead '%s'.\n" ,convertC(ValueBuffer),convertC(NewValue)); |
| 72 | } |
| 73 | |
| 74 | /* If we set the same environment variable with a different value, the |
| 75 | old value should be replaced. |
| 76 | */ |
| 77 | |
| 78 | SetResult = SetEnvironmentVariable(VariableBuffer, |
| 79 | SecondValueBuffer); |
| 80 | |
| 81 | /* If result is 0, the SetEnviron function failed */ |
| 82 | if(SetResult == 0) |
| 83 | { |
| 84 | Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that " |
| 85 | "it failed, even though it should have succeeded in re-setting " |
| 86 | "the variable PALTEST.\n" ); |
| 87 | } |
| 88 | |
| 89 | memset(NewValue,0,BUF_SIZE); |
| 90 | |
| 91 | /* Grab the Environment variable we just set */ |
| 92 | if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) <= 0) |
| 93 | { |
| 94 | Fail("ERROR: GetEnvironmentVariable returned 0 or less, which " |
| 95 | "indicates that no value was read in from the given variable." ); |
| 96 | } |
| 97 | |
| 98 | /* Make sure that the value put into NewValue was indeed the environment |
| 99 | variable we set. |
| 100 | */ |
| 101 | |
| 102 | if(memcmp(NewValue,SecondValueBuffer, |
| 103 | wcslen(SecondValueBuffer)*sizeof(WCHAR)+2) != 0) |
| 104 | { |
| 105 | Fail("ERROR: When retrieving the variable that was just set, a " |
| 106 | "difference was found. Instead of the value being '%s' it " |
| 107 | "was instead '%s'.\n" , |
| 108 | convertC(SecondValueBuffer),convertC(NewValue)); |
| 109 | } |
| 110 | |
| 111 | /* Finally, set this variable with NULL, which should delete it from the |
| 112 | current environment. |
| 113 | */ |
| 114 | |
| 115 | SetResult = SetEnvironmentVariable(VariableBuffer, NULL); |
| 116 | |
| 117 | /* If result is 0, the SetEnviron function failed */ |
| 118 | if(SetResult == 0) |
| 119 | { |
| 120 | Fail("ERROR: SetEnvironmentVariable returned 0, which indicates that " |
| 121 | "it failed, even though it should have succeeded in deleting " |
| 122 | "the variable PALTEST.\n" ); |
| 123 | } |
| 124 | |
| 125 | memset(NewValue,0,BUF_SIZE); |
| 126 | |
| 127 | /* Grab the Environment variable we just set, ensure that it's |
| 128 | empty now. |
| 129 | */ |
| 130 | if(GetEnvironmentVariable(VariableBuffer,NewValue,BUF_SIZE) != 0) |
| 131 | { |
| 132 | Fail("ERROR: GetEnvironmentVariable returned a non-zero value, " |
| 133 | "even though the environment variable which was checked should " |
| 134 | "have been empty." ); |
| 135 | } |
| 136 | |
| 137 | PAL_Terminate(); |
| 138 | return PASS; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |