| 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: Create an environment variable and then use getenv to get |
| 10 | ** a pointer to it. Check that the pointer is valid and that the string |
| 11 | ** is what we expected. Also check that searching for a non-existent |
| 12 | ** variable will cause getenv to return NULL. Also check that function |
| 13 | ** passes when the parameter has it's casing changed (e.g upper case) |
| 14 | ** |
| 15 | ** |
| 16 | **===================================================================*/ |
| 17 | |
| 18 | #include <palsuite.h> |
| 19 | |
| 20 | int __cdecl main(int argc, char **argv) |
| 21 | { |
| 22 | |
| 23 | const char* SetVariable = "PalTestingEnvironmentVariable=The value" ; |
| 24 | const char* VariableName = "PalTestingEnvironmentVariable" ; |
| 25 | const char* VariableValue = "The value" ; |
| 26 | char* result; |
| 27 | |
| 28 | |
| 29 | if (0 != (PAL_Initialize(argc, argv))) |
| 30 | { |
| 31 | return FAIL; |
| 32 | } |
| 33 | |
| 34 | /* Use _putenv to set an environment variable. This ensures that the |
| 35 | variable we're testing on is always present. |
| 36 | */ |
| 37 | |
| 38 | if(_putenv(SetVariable) == -1) |
| 39 | { |
| 40 | Fail("ERROR: _putenv failed to set an environment variable that " |
| 41 | "getenv will be using for testing.\n" ); |
| 42 | } |
| 43 | |
| 44 | /* Call getenv -- ensure it doesn't return NULL and the string it returns |
| 45 | is the value we set above. |
| 46 | */ |
| 47 | |
| 48 | result = getenv(VariableName); |
| 49 | if(result == NULL) |
| 50 | { |
| 51 | Fail("ERROR: The result of getenv on a valid Environment Variable " |
| 52 | "was NULL, which indicates the environment varaible was not " |
| 53 | "found.\n" ); |
| 54 | } |
| 55 | |
| 56 | if(strcmp(result, VariableValue) != 0) |
| 57 | { |
| 58 | Fail("ERROR: The value obtained by getenv() was not equal to the " |
| 59 | "correct value of the environment variable. The correct " |
| 60 | "value is '%s' and the function returned '%s'.\n" , |
| 61 | VariableValue, |
| 62 | result); |
| 63 | } |
| 64 | |
| 65 | /* Try calling getenv on an environment variable which doesn't |
| 66 | exist. |
| 67 | */ |
| 68 | result = getenv("SomeEnvironmentVariableThatReallyDoesNotExist" ); |
| 69 | |
| 70 | if(result != NULL) |
| 71 | { |
| 72 | Fail("ERROR: Called getenv on an environment variable which " |
| 73 | "doesn't exist and it returned '%s' instead of NULL.\n" ,result); |
| 74 | } |
| 75 | |
| 76 | PAL_Terminate(); |
| 77 | return PASS; |
| 78 | } |
| 79 | |