summaryrefslogtreecommitdiff
path: root/test/kdm_naming_test.cc
blob: dda30f68880f932f48eebf4a4f0aef895902f0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
    Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "lib/cinema.h"
#include "lib/config.h"
#include "lib/content_factory.h"
#include "lib/film.h"
#include "lib/kdm_with_metadata.h"
#include "lib/screen.h"
#include "test.h"
#include <boost/test/unit_test.hpp>


using std::dynamic_pointer_cast;
using std::list;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;


static
bool
confirm_overwrite (boost::filesystem::path)
{
	return true;
}


static shared_ptr<dcpomatic::Screen> cinema_a_screen_1;
static shared_ptr<dcpomatic::Screen> cinema_a_screen_2;
static shared_ptr<dcpomatic::Screen> cinema_b_screen_x;
static shared_ptr<dcpomatic::Screen> cinema_b_screen_y;
static shared_ptr<dcpomatic::Screen> cinema_b_screen_z;


BOOST_AUTO_TEST_CASE (single_kdm_naming_test)
{
	auto c = Config::instance();

	auto crypt_cert = c->decryption_chain()->leaf();

	/* Cinema A: UTC +4:30 */
	auto cinema_a = make_shared<Cinema>("Cinema A", list<string>(), "", 4, 30);
	cinema_a_screen_1 = std::make_shared<dcpomatic::Screen>("Screen 1", "", crypt_cert, boost::none, vector<TrustedDevice>());
	cinema_a->add_screen (cinema_a_screen_1);
	cinema_a_screen_2 = std::make_shared<dcpomatic::Screen>("Screen 2", "", crypt_cert, boost::none, vector<TrustedDevice>());
	cinema_a->add_screen (cinema_a_screen_2);
	c->add_cinema (cinema_a);

	/* Cinema B: UTC -1:00 */
	auto cinema_b = make_shared<Cinema>("Cinema B", list<string>(), "", -1, 0);
	cinema_b_screen_x = std::make_shared<dcpomatic::Screen>("Screen X", "", crypt_cert, boost::none, vector<TrustedDevice>());
	cinema_b->add_screen (cinema_b_screen_x);
	cinema_b_screen_y = std::make_shared<dcpomatic::Screen>("Screen Y", "", crypt_cert, boost::none, vector<TrustedDevice>());
	cinema_b->add_screen (cinema_b_screen_y);
	cinema_b_screen_z = std::make_shared<dcpomatic::Screen>("Screen Z", "", crypt_cert, boost::none, vector<TrustedDevice>());
	cinema_b->add_screen (cinema_b_screen_z);
	c->add_cinema (cinema_a);

	/* Film */
	boost::filesystem::remove_all ("build/test/single_kdm_naming_test");
	auto film = new_test_film2 ("single_kdm_naming_test");
	film->set_name ("my_great_film");
	film->examine_and_add_content (content_factory("test/data/flat_black.png")[0]);
	BOOST_REQUIRE (!wait_for_jobs());
	film->set_encrypted (true);
	make_and_verify_dcp (film);
	auto cpls = film->cpls ();
	BOOST_REQUIRE(cpls.size() == 1);

	auto sign_cert = c->signer_chain()->leaf();

	dcp::LocalTime from (sign_cert.not_before());
	from.add_months (2);
	dcp::LocalTime until (sign_cert.not_after());
	until.add_months (-2);

	auto const from_string = from.date() + " " + from.time_of_day(true, false);
	auto const until_string = until.date() + " " + until.time_of_day(true, false);

	auto cpl = cpls.front().cpl_file;
	auto kdm = kdm_for_screen (
			film,
			cpls.front().cpl_file,
			cinema_a_screen_1,
			boost::posix_time::time_from_string(from_string),
			boost::posix_time::time_from_string(until_string),
			dcp::Formulation::MODIFIED_TRANSITIONAL_1,
			false,
			optional<int>()
			);

	write_files (
		{ kdm },
		boost::filesystem::path("build/test/single_kdm_naming_test"),
		dcp::NameFormat("KDM %c - %s - %f - %b - %e"),
		&confirm_overwrite
		);

	auto from_time = from.time_of_day (true, false);
	boost::algorithm::replace_all (from_time, ":", "-");
	auto until_time = until.time_of_day (true, false);
	boost::algorithm::replace_all (until_time, ":", "-");

	auto const dcp_date = boost::gregorian::to_iso_string(film->isdcf_date());
	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);
	BOOST_CHECK_MESSAGE (boost::filesystem::exists("build/test/single_kdm_naming_test/" + ref), "File " << ref << " not found");
}


