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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* t-session-env.c - Module test for session-env.c
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include "util.h"
#include "session-env.h"
#define pass() do { ; } while(0)
#define fail(e) do { fprintf (stderr, "%s:%d: function failed: %s\n", \
__FILE__,__LINE__, gpg_strerror (e)); \
exit (1); \
} while(0)
static int verbose;
static void
listall (session_env_t se)
{
int iterator = 0;
const char *name, *value;
int def;
if (verbose)
printf ("environment of %p\n", se);
while ( (name = session_env_listenv (se, &iterator, &value, &def)) )
if (verbose)
printf (" %s%s=%s\n", def? "[def] ":" ", name, value);
}
static void
show_stdnames (void)
{
const char *name, *assname;
int iterator = 0;
printf ("Known envvars:");
while ((name = session_env_list_stdenvnames (&iterator, &assname)))
{
printf ( " %s", name);
if (assname)
printf ( "(%s)", assname);
}
putchar('\n');
}
static void
test_all (void)
{
gpg_error_t err;
session_env_t se_0, se;
const char *s, *s2;
int idx;
se_0 = session_env_new ();
if (!se_0)
fail (gpg_error_from_syserror ());
se = session_env_new ();
if (!se)
fail (gpg_error_from_syserror ());
err = session_env_putenv (se, NULL);
if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
fail (err);
err = session_env_putenv (se, "");
if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
fail (err);
err = session_env_putenv (se, "=");
if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
fail (err);
/* Delete some nonexistant variables. */
err = session_env_putenv (se, "A");
if (err)
fail (err);
err = session_env_putenv (se, "a");
if (err)
fail (err);
err = session_env_putenv (se, "_aaaa aaaaaasssssssssssss\nddd");
if (err)
fail (err);
/* Create a few variables. */
err = session_env_putenv (se, "EMPTY=");
if (err)
fail (err);
err = session_env_putenv (se, "foo=value_of_foo");
if (err)
fail (err);
err = session_env_putenv (se, "bar=the value_of_bar");
if (err)
fail (err);
err = session_env_putenv (se, "baz=this-is-baz");
if (err)
fail (err);
err = session_env_putenv (se, "BAZ=this-is-big-baz");
if (err)
fail (err);
listall (se);
/* Update one. */
err = session_env_putenv (se, "baz=this-is-another-baz");
if (err)
fail (err);
listall (se);
/* Delete one. */
err = session_env_putenv (se, "bar");
if (err)
fail (err);
listall (se);
/* Insert a new one. */
err = session_env_putenv (se, "FOO=value_of_foo");
if (err)
fail (err);
listall (se);
/* Retrieve a default one. */
s = session_env_getenv_or_default (se, "HOME", NULL);
if (!s)
{
fprintf (stderr, "failed to get default of HOME\n");
exit (1);
}
s = session_env_getenv (se, "HOME");
if (s)
fail(0); /* This is a default value, thus we should not see it. */
s = session_env_getenv_or_default (se, "HOME", NULL);
if (!s)
fail(0); /* But here we should see it. */
/* Add a few more. */
err = session_env_putenv (se, "X1=A value");
if (err)
fail (err);
err = session_env_putenv (se, "X2=Another value");
if (err)
fail (err);
err = session_env_putenv (se, "X3=A value");
if (err)
fail (err);
listall (se);
/* Check that we can overwrite a default value. */
err = session_env_putenv (se, "HOME=/this/is/my/new/home");
if (err)
fail (err);
/* And that we get this string back. */
s = session_env_getenv (se, "HOME");
if (!s)
fail (0);
if (strcmp (s, "/this/is/my/new/home"))
fail (0);
/* A new get default should return the very same string. */
s2 = session_env_getenv_or_default (se, "HOME", NULL);
if (!s2)
fail (0);
if (s2 != s)
fail (0);
listall (se);
/* Check that the other object is clean. */
{
int iterator = 0;
if (session_env_listenv (se_0, &iterator, NULL, NULL))
fail (0);
}
session_env_release (se);
/* Use a new session for quick mass test. */
se = session_env_new ();
if (!se)
fail (gpg_error_from_syserror ());
/* Create. */
for (idx=0; idx < 500; idx++)
{
char buf[100];
snprintf (buf, sizeof buf, "FOO_%d=Value for %x", idx, idx);
err = session_env_putenv (se, buf);
if (err)
fail (err);
}
err = session_env_setenv (se, "TEST1", "value1");
if (err)
fail (err);
err = session_env_setenv (se, "TEST1", "value1-updated");
if (err)
fail (err);
listall (se);
/* Delete all. */
for (idx=0; idx < 500; idx++)
{
char buf[100];
snprintf (buf, sizeof buf, "FOO_%d", idx);
err = session_env_putenv (se, buf);
if (err)
fail (err);
}
err = session_env_setenv (se, "TEST1", NULL);
if (err)
fail (err);
/* Check that all are deleted. */
{
int iterator = 0;
if (session_env_listenv (se, &iterator, NULL, NULL))
fail (0);
}
/* Add a few strings again. */
for (idx=0; idx < 500; idx++)
{
char buf[100];
if (!(idx % 10))
{
if ( !(idx % 3))
snprintf (buf, sizeof buf, "FOO_%d=", idx);
else
snprintf (buf, sizeof buf, "FOO_%d=new value for %x", idx, idx);
err = session_env_putenv (se, buf);
if (err)
fail (err);
}
}
listall (se);
session_env_release (se);
session_env_release (se_0);
}
int
main (int argc, char **argv)
{
if (argc)
{ argc--; argv++; }
if (argc && !strcmp (argv[0], "--verbose"))
{
verbose = 1;
argc--; argv++;
}
show_stdnames ();
test_all ();
return 0;
}
|