summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_putenv/test1/test1.c
blob: 2d096adc78cddf5bd705b28e43dd24cd9b24a788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  test1.c
**
** Purpose:  Create an environment variable with _putenv and then use getenv 
** to check it.  Check that we get the expected errors with invalid input.
**
**
**===================================================================*/

#include <palsuite.h>

struct TestElement
{
    char _putenvString[1024];    /* argument string sent to putenv        */  
    char varName[1024];          /* variable component of argument string */
    char varValue[1024];         /* value component of argument string    */
    BOOL bValidString;           /* valid argument string identifier      */
};

struct TestElement TestCases[] = 
{
    {"PalTestingEnvironmentVariable=A value", "PalTestingEnvironmentVariable",
     "A value", TRUE},
    {"AnotherVariable=", "AnotherVariable", "", TRUE},
    {"YetAnotherVariable", "", "", FALSE},
    {"=ADifferentVariable", "", "ADifferentVariable", FALSE},
    {"", "", "", FALSE}

};

int __cdecl main(int argc, char **argv)
{

    int i;
    char *variableValue;
   
    if (0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
    
    for (i = 0; i < (sizeof(TestCases)/sizeof(struct TestElement)) ; i++) 
    {
        if((_putenv(TestCases[i]._putenvString) == -1) && 
           ( TestCases[i].bValidString == TRUE))
        {
            Fail("ERROR: _putenv failed to set an environment "
                 "variable with a valid format.\n  Call was"
                 "_putenv(%s)\n", TestCases[i]._putenvString);
        }
        /* 
         * For valid _putenvString values, check to see the variable was set
         */
        if (TestCases[i].bValidString == TRUE)
        {       
            variableValue = getenv(TestCases[i].varName);
        
            if (variableValue == NULL)
            { 
                if (*TestCases[i].varValue != '\0')
                {
                    Fail("ERROR: getenv(%s) call returned NULL.\nThe call "
                         "should have returned \"%s\"\n", TestCases[i].varName
                         , TestCases[i].varValue);
                }
            }  
            else if ( strcmp(variableValue, TestCases[i].varValue) != 0) 
            {
                Fail("ERROR: _putenv(%s)\nshould have set the variable "
                     "%s\n to \"%s\".\nA subsequent call to getenv(%s)\n"
                     "returned \"%s\" instead.\n", TestCases[i]._putenvString
                     , TestCases[i].varName, TestCases[i].varValue
                     , TestCases[i].varName, variableValue);
            }
        }
        else 
            /*
             * Check to see that putenv fails for malformed _putenvString values
             */
        {
            variableValue = getenv(TestCases[i].varName);
        
            if (variableValue != NULL)
            { 
                Fail("ERROR: getenv(%s) call should have returned NULL.\n"
                     "Instead it returned \"%s\".\n", TestCases[i].varName
                     , TestCases[i].varValue);
            }
        }
    }

    PAL_Terminate();
    return PASS;
}