Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / dcp_decoder_test.cc
1 /*
2     Copyright (C) 2019-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/dcp_decoder_test.cc
23  *  @brief Test DCPDecoder class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/config.h"
29 #include "lib/content_factory.h"
30 #include "lib/dcp_content.h"
31 #include "lib/dcp_decoder.h"
32 #include "lib/examine_content_job.h"
33 #include "lib/film.h"
34 #include "lib/job_manager.h"
35 #include "lib/piece.h"
36 #include "lib/player.h"
37 #include "test.h"
38 #include <dcp/cpl.h>
39 #include <dcp/dcp.h>
40 #include <boost/test/unit_test.hpp>
41 #include <iostream>
42
43
44 using std::list;
45 using std::string;
46 using std::vector;
47 using std::make_shared;
48 using std::shared_ptr;
49
50
51 /* Check that DCPDecoder reuses old data when it should */
52 BOOST_AUTO_TEST_CASE (check_reuse_old_data_test)
53 {
54         /* Make some DCPs */
55
56         auto ov = new_test_film2 ("check_reuse_old_data_ov", {content_factory("test/data/flat_red.png").front()});
57         make_and_verify_dcp (ov);
58
59         auto ov_content = make_shared<DCPContent>(ov->dir(ov->dcp_name(false)));
60         auto vf = new_test_film2 ("check_reuse_old_data_vf", {ov_content, content_factory("test/data/L.wav").front()});
61         ov_content->set_reference_video (true);
62         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
63
64         auto encrypted = new_test_film2 ("check_reuse_old_data_decrypted");
65         encrypted->examine_and_add_content (content_factory("test/data/flat_red.png").front());
66         BOOST_REQUIRE (!wait_for_jobs());
67         encrypted->set_encrypted (true);
68         make_and_verify_dcp (encrypted);
69
70         dcp::DCP encrypted_dcp (encrypted->dir(encrypted->dcp_name()));
71         encrypted_dcp.read ();
72
73         auto kdm = encrypted->make_kdm (
74                 Config::instance()->decryption_chain()->leaf(),
75                 vector<string>(),
76                 encrypted_dcp.cpls().front()->file().get(),
77                 dcp::LocalTime ("2030-07-21T00:00:00+00:00"),
78                 dcp::LocalTime ("2031-07-21T00:00:00+00:00"),
79                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
80                 true, 0
81                 );
82
83
84         /* Add just the OV to a new project, move it around a bit and check that
85            the _reels get reused.
86         */
87         auto test = new_test_film2 ("check_reuse_old_data_test1");
88         ov_content = make_shared<DCPContent>(ov->dir(ov->dcp_name(false)));
89         test->examine_and_add_content (ov_content);
90         BOOST_REQUIRE (!wait_for_jobs());
91         auto player = make_shared<Player>(test, Image::Alignment::COMPACT);
92
93         auto decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
94         BOOST_REQUIRE (decoder);
95         auto reels = decoder->reels();
96
97         ov_content->set_position (test, dcpomatic::DCPTime(96000));
98         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
99         BOOST_REQUIRE (decoder);
100         BOOST_REQUIRE (reels == decoder->reels());
101
102         /* Add the VF to a new project, then add the OV and check that the
103            _reels did not get reused.
104         */
105         test = new_test_film2 ("check_reuse_old_data_test2");
106         auto vf_content = make_shared<DCPContent>(vf->dir(vf->dcp_name(false)));
107         test->examine_and_add_content (vf_content);
108         BOOST_REQUIRE (!wait_for_jobs());
109         player = make_shared<Player>(test, Image::Alignment::COMPACT);
110
111         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
112         BOOST_REQUIRE (decoder);
113         reels = decoder->reels();
114
115         vf_content->add_ov (ov->dir(ov->dcp_name(false)));
116         JobManager::instance()->add (make_shared<ExamineContentJob>(test, vf_content));
117         BOOST_REQUIRE (!wait_for_jobs());
118         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
119         BOOST_REQUIRE (decoder);
120         BOOST_REQUIRE (reels != decoder->reels());
121
122         /* Add a KDM to an encrypted DCP and check that the _reels did not get reused */
123         test = new_test_film2 ("check_reuse_old_data_test3");
124         auto encrypted_content = make_shared<DCPContent>(encrypted->dir(encrypted->dcp_name(false)));
125         test->examine_and_add_content (encrypted_content);
126         BOOST_REQUIRE (!wait_for_jobs());
127         player = make_shared<Player>(test, Image::Alignment::COMPACT);
128
129         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
130         BOOST_REQUIRE (decoder);
131         reels = decoder->reels();
132
133         encrypted_content->add_kdm (kdm);
134         JobManager::instance()->add (make_shared<ExamineContentJob>(test, encrypted_content));
135         BOOST_REQUIRE (!wait_for_jobs());
136         decoder = std::dynamic_pointer_cast<DCPDecoder>(player->_pieces.front()->decoder);
137         BOOST_REQUIRE (decoder);
138         BOOST_REQUIRE (reels != decoder->reels());
139 }