summaryrefslogtreecommitdiff
path: root/test/digest_test.cc
blob: 39737b7f565101c387b95b87d0fc7b1f4035dcb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
    Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


/** @file  test/digest_test.cc
 *  @brief Check computed DCP digests against references calculated by the `openssl` binary.
 *  @ingroup feature
 */


#include "lib/compose.hpp"
#include "lib/config.h"
#include "lib/dcp_content_type.h"
#include "lib/film.h"
#include "lib/image_content.h"
#include "test.h"
#include <dcp/cpl.h>
#include <dcp/reel.h>
#include <dcp/reel_picture_asset.h>
#include <boost/test/unit_test.hpp>


using std::list;
using std::make_shared;
using std::string;


static string
openssl_hash (boost::filesystem::path file)
{
	auto pipe = popen (String::compose ("openssl sha1 -binary %1 | openssl base64 -e", file.string()).c_str (), "r");
	BOOST_REQUIRE (pipe);
	char buffer[128];
	string output;
	while (!feof (pipe)) {
		if (fgets (buffer, sizeof(buffer), pipe)) {
			output += buffer;
		}
	}
	pclose (pipe);
	if (!output.empty ()) {
		output = output.substr (0, output.length() - 1);
	}
	return output;
}


/** Test the digests made by the DCP writing code on a multi-reel DCP */
BOOST_AUTO_TEST_CASE (digest_test)
{
	auto r = make_shared<ImageContent>("test/data/flat_red.png");
	auto g = make_shared<ImageContent>("test/data/flat_green.png");
	auto b = make_shared<ImageContent>("test/data/flat_blue.png");
	auto film = new_test_film("digest_test", { r, g, b });
	film->set_reel_type (ReelType::BY_VIDEO_CONTENT);

	BOOST_CHECK (Config::instance()->master_encoding_threads() > 1);
	make_and_verify_dcp (film);

	dcp::DCP dcp (film->dir (film->dcp_name ()));
	dcp.read ();
	BOOST_CHECK_EQUAL (dcp.cpls().size(), 1U);
	auto reels = dcp.cpls()[0]->reels();

	auto i = reels.begin ();
	BOOST_REQUIRE (i != reels.end ());
	BOOST_REQUIRE ((*i)->main_picture()->hash());
	BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
	BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
	++i;
	BOOST_REQUIRE (i != reels.end ());
	BOOST_REQUIRE ((*i)->main_picture()->hash());
	BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
	BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
	++i;
	BOOST_REQUIRE (i != reels.end ());
	BOOST_REQUIRE ((*i)->main_picture()->hash());
	BOOST_REQUIRE ((*i)->main_picture()->asset()->file());
	BOOST_CHECK_EQUAL ((*i)->main_picture()->hash().get(), openssl_hash ((*i)->main_picture()->asset()->file().get()));
	++i;
	BOOST_REQUIRE (i == reels.end ());
}