BOOST_AUTO_TEST_CASE (directory_kdm_naming_test, * boost::unit_test::depends_on("single_kdm_naming_test"))
{
	using boost::filesystem::path;

	/* Film */
	boost::filesystem::remove_all ("build/test/directory_kdm_naming_test");
	auto film = new_test_film2 (
		"directory_kdm_naming_test",
		{ content_factory("test/data/flat_black.png")[0] }
		);

	film->set_name ("my_great_film");
	film->set_encrypted (true);
	make_and_verify_dcp (film);
	auto cpls = film->cpls ();
	BOOST_REQUIRE(cpls.size() == 1);

	auto sign_cert = Config::instance()->signer_chain()->leaf();

	dcp::LocalTime from (sign_cert.not_before());
	from.add_months (2);
	dcp::LocalTime until (sign_cert.not_after());
	until.add_months (-2);

	string const from_string = from.date() + " " + from.time_of_day(true, false);
	string const until_string = until.date() + " " + until.time_of_day(true, false);

	list<shared_ptr<dcpomatic::Screen>> screens = {
		cinema_a_screen_2, cinema_b_screen_x, cinema_a_screen_1, (cinema_b_screen_z)
	};

	auto const cpl = cpls.front().cpl_file;
	auto const cpl_id = cpls.front().cpl_id;

	list<KDMWithMetadataPtr> kdms;
	for (auto i: screens) {
		auto kdm = kdm_for_screen (
				film,
				cpls.front().cpl_file,
				i,
				boost::posix_time::time_from_string(from_string),
				boost::posix_time::time_from_string(until_string),
				dcp::Formulation::MODIFIED_TRANSITIONAL_1,
				false,
				optional<int>()
				);

		kdms.push_back (kdm);
	}

	write_directories (
		collect(kdms),
		path("build/test/directory_kdm_naming_test"),
		dcp::NameFormat("%c - %s - %f - %b - %e"),
#ifdef DCPOMATIC_WINDOWS
		/* Use a shorter name on Windows so that the paths aren't too long */
		dcp::NameFormat("KDM %f"),
#else
		dcp::NameFormat("KDM %c - %s - %f - %b - %e - %i"),
#endif
		&confirm_overwrite
		);

	auto from_time = from.time_of_day (true, false);
	boost::algorithm::replace_all (from_time, ":", "-");
	auto until_time = until.time_of_day (true, false);
	boost::algorithm::replace_all (until_time, ":", "-");

	auto const dcp_date = boost::gregorian::to_iso_string(film->isdcf_date());
	auto const dcp_name = String::compose("MyGreatFilm_TST-1_F_XX-XX_MOS_2K_%1_SMPTE_OV", dcp_date);
	auto const common = String::compose("%1_-_%2_%3_-_%4_%5", dcp_name, from.date(), from_time, until.date(), until_time);

	path const base = "build/test/directory_kdm_naming_test";

	path dir_a = String::compose("Cinema_A_-_%s_-_%1", common);
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a), "Directory " << dir_a << " not found");
	path dir_b = String::compose("Cinema_B_-_%s_-_%1", common);
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b), "Directory " << dir_b << " not found");

#ifdef DCPOMATIC_WINDOWS
	path ref = String::compose("KDM_%1.xml", dcp_name);
#else
	path ref = String::compose("KDM_Cinema_A_-_Screen_2_-_%1_-_%2.xml", common, cpl_id);
#endif
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");

#ifdef DCPOMATIC_WINDOWS
	ref = String::compose("KDM_%1.xml", dcp_name);
#else
	ref = String::compose("KDM_Cinema_B_-_Screen_X_-_%1_-_%2.xml", common, cpl_id);
#endif
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");

#ifdef DCPOMATIC_WINDOWS
	ref = String::compose("KDM_%1.xml", dcp_name);
#else
	ref = String::compose("KDM_Cinema_A_-_Screen_1_-_%1_-_%2.xml", common, cpl_id);
#endif
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");

#ifdef DCPOMATIC_WINDOWS
	ref = String::compose("KDM_%1.xml", dcp_name);
#else
	ref = String::compose("KDM_Cinema_B_-_Screen_Z_-_%1_-_%2.xml", common, cpl_id);
#endif
	BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
}