2 Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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.
11 DCP-o-matic 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.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/cross.h"
25 #include <boost/algorithm/string.hpp>
26 #include <boost/asio.hpp>
27 #include <boost/filesystem.hpp>
28 #include <boost/process.hpp>
29 #include <boost/test/unit_test.hpp>
31 #include <sys/types.h>
44 ext2_ls (vector<string> arguments)
46 using namespace boost::process;
48 boost::asio::io_service ios;
50 child ch (search_path("e2ls"), arguments, std_in.close(), std_out > data, ios);
53 auto output = data.get();
56 boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
63 make_empty_file(boost::filesystem::path file, off_t size)
65 auto fd = open (file.string().c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
66 BOOST_REQUIRE (fd != -1);
67 auto const r = posix_fallocate (fd, 0, size);
68 BOOST_REQUIRE_EQUAL (r, 0);
73 /** Use the writer code to make a disk and partition and copy a file (in a directory)
74 * to it, then check that:
75 * - the partition has inode size 128
76 * - the file and directory have reasonable timestamps
77 * - the file can be copied back off the disk
79 BOOST_AUTO_TEST_CASE (disk_writer_test1)
81 using namespace boost::filesystem;
82 using namespace boost::process;
86 path disk = "build/test/disk_writer_test1.disk";
87 path partition = "build/test/disk_writer_test1.partition";
92 /* lwext4 has a lower limit of correct ext2 partition sizes it can make; 32Mb
93 * does not work here: fsck gives errors about an incorrect free blocks count.
95 make_random_file(disk, 256 * 1024 * 1024);
96 make_random_file(partition, 256 * 1024 * 1024);
98 path dcp = "build/test/disk_writer_test1";
99 create_directory (dcp);
100 /* Some arbitrary file size here */
101 make_random_file (dcp / "foo", 1024 * 1024 * 32 - 6128);
103 dcpomatic::write ({dcp}, disk.string(), partition.string(), nullptr);
105 BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
108 boost::asio::io_service ios;
110 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
113 string output = data.get();
115 std::regex reg("Inode size:\\s*(.*)");
116 BOOST_REQUIRE (std::regex_search(output, matches, reg));
117 BOOST_REQUIRE (matches.size() == 2);
118 BOOST_CHECK_EQUAL (matches[1].str(), "128");
121 BOOST_CHECK (ext2_ls({partition.string()}) == vector<string>({"disk_writer_test1", "lost+found"}));
123 string const unset_date = "1-Jan-1970";
125 /* Check timestamp of the directory has been set */
126 auto details = ext2_ls({"-l", partition.string()});
127 BOOST_REQUIRE (details.size() >= 6);
128 BOOST_CHECK (details[5] != unset_date);
130 auto const dir = partition.string() + ":disk_writer_test1";
131 BOOST_CHECK (ext2_ls({dir}) == vector<string>({"foo"}));
133 /* Check timestamp of foo */
134 details = ext2_ls({"-l", dir});
135 BOOST_REQUIRE (details.size() >= 6);
136 BOOST_CHECK (details[5] != unset_date);
138 system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back");
139 check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back");
145 BOOST_AUTO_TEST_CASE (disk_writer_test2)
147 using namespace boost::filesystem;
148 using namespace boost::process;
150 remove_all("build/test/disk_writer_test2.disk");
151 remove_all("build/test/disk_writer_test2.partition");
152 remove_all("build/test/disk_writer_test2");
156 path const disk = "build/test/disk_writer_test2.disk";
157 path const partition = "build/test/disk_writer_test2.partition";
162 /* Using empty files here still triggers the bug and is much quicker than using random data */
163 make_empty_file(disk, 31043616768LL);
164 make_empty_file(partition, 31043571712LL);
166 auto const dcp = TestPaths::private_data() / "xm";
167 dcpomatic::write({dcp}, disk.string(), partition.string(), nullptr);
169 BOOST_CHECK_EQUAL(system("/sbin/e2fsck -fn build/test/disk_writer_test2.partition"), 0);
171 path const check = "build/test/disk_writer_test2";
172 create_directory(check);
175 for (auto original: directory_iterator(dcp)) {
176 auto path_in_copy = path("xm") / original.path().filename();
177 auto path_in_check = check / original.path().filename();
178 system("e2cp " + partition.string() + ":" + path_in_copy.string() + " " + path_in_check.string());
179 check_file(original.path(), path_in_check);
187 BOOST_AUTO_TEST_CASE (disk_writer_test3)
189 using namespace boost::filesystem;
190 using namespace boost::process;
192 remove_all("build/test/disk_writer_test3.disk");
193 remove_all("build/test/disk_writer_test3.partition");
194 remove_all("build/test/disk_writer_test3");
198 path const disk = "build/test/disk_writer_test3.disk";
199 path const partition = "build/test/disk_writer_test3.partition";
204 /* Using empty files here still triggers the bug and is much quicker than using random data */
205 make_empty_file(disk, 31043616768LL);
206 make_empty_file(partition, 31043571712LL);
208 vector<boost::filesystem::path> const dcps = {
209 TestPaths::private_data() / "xm",
210 TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV"
212 dcpomatic::write(dcps, disk.string(), partition.string(), nullptr);
214 BOOST_CHECK_EQUAL(system("/sbin/e2fsck -fn build/test/disk_writer_test3.partition"), 0);
216 path const check = "build/test/disk_writer_test3";
217 create_directory(check);
220 for (auto dcp: dcps) {
221 for (auto original: directory_iterator(dcp)) {
222 auto path_in_copy = dcp.filename() / original.path().filename();
223 auto path_in_check = check / original.path().filename();
224 system("e2cp " + partition.string() + ":" + path_in_copy.string() + " " + path_in_check.string());
225 check_file(original.path(), path_in_check);