Tidy up a bit.
[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 "compose.hpp"
37 #include <boost/test/unit_test.hpp>
38 #include <boost/algorithm/string.hpp>
39 #include <cstdio>
40 #include <iostream>
41
42 using std::list;
43 using std::pair;
44 using std::string;
45 using std::vector;
46 using std::make_pair;
47 using boost::optional;
48
49 static list<pair<string, optional<boost::filesystem::path> > > stages;
50
51 static void
52 stage (string s, optional<boost::filesystem::path> p)
53 {
54         stages.push_back (make_pair (s, p));
55 }
56
57 static void
58 progress (float)
59 {
60
61 }
62
63 static vector<boost::filesystem::path>
64 setup (int n)
65 {
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());
70         }
71
72         vector<boost::filesystem::path> directories;
73         directories.push_back (dcp::String::compose("build/test/verify_test%1", n));
74         return directories;
75
76 }
77
78 /* Check DCP as-is (should be OK) */
79 BOOST_AUTO_TEST_CASE (verify_test1)
80 {
81         vector<boost::filesystem::path> directories = setup (1);
82         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
83
84         boost::filesystem::path const cpl_file = "build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
85
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"));
90         ++st;
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));
94         ++st;
95         BOOST_CHECK_EQUAL (st->first, "Checking reel");
96         BOOST_REQUIRE (!st->second);
97         ++st;
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"));
101         ++st;
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"));
105         ++st;
106         BOOST_REQUIRE (st == stages.end());
107
108         BOOST_CHECK_EQUAL (notes.size(), 0);
109 }
110
111 /* Corrupt the MXFs and check that this is spotted */
112 BOOST_AUTO_TEST_CASE (verify_test2)
113 {
114         vector<boost::filesystem::path> directories = setup (2);
115
116         FILE* mod = fopen("build/test/verify_test2/video.mxf", "r+b");
117         BOOST_REQUIRE (mod);
118         fseek (mod, 4096, SEEK_SET);
119         int x = 42;
120         fwrite (&x, sizeof(x), 1, mod);
121         fclose (mod);
122
123         mod = fopen("build/test/verify_test2/audio.mxf", "r+b");
124         BOOST_REQUIRE (mod);
125         fseek (mod, 4096, SEEK_SET);
126         BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
127         fclose (mod);
128
129         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
130
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.");
136 }
137
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)
140 {
141         vector<boost::filesystem::path> directories = setup (3);
142
143         boost::filesystem::path const pkl_file = "build/test/verify_test3/pkl_74e205d0-d145-42d2-8c49-7b55d058ca55.xml";
144
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);
149         fclose(f);
150
151         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
152
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.");
157         ++i;
158         BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
159         BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for picture asset.");
160         ++i;
161         BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
162         BOOST_CHECK_EQUAL (i->note(), "PKL and CPL hashes differ for sound asset.");
163         ++i;
164 }
165
166 /* Corrupt the ContentKind in the CPL */
167 BOOST_AUTO_TEST_CASE (verify_test4)
168 {
169         vector<boost::filesystem::path> directories = setup (4);
170
171         boost::filesystem::path const cpl_file = "build/test/verify_test4/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
172
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);
177         fclose(f);
178
179         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
180
181         BOOST_CHECK_EQUAL (notes.size(), 1);
182         BOOST_CHECK_EQUAL (notes.front().note(), "Bad content kind 'xfeature'");
183 }