xref: /minix3/external/bsd/atf/dist/atf-c/detail/user_test.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <sys/param.h>
31*11be35a1SLionel Sambuc #include <sys/types.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <limits.h>
34*11be35a1SLionel Sambuc #include <stdio.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <atf-c.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include "test_helpers.h"
40*11be35a1SLionel Sambuc #include "user.h"
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
43*11be35a1SLionel Sambuc  * Test cases for the free functions.
44*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc ATF_TC(euid);
ATF_TC_HEAD(euid,tc)47*11be35a1SLionel Sambuc ATF_TC_HEAD(euid, tc)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_user_euid function");
50*11be35a1SLionel Sambuc }
ATF_TC_BODY(euid,tc)51*11be35a1SLionel Sambuc ATF_TC_BODY(euid, tc)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(atf_user_euid(), geteuid());
54*11be35a1SLionel Sambuc }
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc ATF_TC(is_member_of_group);
ATF_TC_HEAD(is_member_of_group,tc)57*11be35a1SLionel Sambuc ATF_TC_HEAD(is_member_of_group, tc)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_member_of_group "
60*11be35a1SLionel Sambuc                       "function");
61*11be35a1SLionel Sambuc }
ATF_TC_BODY(is_member_of_group,tc)62*11be35a1SLionel Sambuc ATF_TC_BODY(is_member_of_group, tc)
63*11be35a1SLionel Sambuc {
64*11be35a1SLionel Sambuc     gid_t gids[NGROUPS_MAX];
65*11be35a1SLionel Sambuc     gid_t g, maxgid;
66*11be35a1SLionel Sambuc     int ngids;
67*11be35a1SLionel Sambuc     const gid_t maxgid_limit = 1 << 16;
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc     {
70*11be35a1SLionel Sambuc         int i;
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc         ngids = getgroups(NGROUPS_MAX, gids);
73*11be35a1SLionel Sambuc         if (ngids == -1)
74*11be35a1SLionel Sambuc             atf_tc_fail("Call to getgroups failed");
75*11be35a1SLionel Sambuc         maxgid = 0;
76*11be35a1SLionel Sambuc         for (i = 0; i < ngids; i++) {
77*11be35a1SLionel Sambuc             printf("User group %d is %u\n", i, gids[i]);
78*11be35a1SLionel Sambuc             if (maxgid < gids[i])
79*11be35a1SLionel Sambuc                 maxgid = gids[i];
80*11be35a1SLionel Sambuc         }
81*11be35a1SLionel Sambuc         printf("User belongs to %d groups\n", ngids);
82*11be35a1SLionel Sambuc         printf("Last GID is %u\n", maxgid);
83*11be35a1SLionel Sambuc     }
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc     if (maxgid > maxgid_limit) {
86*11be35a1SLionel Sambuc         printf("Test truncated from %u groups to %u to keep the run time "
87*11be35a1SLionel Sambuc                "reasonable enough\n", maxgid, maxgid_limit);
88*11be35a1SLionel Sambuc         maxgid = maxgid_limit;
89*11be35a1SLionel Sambuc     }
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc     for (g = 0; g < maxgid; g++) {
92*11be35a1SLionel Sambuc         bool found = false;
93*11be35a1SLionel Sambuc         int i;
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc         for (i = 0; !found && i < ngids; i++) {
96*11be35a1SLionel Sambuc             if (gids[i] == g)
97*11be35a1SLionel Sambuc                 found = true;
98*11be35a1SLionel Sambuc         }
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc         if (found) {
101*11be35a1SLionel Sambuc             printf("Checking if user belongs to group %d\n", g);
102*11be35a1SLionel Sambuc             ATF_REQUIRE(atf_user_is_member_of_group(g));
103*11be35a1SLionel Sambuc         } else {
104*11be35a1SLionel Sambuc             printf("Checking if user does not belong to group %d\n", g);
105*11be35a1SLionel Sambuc             ATF_REQUIRE(!atf_user_is_member_of_group(g));
106*11be35a1SLionel Sambuc         }
107*11be35a1SLionel Sambuc     }
108*11be35a1SLionel Sambuc }
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc ATF_TC(is_root);
ATF_TC_HEAD(is_root,tc)111*11be35a1SLionel Sambuc ATF_TC_HEAD(is_root, tc)
112*11be35a1SLionel Sambuc {
113*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_root function");
114*11be35a1SLionel Sambuc }
ATF_TC_BODY(is_root,tc)115*11be35a1SLionel Sambuc ATF_TC_BODY(is_root, tc)
116*11be35a1SLionel Sambuc {
117*11be35a1SLionel Sambuc     if (geteuid() == 0)
118*11be35a1SLionel Sambuc         ATF_REQUIRE(atf_user_is_root());
119*11be35a1SLionel Sambuc     else
120*11be35a1SLionel Sambuc         ATF_REQUIRE(!atf_user_is_root());
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc ATF_TC(is_unprivileged);
ATF_TC_HEAD(is_unprivileged,tc)124*11be35a1SLionel Sambuc ATF_TC_HEAD(is_unprivileged, tc)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_unprivileged "
127*11be35a1SLionel Sambuc                       "function");
128*11be35a1SLionel Sambuc }
ATF_TC_BODY(is_unprivileged,tc)129*11be35a1SLionel Sambuc ATF_TC_BODY(is_unprivileged, tc)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc     if (geteuid() != 0)
132*11be35a1SLionel Sambuc         ATF_REQUIRE(atf_user_is_unprivileged());
133*11be35a1SLionel Sambuc     else
134*11be35a1SLionel Sambuc         ATF_REQUIRE(!atf_user_is_unprivileged());
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
138*11be35a1SLionel Sambuc  * Main.
139*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
140*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)141*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
142*11be35a1SLionel Sambuc {
143*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, euid);
144*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, is_member_of_group);
145*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, is_root);
146*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, is_unprivileged);
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc     return atf_no_error();
149*11be35a1SLionel Sambuc }
150