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>
45 create_empty (boost::filesystem::path file, int size)
47 auto fd = open (file.string().c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
48 BOOST_REQUIRE (fd != -1);
49 auto const r = posix_fallocate (fd, 0, size);
50 BOOST_REQUIRE_EQUAL (r, 0);
56 ext2_ls (vector<string> arguments)
58 using namespace boost::process;
60 boost::asio::io_service ios;
62 child ch (search_path("e2ls"), arguments, std_in.close(), std_out > data, ios);
65 auto output = data.get();
68 boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
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;
84 remove_all ("build/test/disk_writer_test1.disk");
85 remove_all ("build/test/disk_writer_test1.partition");
86 remove_all ("build/test/disk_writer_test1");
88 path disk = "build/test/disk_writer_test1.disk";
89 path partition = "build/test/disk_writer_test1.partition";
91 /* lwext4 has a lower limit of correct ext2 partition sizes it can make; 32Mb
92 * does not work here: fsck gives errors about an incorrect free blocks count.
94 create_empty (disk, 256 * 1024 * 1024);
95 create_empty (partition, 256 * 1024 * 1024);
97 path dcp = "build/test/disk_writer_test1";
98 create_directory (dcp);
99 /* Some arbitrary file size here */
100 make_random_file (dcp / "foo", 1024 * 1024 * 32 - 6128);
102 dcpomatic::write (dcp, disk.string(), partition.string(), 0);
104 BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
107 boost::asio::io_service ios;
109 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
112 string output = data.get();
114 std::regex reg("Inode size:\\s*(.*)");
115 BOOST_REQUIRE (std::regex_search(output, matches, reg));
116 BOOST_REQUIRE (matches.size() == 2);
117 BOOST_CHECK_EQUAL (matches[1].str(), "128");
120 BOOST_CHECK (ext2_ls({partition.string()}) == vector<string>({"disk_writer_test1", "lost+found"}));
122 string const unset_date = "1-Jan-1970";
124 /* Check timestamp of the directory has been set */
125 auto details = ext2_ls({"-l", partition.string()});
126 BOOST_REQUIRE (details.size() >= 6);
127 BOOST_CHECK (details[5] != unset_date);
129 auto const dir = partition.string() + ":disk_writer_test1";
130 BOOST_CHECK (ext2_ls({dir}) == vector<string>({"foo"}));
132 /* Check timestamp of foo */
133 details = ext2_ls({"-l", dir});
134 BOOST_REQUIRE (details.size() >= 6);
135 BOOST_CHECK (details[5] != unset_date);
137 system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back");
138 check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back");