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