etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_getlogin.c
blob8c2b1990d7ebdabc6c406643e1d9b7ef9f2de4d4
1 /* $NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
34 #include <sys/param.h>
35 #include <sys/wait.h>
37 #include <atf-c.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 ATF_TC(getlogin_r_err);
44 ATF_TC_HEAD(getlogin_r_err, tc)
46 atf_tc_set_md_var(tc, "descr", "Test errors from getlogin_r(2)");
49 ATF_TC_BODY(getlogin_r_err, tc)
51 char small[0];
53 ATF_REQUIRE(getlogin_r(small, sizeof(small)) == ERANGE);
56 ATF_TC(getlogin_same);
57 ATF_TC_HEAD(getlogin_same, tc)
59 atf_tc_set_md_var(tc, "descr", "getlogin(2) vs. getlogin_r(2)");
62 ATF_TC_BODY(getlogin_same, tc)
64 char buf[MAXLOGNAME];
65 char *str;
67 str = getlogin();
69 if (str == NULL)
70 return;
72 ATF_REQUIRE(getlogin_r(buf, sizeof(buf)) == 0);
74 if (strcmp(str, buf) != 0)
75 atf_tc_fail("getlogin(2) and getlogin_r(2) differ");
78 ATF_TC(setlogin_basic);
79 ATF_TC_HEAD(setlogin_basic, tc)
81 atf_tc_set_md_var(tc, "descr", "Test that setlogin(2) works");
82 atf_tc_set_md_var(tc, "require.user", "root");
85 ATF_TC_BODY(setlogin_basic, tc)
87 char *name;
88 pid_t pid;
89 int sta;
91 pid = fork();
92 ATF_REQUIRE(pid >= 0);
94 if (pid == 0) {
96 (void)setsid();
98 if (setlogin("foobar") != 0)
99 _exit(EXIT_FAILURE);
101 name = getlogin();
103 if (name == NULL)
104 _exit(EXIT_FAILURE);
106 if (strcmp(name, "foobar") != 0)
107 _exit(EXIT_FAILURE);
109 _exit(EXIT_SUCCESS);
112 (void)wait(&sta);
114 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
115 atf_tc_fail("setlogin(2) failed to set login name");
118 ATF_TC(setlogin_err);
119 ATF_TC_HEAD(setlogin_err, tc)
121 atf_tc_set_md_var(tc, "descr", "Test errors from setlogin(2)");
122 atf_tc_set_md_var(tc, "require.user", "root");
125 ATF_TC_BODY(setlogin_err, tc)
127 char buf[MAXLOGNAME + 1];
128 char *name;
129 pid_t pid;
130 int sta;
132 pid = fork();
133 ATF_REQUIRE(pid >= 0);
135 (void)memset(buf, 'x', sizeof(buf));
137 if (pid == 0) {
139 (void)setsid();
141 errno = 0;
143 if (setlogin(buf) != -1)
144 _exit(EINVAL);
146 if (errno != EINVAL)
147 _exit(EINVAL);
149 errno = 0;
151 if (setlogin((void *)-1) != -1)
152 _exit(EFAULT);
154 if (errno != EFAULT)
155 _exit(EFAULT);
157 name = getlogin();
159 if (name == NULL)
160 _exit(EXIT_FAILURE);
162 if (strcmp(name, "foobar") == 0)
163 _exit(EXIT_FAILURE);
165 _exit(EXIT_SUCCESS);
168 (void)wait(&sta);
170 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
172 if (WEXITSTATUS(sta) == EFAULT)
173 atf_tc_fail("expected EFAULT, but the call succeeded");
175 if (WEXITSTATUS(sta) == EINVAL)
176 atf_tc_fail("expected EINVAL, but the call succeeded");
178 atf_tc_fail("setlogin(2) failed, but login name was set");
182 ATF_TC(setlogin_perm);
183 ATF_TC_HEAD(setlogin_perm, tc)
185 atf_tc_set_md_var(tc, "descr", "Test setlogin(2) as normal user");
186 atf_tc_set_md_var(tc, "require.user", "unprivileged");
189 ATF_TC_BODY(setlogin_perm, tc)
191 char *name;
192 pid_t pid;
193 int sta;
195 pid = fork();
196 ATF_REQUIRE(pid >= 0);
198 if (pid == 0) {
200 (void)setsid();
202 errno = 0;
204 if (setlogin("foobar") != -1)
205 _exit(EXIT_FAILURE);
207 if (errno != EPERM)
208 _exit(EXIT_FAILURE);
210 name = getlogin();
212 if (name == NULL)
213 _exit(EXIT_FAILURE);
215 if (strcmp(name, "foobar") == 0)
216 _exit(EXIT_FAILURE);
218 _exit(EXIT_SUCCESS);
221 (void)wait(&sta);
223 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
224 atf_tc_fail("login name was set as an unprivileged user");
227 ATF_TP_ADD_TCS(tp)
230 ATF_TP_ADD_TC(tp, getlogin_r_err);
231 ATF_TP_ADD_TC(tp, getlogin_same);
232 ATF_TP_ADD_TC(tp, setlogin_basic);
233 ATF_TP_ADD_TC(tp, setlogin_err);
234 ATF_TP_ADD_TC(tp, setlogin_perm);
236 return atf_no_error();