Line data Source code
1 : // Copyright (c) 2018-2024 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/snapdev 4 : // contact@m2osw.com 5 : // 6 : // This program is free software: you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation, either version 3 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License 17 : // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 : 19 : /** \file 20 : * \brief Verify that the user_groups() function works. 21 : * 22 : * This file implements tests for the user_groups() function. 23 : */ 24 : 25 : // self 26 : // 27 : #include <snapdev/user_groups.h> 28 : 29 : #include <snapdev/file_contents.h> 30 : #include <snapdev/join_strings.h> 31 : #include <snapdev/ostream_to_buf.h> 32 : #include <snapdev/tokenize_string.h> 33 : 34 : #include "catch_main.h" 35 : 36 : 37 : // last include 38 : // 39 : #include <snapdev/poison.h> 40 : 41 : 42 : 43 : 44 2 : CATCH_TEST_CASE("user_groups", "[os]") 45 : { 46 : // Note: this test fails if you add/remove groups from your user 47 : // and do not re-log-in; one way to test in that case: 48 : // 49 : // $ id -ng 50 : // alexis <-- a.k.a. old group 51 : // $ newgrp snapwebsites 52 : // $ newgrp alexis 53 : // 54 2 : CATCH_START_SECTION("user_groups: groups") 55 : { 56 1 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/system-groups.txt"); 57 1 : CATCH_REQUIRE(system(("groups >" + filename).c_str()) == 0); 58 1 : snapdev::file_contents system_groups(filename); 59 1 : system_groups.read_all(); 60 : 61 1 : char const * user(getenv("USER")); 62 1 : CATCH_REQUIRE(user != nullptr); 63 3 : std::set<std::string> our_groups(snapdev::user_group_names<std::set<std::string>>(user)); 64 : 65 1 : std::set<std::string> system_set; 66 2 : snapdev::tokenize_string( 67 : system_set 68 1 : , system_groups.contents() 69 : , " \n" 70 : , true); 71 : 72 1 : CATCH_REQUIRE(system_set == our_groups); 73 1 : } 74 2 : CATCH_END_SECTION() 75 : 76 2 : CATCH_START_SECTION("user_groups: invalid user") 77 : { 78 3 : std::set<std::string> our_groups(snapdev::user_group_names<std::set<std::string>>("invalid-user")); 79 1 : CATCH_REQUIRE(our_groups.empty()); 80 1 : } 81 2 : CATCH_END_SECTION() 82 2 : } 83 : 84 : 85 : 86 : // vim: ts=4 sw=4 et