Line data Source code
1 : // Copyright (c) 2018-2022 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 file_contents class works.
22 : *
23 : * This file implements tests for the file_content class.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/file_contents.h>
29 :
30 : #include "catch_main.h"
31 :
32 :
33 : // last include
34 : //
35 : #include <snapdev/poison.h>
36 :
37 :
38 :
39 :
40 6 : CATCH_TEST_CASE("file_contents", "[os]")
41 : {
42 8 : CATCH_START_SECTION("file_contents: write -> read contents")
43 : {
44 : // try to create file, but directory is missing
45 : {
46 2 : std::string const content("Open fails in this case\n");
47 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/inexcistant-directory/test-file.txt");
48 :
49 : {
50 2 : snapdev::file_contents content_test_output(filename, false);
51 1 : content_test_output.contents(content);
52 1 : CATCH_REQUIRE_FALSE(content_test_output.write_all());
53 1 : CATCH_REQUIRE(content_test_output.last_error() == "could not open file \""
54 : + filename
55 : + "\" for writing.");
56 : }
57 :
58 : {
59 2 : snapdev::file_contents content_test_input(filename);
60 1 : CATCH_REQUIRE_FALSE(content_test_input.read_all());
61 1 : CATCH_REQUIRE_FALSE(content_test_input.contents() == content);
62 : }
63 : }
64 :
65 : // create directory & file
66 : {
67 2 : std::string const content("We're testing a write...\nand then a read\n");
68 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/test-file.txt");
69 :
70 : {
71 2 : snapdev::file_contents content_test_output(filename, true);
72 1 : content_test_output.contents(content);
73 1 : CATCH_REQUIRE(content_test_output.write_all());
74 : }
75 :
76 : {
77 2 : snapdev::file_contents content_test_input(filename);
78 1 : CATCH_REQUIRE(content_test_input.read_all());
79 1 : CATCH_REQUIRE(static_cast<snapdev::file_contents const &>(content_test_input).contents() == content);
80 : }
81 : }
82 :
83 : // check that if the directory exists, it works as expected
84 : {
85 2 : std::string const content("Try again without the create directory and it still works\n");
86 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/dir-exists.txt");
87 :
88 : {
89 2 : snapdev::file_contents content_test_output(filename, false);
90 1 : content_test_output.contents(content);
91 1 : CATCH_REQUIRE(content_test_output.write_all());
92 : }
93 :
94 : {
95 2 : snapdev::file_contents content_test_input(filename);
96 1 : CATCH_REQUIRE(content_test_input.read_all());
97 1 : CATCH_REQUIRE(content_test_input.contents() == content);
98 : }
99 : }
100 :
101 : {
102 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/dir-exists.txt/object.java");
103 :
104 1 : CATCH_REQUIRE_THROWS_MATCHES(
105 : snapdev::file_contents(filename, true, true)
106 : , std::ios::failure
107 : , Catch::Matchers::ExceptionMessage(
108 : "snapdev::file_contents: the full path to filename for a file_contents object could not be created: iostream error"));
109 : }
110 :
111 : // this one tries to delete the "contents" directory and that fails
112 : // since it's a directory (and it's not even empty)
113 : {
114 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents");
115 1 : snapdev::file_contents content_test_output(filename, false, true);
116 : }
117 : }
118 : CATCH_END_SECTION()
119 :
120 8 : CATCH_START_SECTION("file_contents: write temporary -> read fails")
121 : {
122 2 : std::string const content("We're testing a write...\nand then a read but the file is gone!\n");
123 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/temporary-file.txt");
124 :
125 : {
126 2 : snapdev::file_contents content_test_output(filename, true, true);
127 1 : content_test_output.contents(content);
128 1 : CATCH_REQUIRE(content_test_output.write_all());
129 : }
130 :
131 : {
132 2 : snapdev::file_contents content_test_input(filename);
133 1 : CATCH_REQUIRE_FALSE(content_test_input.read_all());
134 1 : CATCH_REQUIRE(content_test_input.contents() == std::string());
135 : }
136 : }
137 : CATCH_END_SECTION()
138 :
139 8 : CATCH_START_SECTION("file_contents: empty filename is not accepted")
140 : {
141 1 : CATCH_REQUIRE_THROWS_MATCHES(
142 : snapdev::file_contents(std::string(), true, true)
143 : , std::invalid_argument
144 : , Catch::Matchers::ExceptionMessage(
145 : "snapdev::file_contents: the filename of a file_contents object cannot be the empty string."));
146 : }
147 : CATCH_END_SECTION()
148 :
149 8 : CATCH_START_SECTION("file_contents: read from /proc/self/cmd")
150 : {
151 2 : snapdev::file_contents comm("/proc/self/comm");
152 1 : CATCH_REQUIRE(comm.read_all());
153 1 : CATCH_REQUIRE(static_cast<snapdev::file_contents const &>(comm).contents() == "unittest\n");
154 : }
155 : CATCH_END_SECTION()
156 10 : }
157 :
158 :
159 :
160 : // vim: ts=4 sw=4 et
|