3e64bede4c50aebe221da239a0a3860d7a4db027
[dcpomatic.git] / test / find_missing_test.cc
1 /*
2     Copyright (C) 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/content.h"
23 #include "lib/content_factory.h"
24 #include "lib/dcp_content.h"
25 #include "lib/film.h"
26 #include "lib/find_missing.h"
27 #include "test.h"
28 #include <boost/filesystem.hpp>
29 #include <boost/test/unit_test.hpp>
30
31
32 using std::make_shared;
33 using std::string;
34
35
36 BOOST_AUTO_TEST_CASE (find_missing_test_with_single_files)
37 {
38         using namespace boost::filesystem;
39
40         auto name = string{"find_missing_test_with_single_files"};
41
42         /* Make a directory with some content */
43         auto content_dir = path("build/test") / path(name + "_content");
44         remove_all (content_dir);
45         create_directories (content_dir);
46         copy_file ("test/data/flat_red.png", content_dir / "A.png");
47         copy_file ("test/data/flat_red.png", content_dir / "B.png");
48         copy_file ("test/data/flat_red.png", content_dir / "C.png");
49
50         /* Make a film with that content */
51         auto film = new_test_film2 (name + "_film", {
52                 content_factory(content_dir / "A.png")[0],
53                 content_factory(content_dir / "B.png")[0],
54                 content_factory(content_dir / "C.png")[0]
55                 });
56         film->write_metadata ();
57
58         /* Move the content somewhere eles */
59         auto moved = path("build/test") / path(name + "_moved");
60         remove_all (moved);
61         rename (content_dir, moved);
62
63         /* That should make the content paths invalid */
64         for (auto content: film->content()) {
65                 BOOST_CHECK (!content->paths_valid());
66         }
67
68         /* Fix the missing files and check the result */
69         dcpomatic::find_missing (film->content(), moved / "A.png");
70
71         for (auto content: film->content()) {
72                 BOOST_CHECK (content->paths_valid());
73         }
74 }
75
76
77 BOOST_AUTO_TEST_CASE (find_missing_test_with_multiple_files)
78 {
79         using namespace boost::filesystem;
80
81         auto name = string{"find_missing_test_with_multiple_files"};
82
83         /* Copy an arbitrary DCP into a test directory */
84         auto content_dir = path("build/test") / path(name + "_content");
85         remove_all (content_dir);
86         create_directories (content_dir);
87         for (auto ref: directory_iterator("test/data/scaling_test_133_185")) {
88                 copy (ref, content_dir / ref.path().filename());
89         }
90
91         /* Make a film containing that DCP */
92         auto film = new_test_film2 (name + "_film", { make_shared<DCPContent>(content_dir) });
93         film->write_metadata ();
94
95         /* Move the DCP's content elsewhere */
96         auto moved = path("build/test") / path(name + "_moved");
97         remove_all (moved);
98         rename (content_dir, moved);
99
100         /* That should make the content paths invalid */
101         for (auto content: film->content()) {
102                 BOOST_CHECK (!content->paths_valid());
103         }
104
105         /* Fix the missing files and check the result */
106         dcpomatic::find_missing (film->content(), moved / "foo");
107
108         for (auto content: film->content()) {
109                 BOOST_CHECK (content->paths_valid());
110         }
111 }
112
113
114 BOOST_AUTO_TEST_CASE (find_missing_test_with_multiple_files_one_incorrect)
115 {
116         using namespace boost::filesystem;
117
118         auto name = string{"find_missing_test_with_multiple_files_one_incorrect"};
119
120         /* Copy an arbitrary DCP into a test directory */
121         auto content_dir = path("build/test") / path(name + "_content");
122         remove_all (content_dir);
123         create_directories (content_dir);
124         for (auto ref: directory_iterator("test/data/scaling_test_133_185")) {
125                 copy (ref, content_dir / ref.path().filename());
126         }
127
128         /* Make a film containing that DCP */
129         auto film = new_test_film2 (name + "_film", { make_shared<DCPContent>(content_dir) });
130         film->write_metadata ();
131
132         /* Move the DCP's content elsewhere */
133         auto moved = path("build/test") / path(name + "_moved");
134         remove_all (moved);
135         rename (content_dir, moved);
136
137         /* Corrupt one of the files in the moved content, so that it should not be found in the find_missing
138          * step
139          */
140         auto cpl = find_file(moved, "cpl_");
141         remove (cpl);
142         copy ("test/data/scaling_test_133_185/ASSETMAP.xml", cpl);
143
144         /* The film's contents should be invalid */
145         for (auto content: film->content()) {
146                 BOOST_CHECK (!content->paths_valid());
147         }
148
149         dcpomatic::find_missing (film->content(), moved / "foo");
150
151         /* And even after find_missing there should still be missing content */
152         for (auto content: film->content()) {
153                 BOOST_CHECK (!content->paths_valid());
154         }
155 }
156