Updated the german translation
[gnupg.git] / common / t-session-env.c
blob94b6683285438b97692c2ae96ec3b4dc7e22e0a1
1 /* t-session-env.c - Module test for session-env.c
2 * Copyright (C) 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <assert.h>
26 #include "util.h"
27 #include "session-env.h"
29 #define pass() do { ; } while(0)
30 #define fail(e) do { fprintf (stderr, "%s:%d: function failed: %s\n", \
31 __FILE__,__LINE__, gpg_strerror (e)); \
32 exit (1); \
33 } while(0)
35 static int verbose;
37 static void
38 listall (session_env_t se)
40 int iterator = 0;
41 const char *name, *value;
42 int def;
44 if (verbose)
45 printf ("environment of %p\n", se);
46 while ( (name = session_env_listenv (se, &iterator, &value, &def)) )
47 if (verbose)
48 printf (" %s%s=%s\n", def? "[def] ":" ", name, value);
53 static void
54 show_stdnames (void)
56 const char *name, *assname;
57 int iterator = 0;
59 printf ("Known envvars:");
60 while ((name = session_env_list_stdenvnames (&iterator, &assname)))
62 printf ( " %s", name);
63 if (assname)
64 printf ( "(%s)", assname);
66 putchar('\n');
70 static void
71 test_all (void)
73 gpg_error_t err;
74 session_env_t se_0, se;
75 const char *s, *s2;
76 int idx;
78 se_0 = session_env_new ();
79 if (!se_0)
80 fail (gpg_error_from_syserror ());
81 se = session_env_new ();
82 if (!se)
83 fail (gpg_error_from_syserror ());
85 err = session_env_putenv (se, NULL);
86 if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
87 fail (err);
88 err = session_env_putenv (se, "");
89 if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
90 fail (err);
91 err = session_env_putenv (se, "=");
92 if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
93 fail (err);
95 /* Delete some nonexistant variables. */
96 err = session_env_putenv (se, "A");
97 if (err)
98 fail (err);
99 err = session_env_putenv (se, "a");
100 if (err)
101 fail (err);
102 err = session_env_putenv (se, "_aaaa aaaaaasssssssssssss\nddd");
103 if (err)
104 fail (err);
106 /* Create a few variables. */
107 err = session_env_putenv (se, "EMPTY=");
108 if (err)
109 fail (err);
110 err = session_env_putenv (se, "foo=value_of_foo");
111 if (err)
112 fail (err);
113 err = session_env_putenv (se, "bar=the value_of_bar");
114 if (err)
115 fail (err);
116 err = session_env_putenv (se, "baz=this-is-baz");
117 if (err)
118 fail (err);
119 err = session_env_putenv (se, "BAZ=this-is-big-baz");
120 if (err)
121 fail (err);
123 listall (se);
125 /* Update one. */
126 err = session_env_putenv (se, "baz=this-is-another-baz");
127 if (err)
128 fail (err);
130 listall (se);
132 /* Delete one. */
133 err = session_env_putenv (se, "bar");
134 if (err)
135 fail (err);
137 listall (se);
139 /* Insert a new one. */
140 err = session_env_putenv (se, "FOO=value_of_foo");
141 if (err)
142 fail (err);
144 listall (se);
146 /* Retrieve a default one. */
147 s = session_env_getenv_or_default (se, "HOME", NULL);
148 if (!s)
150 fprintf (stderr, "failed to get default of HOME\n");
151 exit (1);
154 s = session_env_getenv (se, "HOME");
155 if (s)
156 fail(0); /* This is a default value, thus we should not see it. */
158 s = session_env_getenv_or_default (se, "HOME", NULL);
159 if (!s)
160 fail(0); /* But here we should see it. */
162 /* Add a few more. */
163 err = session_env_putenv (se, "X1=A value");
164 if (err)
165 fail (err);
166 err = session_env_putenv (se, "X2=Another value");
167 if (err)
168 fail (err);
169 err = session_env_putenv (se, "X3=A value");
170 if (err)
171 fail (err);
173 listall (se);
175 /* Check that we can overwrite a default value. */
176 err = session_env_putenv (se, "HOME=/this/is/my/new/home");
177 if (err)
178 fail (err);
179 /* And that we get this string back. */
180 s = session_env_getenv (se, "HOME");
181 if (!s)
182 fail (0);
183 if (strcmp (s, "/this/is/my/new/home"))
184 fail (0);
185 /* A new get default should return the very same string. */
186 s2 = session_env_getenv_or_default (se, "HOME", NULL);
187 if (!s2)
188 fail (0);
189 if (s2 != s)
190 fail (0);
192 listall (se);
194 /* Check that the other object is clean. */
196 int iterator = 0;
198 if (session_env_listenv (se_0, &iterator, NULL, NULL))
199 fail (0);
203 session_env_release (se);
205 /* Use a new session for quick mass test. */
206 se = session_env_new ();
207 if (!se)
208 fail (gpg_error_from_syserror ());
210 /* Create. */
211 for (idx=0; idx < 500; idx++)
213 char buf[100];
215 snprintf (buf, sizeof buf, "FOO_%d=Value for %x", idx, idx);
216 err = session_env_putenv (se, buf);
217 if (err)
218 fail (err);
220 err = session_env_setenv (se, "TEST1", "value1");
221 if (err)
222 fail (err);
223 err = session_env_setenv (se, "TEST1", "value1-updated");
224 if (err)
225 fail (err);
227 listall (se);
229 /* Delete all. */
230 for (idx=0; idx < 500; idx++)
232 char buf[100];
234 snprintf (buf, sizeof buf, "FOO_%d", idx);
235 err = session_env_putenv (se, buf);
236 if (err)
237 fail (err);
239 err = session_env_setenv (se, "TEST1", NULL);
240 if (err)
241 fail (err);
243 /* Check that all are deleted. */
245 int iterator = 0;
247 if (session_env_listenv (se, &iterator, NULL, NULL))
248 fail (0);
251 /* Add a few strings again. */
252 for (idx=0; idx < 500; idx++)
254 char buf[100];
256 if (!(idx % 10))
258 if ( !(idx % 3))
259 snprintf (buf, sizeof buf, "FOO_%d=", idx);
260 else
261 snprintf (buf, sizeof buf, "FOO_%d=new value for %x", idx, idx);
262 err = session_env_putenv (se, buf);
263 if (err)
264 fail (err);
268 listall (se);
270 session_env_release (se);
272 session_env_release (se_0);
278 main (int argc, char **argv)
280 if (argc)
281 { argc--; argv++; }
282 if (argc && !strcmp (argv[0], "--verbose"))
284 verbose = 1;
285 argc--; argv++;
289 show_stdnames ();
290 test_all ();
292 return 0;