Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / dcp_digest_file_test.cc
1 /*
2     Copyright (C) 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/config.h"
23 #include "lib/content_factory.h"
24 #include "lib/dcp_content.h"
25 #include "lib/dcp_digest_file.h"
26 #include "lib/film.h"
27 #include "test.h"
28 #include <dcp/cpl.h>
29 #include <dcp/dcp.h>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/test/unit_test.hpp>
32
33
34 using std::ifstream;
35 using std::make_shared;
36 using std::string;
37 using boost::optional;
38
39
40 BOOST_AUTO_TEST_CASE (dcp_digest_file_test)
41 {
42         dcp::DCP dcp("test/data/dcp_digest_test_dcp");
43         dcp.read ();
44         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
45
46         write_dcp_digest_file ("build/test/digest.xml", dcp.cpls()[0], "e684e49e89182e907dabe5d9b3bd81ba");
47
48         check_xml ("test/data/digest.xml", "build/test/digest.xml", {});
49 }
50
51
52 BOOST_AUTO_TEST_CASE (dcp_digest_file_test2)
53 {
54         auto get_key_from_digest = [](boost::filesystem::path filename) -> optional<string> {
55                 ifstream digest(filename.string().c_str());
56                 while (digest.good()) {
57                         string line;
58                         getline (digest, line);
59                         boost::algorithm::trim (line);
60                         if (boost::starts_with(line, "<Key>") && line.length() > 37) {
61                                 return line.substr(5, 32);
62                         }
63                 }
64
65                 return {};
66         };
67
68         auto red = content_factory("test/data/flat_red.png").front();
69         auto ov = new_test_film2 ("dcp_digest_file_test2_ov", { red });
70         ov->set_encrypted (true);
71         make_and_verify_dcp (ov);
72
73         auto ov_key_check = get_key_from_digest ("build/test/dcp_digest_file_test2_ov/" + ov->dcp_name() + ".dcpdig");
74         BOOST_REQUIRE (static_cast<bool>(ov_key_check));
75         BOOST_CHECK_EQUAL (*ov_key_check, ov->key().hex());
76
77         dcp::DCP find_cpl (ov->dir(ov->dcp_name()));
78         find_cpl.read ();
79         BOOST_REQUIRE (!find_cpl.cpls().empty());
80         auto ov_cpl = find_cpl.cpls()[0]->file();
81         BOOST_REQUIRE (static_cast<bool>(ov_cpl));
82
83         auto kdm = ov->make_kdm (
84                 Config::instance()->decryption_chain()->leaf(),
85                 {},
86                 ov_cpl.get(),
87                 dcp::LocalTime(), dcp::LocalTime(),
88                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
89                 true,
90                 0
91                 );
92
93         auto ov_dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
94         ov_dcp->add_kdm (kdm);
95         ov_dcp->set_reference_video (true);
96         ov_dcp->set_reference_audio (true);
97         auto vf = new_test_film2 ("dcp_digest_file_test2_vf", { ov_dcp });
98         vf->set_encrypted (true);
99         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
100
101         auto vf_key_check = get_key_from_digest ("build/test/dcp_digest_file_test2_vf/" + vf->dcp_name() + ".dcpdig");
102         BOOST_REQUIRE (static_cast<bool>(vf_key_check));
103         BOOST_CHECK_EQUAL (*vf_key_check, ov->key().hex());
104 }
105