Line data Source code
1 : // Copyright (c) 2018-2025 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 : #include <snapdev/username.h>
34 :
35 : #include "catch_main.h"
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 :
45 2 : CATCH_TEST_CASE("user_groups", "[os]")
46 : {
47 : // Note: this test fails if you add/remove groups from your user
48 : // and do not re-log-in; one way to test in that case:
49 : //
50 : // $ id -ng
51 : // alexis <-- a.k.a. old group
52 : // $ newgrp snapwebsites
53 : // $ newgrp alexis
54 : //
55 2 : CATCH_START_SECTION("user_groups: groups")
56 : {
57 1 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/system-groups.txt");
58 1 : CATCH_REQUIRE(system(("groups >" + filename).c_str()) == 0);
59 1 : snapdev::file_contents system_groups(filename);
60 1 : system_groups.read_all();
61 :
62 1 : std::string const user(snapdev::username());
63 1 : 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 4 : 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
|