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/>.
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)); \
38 listall (session_env_t se
)
41 const char *name
, *value
;
45 printf ("environment of %p\n", se
);
46 while ( (name
= session_env_listenv (se
, &iterator
, &value
, &def
)) )
48 printf (" %s%s=%s\n", def
? "[def] ":" ", name
, value
);
56 const char *name
, *assname
;
59 printf ("Known envvars:");
60 while ((name
= session_env_list_stdenvnames (&iterator
, &assname
)))
62 printf ( " %s", name
);
64 printf ( "(%s)", assname
);
74 session_env_t se_0
, se
;
78 se_0
= session_env_new ();
80 fail (gpg_error_from_syserror ());
81 se
= session_env_new ();
83 fail (gpg_error_from_syserror ());
85 err
= session_env_putenv (se
, NULL
);
86 if (gpg_err_code (err
) != GPG_ERR_INV_VALUE
)
88 err
= session_env_putenv (se
, "");
89 if (gpg_err_code (err
) != GPG_ERR_INV_VALUE
)
91 err
= session_env_putenv (se
, "=");
92 if (gpg_err_code (err
) != GPG_ERR_INV_VALUE
)
95 /* Delete some nonexistant variables. */
96 err
= session_env_putenv (se
, "A");
99 err
= session_env_putenv (se
, "a");
102 err
= session_env_putenv (se
, "_aaaa aaaaaasssssssssssss\nddd");
106 /* Create a few variables. */
107 err
= session_env_putenv (se
, "EMPTY=");
110 err
= session_env_putenv (se
, "foo=value_of_foo");
113 err
= session_env_putenv (se
, "bar=the value_of_bar");
116 err
= session_env_putenv (se
, "baz=this-is-baz");
119 err
= session_env_putenv (se
, "BAZ=this-is-big-baz");
126 err
= session_env_putenv (se
, "baz=this-is-another-baz");
133 err
= session_env_putenv (se
, "bar");
139 /* Insert a new one. */
140 err
= session_env_putenv (se
, "FOO=value_of_foo");
146 /* Retrieve a default one. */
147 s
= session_env_getenv_or_default (se
, "HOME", NULL
);
150 fprintf (stderr
, "failed to get default of HOME\n");
154 s
= session_env_getenv (se
, "HOME");
156 fail(0); /* This is a default value, thus we should not see it. */
158 s
= session_env_getenv_or_default (se
, "HOME", NULL
);
160 fail(0); /* But here we should see it. */
162 /* Add a few more. */
163 err
= session_env_putenv (se
, "X1=A value");
166 err
= session_env_putenv (se
, "X2=Another value");
169 err
= session_env_putenv (se
, "X3=A value");
175 /* Check that we can overwrite a default value. */
176 err
= session_env_putenv (se
, "HOME=/this/is/my/new/home");
179 /* And that we get this string back. */
180 s
= session_env_getenv (se
, "HOME");
183 if (strcmp (s
, "/this/is/my/new/home"))
185 /* A new get default should return the very same string. */
186 s2
= session_env_getenv_or_default (se
, "HOME", NULL
);
194 /* Check that the other object is clean. */
198 if (session_env_listenv (se_0
, &iterator
, NULL
, NULL
))
203 session_env_release (se
);
205 /* Use a new session for quick mass test. */
206 se
= session_env_new ();
208 fail (gpg_error_from_syserror ());
211 for (idx
=0; idx
< 500; idx
++)
215 snprintf (buf
, sizeof buf
, "FOO_%d=Value for %x", idx
, idx
);
216 err
= session_env_putenv (se
, buf
);
220 err
= session_env_setenv (se
, "TEST1", "value1");
223 err
= session_env_setenv (se
, "TEST1", "value1-updated");
230 for (idx
=0; idx
< 500; idx
++)
234 snprintf (buf
, sizeof buf
, "FOO_%d", idx
);
235 err
= session_env_putenv (se
, buf
);
239 err
= session_env_setenv (se
, "TEST1", NULL
);
243 /* Check that all are deleted. */
247 if (session_env_listenv (se
, &iterator
, NULL
, NULL
))
251 /* Add a few strings again. */
252 for (idx
=0; idx
< 500; idx
++)
259 snprintf (buf
, sizeof buf
, "FOO_%d=", idx
);
261 snprintf (buf
, sizeof buf
, "FOO_%d=new value for %x", idx
, idx
);
262 err
= session_env_putenv (se
, buf
);
270 session_env_release (se
);
272 session_env_release (se_0
);
278 main (int argc
, char **argv
)
282 if (argc
&& !strcmp (argv
[0], "--verbose"))