LCOV - code coverage report
Current view: top level - tests - catch_file_inheritance.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 39 51 76.5 %
Date: 2022-06-30 17:20:55 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2012-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/libexcept
       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
      17             : // along with this program; if not, write to the Free Software
      18             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      19             : 
      20             : // libexcept
      21             : //
      22             : #include    <libexcept/file_inheritance.h>
      23             : 
      24             : 
      25             : // self
      26             : //
      27             : #include    "catch_main.h"
      28             : 
      29             : 
      30             : // snapdev
      31             : //
      32             : #include    <snapdev/join_strings.h>
      33             : #include    <snapdev/tokenize_string.h>
      34             : 
      35             : 
      36             : // C++
      37             : //
      38             : #include    <fstream>
      39             : 
      40             : 
      41             : 
      42           5 : CATCH_TEST_CASE("file_inheritance", "[file_inheritance]")
      43             : {
      44           6 :     std::string path(SNAP_CATCH2_NAMESPACE::g_verify_file_inheriance_path);
      45           3 :     path += "/verify-file-inheritance";
      46             : 
      47           6 :     CATCH_START_SECTION("file_inheritance: check command line")
      48             :     {
      49           2 :         std::string cmd(libexcept::get_command_line(getpid()));
      50           1 :         char * p(realpath(cmd.c_str(), nullptr));
      51           1 :         CATCH_REQUIRE(p != nullptr);
      52             : 
      53             :         // this changes depending on whether we run the test directly
      54             :         // or from within coverage, so I check the paths with segments
      55             :         // allowing various levels on either side
      56             :         //
      57           2 :         std::list<std::string> cmd_seg;
      58           1 :         snapdev::tokenize_string(cmd_seg, p, "/", true);
      59             : 
      60             :         // in the normal testing, we have a full filename to the unittest
      61             :         // in the binary tree
      62             :         //
      63             :         // in case of the coverage test, we just have "BUILD/tests/unittest"
      64             :         //
      65           2 :         std::string const expected(SNAP_CATCH2_NAMESPACE::g_verify_file_inheriance_path + "/unittest");
      66           2 :         std::list<std::string> expected_seg;
      67           1 :         snapdev::tokenize_string(expected_seg, expected, "/", true);
      68           1 : std::cerr << "--------- starting with [" << p        << "] (p <- cmd)\n";
      69           1 : std::cerr << "---------           and [" << expected << "] (expected)\n";
      70             : 
      71           1 :         auto cmd_it(cmd_seg.begin());
      72           1 :         auto expected_it(expected_seg.begin());
      73          54 :         while(cmd_it != cmd_seg.end()
      74          44 :            && expected_it != expected_seg.end())
      75             :         {
      76          10 :             if(*cmd_it == *expected_it)
      77             :             {
      78           9 :                 ++cmd_it;
      79           9 :                 ++expected_it;
      80             :             }
      81           1 :             else if(*cmd_it == "BUILD")
      82             :             {
      83           0 :                 for(;
      84           0 :                     expected_it != expected_seg.end() && *expected_it != "BUILD";
      85             :                     ++expected_it);
      86             :             }
      87           1 :             else if(*cmd_it == "coverage")
      88             :             {
      89           1 :                 ++cmd_it;
      90           1 :                 CATCH_REQUIRE(cmd_it != cmd_seg.end());
      91           1 :                 CATCH_REQUIRE(*cmd_it == "BUILD");
      92           1 :                 ++cmd_it;
      93             :             }
      94             :             else
      95             :             {
      96             :                 // not a match
      97           0 :                 break;
      98             :             }
      99             :         }
     100           3 :         if(cmd_it != cmd_seg.end()
     101           3 :         || expected_it != expected_seg.end())
     102             :         {
     103           0 :             std::list<std::string> rem(cmd_it, cmd_seg.end());
     104           0 :             if(!rem.empty())
     105             :             {
     106             :                 std::cerr << "error: cmd_it still has: "
     107           0 :                     << snapdev::join_strings(rem, "/")
     108             :                     << " from "
     109             :                     << cmd
     110           0 :                     << '\n';
     111             :             }
     112             : 
     113           0 :             rem = std::list<std::string>(expected_it, expected_seg.end());
     114           0 :             if(!rem.empty())
     115             :             {
     116             :                 std::cerr << "error: expected_it still has: "
     117           0 :                     << snapdev::join_strings(rem, "/")
     118             :                     << " from "
     119             :                     << expected
     120           0 :                     << '\n';
     121             :             }
     122             : 
     123           0 :             CATCH_REQUIRE(!"cmd_it != expected_it");
     124             :         }
     125             : 
     126           1 :         free(p);
     127             :     }
     128             :     CATCH_END_SECTION()
     129             : 
     130           6 :     CATCH_START_SECTION("file_inheritance: verify that a process succeed in a clean environment")
     131             :     {
     132           1 :         int const r(system(path.c_str()));
     133           1 :         CATCH_REQUIRE(r == 0);
     134             :     }
     135             :     CATCH_END_SECTION()
     136             : 
     137           6 :     CATCH_START_SECTION("file_inheritance: verify that a process fails if unexpected files are inherited")
     138             :     {
     139             :         // create a file and keep it open when we call system()
     140             :         //
     141           2 :         std::ofstream out(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/file-to-inherit.txt");
     142           1 :         out << "Test" << std::endl;
     143             : 
     144           1 :         int const r(system(path.c_str()));
     145           1 :         CATCH_REQUIRE(r != 0);
     146             :     }
     147             :     CATCH_END_SECTION()
     148           9 : }
     149             : 
     150             : 
     151             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13