37c8f42d56cec9d3474f4965d9372c65ec8b0c19
[dcpomatic.git] / test / disk_writer_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "lib/cross.h"
23 #include "lib/ext.h"
24 #include "test.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>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <iostream>
34 #include <future>
35 #include <regex>
36
37
38 using std::future;
39 using std::string;
40 using std::vector;
41
42
43 static
44 void
45 create_empty (boost::filesystem::path file, int size)
46 {
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);
51         close (fd);
52 }
53
54
55 vector<string>
56 ext2_ls (string path)
57 {
58         using namespace boost::process;
59
60         boost::asio::io_service ios;
61         future<string> data;
62         /* e2ls is from 'e2tools */
63         child ch (search_path("e2ls"), path, std_in.close(), std_out > data, ios);
64         ios.run();
65
66         auto output = data.get();
67         boost::trim (output);
68         vector<string> parts;
69         boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
70         return parts;
71 }
72
73
74 /** Use the writer code to make a disk and partition and copy a file to it, then check
75  *  that the partition has inode size 128 and that the file can be copied back off using
76  *  e2tools.
77  */
78 BOOST_AUTO_TEST_CASE (disk_writer_test1)
79 {
80         using namespace boost::filesystem;
81         using namespace boost::process;
82
83         remove_all ("build/test/disk_writer_test1.disk");
84         remove_all ("build/test/disk_writer_test1.partition");
85         remove_all ("build/test/disk_writer_test1");
86
87         path disk = "build/test/disk_writer_test1.disk";
88         path partition = "build/test/disk_writer_test1.partition";
89
90         /* lwext4 has a lower limit of correct ext2 partition sizes it can make; 32Mb
91          * does not work here: fsck gives errors about an incorrect free blocks count.
92          */
93         create_empty (disk, 256 * 1024 * 1024);
94         create_empty (partition, 256 * 1024 * 1024);
95
96         path dcp = "build/test/disk_writer_test1";
97         create_directory (dcp);
98         /* Some arbitrary file size here */
99         make_random_file (dcp / "foo", 1024 * 1024 * 32 - 6128);
100
101         PrivilegeEscalator::test = true;
102         dcpomatic::write (dcp, disk.string(), partition.string(), 0);
103
104         BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
105
106         {
107                 boost::asio::io_service ios;
108                 future<string> data;
109                 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
110                 ios.run();
111
112                 string output = data.get();
113                 std::smatch matches;
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");
118         }
119
120         BOOST_CHECK (ext2_ls(partition.string()) == vector<string>({"disk_writer_test1", "lost+found"}));
121         BOOST_CHECK (ext2_ls(partition.string() + ":disk_writer_test1") == vector<string>({"foo"}));
122
123         system ("e2cp " + partition.string() + ":disk_writer_test1/foo build/test/disk_writer_test1_foo_back");
124         check_file ("build/test/disk_writer_test1/foo", "build/test/disk_writer_test1_foo_back");
125 }
126