summaryrefslogtreecommitdiff
path: root/src/bin/evil/evil_test_environment.c
blob: 0d31b7912a4b8c102586d8ccd5edf60780e5e2e8 (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <string.h>

#include <Evil.h>

#include "evil_suite.h"
#include "evil_test_environment.h"


static int
test_env_test_setenv_NULL(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV", NULL, 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");

   return val ? 0 : 1;
}

static int
test_env_test_setenv_NULL_after_set(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV", "val", 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");
   if (!val)
     return 0;

   if (strcmp(val, "val"))
     return 0;

   res = setenv("EVIL_TEST_ENV", NULL, 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");

   return val ? 0 : 1;
}

static int
test_env_test_getenv_one(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV", "val", 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");
   if (!val)
     return 0;

   if (strcmp(val, "val"))
     return 0;

   return 1;
}

static int
test_env_test_getenv_two(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV1", "val1", 1);
   if (res < 0)
     return 0;

   res = setenv("EVIL_TEST_ENV2", "val2", 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV1");
   if (!val)
     return 0;
   if (strcmp(val, "val1"))
     return 0;

   val = getenv("EVIL_TEST_ENV2");
   if (!val)
     return 0;

   if (strcmp(val, "val2"))
     return 0;

   return 1;
}

static int
test_env_test_getenv_two_swapped(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV1", "val1", 1);
   if (res < 0)
     return 0;

   res = setenv("EVIL_TEST_ENV2", "val2", 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV2");
   if (!val)
     return 0;
   if (strcmp(val, "val2"))
     return 0;

   val = getenv("EVIL_TEST_ENV1");
   if (!val)
     return 0;

   if (strcmp(val, "val1"))
     return 0;

   return 1;
}

static int
test_env_test_unsetenv(void)
{
   char *val;
   int   res;

   res = setenv("EVIL_TEST_ENV", "val", 1);
   if (res < 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");
   if (!val)
     return 0;

   if (unsetenv("EVIL_TEST_ENV") != 0)
     return 0;

   val = getenv("EVIL_TEST_ENV");
   if (val)
     return 0;

   return 1;
}

static int
test_env_tests_run(suite *s)
{
   int res;

   res  = test_env_test_setenv_NULL();
   res &= test_env_test_setenv_NULL_after_set();
   res &= test_env_test_getenv_one();
   res &= test_env_test_getenv_two();
   res &= test_env_test_getenv_two_swapped();
   res &= test_env_test_unsetenv();

   return res;
}

int
test_environment(suite *s)
{

   return test_env_tests_run(s);
}