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