Line data Source code
1 : // Copyright (c) 2018-2021 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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Verify that the user_groups() function works.
22 : *
23 : * This file implements tests for the user_groups() function.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/user_groups.h>
29 :
30 : #include <snapdev/file_contents.h>
31 : #include <snapdev/join_strings.h>
32 : #include <snapdev/ostream_to_buf.h>
33 : #include <snapdev/tokenize_string.h>
34 :
35 : #include "catch_main.h"
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 :
45 4 : 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 4 : CATCH_START_SECTION("user_groups: groups")
56 : {
57 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/system-groups.txt");
58 1 : CATCH_REQUIRE(system(("groups >" + filename).c_str()) == 0);
59 2 : snap::file_contents system_groups(filename);
60 1 : system_groups.read_all();
61 :
62 1 : char const * user(getenv("USER"));
63 1 : CATCH_REQUIRE(user != nullptr);
64 2 : std::set<std::string> our_groups(snap::user_group_names<std::set<std::string>>(user));
65 :
66 2 : std::set<std::string> system_set;
67 2 : snap::tokenize_string(
68 : system_set
69 1 : , system_groups.contents()
70 : , " \n"
71 : , true);
72 :
73 1 : CATCH_REQUIRE(system_set == our_groups);
74 : }
75 : CATCH_END_SECTION()
76 :
77 4 : CATCH_START_SECTION("user_groups: invalid user")
78 : {
79 2 : std::set<std::string> our_groups(snap::user_group_names<std::set<std::string>>("invalid-user"));
80 1 : CATCH_REQUIRE(our_groups.empty());
81 : }
82 : CATCH_END_SECTION()
83 8 : }
84 :
85 :
86 :
87 : // vim: ts=4 sw=4 et
|