155839Sasmodai/* 255839Sasmodai * Automated Testing Framework (atf) 355839Sasmodai * 455839Sasmodai * Copyright (c) 2007 The NetBSD Foundation, Inc. 555839Sasmodai * All rights reserved. 655839Sasmodai * 755839Sasmodai * Redistribution and use in source and binary forms, with or without 855839Sasmodai * modification, are permitted provided that the following conditions 955839Sasmodai * are met: 1055839Sasmodai * 1. Redistributions of source code must retain the above copyright 1155839Sasmodai * notice, this list of conditions and the following disclaimer. 1255839Sasmodai * 2. Redistributions in binary form must reproduce the above copyright 1355839Sasmodai * notice, this list of conditions and the following disclaimer in the 1455839Sasmodai * documentation and/or other materials provided with the distribution. 1555839Sasmodai * 1655839Sasmodai * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 1755839Sasmodai * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1855839Sasmodai * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1955839Sasmodai * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2055839Sasmodai * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 2155839Sasmodai * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2255839Sasmodai * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 2355839Sasmodai * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2455839Sasmodai * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 2555839Sasmodai * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2655839Sasmodai * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 2775584Sru * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2855839Sasmodai */ 2975584Sru 3055839Sasmodai#include <sys/param.h> 3175584Sru#include <sys/types.h> 3255839Sasmodai 3355839Sasmodai#include <limits.h> 3455839Sasmodai#include <stdio.h> 3555839Sasmodai#include <unistd.h> 3675584Sru 3755839Sasmodai#include <atf-c.h> 3855839Sasmodai 3955839Sasmodai#include "test_helpers.h" 4055839Sasmodai#include "user.h" 4155839Sasmodai 4255839Sasmodai/* --------------------------------------------------------------------- 4355839Sasmodai * Test cases for the free functions. 4455839Sasmodai * --------------------------------------------------------------------- */ 4555839Sasmodai 4655839SasmodaiATF_TC(euid); 4755839SasmodaiATF_TC_HEAD(euid, tc) 4855839Sasmodai{ 4955839Sasmodai atf_tc_set_md_var(tc, "descr", "Tests the atf_user_euid function"); 5055839Sasmodai} 5155839SasmodaiATF_TC_BODY(euid, tc) 5255839Sasmodai{ 5355839Sasmodai ATF_REQUIRE_EQ(atf_user_euid(), geteuid()); 5455839Sasmodai} 5555839Sasmodai 5655839SasmodaiATF_TC(is_member_of_group); 5755839SasmodaiATF_TC_HEAD(is_member_of_group, tc) 5855839Sasmodai{ 5955839Sasmodai atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_member_of_group " 6055839Sasmodai "function"); 6155839Sasmodai} 6255839SasmodaiATF_TC_BODY(is_member_of_group, tc) 6355839Sasmodai{ 6455839Sasmodai gid_t gids[NGROUPS_MAX]; 6555839Sasmodai gid_t g, maxgid; 6655839Sasmodai int ngids; 6755839Sasmodai const gid_t maxgid_limit = 1 << 16; 6875584Sru 6975584Sru { 7075584Sru int i; 7155839Sasmodai 7255839Sasmodai ngids = getgroups(NGROUPS_MAX, gids); 7355839Sasmodai if (ngids == -1) 7455839Sasmodai atf_tc_fail("Call to getgroups failed"); 7555839Sasmodai maxgid = 0; 7655839Sasmodai for (i = 0; i < ngids; i++) { 7755839Sasmodai printf("User group %d is %u\n", i, gids[i]); 7855839Sasmodai if (maxgid < gids[i]) 7975584Sru maxgid = gids[i]; 8055839Sasmodai } 8155839Sasmodai printf("User belongs to %d groups\n", ngids); 8255839Sasmodai printf("Last GID is %u\n", maxgid); 8355839Sasmodai } 8455839Sasmodai 8555839Sasmodai if (maxgid > maxgid_limit) { 8655839Sasmodai printf("Test truncated from %u groups to %u to keep the run time " 8755839Sasmodai "reasonable enough\n", maxgid, maxgid_limit); 8855839Sasmodai maxgid = maxgid_limit; 8955839Sasmodai } 9055839Sasmodai 9175584Sru for (g = 0; g < maxgid; g++) { 9255839Sasmodai bool found = false; 9355839Sasmodai int i; 9455839Sasmodai 9555839Sasmodai for (i = 0; !found && i < ngids; i++) { 9655839Sasmodai if (gids[i] == g) 9755839Sasmodai found = true; 9855839Sasmodai } 9955839Sasmodai 10055839Sasmodai if (found) { 10155839Sasmodai printf("Checking if user belongs to group %d\n", g); 10255839Sasmodai ATF_REQUIRE(atf_user_is_member_of_group(g)); 10355839Sasmodai } else { 10455839Sasmodai printf("Checking if user does not belong to group %d\n", g); 10555839Sasmodai ATF_REQUIRE(!atf_user_is_member_of_group(g)); 10655839Sasmodai } 10755839Sasmodai } 10855839Sasmodai} 10955839Sasmodai 11055839SasmodaiATF_TC(is_root); 11155839SasmodaiATF_TC_HEAD(is_root, tc) 11255839Sasmodai{ 11355839Sasmodai atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_root function"); 11455839Sasmodai} 11555839SasmodaiATF_TC_BODY(is_root, tc) 11655839Sasmodai{ 11755839Sasmodai if (geteuid() == 0) 11855839Sasmodai ATF_REQUIRE(atf_user_is_root()); 11955839Sasmodai else 12055839Sasmodai ATF_REQUIRE(!atf_user_is_root()); 12155839Sasmodai} 12255839Sasmodai 12355839SasmodaiATF_TC(is_unprivileged); 12455839SasmodaiATF_TC_HEAD(is_unprivileged, tc) 12555839Sasmodai{ 12655839Sasmodai atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_unprivileged " 12755839Sasmodai "function"); 12855839Sasmodai} 12955839SasmodaiATF_TC_BODY(is_unprivileged, tc) 13055839Sasmodai{ 13155839Sasmodai if (geteuid() != 0) 13255839Sasmodai ATF_REQUIRE(atf_user_is_unprivileged()); 13355839Sasmodai else 13455839Sasmodai ATF_REQUIRE(!atf_user_is_unprivileged()); 13555839Sasmodai} 13655839Sasmodai 13755839Sasmodai/* --------------------------------------------------------------------- 13855839Sasmodai * Main. 13955839Sasmodai * --------------------------------------------------------------------- */ 14055839Sasmodai 14155839SasmodaiATF_TP_ADD_TCS(tp) 14255839Sasmodai{ 14355839Sasmodai ATF_TP_ADD_TC(tp, euid); 14455839Sasmodai ATF_TP_ADD_TC(tp, is_member_of_group); 14555839Sasmodai ATF_TP_ADD_TC(tp, is_root); 14655839Sasmodai ATF_TP_ADD_TC(tp, is_unprivileged); 14755839Sasmodai 14855839Sasmodai return atf_no_error(); 14955839Sasmodai} 15055839Sasmodai