Use PKL when verifying DCPs.
[libdcp.git] / test / verify_test.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34 #include "verify.h"
35 #include "util.h"
36 #include <boost/test/unit_test.hpp>
37 #include <cstdio>
38 #include <iostream>
39
40 using std::list;
41 using std::pair;
42 using std::string;
43 using std::vector;
44 using std::make_pair;
45 using boost::optional;
46
47 static list<pair<string, optional<boost::filesystem::path> > > stages;
48
49 static void
50 stage (string s, optional<boost::filesystem::path> p)
51 {
52         stages.push_back (make_pair (s, p));
53 }
54
55 static void
56 progress (float)
57 {
58
59 }
60
61 BOOST_AUTO_TEST_CASE (verify_test1)
62 {
63         boost::filesystem::remove_all ("build/test/verify_test1");
64         boost::filesystem::create_directory ("build/test/verify_test1");
65         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
66                 boost::filesystem::copy_file (i->path(), "build/test/verify_test1" / i->path().filename());
67         }
68
69         /* Check DCP as-is (should be OK) */
70
71         vector<boost::filesystem::path> directories;
72         directories.push_back ("build/test/verify_test1");
73         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
74
75         boost::filesystem::path const cpl_file = "build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
76
77         list<pair<string, optional<boost::filesystem::path> > >::const_iterator st = stages.begin();
78         BOOST_CHECK_EQUAL (st->first, "Checking DCP");
79         BOOST_REQUIRE (st->second);
80         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1"));
81         ++st;
82         BOOST_CHECK_EQUAL (st->first, "Checking CPL");
83         BOOST_REQUIRE (st->second);
84         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical(cpl_file));
85         ++st;
86         BOOST_CHECK_EQUAL (st->first, "Checking reel");
87         BOOST_REQUIRE (!st->second);
88         ++st;
89         BOOST_CHECK_EQUAL (st->first, "Checking picture asset hash");
90         BOOST_REQUIRE (st->second);
91         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/video.mxf"));
92         ++st;
93         BOOST_CHECK_EQUAL (st->first, "Checking sound asset hash");
94         BOOST_REQUIRE (st->second);
95         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/audio.mxf"));
96         ++st;
97         BOOST_REQUIRE (st == stages.end());
98
99         BOOST_CHECK_EQUAL (notes.size(), 0);
100
101         /* Corrupt the MXFs and check that this is spotted */
102
103         FILE* mod = fopen("build/test/verify_test1/video.mxf", "r+b");
104         BOOST_REQUIRE (mod);
105         fseek (mod, 4096, SEEK_SET);
106         int x = 42;
107         fwrite (&x, sizeof(x), 1, mod);
108         fclose (mod);
109
110         mod = fopen("build/test/verify_test1/audio.mxf", "r+b");
111         BOOST_REQUIRE (mod);
112         fseek (mod, 4096, SEEK_SET);
113         BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
114         fclose (mod);
115
116         notes = dcp::verify (directories, &stage, &progress);
117         BOOST_CHECK_EQUAL (notes.size(), 2);
118         BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
119         BOOST_CHECK_EQUAL (notes.front().note(), "Picture asset hash is incorrect.");
120         BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
121         BOOST_CHECK_EQUAL (notes.back().note(), "Sound asset hash is incorrect.");
122
123         /* Corrupt the hashes in the CPL and check that the disagreement between CPL and PKL is spotted */
124         string const cpl = dcp::file_to_string (cpl_file);
125         string hacked_cpl = "";
126         for (size_t i = 0; i < (cpl.length() - 6); ++i) {
127                 if (cpl.substr(i, 6) == "<Hash>") {
128                         hacked_cpl += "<Hash>x";
129                         i += 6;
130                 } else {
131                         hacked_cpl += cpl[i];
132                 }
133         }
134         hacked_cpl += "list>";
135
136         FILE* f = fopen(cpl_file.string().c_str(), "w");
137         fwrite(hacked_cpl.c_str(), hacked_cpl.length(), 1, f);
138         fclose(f);
139
140         notes = dcp::verify (directories, &stage, &progress);
141         BOOST_CHECK_EQUAL (notes.size(), 2);
142         BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
143         BOOST_CHECK_EQUAL (notes.front().note(), "PKL and CPL hashes differ for picture asset.");
144         BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
145         BOOST_CHECK_EQUAL (notes.back().note(), "PKL and CPL hashes differ for sound asset.");
146
147 }