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