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