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