LCOV - code coverage report
Current view: top level - tests - catch_change_owner.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 56 62.5 %
Date: 2021-08-22 18:14:51 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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 chownnm() function works.
      22             :  *
      23             :  * This file implements tests for the chownnm() function.
      24             :  */
      25             : 
      26             : // self
      27             : //
      28             : #include    <snapdev/chownnm.h>
      29             : 
      30             : #include    <snapdev/file_contents.h>
      31             : #include    <snapdev/user_groups.h>
      32             : 
      33             : #include    "catch_main.h"
      34             : 
      35             : 
      36             : // C++ lib
      37             : //
      38             : #include    <set>
      39             : 
      40             : 
      41             : // last include
      42             : //
      43             : #include    <snapdev/poison.h>
      44             : 
      45             : 
      46             : 
      47             : 
      48           4 : CATCH_TEST_CASE("chownnm", "[os]")
      49             : {
      50           4 :     CATCH_START_SECTION("chownnm: change group")
      51             :     {
      52           1 :         struct group const * grp(getgrnam("snapwebsites"));
      53           1 :         if(grp == nullptr)
      54             :         {
      55           0 :             std::cerr << "warning: skipping change group test because \"snapwebsites\" group doesn't exist.\n";
      56             :         }
      57             :         else
      58             :         {
      59           1 :             char const * user(getenv("USER"));
      60           1 :             bool permitted(true);
      61           1 :             CATCH_REQUIRE(user != nullptr);
      62           1 :             if(strcmp(user, "root") != 0)
      63             :             {
      64           2 :                 std::set<std::string> our_groups(snap::user_group_names<std::set<std::string>>(user));
      65           1 :                 if(our_groups.find("snapwebsites") == our_groups.end())
      66             :                 {
      67           0 :                     permitted = false;
      68           0 :                     std::cerr << "error: we expect the tester to be the \"root\" user or part of the \"snapwebsites\" group to run this test section.\n";
      69             :                 }
      70             :             }
      71             : 
      72           1 :             if(permitted)
      73             :             {
      74           2 :                 std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/group-test.txt");
      75           2 :                 snap::file_contents system_groups(filename);
      76           1 :                 system_groups.contents("test file--testing changing group\n");
      77           1 :                 system_groups.write_all();
      78             : 
      79           1 :                 struct stat st;
      80           1 :                 CATCH_REQUIRE(stat(filename.c_str(), &st) == 0);
      81           1 :                 if(st.st_gid == grp->gr_gid)
      82             :                 {
      83           0 :                     std::cerr << "warning: your default group is \"snapwebsites\" so the test is not going to change anything\n";
      84             :                 }
      85             : 
      86           1 :                 CATCH_REQUIRE(snap::chownnm(filename, std::string(), "snapwebsites") == 0);
      87           1 :                 struct stat verify;
      88           1 :                 CATCH_REQUIRE(stat(filename.c_str(), &verify) == 0);
      89           1 :                 CATCH_REQUIRE(verify.st_gid == grp->gr_gid);
      90             : 
      91             :                 // restore former group
      92             :                 //
      93           1 :                 struct group * org_group(getgrgid(st.st_gid));
      94           1 :                 CATCH_REQUIRE(org_group != nullptr);
      95           1 :                 CATCH_REQUIRE(snap::chownnm(filename, std::string(), org_group->gr_name) == 0);
      96           1 :                 CATCH_REQUIRE(stat(filename.c_str(), &verify) == 0);
      97           1 :                 CATCH_REQUIRE(verify.st_gid == org_group->gr_gid);
      98             :             }
      99             :         }
     100             :     }
     101             :     CATCH_END_SECTION()
     102             : 
     103           4 :     CATCH_START_SECTION("chownnm: change owner")
     104             :     {
     105             :         // TODO: this is contradictory since we can't run this test as root...
     106             :         //
     107           1 :         char const * user(getenv("USER"));
     108           1 :         CATCH_REQUIRE(user != nullptr);
     109           1 :         struct passwd const * pwd(getpwnam("snapwebsites"));
     110           1 :         if(pwd == nullptr
     111           1 :         || strcmp(user, "root") != 0)
     112             :         {
     113           1 :             std::cerr << "warning: skipping change owner test because your are not root and/or the \"snapwebsites\" user doesn't exist.\n";
     114             :         }
     115             :         else
     116             :         {
     117           0 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/owner-test.txt");
     118           0 :             snap::file_contents system_groups(filename);
     119           0 :             system_groups.contents("test file--testing changing owner\n");
     120           0 :             system_groups.write_all();
     121             : 
     122           0 :             struct stat st;
     123           0 :             CATCH_REQUIRE(stat(filename.c_str(), &st) == 0);
     124           0 :             if(st.st_uid == pwd->pw_uid)
     125             :             {
     126             :                 // this should not happen since we tested above that we are
     127             :                 // root to properly run this test (otherwise we bail out)
     128             :                 //
     129           0 :                 std::cerr << "warning: your default owner is \"snapwebsites\" so the test is not going to change anything\n";
     130             :             }
     131             : 
     132           0 :             CATCH_REQUIRE(snap::chownnm(filename, "snapwebsites", std::string()) == 0);
     133           0 :             struct stat verify;
     134           0 :             CATCH_REQUIRE(stat(filename.c_str(), &verify) == 0);
     135           0 :             CATCH_REQUIRE(verify.st_uid == pwd->pw_uid);
     136             : 
     137             :             // restore former group
     138             :             //
     139           0 :             struct passwd * org_owner(getpwuid(st.st_uid));
     140           0 :             CATCH_REQUIRE(org_owner != nullptr);
     141           0 :             CATCH_REQUIRE(snap::chownnm(filename, std::string(), org_owner->pw_name) == 0);
     142           0 :             CATCH_REQUIRE(stat(filename.c_str(), &verify) == 0);
     143           0 :             CATCH_REQUIRE(verify.st_uid == org_owner->pw_uid);
     144             :         }
     145             :     }
     146             :     CATCH_END_SECTION()
     147           8 : }
     148             : 
     149             : 
     150             : 
     151             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13