1240116Smarcel/* 2240116Smarcel * Automated Testing Framework (atf) 3240116Smarcel * 4240116Smarcel * Copyright (c) 2007 The NetBSD Foundation, Inc. 5240116Smarcel * All rights reserved. 6240116Smarcel * 7240116Smarcel * Redistribution and use in source and binary forms, with or without 8240116Smarcel * modification, are permitted provided that the following conditions 9240116Smarcel * are met: 10240116Smarcel * 1. Redistributions of source code must retain the above copyright 11240116Smarcel * notice, this list of conditions and the following disclaimer. 12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright 13240116Smarcel * notice, this list of conditions and the following disclaimer in the 14240116Smarcel * documentation and/or other materials provided with the distribution. 15240116Smarcel * 16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28240116Smarcel */ 29240116Smarcel 30240116Smarcel#include <sys/param.h> 31240116Smarcel#include <sys/types.h> 32240116Smarcel 33240116Smarcel#include <limits.h> 34240116Smarcel#include <stdio.h> 35240116Smarcel#include <unistd.h> 36240116Smarcel 37240116Smarcel#include <atf-c.h> 38240116Smarcel 39240116Smarcel#include "test_helpers.h" 40240116Smarcel#include "user.h" 41240116Smarcel 42240116Smarcel/* --------------------------------------------------------------------- 43240116Smarcel * Test cases for the free functions. 44240116Smarcel * --------------------------------------------------------------------- */ 45240116Smarcel 46240116SmarcelATF_TC(euid); 47240116SmarcelATF_TC_HEAD(euid, tc) 48240116Smarcel{ 49240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the atf_user_euid function"); 50240116Smarcel} 51240116SmarcelATF_TC_BODY(euid, tc) 52240116Smarcel{ 53240116Smarcel ATF_REQUIRE_EQ(atf_user_euid(), geteuid()); 54240116Smarcel} 55240116Smarcel 56240116SmarcelATF_TC(is_member_of_group); 57240116SmarcelATF_TC_HEAD(is_member_of_group, tc) 58240116Smarcel{ 59240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_member_of_group " 60240116Smarcel "function"); 61240116Smarcel} 62240116SmarcelATF_TC_BODY(is_member_of_group, tc) 63240116Smarcel{ 64240116Smarcel gid_t gids[NGROUPS_MAX]; 65240116Smarcel gid_t g, maxgid; 66240116Smarcel int ngids; 67240116Smarcel const gid_t maxgid_limit = 1 << 16; 68240116Smarcel 69240116Smarcel { 70240116Smarcel int i; 71240116Smarcel 72240116Smarcel ngids = getgroups(NGROUPS_MAX, gids); 73240116Smarcel if (ngids == -1) 74240116Smarcel atf_tc_fail("Call to getgroups failed"); 75240116Smarcel maxgid = 0; 76240116Smarcel for (i = 0; i < ngids; i++) { 77240116Smarcel printf("User group %d is %u\n", i, gids[i]); 78240116Smarcel if (maxgid < gids[i]) 79240116Smarcel maxgid = gids[i]; 80240116Smarcel } 81240116Smarcel printf("User belongs to %d groups\n", ngids); 82240116Smarcel printf("Last GID is %u\n", maxgid); 83240116Smarcel } 84240116Smarcel 85240116Smarcel if (maxgid > maxgid_limit) { 86240116Smarcel printf("Test truncated from %u groups to %u to keep the run time " 87240116Smarcel "reasonable enough\n", maxgid, maxgid_limit); 88240116Smarcel maxgid = maxgid_limit; 89240116Smarcel } 90240116Smarcel 91240116Smarcel for (g = 0; g < maxgid; g++) { 92240116Smarcel bool found = false; 93240116Smarcel int i; 94240116Smarcel 95240116Smarcel for (i = 0; !found && i < ngids; i++) { 96240116Smarcel if (gids[i] == g) 97240116Smarcel found = true; 98240116Smarcel } 99240116Smarcel 100240116Smarcel if (found) { 101240116Smarcel printf("Checking if user belongs to group %d\n", g); 102240116Smarcel ATF_REQUIRE(atf_user_is_member_of_group(g)); 103240116Smarcel } else { 104240116Smarcel printf("Checking if user does not belong to group %d\n", g); 105240116Smarcel ATF_REQUIRE(!atf_user_is_member_of_group(g)); 106240116Smarcel } 107240116Smarcel } 108240116Smarcel} 109240116Smarcel 110240116SmarcelATF_TC(is_root); 111240116SmarcelATF_TC_HEAD(is_root, tc) 112240116Smarcel{ 113240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_root function"); 114240116Smarcel} 115240116SmarcelATF_TC_BODY(is_root, tc) 116240116Smarcel{ 117240116Smarcel if (geteuid() == 0) 118240116Smarcel ATF_REQUIRE(atf_user_is_root()); 119240116Smarcel else 120240116Smarcel ATF_REQUIRE(!atf_user_is_root()); 121240116Smarcel} 122240116Smarcel 123240116SmarcelATF_TC(is_unprivileged); 124240116SmarcelATF_TC_HEAD(is_unprivileged, tc) 125240116Smarcel{ 126240116Smarcel atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_unprivileged " 127240116Smarcel "function"); 128240116Smarcel} 129240116SmarcelATF_TC_BODY(is_unprivileged, tc) 130240116Smarcel{ 131240116Smarcel if (geteuid() != 0) 132240116Smarcel ATF_REQUIRE(atf_user_is_unprivileged()); 133240116Smarcel else 134240116Smarcel ATF_REQUIRE(!atf_user_is_unprivileged()); 135240116Smarcel} 136240116Smarcel 137240116Smarcel/* --------------------------------------------------------------------- 138240116Smarcel * Main. 139240116Smarcel * --------------------------------------------------------------------- */ 140240116Smarcel 141240116SmarcelATF_TP_ADD_TCS(tp) 142240116Smarcel{ 143240116Smarcel ATF_TP_ADD_TC(tp, euid); 144240116Smarcel ATF_TP_ADD_TC(tp, is_member_of_group); 145240116Smarcel ATF_TP_ADD_TC(tp, is_root); 146240116Smarcel ATF_TP_ADD_TC(tp, is_unprivileged); 147240116Smarcel 148240116Smarcel return atf_no_error(); 149240116Smarcel} 150