Read 16-bit audio DCPs correctly.
[dcpomatic.git] / test / disk_writer_test.cc
1 /*
2     Copyright (C) 2020-2021 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 vector<string>
44 ext2_ls (vector<string> arguments)
45 {
46         using namespace boost::process;
47
48         boost::asio::io_service ios;
49         future<string> data;
50         child ch (search_path("e2ls"), arguments, std_in.close(), std_out > data, ios);
51         ios.run();
52
53         auto output = data.get();
54         boost::trim (output);
55         vector<string> parts;
56         boost::split (parts, output, boost::is_any_of("\t "), boost::token_compress_on);
57         return parts;
58 }
59
60
61 static
62 void
63 make_empty_file(boost::filesystem::path file, off_t size)
64 {
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);
69        close (fd);
70 }
71
72
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
78  */
79 BOOST_AUTO_TEST_CASE (disk_writer_test1)
80 {
81         using namespace boost::filesystem;
82         using namespace boost::process;
83
84         Cleanup cl;
85
86         path disk = "build/test/disk_writer_test1.disk";
87         path partition = "build/test/disk_writer_test1.partition";
88
89         cl.add(disk);
90         cl.add(partition);
91
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.
94          */
95         make_random_file(disk, 256 * 1024 * 1024);
96         make_random_file(partition, 256 * 1024 * 1024);
97
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);
102
103         dcpomatic::write ({dcp}, disk.string(), partition.string(), nullptr);
104
105         BOOST_CHECK_EQUAL (system("/sbin/e2fsck -fn build/test/disk_writer_test1.partition"), 0);
106
107         {
108                 boost::asio::io_service ios;
109                 future<string> data;
110                 child ch ("/sbin/tune2fs", args({"-l", partition.string()}), std_in.close(), std_out > data, ios);
111                 ios.run();
112
113                 string output = data.get();
114                 std::smatch matches;
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");
119         }
120
121         BOOST_CHECK (ext2_ls({partition.string()}) == vector<string>({"disk_writer_test1", "lost+found"}));
122
123         string const unset_date = "1-Jan-1970";
124
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);
129
130         auto const dir = partition.string() + ":disk_writer_test1";
131         BOOST_CHECK (ext2_ls({dir}) == vector<string>({"foo"}));
132
133         /* Check timestamp of foo */
134         details = ext2_ls({"-l", dir});
135         BOOST_REQUIRE (details.size() >= 6);
136         BOOST_CHECK (details[5] != unset_date);
137
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");
140
141         cl.run();
142 }
143
144
145 BOOST_AUTO_TEST_CASE (disk_writer_test2)
146 {
147         using namespace boost::filesystem;
148         using namespace boost::process;
149
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");
153
154         Cleanup cl;
155
156         path const disk = "build/test/disk_writer_test2.disk";
157         path const partition = "build/test/disk_writer_test2.partition";
158
159         cl.add(disk);
160         cl.add(partition);
161
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);
165
166         auto const dcp = TestPaths::private_data() / "xm";
167         dcpomatic::write({dcp}, disk.string(), partition.string(), nullptr);
168
169         BOOST_CHECK_EQUAL(system("/sbin/e2fsck -fn build/test/disk_writer_test2.partition"), 0);
170
171         path const check = "build/test/disk_writer_test2";
172         create_directory(check);
173         cl.add(check);
174
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);
180         }
181
182         cl.run();
183 }
184
185
186
187 BOOST_AUTO_TEST_CASE (disk_writer_test3)
188 {
189         using namespace boost::filesystem;
190         using namespace boost::process;
191
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");
195
196         Cleanup cl;
197
198         path const disk = "build/test/disk_writer_test3.disk";
199         path const partition = "build/test/disk_writer_test3.partition";
200
201         cl.add(disk);
202         cl.add(partition);
203
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);
207
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"
211         };
212         dcpomatic::write(dcps, disk.string(), partition.string(), nullptr);
213
214         BOOST_CHECK_EQUAL(system("/sbin/e2fsck -fn build/test/disk_writer_test3.partition"), 0);
215
216         path const check = "build/test/disk_writer_test3";
217         create_directory(check);
218         cl.add(check);
219
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);
226                 }
227         }
228
229         cl.run();
230 }