summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/env
diff options
context:
space:
mode:
authormillert <>2010-08-23 22:34:37 +0000
committermillert <>2010-08-23 22:34:37 +0000
commitf0061574c419ec3bf23ea4d80cdc326b5f69057d (patch)
treec3ad715a45ff6f35f307ac8b1cf82617f625813a /src/regress/lib/libc/env
parent7242e103c3ca65b3f003c382301a3d65fe40846e (diff)
downloadopenbsd-f0061574c419ec3bf23ea4d80cdc326b5f69057d.tar.gz
openbsd-f0061574c419ec3bf23ea4d80cdc326b5f69057d.tar.bz2
openbsd-f0061574c419ec3bf23ea4d80cdc326b5f69057d.zip
Add setenv/putenv regress
Diffstat (limited to 'src/regress/lib/libc/env')
-rw-r--r--src/regress/lib/libc/env/Makefile5
-rw-r--r--src/regress/lib/libc/env/envtest.c125
2 files changed, 130 insertions, 0 deletions
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
3PROG= 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
26extern char **environ;
27
28static int
29count_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
44static void
45fake_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
59int
60main(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}