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