Fix parameters when running tests with valgrind.
[libdcp.git] / test / test.cc
1 /*
2     Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #define BOOST_TEST_DYN_LINK
35 #define BOOST_TEST_MODULE libdcp_test
36 #include "util.h"
37 #include "test.h"
38 #include <asdcp/KM_util.h>
39 #include <asdcp/KM_prng.h>
40 #include <libxml++/libxml++.h>
41 #include <boost/test/unit_test.hpp>
42 #include <cstdio>
43 #include <iostream>
44
45 using std::string;
46 using std::min;
47 using std::list;
48
49 boost::filesystem::path private_test;
50 boost::filesystem::path xsd_test = "build/test/xsd with spaces";
51
52 struct TestConfig
53 {
54         TestConfig()
55         {
56                 dcp::init ();
57                 if (boost::unit_test::framework::master_test_suite().argc >= 2) {
58                         private_test = boost::unit_test::framework::master_test_suite().argv[1];
59                 }
60
61                 using namespace boost::filesystem;
62                 boost::system::error_code ec;
63                 remove_all (xsd_test, ec);
64                 boost::filesystem::create_directory (xsd_test);
65                 for (directory_iterator i = directory_iterator("xsd"); i != directory_iterator(); ++i) {
66                         copy_file (*i, xsd_test / i->path().filename());
67                 }
68         }
69 };
70
71 void
72 check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
73 {
74         BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
75         BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
76
77         if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
78                 return;
79         }
80
81         xmlpp::Element::NodeList ref_children = ref->get_children ();
82         xmlpp::Element::NodeList test_children = test->get_children ();
83         BOOST_REQUIRE_MESSAGE (
84                 ref_children.size () == test_children.size (),
85                 "child counts of " << ref->get_name() << " differ; ref has " << ref_children.size() << ", test has " << test_children.size()
86                 );
87
88         xmlpp::Element::NodeList::iterator k = ref_children.begin ();
89         xmlpp::Element::NodeList::iterator l = test_children.begin ();
90         while (k != ref_children.end ()) {
91
92                 /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
93
94                 xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
95                 xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
96                 BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
97                 if (ref_el && test_el) {
98                         check_xml (ref_el, test_el, ignore);
99                 }
100
101                 xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
102                 xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
103                 BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
104                 if (ref_cn && test_cn) {
105                         BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
106                 }
107
108                 ++k;
109                 ++l;
110         }
111
112         xmlpp::Element::AttributeList ref_attributes = ref->get_attributes ();
113         xmlpp::Element::AttributeList test_attributes = test->get_attributes ();
114         BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ());
115
116         xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin();
117         xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin();
118         while (m != ref_attributes.end ()) {
119                 BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name());
120                 BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value());
121
122                 ++m;
123                 ++n;
124         }
125 }
126
127 void
128 check_xml (string ref, string test, list<string> ignore)
129 {
130         xmlpp::DomParser* ref_parser = new xmlpp::DomParser ();
131         ref_parser->parse_memory (ref);
132         xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node ();
133         xmlpp::DomParser* test_parser = new xmlpp::DomParser ();
134         test_parser->parse_memory (test);
135         xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
136
137         check_xml (ref_root, test_root, ignore);
138 }
139
140 void
141 check_file (boost::filesystem::path ref, boost::filesystem::path check)
142 {
143         uintmax_t N = boost::filesystem::file_size (ref);
144         BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check));
145         FILE* ref_file = dcp::fopen_boost (ref, "rb");
146         BOOST_CHECK (ref_file);
147         FILE* check_file = dcp::fopen_boost (check, "rb");
148         BOOST_CHECK (check_file);
149
150         int const buffer_size = 65536;
151         uint8_t* ref_buffer = new uint8_t[buffer_size];
152         uint8_t* check_buffer = new uint8_t[buffer_size];
153
154         string error;
155         error = "File " + check.string() + " differs from reference " + ref.string();
156
157         while (N) {
158                 uintmax_t this_time = min (uintmax_t (buffer_size), N);
159                 size_t r = fread (ref_buffer, 1, this_time, ref_file);
160                 BOOST_CHECK_EQUAL (r, this_time);
161                 r = fread (check_buffer, 1, this_time, check_file);
162                 BOOST_CHECK_EQUAL (r, this_time);
163
164                 BOOST_CHECK_MESSAGE (memcmp (ref_buffer, check_buffer, this_time) == 0, error);
165                 if (memcmp (ref_buffer, check_buffer, this_time)) {
166                         break;
167                 }
168
169                 N -= this_time;
170         }
171
172         delete[] ref_buffer;
173         delete[] check_buffer;
174
175         fclose (ref_file);
176         fclose (check_file);
177 }
178
179
180 RNGFixer::RNGFixer ()
181 {
182         Kumu::cth_test = true;
183         Kumu::FortunaRNG().Reset();
184 }
185
186
187 RNGFixer::~RNGFixer ()
188 {
189         Kumu::cth_test = false;
190 }
191
192
193 BOOST_GLOBAL_FIXTURE (TestConfig);