2 Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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.
11 libdcp 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.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
36 #include "compose.hpp"
37 #include <boost/test/unit_test.hpp>
38 #include <boost/algorithm/string.hpp>
47 using boost::optional;
49 static list<pair<string, optional<boost::filesystem::path> > > stages;
52 stage (string s, optional<boost::filesystem::path> p)
54 stages.push_back (make_pair (s, p));
63 static vector<boost::filesystem::path>
66 boost::filesystem::remove_all (dcp::String::compose("build/test/verify_test%1", n));
67 boost::filesystem::create_directory (dcp::String::compose("build/test/verify_test%1", n));
68 for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
69 boost::filesystem::copy_file (i->path(), dcp::String::compose("build/test/verify_test%1", n) / i->path().filename());
72 vector<boost::filesystem::path> directories;
73 directories.push_back (dcp::String::compose("build/test/verify_test%1", n));
78 /* Check DCP as-is (should be OK) */
79 BOOST_AUTO_TEST_CASE (verify_test1)
81 vector<boost::filesystem::path> directories = setup (1);
82 list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
84 boost::filesystem::path const cpl_file = "build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
86 list<pair<string, optional<boost::filesystem::path> > >::const_iterator st = stages.begin();
87 BOOST_CHECK_EQUAL (st->first, "Checking DCP");
88 BOOST_REQUIRE (st->second);
89 BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1"));
91 BOOST_CHECK_EQUAL (st->first, "Checking CPL");
92 BOOST_REQUIRE (st->second);
93 BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical(cpl_file));
95 BOOST_CHECK_EQUAL (st->first, "Checking reel");
96 BOOST_REQUIRE (!st->second);
98 BOOST_CHECK_EQUAL (st->first, "Checking picture asset hash");
99 BOOST_REQUIRE (st->second);
100 BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/video.mxf"));
102 BOOST_CHECK_EQUAL (st->first, "Checking sound asset hash");
103 BOOST_REQUIRE (st->second);
104 BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/audio.mxf"));
106 BOOST_REQUIRE (st == stages.end());
108 BOOST_CHECK_EQUAL (notes.size(), 0);
111 /* Corrupt the MXFs and check that this is spotted */
112 BOOST_AUTO_TEST_CASE (verify_test2)
114 vector<boost::filesystem::path> directories = setup (2);
116 FILE* mod = fopen("build/test/verify_test2/video.mxf", "r+b");
118 fseek (mod, 4096, SEEK_SET);
120 fwrite (&x, sizeof(x), 1, mod);
123 mod = fopen("build/test/verify_test2/audio.mxf", "r+b");
125 fseek (mod, 4096, SEEK_SET);
126 BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
129 list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
131 BOOST_CHECK_EQUAL (notes.size(), 2);
132 BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
133 BOOST_CHECK_EQUAL (notes.front().note(), "Picture asset hash is incorrect.");
134 BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
135 BOOST_CHECK_EQUAL (notes.back().note(), "Sound asset hash is incorrect.");
138 /* Corrupt the hashes in the PKL and check that the disagreement between CPL and PKL is spotted */
139 BOOST_AUTO_TEST_CASE (verify_test3)
141 vector<boost::filesystem::path> directories = setup (3);
143 boost::filesystem::path const pkl_file = "build/test/verify_test3/pkl_74e205d0-d145-42d2-8c49-7b55d058ca55.xml";
145 string pkl = dcp::file_to_string (pkl_file);
146 boost::algorithm::replace_all (pkl, "<Hash>", "<Hash>x");
147 FILE* f = fopen(pkl_file.string().c_str(), "w");
148 fwrite(pkl.c_str(), pkl.length(), 1, f);
151 list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
153 BOOST_REQUIRE_EQUAL (notes.size(), 3);
154 list<dcp::VerificationNote>::const_iterator i = notes.begin();
155 BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
156 BOOST_CHECK_EQUAL (i->note(), "CPL hash is incorrect.");
158 BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
159 BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for picture asset.");
161 BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
162 BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for sound asset.");
166 /* Corrupt the ContentKind in the CPL */
167 BOOST_AUTO_TEST_CASE (verify_test4)
169 vector<boost::filesystem::path> directories = setup (4);
171 boost::filesystem::path const cpl_file = "build/test/verify_test4/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
173 string cpl = dcp::file_to_string (cpl_file);
174 boost::algorithm::replace_all (cpl, "<ContentKind>", "<ContentKind>x");
175 FILE* f = fopen(cpl_file.string().c_str(), "w");
176 fwrite(cpl.c_str(), cpl.length(), 1, f);
179 list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
181 BOOST_CHECK_EQUAL (notes.size(), 1);
182 BOOST_CHECK_EQUAL (notes.front().note(), "Bad content kind 'xfeature'");