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 :
69 1 : auto cmd_it(cmd_seg.begin());
70 1 : auto expected_it(expected_seg.begin());
71 54 : while(cmd_it != cmd_seg.end()
72 44 : && expected_it != expected_seg.end())
73 : {
74 10 : if(*cmd_it == *expected_it)
75 : {
76 9 : ++cmd_it;
77 9 : ++expected_it;
78 : }
79 1 : else if(*cmd_it == "BUILD")
80 : {
81 0 : for(;
82 0 : expected_it != expected_seg.end() && *expected_it != "BUILD";
83 : ++expected_it);
84 : }
85 1 : else if(*cmd_it == "coverage")
86 : {
87 1 : ++cmd_it;
88 1 : CATCH_REQUIRE(cmd_it != cmd_seg.end());
89 1 : CATCH_REQUIRE(*cmd_it == "BUILD");
90 1 : ++cmd_it;
91 : }
92 : else
93 : {
94 : // not a match
95 0 : break;
96 : }
97 : }
98 3 : if(cmd_it != cmd_seg.end()
99 3 : || expected_it != expected_seg.end())
100 : {
101 0 : std::list<std::string> rem(cmd_it, cmd_seg.end());
102 0 : if(!rem.empty())
103 : {
104 : std::cerr << "error: cmd_it still has: "
105 0 : << snapdev::join_strings(rem, "/")
106 : << " from "
107 : << cmd
108 0 : << '\n';
109 : }
110 :
111 0 : rem = std::list<std::string>(expected_it, expected_seg.end());
112 0 : if(!rem.empty())
113 : {
114 : std::cerr << "error: expected_it still has: "
115 0 : << snapdev::join_strings(rem, "/")
116 : << " from "
117 : << expected
118 0 : << '\n';
119 : }
120 :
121 0 : CATCH_REQUIRE(!"cmd_it != expected_it");
122 : }
123 :
124 1 : free(p);
125 : }
126 : CATCH_END_SECTION()
127 :
128 6 : CATCH_START_SECTION("file_inheritance: verify that a process succeed in a clean environment")
129 : {
130 1 : int const r(system(path.c_str()));
131 1 : CATCH_REQUIRE(r == 0);
132 : }
133 : CATCH_END_SECTION()
134 :
135 6 : CATCH_START_SECTION("file_inheritance: verify that a process fails if unexpected files are inherited")
136 : {
137 : // create a file and keep it open when we call system()
138 : //
139 2 : std::ofstream out(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/file-to-inherit.txt");
140 1 : out << "Test" << std::endl;
141 :
142 1 : int const r(system(path.c_str()));
143 1 : CATCH_REQUIRE(r != 0);
144 : }
145 : CATCH_END_SECTION()
146 9 : }
147 :
148 :
149 : // vim: ts=4 sw=4 et
|