diff options
| author | millert <> | 2010-08-23 22:34:37 +0000 | 
|---|---|---|
| committer | millert <> | 2010-08-23 22:34:37 +0000 | 
| commit | 6812fb9a0d10abff6035d840d620e7f9392eb597 (patch) | |
| tree | c3ad715a45ff6f35f307ac8b1cf82617f625813a | |
| parent | 1e367116e85e839002afc69614bb7d21565126b6 (diff) | |
| download | openbsd-6812fb9a0d10abff6035d840d620e7f9392eb597.tar.gz openbsd-6812fb9a0d10abff6035d840d620e7f9392eb597.tar.bz2 openbsd-6812fb9a0d10abff6035d840d620e7f9392eb597.zip | |
Add setenv/putenv regress
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
| -rw-r--r-- | src/regress/lib/libc/env/Makefile | 5 | ||||
| -rw-r--r-- | src/regress/lib/libc/env/envtest.c | 125 | 
3 files changed, 132 insertions, 2 deletions
| 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 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.31 2010/02/11 07:35:38 guenther Exp $ | 1 | # $OpenBSD: Makefile,v 1.32 2010/08/23 22:34:37 millert Exp $ | 
| 2 | 2 | ||
| 3 | SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fnmatch | 3 | SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fnmatch | 
| 4 | SUBDIR+= fpclassify getaddrinfo getcap getopt_long glob hsearch longjmp | 4 | SUBDIR+= fpclassify getaddrinfo getcap getopt_long glob hsearch longjmp | 
| 5 | SUBDIR+= locale malloc netdb popen printf regex setjmp setjmp-signal | 5 | SUBDIR+= locale malloc netdb popen printf regex setjmp setjmp-signal | 
| 6 | SUBDIR+= sigreturn sigsetjmp sprintf strerror strtod strtonum telldir time vis | 6 | SUBDIR+= sigreturn sigsetjmp sprintf strerror strtod strtonum telldir time vis | 
| 7 | SUBDIR+= orientation stdio_threading mkstemp | 7 | SUBDIR+= orientation stdio_threading mkstemp env | 
| 8 | 8 | ||
| 9 | .if (${MACHINE_ARCH} != "vax") | 9 | .if (${MACHINE_ARCH} != "vax") | 
| 10 | SUBDIR+= ieeefp | 10 | 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 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.1 2010/08/23 22:34:37 millert Exp $ | ||
| 2 | |||
| 3 | PROG= envtest | ||
| 4 | |||
| 5 | .include <bsd.regress.mk> | ||
| 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 @@ | |||
| 1 | /* $OpenBSD: envtest.c,v 1.1 2010/08/23 22:34:37 millert Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> | ||
| 5 | * | ||
| 6 | * Permission to use, copy, modify, and distribute this software for any | ||
| 7 | * purpose with or without fee is hereby granted, provided that the above | ||
| 8 | * copyright notice and this permission notice appear in all copies. | ||
| 9 | * | ||
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <sys/types.h> | ||
| 20 | |||
| 21 | #include <stdio.h> | ||
| 22 | #include <stdlib.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | |||
| 26 | extern char **environ; | ||
| 27 | |||
| 28 | static int | ||
| 29 | count_instances(const char *name) | ||
| 30 | { | ||
| 31 | int count = 0; | ||
| 32 | size_t namelen; | ||
| 33 | char **ep; | ||
| 34 | |||
| 35 | namelen = strlen(name); | ||
| 36 | for (ep = environ; *ep != NULL; ep++) { | ||
| 37 | if (strncmp(name, *ep, namelen) == 0 && (*ep)[namelen] == '=') | ||
| 38 | count++; | ||
| 39 | } | ||
| 40 | |||
| 41 | return count; | ||
| 42 | } | ||
| 43 | |||
| 44 | static void | ||
| 45 | fake_env(void) | ||
| 46 | { | ||
| 47 | static char *fakenv[7]; | ||
| 48 | |||
| 49 | fakenv[0] = "HOME=/root"; | ||
| 50 | fakenv[1] = "USER=root"; | ||
| 51 | fakenv[2] = "LOGNAME=root"; | ||
| 52 | fakenv[3] = "SHELL=/bin/sh"; | ||
| 53 | fakenv[4] = "USER=root"; | ||
| 54 | fakenv[5] = NULL; | ||
| 55 | |||
| 56 | environ = fakenv; | ||
| 57 | } | ||
| 58 | |||
| 59 | int | ||
| 60 | main(int argc, char *argv[]) | ||
| 61 | { | ||
| 62 | char *buf; | ||
| 63 | int n, failures = 0; | ||
| 64 | size_t len, bufsize; | ||
| 65 | |||
| 66 | /* Enable malloc security options. */ | ||
| 67 | setenv("MALLOC_OPTIONS", "S", 0); | ||
| 68 | |||
| 69 | fake_env(); | ||
| 70 | n = count_instances("USER"); | ||
| 71 | if (n != 2) { | ||
| 72 | fprintf(stderr, "initial: %d instances of USER, expected %d\n", | ||
| 73 | n, 2); | ||
| 74 | failures++; | ||
| 75 | } | ||
| 76 | |||
| 77 | if (unsetenv("USER") != 0) { | ||
| 78 | fprintf(stderr, "unsetenv: failed to remove USER\n"); | ||
| 79 | failures++; | ||
| 80 | } | ||
| 81 | n = count_instances("USER"); | ||
| 82 | if (n != 0) { | ||
| 83 | fprintf(stderr, "unsetenv: %d instances of USER, expected %d\n", | ||
| 84 | n, 0); | ||
| 85 | failures++; | ||
| 86 | } | ||
| 87 | |||
| 88 | fake_env(); | ||
| 89 | if (setenv("USER", "nobody", 0) != 0) { | ||
| 90 | fprintf(stderr, "setenv: failed to set USER\n"); | ||
| 91 | failures++; | ||
| 92 | } | ||
| 93 | n = count_instances("USER"); | ||
| 94 | if (n != 2) { | ||
| 95 | fprintf(stderr, "setenv: %d instances of USER, expected %d\n", | ||
| 96 | n, 2); | ||
| 97 | failures++; | ||
| 98 | } | ||
| 99 | |||
| 100 | fake_env(); | ||
| 101 | if (setenv("USER", "nobody", 1) != 0) { | ||
| 102 | fprintf(stderr, "setenv: failed to set USER\n"); | ||
| 103 | failures++; | ||
| 104 | } | ||
| 105 | n = count_instances("USER"); | ||
| 106 | if (n != 1) { | ||
| 107 | fprintf(stderr, "setenv: %d instances of USER, expected %d\n", | ||
| 108 | n, 1); | ||
| 109 | failures++; | ||
| 110 | } | ||
| 111 | |||
| 112 | fake_env(); | ||
| 113 | if (putenv("USER=nobody") != 0) { | ||
| 114 | fprintf(stderr, "putenv: failed to set USER\n"); | ||
| 115 | failures++; | ||
| 116 | } | ||
| 117 | n = count_instances("USER"); | ||
| 118 | if (n != 1) { | ||
| 119 | fprintf(stderr, "putenv: %d instances of USER, expected %d\n", | ||
| 120 | n, 1); | ||
| 121 | failures++; | ||
| 122 | } | ||
| 123 | |||
| 124 | return failures; | ||
| 125 | } | ||
