Tidy up and slightly extend verify tests.
[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 <boost/algorithm/string.hpp>
38 #include <cstdio>
39 #include <iostream>
40
41 using std::list;
42 using std::pair;
43 using std::string;
44 using std::vector;
45 using std::make_pair;
46 using boost::optional;
47
48 static list<pair<string, optional<boost::filesystem::path> > > stages;
49
50 static void
51 stage (string s, optional<boost::filesystem::path> p)
52 {
53         stages.push_back (make_pair (s, p));
54 }
55
56 static void
57 progress (float)
58 {
59
60 }
61
62 /* Check DCP as-is (should be OK) */
63 BOOST_AUTO_TEST_CASE (verify_test1)
64 {
65         boost::filesystem::remove_all ("build/test/verify_test1");
66         boost::filesystem::create_directory ("build/test/verify_test1");
67         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
68                 boost::filesystem::copy_file (i->path(), "build/test/verify_test1" / i->path().filename());
69         }
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
102 /* Corrupt the MXFs and check that this is spotted */
103 BOOST_AUTO_TEST_CASE (verify_test2)
104 {
105         boost::filesystem::remove_all ("build/test/verify_test2");
106         boost::filesystem::create_directory ("build/test/verify_test2");
107         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
108                 boost::filesystem::copy_file (i->path(), "build/test/verify_test2" / i->path().filename());
109         }
110
111         FILE* mod = fopen("build/test/verify_test2/video.mxf", "r+b");
112         BOOST_REQUIRE (mod);
113         fseek (mod, 4096, SEEK_SET);
114         int x = 42;
115         fwrite (&x, sizeof(x), 1, mod);
116         fclose (mod);
117
118         mod = fopen("build/test/verify_test2/audio.mxf", "r+b");
119         BOOST_REQUIRE (mod);
120         fseek (mod, 4096, SEEK_SET);
121         BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
122         fclose (mod);
123
124         vector<boost::filesystem::path> directories;
125         directories.push_back ("build/test/verify_test2");
126         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
127         BOOST_CHECK_EQUAL (notes.size(), 2);
128         BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
129         BOOST_CHECK_EQUAL (notes.front().note(), "Picture asset hash is incorrect.");
130         BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
131         BOOST_CHECK_EQUAL (notes.back().note(), "Sound asset hash is incorrect.");
132 }
133
134 /* Corrupt the hashes in the PKL and check that the disagreement between CPL and PKL is spotted */
135 BOOST_AUTO_TEST_CASE (verify_test3)
136 {
137         boost::filesystem::remove_all ("build/test/verify_test3");
138         boost::filesystem::create_directory ("build/test/verify_test3");
139         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
140                 boost::filesystem::copy_file (i->path(), "build/test/verify_test3" / i->path().filename());
141         }
142
143         boost::filesystem::path const pkl_file = "build/test/verify_test3/pkl_74e205d0-d145-42d2-8c49-7b55d058ca55.xml";
144         boost::filesystem::path const cpl_file = "build/test/verify_test3/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
145
146         string pkl = dcp::file_to_string (pkl_file);
147         boost::algorithm::replace_all (pkl, "<Hash>", "<Hash>x");
148         FILE* f = fopen(pkl_file.string().c_str(), "w");
149         fwrite(pkl.c_str(), pkl.length(), 1, f);
150         fclose(f);
151
152         vector<boost::filesystem::path> directories;
153         directories.push_back ("build/test/verify_test3");
154         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
155         BOOST_CHECK_EQUAL (notes.size(), 3);
156         list<dcp::VerificationNote>::const_iterator i = notes.begin();
157         BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
158         BOOST_CHECK_EQUAL (i->note(), "CPL hash is incorrect.");
159         ++i;
160         BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
161         BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for picture asset.");
162         ++i;
163         BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
164         BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for sound asset.");
165         ++i;
166 }
167
168 /* Corrupt the ContentKind in the CPL */
169 BOOST_AUTO_TEST_CASE (verify_test4)
170 {
171         boost::filesystem::remove_all ("build/test/verify_test4");
172         boost::filesystem::create_directory ("build/test/verify_test4");
173         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
174                 boost::filesystem::copy_file (i->path(), "build/test/verify_test4" / i->path().filename());
175         }
176
177         boost::filesystem::path const cpl_file = "build/test/verify_test4/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
178
179         string cpl = dcp::file_to_string (cpl_file);
180         boost::algorithm::replace_all (cpl, "<ContentKind>", "<ContentKind>x");
181         FILE* f = fopen(cpl_file.string().c_str(), "w");
182         fwrite(cpl.c_str(), cpl.length(), 1, f);
183         fclose(f);
184
185         vector<boost::filesystem::path> directories;
186         directories.push_back ("build/test/verify_test4");
187         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
188         BOOST_CHECK_EQUAL (notes.size(), 1);
189         BOOST_CHECK_EQUAL (notes.front().note(), "Bad content kind 'xfeature'");
190 }