1 // 2 // Automated Testing Framework (atf) 3 // 4 // Copyright (c) 2007 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 30 extern "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++.hpp> 41 42 #include "user.hpp" 43 44 // ------------------------------------------------------------------------ 45 // Test cases for the free functions. 46 // ------------------------------------------------------------------------ 47 48 ATF_TEST_CASE(euid); 49 ATF_TEST_CASE_HEAD(euid) 50 { 51 set_md_var("descr", "Tests the euid function"); 52 } 53 ATF_TEST_CASE_BODY(euid) 54 { 55 using tools::user::euid; 56 57 ATF_REQUIRE_EQ(euid(), ::geteuid()); 58 } 59 60 ATF_TEST_CASE(is_member_of_group); 61 ATF_TEST_CASE_HEAD(is_member_of_group) 62 { 63 set_md_var("descr", "Tests the is_member_of_group function"); 64 } 65 ATF_TEST_CASE_BODY(is_member_of_group) 66 { 67 using tools::user::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 const gid_t maxgid_limit = 1 << 16; 86 if (maxgid > maxgid_limit) { 87 std::cout << "Test truncated from " << maxgid << " groups to " 88 << maxgid_limit << " to keep the run time reasonable " 89 "enough\n"; 90 maxgid = maxgid_limit; 91 } 92 93 for (gid_t g = 0; g <= maxgid; g++) { 94 if (groups.find(g) == groups.end()) { 95 std::cout << "Checking if user does not belong to group " 96 << g << "\n"; 97 ATF_REQUIRE(!is_member_of_group(g)); 98 } else { 99 std::cout << "Checking if user belongs to group " << g << "\n"; 100 ATF_REQUIRE(is_member_of_group(g)); 101 } 102 } 103 } 104 105 ATF_TEST_CASE(is_root); 106 ATF_TEST_CASE_HEAD(is_root) 107 { 108 set_md_var("descr", "Tests the is_root function"); 109 } 110 ATF_TEST_CASE_BODY(is_root) 111 { 112 using tools::user::is_root; 113 114 if (::geteuid() == 0) { 115 ATF_REQUIRE(is_root()); 116 } else { 117 ATF_REQUIRE(!is_root()); 118 } 119 } 120 121 ATF_TEST_CASE(is_unprivileged); 122 ATF_TEST_CASE_HEAD(is_unprivileged) 123 { 124 set_md_var("descr", "Tests the is_unprivileged function"); 125 } 126 ATF_TEST_CASE_BODY(is_unprivileged) 127 { 128 using tools::user::is_unprivileged; 129 130 if (::geteuid() != 0) { 131 ATF_REQUIRE(is_unprivileged()); 132 } else { 133 ATF_REQUIRE(!is_unprivileged()); 134 } 135 } 136 137 // ------------------------------------------------------------------------ 138 // Main. 139 // ------------------------------------------------------------------------ 140 141 ATF_INIT_TEST_CASES(tcs) 142 { 143 // Add the tests for the free functions. 144 ATF_ADD_TEST_CASE(tcs, euid); 145 ATF_ADD_TEST_CASE(tcs, is_member_of_group); 146 ATF_ADD_TEST_CASE(tcs, is_root); 147 ATF_ADD_TEST_CASE(tcs, is_unprivileged); 148 } 149