xref: /openbsd/regress/lib/libc/env/envtest.c (revision 4bdff4be)
1 /*	$OpenBSD: envtest.c,v 1.3 2021/09/01 09:26:32 jasper Exp $ */
2 
3 /*
4  * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
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 	fake_env();
67 	n = count_instances("USER");
68 	if (n != 2) {
69 		fprintf(stderr, "initial: %d instances of USER, expected %d\n",
70 		    n, 2);
71 		failures++;
72 	}
73 
74 	if (unsetenv("USER") != 0) {
75 		fprintf(stderr, "unsetenv: failed to remove USER\n");
76 		failures++;
77 	}
78 	n = count_instances("USER");
79 	if (n != 0) {
80 		fprintf(stderr, "unsetenv: %d instances of USER, expected %d\n",
81 		    n, 0);
82 		failures++;
83 	}
84 
85 	fake_env();
86 	if (setenv("USER", "nobody", 0) != 0) {
87 		fprintf(stderr, "setenv: failed to set USER\n");
88 		failures++;
89 	}
90 	n = count_instances("USER");
91 	if (n != 2) {
92 		fprintf(stderr, "setenv: %d instances of USER, expected %d\n",
93 		    n, 2);
94 		failures++;
95 	}
96 
97 	fake_env();
98 	if (setenv("USER", "nobody", 1) != 0) {
99 		fprintf(stderr, "setenv: failed to set USER\n");
100 		failures++;
101 	}
102 	n = count_instances("USER");
103 	if (n != 1) {
104 		fprintf(stderr, "setenv: %d instances of USER, expected %d\n",
105 		    n, 1);
106 		failures++;
107 	}
108 
109 	fake_env();
110 	if (putenv("USER=nobody") != 0) {
111 		fprintf(stderr, "putenv: failed to set USER\n");
112 		failures++;
113 	}
114 	n = count_instances("USER");
115 	if (n != 1) {
116 		fprintf(stderr, "putenv: %d instances of USER, expected %d\n",
117 		    n, 1);
118 		failures++;
119 	}
120 
121 	return failures;
122 }
123