Cleanup: extract encrypt() call from Film::make_kdm().
[dcpomatic.git] / test / import_dcp_test.cc
1 /*
2     Copyright (C) 2014-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/import_dcp_test.cc
23  *  @brief Test import of encrypted DCPs.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/config.h"
29 #include "lib/constants.h"
30 #include "lib/content_factory.h"
31 #include "lib/cross.h"
32 #include "lib/dcp_content.h"
33 #include "lib/dcp_content_type.h"
34 #include "lib/dcp_subtitle_content.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/film.h"
38 #include "lib/job_manager.h"
39 #include "lib/ratio.h"
40 #include "lib/screen.h"
41 #include "lib/video_content.h"
42 #include "test.h"
43 #include <dcp/cpl.h>
44 #include <boost/test/unit_test.hpp>
45
46
47 using std::dynamic_pointer_cast;
48 using std::make_shared;
49 using std::map;
50 using std::string;
51 using std::vector;
52
53
54 /** Make an encrypted DCP, import it and make a new unencrypted DCP */
55 BOOST_AUTO_TEST_CASE (import_dcp_test)
56 {
57         ConfigRestorer cr;
58
59         auto A = new_test_film ("import_dcp_test");
60         A->set_container (Ratio::from_id ("185"));
61         A->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
62         A->set_name ("frobozz");
63         A->set_interop (false);
64
65         auto c = make_shared<FFmpegContent>("test/data/test.mp4");
66         A->examine_and_add_content (c);
67         A->set_encrypted (true);
68         BOOST_CHECK (!wait_for_jobs ());
69
70         make_and_verify_dcp (A);
71
72         dcp::DCP A_dcp ("build/test/import_dcp_test/" + A->dcp_name());
73         A_dcp.read ();
74
75         Config::instance()->set_decryption_chain (make_shared<dcp::CertificateChain>(openssl_path(), CERTIFICATE_VALIDITY_PERIOD));
76
77         auto signer = Config::instance()->signer_chain();
78         BOOST_REQUIRE(signer->valid());
79
80         auto const decrypted_kdm = A->make_kdm(A_dcp.cpls().front()->file().get(), dcp::LocalTime ("2030-07-21T00:00:00+00:00"), dcp::LocalTime ("2031-07-21T00:00:00+00:00"));
81         auto const kdm = decrypted_kdm.encrypt(signer, Config::instance()->decryption_chain()->leaf(), {}, dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, 0);
82
83         auto B = new_test_film ("import_dcp_test2");
84         B->set_container (Ratio::from_id ("185"));
85         B->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
86         B->set_name ("frobozz");
87         B->set_interop (false);
88
89         auto d = make_shared<DCPContent>("build/test/import_dcp_test/" + A->dcp_name());
90         B->examine_and_add_content (d);
91         BOOST_CHECK (!wait_for_jobs ());
92         d->add_kdm (kdm);
93         JobManager::instance()->add (make_shared<ExamineContentJob>(B, d));
94         BOOST_CHECK (!wait_for_jobs ());
95
96         make_and_verify_dcp (B);
97
98         /* Should be 1s red, 1s green, 1s blue */
99         check_dcp ("test/data/import_dcp_test2", "build/test/import_dcp_test2/" + B->dcp_name());
100 }
101
102
103 /** Check that DCP markers are imported correctly */
104 BOOST_AUTO_TEST_CASE (import_dcp_markers_test)
105 {
106         Cleanup cl;
107
108         /* Make a DCP with some markers */
109         auto content = content_factory("test/data/flat_red.png");
110         auto film = new_test_film2 ("import_dcp_markers_test", content, &cl);
111
112         content[0]->video->set_length (24 * 60 * 10);
113
114         film->set_marker(dcp::Marker::FFOC, dcpomatic::DCPTime::from_frames(1, 24));
115         film->set_marker(dcp::Marker::FFMC, dcpomatic::DCPTime::from_seconds(9.4));
116         film->set_marker(dcp::Marker::LFMC, dcpomatic::DCPTime::from_seconds(9.99));
117
118         make_and_verify_dcp (film);
119
120         /* Import the DCP to a new film and check the markers */
121         auto imported = make_shared<DCPContent>(film->dir(film->dcp_name()));
122         auto film2 = new_test_film2 ("import_dcp_markers_test2", {imported}, &cl);
123         film2->write_metadata ();
124
125         /* When import_dcp_markers_test was made a LFOC marker will automatically
126          * have been added.
127          */
128         BOOST_CHECK_EQUAL (imported->markers().size(), 4U);
129
130         auto markers = imported->markers();
131         BOOST_REQUIRE(markers.find(dcp::Marker::FFMC) != markers.end());
132         BOOST_CHECK(markers[dcp::Marker::FFMC] == dcpomatic::ContentTime(904000));
133         BOOST_REQUIRE(markers.find(dcp::Marker::LFMC) != markers.end());
134         BOOST_CHECK(markers[dcp::Marker::LFMC] == dcpomatic::ContentTime(960000));
135
136         /* Load that film and check that the markers have been loaded */
137         auto film3 = make_shared<Film>(boost::filesystem::path("build/test/import_dcp_markers_test2"));
138         film3->read_metadata ();
139         BOOST_REQUIRE_EQUAL (film3->content().size(), 1U);
140         auto reloaded = dynamic_pointer_cast<DCPContent>(film3->content().front());
141         BOOST_REQUIRE (reloaded);
142
143         BOOST_CHECK_EQUAL (reloaded->markers().size(), 4U);
144
145         markers = reloaded->markers();
146         BOOST_REQUIRE(markers.find(dcp::Marker::FFMC) != markers.end());
147         BOOST_CHECK(markers[dcp::Marker::FFMC] == dcpomatic::ContentTime(904000));
148         BOOST_REQUIRE(markers.find(dcp::Marker::LFMC) != markers.end());
149         BOOST_CHECK(markers[dcp::Marker::LFMC] == dcpomatic::ContentTime(960000));
150
151         cl.run ();
152 }
153
154
155 /** Check that DCP metadata (ratings and content version) are imported correctly */
156 BOOST_AUTO_TEST_CASE (import_dcp_metadata_test)
157 {
158         /* Make a DCP with some ratings and a content version */
159         auto film = new_test_film2 ("import_dcp_metadata_test");
160         auto content = content_factory("test/data/flat_red.png")[0];
161         film->examine_and_add_content (content);
162         BOOST_REQUIRE (!wait_for_jobs());
163
164         content->video->set_length (10);
165
166         vector<dcp::Rating> ratings = { {"BBFC", "15"}, {"MPAA", "NC-17"} };
167         film->set_ratings (ratings);
168
169         vector<string> cv = { "Fred "};
170         film->set_content_versions (cv);
171
172         make_and_verify_dcp (film);
173
174         /* Import the DCP to a new film and check the metadata */
175         auto film2 = new_test_film2 ("import_dcp_metadata_test2");
176         auto imported = make_shared<DCPContent>(film->dir(film->dcp_name()));
177         film2->examine_and_add_content (imported);
178         BOOST_REQUIRE (!wait_for_jobs());
179         film2->write_metadata ();
180
181         BOOST_CHECK (imported->ratings() == ratings);
182         BOOST_CHECK (imported->content_versions() == cv);
183
184         /* Load that film and check that the metadata has been loaded */
185         auto film3 = make_shared<Film>(boost::filesystem::path("build/test/import_dcp_metadata_test2"));
186         film3->read_metadata ();
187         BOOST_REQUIRE_EQUAL (film3->content().size(), 1U);
188         auto reloaded = dynamic_pointer_cast<DCPContent>(film3->content().front());
189         BOOST_REQUIRE (reloaded);
190
191         BOOST_CHECK (reloaded->ratings() == ratings);
192         BOOST_CHECK (reloaded->content_versions() == cv);
193 }
194