diff options
author | Ulrich Drepper <drepper@redhat.com> | 1999-07-29 20:36:56 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 1999-07-29 20:36:56 +0000 |
commit | a827898574892dce3e83ed4b08a51927c16445a5 (patch) | |
tree | 46e90aaeb1303d5afd93fc772c3f3bd65a9a4fa2 | |
parent | 50d3390d3b54d384be11ece9e34f17e30cfd742c (diff) | |
download | glibc-a827898574892dce3e83ed4b08a51927c16445a5.tar.gz glibc-a827898574892dce3e83ed4b08a51927c16445a5.tar.xz glibc-a827898574892dce3e83ed4b08a51927c16445a5.zip |
Tests for setenv, unsetenv, putenv, etc.
-rw-r--r-- | stdlib/tst-environ.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/stdlib/tst-environ.c b/stdlib/tst-environ.c new file mode 100644 index 0000000000..01083f5511 --- /dev/null +++ b/stdlib/tst-environ.c @@ -0,0 +1,141 @@ +#include <stdio.h> +#include <stdlib.h> + + +#define VAR "FOOBAR" + +char putenv_val[100] = VAR "=some longer value"; + +int +main (void) +{ + int result = 0; + const char *valp; + + /* First test: remove entry FOOBAR, whether it exists or not. */ + unsetenv (VAR); + + /* Now getting the value should fail. */ + if (getenv (VAR) != NULL) + { + printf ("There should be no `%s' value\n", VAR); + result = 1; + } + + /* Now add a value, with the replace flag cleared. */ + if (setenv (VAR, "one", 0) != 0) + { + printf ("setenv #1 failed: %m\n"); + result = 1; + } + + /* Getting this value should now be possible. */ + valp = getenv (VAR); + if (valp == NULL || strcmp (valp, "one") != 0) + { + puts ("getenv #2 failed"); + result = 1; + } + + /* Try to replace without the replace flag set. This should fail. */ + if (setenv (VAR, "two", 0) != 0) + { + printf ("setenv #2 failed: %m\n"); + result = 1; + } + + /* The value shouldn't have changed. */ + valp = getenv (VAR); + if (valp == NULL || strcmp (valp, "one") != 0) + { + puts ("getenv #3 failed"); + result = 1; + } + + /* Now replace the value using putenv. */ + if (putenv (putenv_val) != 0) + { + printf ("putenv #1 failed: %m\n"); + result = 1; + } + + /* The value should have changed now. */ + valp = getenv (VAR); + if (valp == NULL || strcmp (valp, "some longer value") != 0) + { + printf ("getenv #4 failed (is \"%s\")\n", valp); + result = 1; + } + + /* Now one tricky check: changing the variable passed in putenv should + change the environment. */ + strcpy (&putenv_val[sizeof VAR], "a short one"); + + /* The value should have changed again. */ + valp = getenv (VAR); + if (valp == NULL || strcmp (valp, "a short one") != 0) + { + puts ("getenv #5 failed"); + result = 1; + } + + /* It should even be possible to rename the variable. */ + strcpy (putenv_val, "XYZZY=some other value"); + + /* Now a lookup using the old name should fail. */ + if (getenv (VAR) != NULL) + { + puts ("getenv #6 failed"); + result = 1; + } + + /* But using the new name it should work. */ + valp = getenv ("XYZZY"); + if (valp == NULL || strcmp (valp, "some other value") != 0) + { + puts ("getenv #7 failed"); + result = 1; + } + + /* Create a new variable with the old name. */ + if (setenv (VAR, "a new value", 0) != 0) + { + printf ("setenv #3 failed: %m\n"); + result = 1; + } + + /* At this point a getenv call must return the new value. */ + valp = getenv (VAR); + if (valp == NULL || strcmp (valp, "a new value") != 0) + { + puts ("getenv #8 failed"); + result = 1; + } + + /* Black magic: rename the variable we added using putenv back. */ + strcpy (putenv_val, VAR "=old name new value"); + + /* This is interesting. We have two variables with the same name. + Getting a value should return one of them. */ + valp = getenv (VAR); + if (valp == NULL + || (strcmp (valp, "a new value") != 0 + && strcmp (valp, "old name new value") != 0)) + { + puts ("getenv #9 failed"); + result = 1; + } + + /* More fun ahead: we are now removing the variable. This should remove + both values. */ + putenv (VAR); + + /* Getting the value should now fail. */ + if (getenv (VAR) != NULL) + { + printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR)); + result = 1; + } + + return result; +} |