Use dcp::File in DCP-o-matic (#2231).
[dcpomatic.git] / test / cpl_hash_test.cc
1 /*
2     Copyright (C) 2020-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/cpl_hash_test.cc
23  *  @brief Make sure that <Hash> tags are always written to CPLs where required.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/content_factory.h"
29 #include "lib/cross.h"
30 #include "lib/dcp_content.h"
31 #include "lib/film.h"
32 #include "test.h"
33 #include <boost/algorithm/string.hpp>
34 #include <boost/test/unit_test.hpp>
35
36
37 using std::make_shared;
38 using std::shared_ptr;
39 using std::string;
40
41
42 BOOST_AUTO_TEST_CASE (hash_added_to_imported_dcp_test)
43 {
44         using namespace boost::filesystem;
45
46         string const ov_name = "hash_added_to_imported_dcp_test_ov";
47         shared_ptr<Film> ov = new_test_film2 (
48                 ov_name,
49                 { content_factory("test/data/flat_red.png").front() }
50                 );
51         make_and_verify_dcp (ov);
52
53         /* Remove <Hash> tags from the CPL */
54         for (auto i: directory_iterator(String::compose("build/test/%1/%2", ov_name, ov->dcp_name()))) {
55                 if (boost::algorithm::starts_with(i.path().filename().string(), "cpl_")) {
56                         dcp::File in(i.path(), "r");
57                         BOOST_REQUIRE (in);
58                         dcp::File out(i.path().string() + ".tmp", "w");
59                         BOOST_REQUIRE (out);
60                         char buffer[256];
61                         while (in.gets(buffer, sizeof(buffer))) {
62                                 if (string(buffer).find("Hash") == string::npos) {
63                                         out.puts(buffer);
64                                 }
65                         }
66                         in.close();
67                         out.close();
68                         rename (i.path().string() + ".tmp", i.path());
69                 }
70         }
71
72         string const vf_name = "hash_added_to_imported_dcp_test_vf";
73         auto ov_content = make_shared<DCPContent>(String::compose("build/test/%1/%2", ov_name, ov->dcp_name()));
74         auto vf = new_test_film2 (
75                 vf_name, { ov_content }
76                 );
77
78         ov_content->set_reference_video (true);
79         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
80
81         /* Check for Hash tags in the VF DCP */
82         int hashes = 0;
83         for (auto i: directory_iterator(String::compose("build/test/%1/%2", vf_name, vf->dcp_name()))) {
84                 if (boost::algorithm::starts_with(i.path().filename().string(), "cpl_")) {
85                         dcp::File in(i.path(), "r");
86                         BOOST_REQUIRE (in);
87                         char buffer[256];
88                         while (in.gets(buffer, sizeof(buffer))) {
89                                 if (string(buffer).find("Hash") != string::npos) {
90                                         ++hashes;
91                                 }
92                         }
93                 }
94         }
95         BOOST_CHECK_EQUAL (hashes, 2);
96 }
97