summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys/t_getlogin.c
diff options
context:
space:
mode:
authorbluhm <>2019-11-19 19:57:04 +0000
committerbluhm <>2019-11-19 19:57:04 +0000
commit9185f840eda265016178aeb0dcdba964f8f6f3e2 (patch)
treeda100b3712514c566fe948116f7926ad7f725401 /src/regress/lib/libc/sys/t_getlogin.c
parent6a6fe688152b422f3d65c970dad56e7d9d28b1ee (diff)
downloadopenbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.tar.gz
openbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.tar.bz2
openbsd-9185f840eda265016178aeb0dcdba964f8f6f3e2.zip
Import NetBSD system call regression tests. They were written with
ATF (Automated Testing Framework), so we use a small wrapper to map it to our bsd.regress.mk framework. Only half of the 80 NetBSD tests have been taken, the others need more work to adapt. Of them 34 syscall tests pass. Moritz Buhl ported the tests to OpenBSD.
Diffstat (limited to 'src/regress/lib/libc/sys/t_getlogin.c')
-rw-r--r--src/regress/lib/libc/sys/t_getlogin.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_getlogin.c b/src/regress/lib/libc/sys/t_getlogin.c
new file mode 100644
index 0000000000..3c98cd7de8
--- /dev/null
+++ b/src/regress/lib/libc/sys/t_getlogin.c
@@ -0,0 +1,241 @@
1/* $OpenBSD: t_getlogin.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2/* $NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3
4/*-
5 * Copyright (c) 2011 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jukka Ruohonen.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/cdefs.h>
36__RCSID("$NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
37
38#include <sys/param.h>
39#include <sys/wait.h>
40
41#include "atf-c.h"
42#include <errno.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47ATF_TC(getlogin_r_err);
48ATF_TC_HEAD(getlogin_r_err, tc)
49{
50 atf_tc_set_md_var(tc, "descr", "Test errors from getlogin_r(2)");
51}
52
53ATF_TC_BODY(getlogin_r_err, tc)
54{
55 char small[0];
56
57 ATF_REQUIRE(getlogin_r(small, sizeof(small)) == ERANGE);
58}
59
60ATF_TC(getlogin_same);
61ATF_TC_HEAD(getlogin_same, tc)
62{
63 atf_tc_set_md_var(tc, "descr", "getlogin(2) vs. getlogin_r(2)");
64}
65
66ATF_TC_BODY(getlogin_same, tc)
67{
68 char buf[MAXLOGNAME];
69 char *str;
70
71 str = getlogin();
72
73 if (str == NULL)
74 return;
75
76 ATF_REQUIRE(getlogin_r(buf, sizeof(buf)) == 0);
77
78 if (strcmp(str, buf) != 0)
79 atf_tc_fail("getlogin(2) and getlogin_r(2) differ");
80}
81
82ATF_TC(setlogin_basic);
83ATF_TC_HEAD(setlogin_basic, tc)
84{
85 atf_tc_set_md_var(tc, "descr", "Test that setlogin(2) works");
86 atf_tc_set_md_var(tc, "require.user", "root");
87}
88
89ATF_TC_BODY(setlogin_basic, tc)
90{
91 char *name;
92 pid_t pid;
93 int sta;
94
95 pid = fork();
96 ATF_REQUIRE(pid >= 0);
97
98 if (pid == 0) {
99
100 (void)setsid();
101
102 if (setlogin("foobar") != 0)
103 _exit(EXIT_FAILURE);
104
105 name = getlogin();
106
107 if (name == NULL)
108 _exit(EXIT_FAILURE);
109
110 if (strcmp(name, "foobar") != 0)
111 _exit(EXIT_FAILURE);
112
113 _exit(EXIT_SUCCESS);
114 }
115
116 (void)wait(&sta);
117
118 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
119 atf_tc_fail("setlogin(2) failed to set login name");
120}
121
122ATF_TC(setlogin_err);
123ATF_TC_HEAD(setlogin_err, tc)
124{
125 atf_tc_set_md_var(tc, "descr", "Test errors from setlogin(2)");
126 atf_tc_set_md_var(tc, "require.user", "root");
127}
128
129ATF_TC_BODY(setlogin_err, tc)
130{
131 char buf[MAXLOGNAME + 1];
132 char *name;
133 pid_t pid;
134 int sta;
135
136 pid = fork();
137 ATF_REQUIRE(pid >= 0);
138
139 (void)memset(buf, 'x', sizeof(buf));
140
141 if (pid == 0) {
142
143 (void)setsid();
144
145 errno = 0;
146
147 if (setlogin(buf) != -1)
148 _exit(EINVAL);
149
150 if (errno != EINVAL)
151 _exit(EINVAL);
152
153 errno = 0;
154
155 if (setlogin((void *)-1) != -1)
156 _exit(EFAULT);
157
158 if (errno != EFAULT)
159 _exit(EFAULT);
160
161 name = getlogin();
162
163 if (name == NULL)
164 _exit(EXIT_FAILURE);
165
166 if (strcmp(name, "foobar") == 0)
167 _exit(EXIT_FAILURE);
168
169 _exit(EXIT_SUCCESS);
170 }
171
172 (void)wait(&sta);
173
174 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
175
176 if (WEXITSTATUS(sta) == EFAULT)
177 atf_tc_fail("expected EFAULT, but the call succeeded");
178
179 if (WEXITSTATUS(sta) == EINVAL)
180 atf_tc_fail("expected EINVAL, but the call succeeded");
181
182 atf_tc_fail("setlogin(2) failed, but login name was set");
183 }
184}
185
186ATF_TC(setlogin_perm);
187ATF_TC_HEAD(setlogin_perm, tc)
188{
189 atf_tc_set_md_var(tc, "descr", "Test setlogin(2) as normal user");
190 atf_tc_set_md_var(tc, "require.user", "unprivileged");
191}
192
193ATF_TC_BODY(setlogin_perm, tc)
194{
195 char *name;
196 pid_t pid;
197 int sta;
198
199 pid = fork();
200 ATF_REQUIRE(pid >= 0);
201
202 if (pid == 0) {
203
204 (void)setsid();
205
206 errno = 0;
207
208 if (setlogin("foobar") != -1)
209 _exit(EXIT_FAILURE);
210
211 if (errno != EPERM)
212 _exit(EXIT_FAILURE);
213
214 name = getlogin();
215
216 if (name == NULL)
217 _exit(EXIT_FAILURE);
218
219 if (strcmp(name, "foobar") == 0)
220 _exit(EXIT_FAILURE);
221
222 _exit(EXIT_SUCCESS);
223 }
224
225 (void)wait(&sta);
226
227 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
228 atf_tc_fail("login name was set as an unprivileged user");
229}
230
231ATF_TP_ADD_TCS(tp)
232{
233
234 ATF_TP_ADD_TC(tp, getlogin_r_err);
235 ATF_TP_ADD_TC(tp, getlogin_same);
236 ATF_TP_ADD_TC(tp, setlogin_basic);
237 ATF_TP_ADD_TC(tp, setlogin_err);
238 ATF_TP_ADD_TC(tp, setlogin_perm);
239
240 return atf_no_error();
241}