Add test of reel digests.
[dcpomatic.git] / test / digest_test.cc
1 /*
2     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/film.h"
22 #include "lib/image_content.h"
23 #include "lib/dcp_content_type.h"
24 #include "lib/compose.hpp"
25 #include "test.h"
26 #include <dcp/cpl.h>
27 #include <dcp/reel.h>
28 #include <dcp/reel_picture_asset.h>
29 #include <boost/test/unit_test.hpp>
30
31 using std::list;
32 using std::string;
33 using boost::shared_ptr;
34
35 static string
36 openssl_hash (boost::filesystem::path file)
37 {
38         FILE* pipe = popen (String::compose ("openssl sha1 -binary %1 | openssl base64 -e", file.string()).c_str (), "r");
39         BOOST_REQUIRE (pipe);
40         char buffer[128];
41         string output;
42         while (!feof (pipe)) {
43                 if (fgets (buffer, sizeof(buffer), pipe)) {
44                         output += buffer;
45                 }
46         }
47         pclose (pipe);
48         if (!output.empty ()) {
49                 output = output.substr (0, output.length() - 1);
50         }
51         return output;
52 }
53
54 /** Test the digests made by the DCP writing code on a multi-reel DCP */
55 BOOST_AUTO_TEST_CASE (digest_test)
56 {
57         shared_ptr<Film> film = new_test_film ("digest_test");
58         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
59         film->set_name ("digest_test");
60         shared_ptr<ImageContent> r (new ImageContent (film, "test/data/flat_red.png"));
61         shared_ptr<ImageContent> g (new ImageContent (film, "test/data/flat_green.png"));
62         shared_ptr<ImageContent> b (new ImageContent (film, "test/data/flat_blue.png"));
63         film->examine_and_add_content (r);
64         film->examine_and_add_content (g);
65         film->examine_and_add_content (b);
66         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
67         wait_for_jobs ();
68
69         film->make_dcp ();
70         wait_for_jobs ();
71
72         dcp::DCP dcp (film->dir (film->dcp_name ()));
73         dcp.read ();
74         BOOST_CHECK_EQUAL (dcp.cpls().size(), 1);
75         list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
76
77         list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin ();
78         BOOST_REQUIRE (i != reels.end ());
79         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file ()));
80         ++i;
81         BOOST_REQUIRE (i != reels.end ());
82         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file ()));
83         ++i;
84         BOOST_REQUIRE (i != reels.end ());
85         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file ()));
86         ++i;
87         BOOST_REQUIRE (i == reels.end ());
88 }