Line data Source code
1 : // Copyright (c) 2011-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 the as_root class.
21 : *
22 : * This file implements tests to verify that the as_root class works.
23 : *
24 : * \note
25 : * Since our tests do not get the suid flag set and are not root, we
26 : * can't really test that we can become root.
27 : */
28 :
29 : // self
30 : //
31 : #include "catch_main.h"
32 :
33 :
34 : // snapdev
35 : //
36 : #include <snapdev/as_root.h>
37 :
38 :
39 : // C++
40 : //
41 : #include <fstream>
42 :
43 :
44 :
45 :
46 3 : CATCH_TEST_CASE("as_root", "[raii][os]")
47 : {
48 3 : CATCH_START_SECTION("as_root: failing switch to root user")
49 : {
50 1 : snapdev::as_root test;
51 1 : CATCH_REQUIRE_FALSE(test.is_switched());
52 1 : CATCH_REQUIRE(test.error_number() == EPERM);
53 1 : }
54 3 : CATCH_END_SECTION()
55 :
56 3 : CATCH_START_SECTION("as_root: working to (not) switch to ourself")
57 : {
58 1 : snapdev::as_root test(getuid()); // pretty useless, but it works
59 1 : CATCH_REQUIRE(test.is_switched());
60 1 : CATCH_REQUIRE(test.error_number() == 0);
61 1 : }
62 3 : CATCH_END_SECTION()
63 :
64 3 : CATCH_START_SECTION("drop root privileges")
65 : {
66 : // we do not expect tests to be run by the root user
67 : //
68 3 : snapdev::drop_root_privileges();
69 :
70 : // if reach here, it worked
71 : //
72 1 : CATCH_REQUIRE(true);
73 : }
74 3 : CATCH_END_SECTION()
75 3 : }
76 :
77 :
78 : // vim: ts=4 sw=4 et
|