diff options
author | 2010-08-23 22:34:37 +0000 | |
---|---|---|
committer | 2010-08-23 22:34:37 +0000 | |
commit | b4b48ae9b135e385d3a030be92e58c952058de5f (patch) | |
tree | d2765a0d9d6598cff58a3639f116e0974f8b8f0b | |
parent | Check for duplicate variables in the environment when setting a value (diff) | |
download | wireguard-openbsd-b4b48ae9b135e385d3a030be92e58c952058de5f.tar.xz wireguard-openbsd-b4b48ae9b135e385d3a030be92e58c952058de5f.zip |
Add setenv/putenv regress
-rw-r--r-- | regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | regress/lib/libc/env/Makefile | 5 | ||||
-rw-r--r-- | regress/lib/libc/env/envtest.c | 125 |
3 files changed, 132 insertions, 2 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index ddbb9505fe6..d05c583efcd 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile,v 1.31 2010/02/11 07:35:38 guenther Exp $ +# $OpenBSD: Makefile,v 1.32 2010/08/23 22:34:37 millert Exp $ SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fnmatch SUBDIR+= fpclassify getaddrinfo getcap getopt_long glob hsearch longjmp SUBDIR+= locale malloc netdb popen printf regex setjmp setjmp-signal SUBDIR+= sigreturn sigsetjmp sprintf strerror strtod strtonum telldir time vis -SUBDIR+= orientation stdio_threading mkstemp +SUBDIR+= orientation stdio_threading mkstemp env .if (${MACHINE_ARCH} != "vax") SUBDIR+= ieeefp diff --git a/regress/lib/libc/env/Makefile b/regress/lib/libc/env/Makefile new file mode 100644 index 00000000000..92e04369f42 --- /dev/null +++ b/regress/lib/libc/env/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2010/08/23 22:34:37 millert Exp $ + +PROG= envtest + +.include <bsd.regress.mk> diff --git a/regress/lib/libc/env/envtest.c b/regress/lib/libc/env/envtest.c new file mode 100644 index 00000000000..33fc4634048 --- /dev/null +++ b/regress/lib/libc/env/envtest.c @@ -0,0 +1,125 @@ +/* $OpenBSD: envtest.c,v 1.1 2010/08/23 22:34:37 millert Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +extern char **environ; + +static int +count_instances(const char *name) +{ + int count = 0; + size_t namelen; + char **ep; + + namelen = strlen(name); + for (ep = environ; *ep != NULL; ep++) { + if (strncmp(name, *ep, namelen) == 0 && (*ep)[namelen] == '=') + count++; + } + + return count; +} + +static void +fake_env(void) +{ + static char *fakenv[7]; + + fakenv[0] = "HOME=/root"; + fakenv[1] = "USER=root"; + fakenv[2] = "LOGNAME=root"; + fakenv[3] = "SHELL=/bin/sh"; + fakenv[4] = "USER=root"; + fakenv[5] = NULL; + + environ = fakenv; +} + +int +main(int argc, char *argv[]) +{ + char *buf; + int n, failures = 0; + size_t len, bufsize; + + /* Enable malloc security options. */ + setenv("MALLOC_OPTIONS", "S", 0); + + fake_env(); + n = count_instances("USER"); + if (n != 2) { + fprintf(stderr, "initial: %d instances of USER, expected %d\n", + n, 2); + failures++; + } + + if (unsetenv("USER") != 0) { + fprintf(stderr, "unsetenv: failed to remove USER\n"); + failures++; + } + n = count_instances("USER"); + if (n != 0) { + fprintf(stderr, "unsetenv: %d instances of USER, expected %d\n", + n, 0); + failures++; + } + + fake_env(); + if (setenv("USER", "nobody", 0) != 0) { + fprintf(stderr, "setenv: failed to set USER\n"); + failures++; + } + n = count_instances("USER"); + if (n != 2) { + fprintf(stderr, "setenv: %d instances of USER, expected %d\n", + n, 2); + failures++; + } + + fake_env(); + if (setenv("USER", "nobody", 1) != 0) { + fprintf(stderr, "setenv: failed to set USER\n"); + failures++; + } + n = count_instances("USER"); + if (n != 1) { + fprintf(stderr, "setenv: %d instances of USER, expected %d\n", + n, 1); + failures++; + } + + fake_env(); + if (putenv("USER=nobody") != 0) { + fprintf(stderr, "putenv: failed to set USER\n"); + failures++; + } + n = count_instances("USER"); + if (n != 1) { + fprintf(stderr, "putenv: %d instances of USER, expected %d\n", + n, 1); + failures++; + } + + return failures; +} |