From f0061574c419ec3bf23ea4d80cdc326b5f69057d Mon Sep 17 00:00:00 2001 From: millert <> Date: Mon, 23 Aug 2010 22:34:37 +0000 Subject: Add setenv/putenv regress --- src/regress/lib/libc/Makefile | 4 +- src/regress/lib/libc/env/Makefile | 5 ++ src/regress/lib/libc/env/envtest.c | 125 +++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/regress/lib/libc/env/Makefile create mode 100644 src/regress/lib/libc/env/envtest.c diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index ddbb9505fe..d05c583efc 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/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/src/regress/lib/libc/env/Makefile b/src/regress/lib/libc/env/Makefile new file mode 100644 index 0000000000..92e04369f4 --- /dev/null +++ b/src/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 diff --git a/src/regress/lib/libc/env/envtest.c b/src/regress/lib/libc/env/envtest.c new file mode 100644 index 0000000000..33fc463404 --- /dev/null +++ b/src/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 + * + * 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 + +#include +#include +#include +#include + +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; +} -- cgit v1.2.3-55-g6feb