Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / file_group_test.cc
1 /*
2     Copyright (C) 2013-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 /** @file  test/file_group_test.cc
23  *  @brief Test FileGroup class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/file_group.h"
29 #include <boost/test/unit_test.hpp>
30 #include <boost/filesystem.hpp>
31 #include <stdint.h>
32 #include <cstdio>
33
34
35 using std::vector;
36
37
38 BOOST_AUTO_TEST_CASE (file_group_test)
39 {
40         /* Random data; must be big enough for all the files */
41         uint8_t data[65536];
42         for (int i = 0; i < 65536; ++i) {
43                 data[i] = rand() & 0xff;
44         }
45
46         int const num_files = 4;
47
48         int length[] = {
49                 99,
50                 18941,
51                 33110,
52                 42
53         };
54
55         int total_length = 0;
56         for (int i = 0; i < num_files; ++i) {
57                 total_length += length[i];
58         }
59
60         boost::filesystem::create_directories ("build/test/file_group_test");
61         vector<boost::filesystem::path> name = {
62                 "build/test/file_group_test/A",
63                 "build/test/file_group_test/B",
64                 "build/test/file_group_test/C",
65                 "build/test/file_group_test/D"
66         };
67
68         int base = 0;
69         for (int i = 0; i < num_files; ++i) {
70                 auto f = fopen (name[i].string().c_str(), "wb");
71                 fwrite (data + base, 1, length[i], f);
72                 fclose (f);
73                 base += length[i];
74         }
75
76         FileGroup fg (name);
77         uint8_t test[65536];
78
79         int pos = 0;
80
81         /* Basic read from 0 */
82         BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
83         BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
84         pos += 64;
85
86         /* Another read following the previous */
87         BOOST_CHECK_EQUAL (fg.read(test, 4), 4);
88         BOOST_CHECK_EQUAL (memcmp(data + pos, test, 4), 0);
89         pos += 4;
90
91         /* Read overlapping A and B */
92         BOOST_CHECK_EQUAL (fg.read(test, 128), 128);
93         BOOST_CHECK_EQUAL (memcmp(data + pos, test, 128), 0);
94         pos += 128;
95
96         /* Read overlapping B/C/D and over-reading by a lot */
97         BOOST_CHECK_EQUAL (fg.read(test, total_length * 3), total_length - pos);
98         BOOST_CHECK_EQUAL (memcmp(data + pos, test, total_length - pos), 0);
99
100         /* Over-read by a little */
101         BOOST_CHECK_EQUAL (fg.seek(0, SEEK_SET), 0);
102         BOOST_CHECK_EQUAL (fg.read(test, total_length), total_length);
103         BOOST_CHECK_EQUAL (fg.read(test, 1), 0);
104
105         /* Seeking off the end of the file should not give an error */
106         BOOST_CHECK_EQUAL (fg.seek(total_length * 2, SEEK_SET), total_length * 2);
107         /* and attempting to read should return nothing */
108         BOOST_CHECK_EQUAL (fg.read(test, 64), 0);
109         /* but the requested seek should be remembered, so if we now go back (relatively) */
110         BOOST_CHECK_EQUAL (fg.seek(-total_length * 2, SEEK_CUR), 0);
111         /* we should be at the start again */
112         BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
113         BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
114
115         /* SEEK_SET */
116         BOOST_CHECK_EQUAL (fg.seek(999, SEEK_SET), 999);
117         BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
118         BOOST_CHECK_EQUAL (memcmp(data + 999, test, 64), 0);
119
120         /* SEEK_CUR */
121         BOOST_CHECK_EQUAL (fg.seek(42, SEEK_CUR), 999 + 64 + 42);
122         BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
123         BOOST_CHECK_EQUAL (memcmp(data + 999 + 64 + 42, test, 64), 0);
124
125         /* SEEK_END */
126         BOOST_CHECK_EQUAL (fg.seek(1077, SEEK_END), total_length - 1077);
127         BOOST_CHECK_EQUAL (fg.read(test, 256), 256);
128         BOOST_CHECK_EQUAL (memcmp(data + total_length - 1077, test, 256), 0);
129 }