1 /* $NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
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
)
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
)
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
)
92 ATF_REQUIRE(pid
>= 0);
98 if (setlogin("foobar") != 0)
106 if (strcmp(name
, "foobar") != 0)
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];
133 ATF_REQUIRE(pid
>= 0);
135 (void)memset(buf
, 'x', sizeof(buf
));
143 if (setlogin(buf
) != -1)
151 if (setlogin((void *)-1) != -1)
162 if (strcmp(name
, "foobar") == 0)
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
)
196 ATF_REQUIRE(pid
>= 0);
204 if (setlogin("foobar") != -1)
215 if (strcmp(name
, "foobar") == 0)
223 if (WIFEXITED(sta
) == 0 || WEXITSTATUS(sta
) != EXIT_SUCCESS
)
224 atf_tc_fail("login name was set as an unprivileged user");
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();