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