Merge branch 'master' of ssh://main.carlh.net/home/carl/git/libdcp
[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 <boost/test/unit_test.hpp>
36 #include <cstdio>
37
38 using std::list;
39 using std::pair;
40 using std::string;
41 using std::vector;
42 using std::make_pair;
43 using boost::optional;
44
45 static list<pair<string, optional<boost::filesystem::path> > > stages;
46
47 static void
48 stage (string s, optional<boost::filesystem::path> p)
49 {
50         stages.push_back (make_pair (s, p));
51 }
52
53 static void
54 progress (float)
55 {
56
57 }
58
59 BOOST_AUTO_TEST_CASE (verify_test1)
60 {
61         boost::filesystem::remove_all ("build/test/verify_test1");
62         boost::filesystem::create_directory ("build/test/verify_test1");
63         for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
64                 boost::filesystem::copy_file (i->path(), "build/test/verify_test1" / i->path().filename());
65         }
66
67         vector<boost::filesystem::path> directories;
68         directories.push_back ("build/test/verify_test1");
69         list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
70
71         list<pair<string, optional<boost::filesystem::path> > >::const_iterator st = stages.begin();
72         BOOST_CHECK_EQUAL (st->first, "Checking DCP");
73         BOOST_REQUIRE (st->second);
74         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1"));
75         ++st;
76         BOOST_CHECK_EQUAL (st->first, "Checking CPL");
77         BOOST_REQUIRE (st->second);
78         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml"));
79         ++st;
80         BOOST_CHECK_EQUAL (st->first, "Checking reel");
81         BOOST_REQUIRE (!st->second);
82         ++st;
83         BOOST_CHECK_EQUAL (st->first, "Checking picture asset hash");
84         BOOST_REQUIRE (st->second);
85         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/video.mxf"));
86         ++st;
87         BOOST_CHECK_EQUAL (st->first, "Checking sound asset hash");
88         BOOST_REQUIRE (st->second);
89         BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/audio.mxf"));
90         ++st;
91         BOOST_REQUIRE (st == stages.end());
92
93         BOOST_CHECK_EQUAL (notes.size(), 0);
94
95         FILE* mod = fopen("build/test/verify_test1/video.mxf", "r+b");
96         BOOST_REQUIRE (mod);
97         fseek (mod, 4096, SEEK_SET);
98         int x = 42;
99         fwrite (&x, sizeof(x), 1, mod);
100         fclose (mod);
101
102         mod = fopen("build/test/verify_test1/audio.mxf", "r+b");
103         BOOST_REQUIRE (mod);
104         fseek (mod, 4096, SEEK_SET);
105         BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
106         fclose (mod);
107
108         notes = dcp::verify (directories, &stage, &progress);
109         BOOST_CHECK_EQUAL (notes.size(), 2);
110         BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
111         BOOST_CHECK_EQUAL (notes.front().note(), "Picture asset hash is incorrect.");
112         BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
113         BOOST_CHECK_EQUAL (notes.back().note(), "Sound asset hash is incorrect.");
114 }