Apply fixup to test/data submodule and bring it back to the v2.18.x branch.
[dcpomatic.git] / test / kdm_naming_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/cinema.h"
23 #include "lib/config.h"
24 #include "lib/content_factory.h"
25 #include "lib/film.h"
26 #include "lib/kdm_with_metadata.h"
27 #include "lib/screen.h"
28 #include "test.h"
29 #include <boost/test/unit_test.hpp>
30
31
32 using std::dynamic_pointer_cast;
33 using std::list;
34 using std::make_shared;
35 using std::shared_ptr;
36 using std::string;
37 using std::vector;
38 using boost::optional;
39
40
41 static
42 bool
43 confirm_overwrite (boost::filesystem::path)
44 {
45         return true;
46 }
47
48
49 static shared_ptr<dcpomatic::Screen> cinema_a_screen_1;
50 static shared_ptr<dcpomatic::Screen> cinema_a_screen_2;
51 static shared_ptr<dcpomatic::Screen> cinema_b_screen_x;
52 static shared_ptr<dcpomatic::Screen> cinema_b_screen_y;
53 static shared_ptr<dcpomatic::Screen> cinema_b_screen_z;
54
55
56 BOOST_AUTO_TEST_CASE (single_kdm_naming_test)
57 {
58         auto c = Config::instance();
59
60         auto crypt_cert = c->decryption_chain()->leaf();
61
62         auto cinema_a = make_shared<Cinema>("Cinema A", vector<string>(), "", dcp::UTCOffset{4, 30});
63         cinema_a_screen_1 = std::make_shared<dcpomatic::Screen>("Screen 1", "", crypt_cert, boost::none, vector<TrustedDevice>());
64         cinema_a->add_screen (cinema_a_screen_1);
65         cinema_a_screen_2 = std::make_shared<dcpomatic::Screen>("Screen 2", "", crypt_cert, boost::none, vector<TrustedDevice>());
66         cinema_a->add_screen (cinema_a_screen_2);
67         c->add_cinema (cinema_a);
68
69         auto cinema_b = make_shared<Cinema>("Cinema B", vector<string>(), "", dcp::UTCOffset{-1, 0});
70         cinema_b_screen_x = std::make_shared<dcpomatic::Screen>("Screen X", "", crypt_cert, boost::none, vector<TrustedDevice>());
71         cinema_b->add_screen (cinema_b_screen_x);
72         cinema_b_screen_y = std::make_shared<dcpomatic::Screen>("Screen Y", "", crypt_cert, boost::none, vector<TrustedDevice>());
73         cinema_b->add_screen (cinema_b_screen_y);
74         cinema_b_screen_z = std::make_shared<dcpomatic::Screen>("Screen Z", "", crypt_cert, boost::none, vector<TrustedDevice>());
75         cinema_b->add_screen (cinema_b_screen_z);
76         c->add_cinema (cinema_a);
77
78         /* Film */
79         boost::filesystem::remove_all ("build/test/single_kdm_naming_test");
80         auto film = new_test_film2 ("single_kdm_naming_test");
81         film->set_name ("my_great_film");
82         film->examine_and_add_content (content_factory("test/data/flat_black.png")[0]);
83         BOOST_REQUIRE (!wait_for_jobs());
84         film->set_encrypted (true);
85         make_and_verify_dcp (film);
86         auto cpls = film->cpls ();
87         BOOST_REQUIRE(cpls.size() == 1);
88
89         auto sign_cert = c->signer_chain()->leaf();
90
91         dcp::LocalTime from = sign_cert.not_before();
92         from.add_months (2);
93         dcp::LocalTime until = sign_cert.not_after();
94         until.add_months (-2);
95
96         std::vector<KDMCertificatePeriod> period_checks;
97
98         auto cpl = cpls.front().cpl_file;
99         std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm = [film, cpl](dcp::LocalTime begin, dcp::LocalTime end) {
100                 return film->make_kdm(cpl, begin, end);
101         };
102         auto kdm = kdm_for_screen (
103                         make_kdm,
104                         cinema_a_screen_1,
105                         from,
106                         until,
107                         dcp::Formulation::MODIFIED_TRANSITIONAL_1,
108                         false,
109                         optional<int>(),
110                         period_checks
111                         );
112
113         write_files (
114                 { kdm },
115                 boost::filesystem::path("build/test/single_kdm_naming_test"),
116                 dcp::NameFormat("KDM %c - %s - %f - %b - %e"),
117                 &confirm_overwrite
118                 );
119
120         auto from_time = from.time_of_day (true, false);
121         boost::algorithm::replace_all (from_time, ":", "-");
122         auto until_time = until.time_of_day (true, false);
123         boost::algorithm::replace_all (until_time, ":", "-");
124
125         auto const dcp_date = boost::gregorian::to_iso_string(film->isdcf_date());
126         auto const ref = String::compose("KDM_Cinema_A_-_Screen_1_-_MyGreatFilm_TST-1_F_XX-XX_MOS_2K_%1_SMPTE_OV_-_%2_%3_-_%4_%5.xml", dcp_date, from.date(), from_time, until.date(), until_time);
127         BOOST_CHECK_MESSAGE (boost::filesystem::exists("build/test/single_kdm_naming_test/" + ref), "File " << ref << " not found");
128 }
129
130
131 BOOST_AUTO_TEST_CASE (directory_kdm_naming_test, * boost::unit_test::depends_on("single_kdm_naming_test"))
132 {
133         using boost::filesystem::path;
134
135         /* Film */
136         boost::filesystem::remove_all ("build/test/directory_kdm_naming_test");
137         auto film = new_test_film2 (
138                 "directory_kdm_naming_test",
139                 { content_factory("test/data/flat_black.png")[0] }
140                 );
141
142         film->set_name ("my_great_film");
143         film->set_encrypted (true);
144         make_and_verify_dcp (film);
145         auto cpls = film->cpls ();
146         BOOST_REQUIRE(cpls.size() == 1);
147
148         auto sign_cert = Config::instance()->signer_chain()->leaf();
149
150         dcp::LocalTime from (sign_cert.not_before());
151         from.add_months (2);
152         dcp::LocalTime until (sign_cert.not_after());
153         until.add_months (-2);
154
155         vector<shared_ptr<dcpomatic::Screen>> screens = {
156                 cinema_a_screen_2, cinema_b_screen_x, cinema_a_screen_1, (cinema_b_screen_z)
157         };
158
159         auto const cpl = cpls.front().cpl_file;
160         auto const cpl_id = cpls.front().cpl_id;
161
162         std::vector<KDMCertificatePeriod> period_checks;
163         list<KDMWithMetadataPtr> kdms;
164
165         std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm = [film, cpls](dcp::LocalTime begin, dcp::LocalTime end) {
166                 return film->make_kdm(cpls.front().cpl_file, begin, end);
167         };
168
169         for (auto i: screens) {
170                 auto kdm = kdm_for_screen (
171                                 make_kdm,
172                                 i,
173                                 from,
174                                 until,
175                                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
176                                 false,
177                                 optional<int>(),
178                                 period_checks
179                                 );
180
181                 kdms.push_back (kdm);
182         }
183
184         write_directories (
185                 collect(kdms),
186                 path("build/test/directory_kdm_naming_test"),
187                 dcp::NameFormat("%c - %s - %f - %b - %e"),
188 #ifdef DCPOMATIC_WINDOWS
189                 /* Use a shorter name on Windows so that the paths aren't too long */
190                 dcp::NameFormat("KDM %f"),
191 #else
192                 dcp::NameFormat("KDM %c - %s - %f - %b - %e - %i"),
193 #endif
194                 &confirm_overwrite
195                 );
196
197         auto from_time = from.time_of_day (true, false);
198         boost::algorithm::replace_all (from_time, ":", "-");
199         auto until_time = until.time_of_day (true, false);
200         boost::algorithm::replace_all (until_time, ":", "-");
201
202         auto const dcp_date = boost::gregorian::to_iso_string(film->isdcf_date());
203         auto const dcp_name = String::compose("MyGreatFilm_TST-1_F_XX-XX_MOS_2K_%1_SMPTE_OV", dcp_date);
204         auto const common = String::compose("%1_-_%2_%3_-_%4_%5", dcp_name, from.date(), from_time, until.date(), until_time);
205
206         path const base = "build/test/directory_kdm_naming_test";
207
208         path dir_a = String::compose("Cinema_A_-_%s_-_%1", common);
209         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a), "Directory " << dir_a << " not found");
210         path dir_b = String::compose("Cinema_B_-_%s_-_%1", common);
211         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b), "Directory " << dir_b << " not found");
212
213 #ifdef DCPOMATIC_WINDOWS
214         path ref = String::compose("KDM_%1.xml", dcp_name);
215 #else
216         path ref = String::compose("KDM_Cinema_A_-_Screen_2_-_%1_-_%2.xml", common, cpl_id);
217 #endif
218         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
219
220 #ifdef DCPOMATIC_WINDOWS
221         ref = String::compose("KDM_%1.xml", dcp_name);
222 #else
223         ref = String::compose("KDM_Cinema_B_-_Screen_X_-_%1_-_%2.xml", common, cpl_id);
224 #endif
225         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
226
227 #ifdef DCPOMATIC_WINDOWS
228         ref = String::compose("KDM_%1.xml", dcp_name);
229 #else
230         ref = String::compose("KDM_Cinema_A_-_Screen_1_-_%1_-_%2.xml", common, cpl_id);
231 #endif
232         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
233
234 #ifdef DCPOMATIC_WINDOWS
235         ref = String::compose("KDM_%1.xml", dcp_name);
236 #else
237         ref = String::compose("KDM_Cinema_B_-_Screen_Z_-_%1_-_%2.xml", common, cpl_id);
238 #endif
239         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
240 }
241