Take Film pointer out of Content.
[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 /** @file  test/digest_test.cc
22  *  @brief Check computed DCP digests against references calculated by the `openssl` binary.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/image_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
31 #include "test.h"
32 #include <dcp/cpl.h>
33 #include <dcp/reel.h>
34 #include <dcp/reel_picture_asset.h>
35 #include <boost/test/unit_test.hpp>
36
37 using std::list;
38 using std::string;
39 using boost::shared_ptr;
40
41 static string
42 openssl_hash (boost::filesystem::path file)
43 {
44         FILE* pipe = popen (String::compose ("openssl sha1 -binary %1 | openssl base64 -e", file.string()).c_str (), "r");
45         BOOST_REQUIRE (pipe);
46         char buffer[128];
47         string output;
48         while (!feof (pipe)) {
49                 if (fgets (buffer, sizeof(buffer), pipe)) {
50                         output += buffer;
51                 }
52         }
53         pclose (pipe);
54         if (!output.empty ()) {
55                 output = output.substr (0, output.length() - 1);
56         }
57         return output;
58 }
59
60 /** Test the digests made by the DCP writing code on a multi-reel DCP */
61 BOOST_AUTO_TEST_CASE (digest_test)
62 {
63         shared_ptr<Film> film = new_test_film ("digest_test");
64         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
65         film->set_name ("digest_test");
66         shared_ptr<ImageContent> r (new ImageContent("test/data/flat_red.png"));
67         shared_ptr<ImageContent> g (new ImageContent("test/data/flat_green.png"));
68         shared_ptr<ImageContent> b (new ImageContent("test/data/flat_blue.png"));
69         film->examine_and_add_content (r);
70         film->examine_and_add_content (g);
71         film->examine_and_add_content (b);
72         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
73         wait_for_jobs ();
74
75         Config::instance()->set_master_encoding_threads (4);
76         film->make_dcp ();
77         wait_for_jobs ();
78         Config::instance()->set_master_encoding_threads (1);
79
80         dcp::DCP dcp (film->dir (film->dcp_name ()));
81         dcp.read ();
82         BOOST_CHECK_EQUAL (dcp.cpls().size(), 1);
83         list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
84
85         list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin ();
86         BOOST_REQUIRE (i != reels.end ());
87         BOOST_REQUIRE ((*i)->main_picture()->hash());
88         BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
89         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
90         ++i;
91         BOOST_REQUIRE (i != reels.end ());
92         BOOST_REQUIRE ((*i)->main_picture()->hash());
93         BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
94         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
95         ++i;
96         BOOST_REQUIRE (i != reels.end ());
97         BOOST_REQUIRE ((*i)->main_picture()->hash());
98         BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
99         BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
100         ++i;
101         BOOST_REQUIRE (i == reels.end ());
102 }