xref: /openbsd-src/regress/lib/libc/env/envtest.c (revision c13ff7ac6c9d7ada6c41f3c3e389cb815654d94e)
1*c13ff7acSjasper /*	$OpenBSD: envtest.c,v 1.3 2021/09/01 09:26:32 jasper Exp $ */
2b4b48ae9Smillert 
3b4b48ae9Smillert /*
4bf198cc6Smillert  * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
5b4b48ae9Smillert  *
6b4b48ae9Smillert  * Permission to use, copy, modify, and distribute this software for any
7b4b48ae9Smillert  * purpose with or without fee is hereby granted, provided that the above
8b4b48ae9Smillert  * copyright notice and this permission notice appear in all copies.
9b4b48ae9Smillert  *
10b4b48ae9Smillert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11b4b48ae9Smillert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12b4b48ae9Smillert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13b4b48ae9Smillert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14b4b48ae9Smillert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15b4b48ae9Smillert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16b4b48ae9Smillert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17b4b48ae9Smillert  */
18b4b48ae9Smillert 
19b4b48ae9Smillert #include <sys/types.h>
20b4b48ae9Smillert 
21b4b48ae9Smillert #include <stdio.h>
22b4b48ae9Smillert #include <stdlib.h>
23b4b48ae9Smillert #include <string.h>
24b4b48ae9Smillert #include <unistd.h>
25b4b48ae9Smillert 
26b4b48ae9Smillert extern char **environ;
27b4b48ae9Smillert 
28b4b48ae9Smillert static int
count_instances(const char * name)29b4b48ae9Smillert count_instances(const char *name)
30b4b48ae9Smillert {
31b4b48ae9Smillert 	int count = 0;
32b4b48ae9Smillert 	size_t namelen;
33b4b48ae9Smillert 	char **ep;
34b4b48ae9Smillert 
35b4b48ae9Smillert 	namelen = strlen(name);
36b4b48ae9Smillert 	for (ep = environ; *ep != NULL; ep++) {
37b4b48ae9Smillert 		if (strncmp(name, *ep, namelen) == 0 && (*ep)[namelen] == '=')
38b4b48ae9Smillert 			count++;
39b4b48ae9Smillert 	}
40b4b48ae9Smillert 
41b4b48ae9Smillert 	return count;
42b4b48ae9Smillert }
43b4b48ae9Smillert 
44b4b48ae9Smillert static void
fake_env(void)45b4b48ae9Smillert fake_env(void)
46b4b48ae9Smillert {
47b4b48ae9Smillert 	static char *fakenv[7];
48b4b48ae9Smillert 
49b4b48ae9Smillert 	fakenv[0] = "HOME=/root";
50b4b48ae9Smillert 	fakenv[1] = "USER=root";
51b4b48ae9Smillert 	fakenv[2] = "LOGNAME=root";
52b4b48ae9Smillert 	fakenv[3] = "SHELL=/bin/sh";
53b4b48ae9Smillert 	fakenv[4] = "USER=root";
54b4b48ae9Smillert 	fakenv[5] = NULL;
55b4b48ae9Smillert 
56b4b48ae9Smillert 	environ = fakenv;
57b4b48ae9Smillert }
58b4b48ae9Smillert 
59b4b48ae9Smillert int
main(int argc,char * argv[])60b4b48ae9Smillert main(int argc, char *argv[])
61b4b48ae9Smillert {
62b4b48ae9Smillert 	char *buf;
63b4b48ae9Smillert 	int n, failures = 0;
64b4b48ae9Smillert 	size_t len, bufsize;
65b4b48ae9Smillert 
66b4b48ae9Smillert 	fake_env();
67b4b48ae9Smillert 	n = count_instances("USER");
68b4b48ae9Smillert 	if (n != 2) {
69b4b48ae9Smillert 		fprintf(stderr, "initial: %d instances of USER, expected %d\n",
70b4b48ae9Smillert 		    n, 2);
71b4b48ae9Smillert 		failures++;
72b4b48ae9Smillert 	}
73b4b48ae9Smillert 
74b4b48ae9Smillert 	if (unsetenv("USER") != 0) {
75b4b48ae9Smillert 		fprintf(stderr, "unsetenv: failed to remove USER\n");
76b4b48ae9Smillert 		failures++;
77b4b48ae9Smillert 	}
78b4b48ae9Smillert 	n = count_instances("USER");
79b4b48ae9Smillert 	if (n != 0) {
80b4b48ae9Smillert 		fprintf(stderr, "unsetenv: %d instances of USER, expected %d\n",
81b4b48ae9Smillert 		    n, 0);
82b4b48ae9Smillert 		failures++;
83b4b48ae9Smillert 	}
84b4b48ae9Smillert 
85b4b48ae9Smillert 	fake_env();
86b4b48ae9Smillert 	if (setenv("USER", "nobody", 0) != 0) {
87b4b48ae9Smillert 		fprintf(stderr, "setenv: failed to set USER\n");
88b4b48ae9Smillert 		failures++;
89b4b48ae9Smillert 	}
90b4b48ae9Smillert 	n = count_instances("USER");
91b4b48ae9Smillert 	if (n != 2) {
92b4b48ae9Smillert 		fprintf(stderr, "setenv: %d instances of USER, expected %d\n",
93b4b48ae9Smillert 		    n, 2);
94b4b48ae9Smillert 		failures++;
95b4b48ae9Smillert 	}
96b4b48ae9Smillert 
97b4b48ae9Smillert 	fake_env();
98b4b48ae9Smillert 	if (setenv("USER", "nobody", 1) != 0) {
99b4b48ae9Smillert 		fprintf(stderr, "setenv: failed to set USER\n");
100b4b48ae9Smillert 		failures++;
101b4b48ae9Smillert 	}
102b4b48ae9Smillert 	n = count_instances("USER");
103b4b48ae9Smillert 	if (n != 1) {
104b4b48ae9Smillert 		fprintf(stderr, "setenv: %d instances of USER, expected %d\n",
105b4b48ae9Smillert 		    n, 1);
106b4b48ae9Smillert 		failures++;
107b4b48ae9Smillert 	}
108b4b48ae9Smillert 
109b4b48ae9Smillert 	fake_env();
110b4b48ae9Smillert 	if (putenv("USER=nobody") != 0) {
111b4b48ae9Smillert 		fprintf(stderr, "putenv: failed to set USER\n");
112b4b48ae9Smillert 		failures++;
113b4b48ae9Smillert 	}
114b4b48ae9Smillert 	n = count_instances("USER");
115b4b48ae9Smillert 	if (n != 1) {
116b4b48ae9Smillert 		fprintf(stderr, "putenv: %d instances of USER, expected %d\n",
117b4b48ae9Smillert 		    n, 1);
118b4b48ae9Smillert 		failures++;
119b4b48ae9Smillert 	}
120b4b48ae9Smillert 
121b4b48ae9Smillert 	return failures;
122b4b48ae9Smillert }
123