Add fix_empty_font_ids() to replace empty Font ids with a dummy string.
[libdcp.git] / test / util_test.cc
1 /*
2     Copyright (C) 2013-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 #include "util.h"
35 #include "local_time.h"
36 #include <boost/test/unit_test.hpp>
37 #include <fstream>
38
39 using std::ifstream;
40 using std::string;
41 using std::list;
42
43 /** Test dcp::base64_decode */
44 BOOST_AUTO_TEST_CASE (base64_decode_test)
45 {
46         int const N = 256;
47
48         ifstream f ("test/data/base64_test");
49         BOOST_CHECK (f.good ());
50         string s;
51         while (f.good ()) {
52                 string l;
53                 getline (f, l);
54                 s += l;
55         }
56
57         ifstream g ("test/ref/base64_test_decoded", std::ios::binary);
58         BOOST_CHECK (g.good ());
59         unsigned char ref_decoded[N];
60         for (int i = 0; i < N; ++i) {
61                 char c;
62                 g.get (c);
63                 ref_decoded[i] = static_cast<unsigned char> (c);
64         }
65
66         unsigned char decoded[N];
67         int const r = dcp::base64_decode (s, decoded, N);
68         BOOST_CHECK_EQUAL (r, N);
69
70         for (int i = 0; i < N; ++i) {
71                 BOOST_CHECK_EQUAL (decoded[i], ref_decoded[i]);
72         }
73 }
74
75 /** Test dcp::content_kind_from_string */
76 BOOST_AUTO_TEST_CASE (content_kind_test)
77 {
78         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("feature"), dcp::FEATURE);
79         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Feature"), dcp::FEATURE);
80         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("FeaturE"), dcp::FEATURE);
81         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Short"), dcp::SHORT);
82         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("trailer"), dcp::TRAILER);
83         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("test"), dcp::TEST);
84         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("transitional"), dcp::TRANSITIONAL);
85         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("rating"), dcp::RATING);
86         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("teaser"), dcp::TEASER);
87         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("policy"), dcp::POLICY);
88         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("psa"), dcp::PUBLIC_SERVICE_ANNOUNCEMENT);
89         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("advertisement"), dcp::ADVERTISEMENT);
90 }
91
92 /** Test dcp::relative_to_root */
93 BOOST_AUTO_TEST_CASE (relative_to_root_test)
94 {
95         {
96                 boost::filesystem::path root = "a";
97                 root /= "b";
98
99                 boost::filesystem::path file = "a";
100                 file /= "b";
101                 file /= "c";
102
103                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
104                 BOOST_CHECK (rel);
105                 BOOST_CHECK_EQUAL (rel.get(), boost::filesystem::path ("c"));
106         }
107
108         {
109                 boost::filesystem::path root = "a";
110                 root /= "b";
111                 root /= "c";
112
113                 boost::filesystem::path file = "a";
114                 file /= "b";
115
116                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
117                 BOOST_CHECK (!rel);
118         }
119
120         {
121                 boost::filesystem::path root = "a";
122
123                 boost::filesystem::path file = "a";
124                 file /= "b";
125                 file /= "c";
126
127                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
128                 BOOST_CHECK (rel);
129
130                 boost::filesystem::path check = "b";
131                 check /= "c";
132                 BOOST_CHECK_EQUAL (rel.get(), check);
133         }
134 }
135
136 /** Test private_key_fingerprint() */
137 BOOST_AUTO_TEST_CASE (private_key_fingerprint_test)
138 {
139         BOOST_CHECK_EQUAL (dcp::private_key_fingerprint (dcp::file_to_string ("test/data/private.key")), "Jdz1bFpCcKI7R16Ccx9JHYytag0=");
140 }
141
142 BOOST_AUTO_TEST_CASE (day_less_than_or_equal_test)
143 {
144         {
145                 /* equal */
146                 struct tm a;
147                 a.tm_mday = 5;
148                 a.tm_mon = 3;
149                 a.tm_year = 78;
150
151                 dcp::LocalTime b ("1978-04-05T00:00:00");
152                 BOOST_CHECK (day_less_than_or_equal(a, b));
153         }
154
155         {
156                 /* every part of a less than b */
157                 struct tm a;
158                 a.tm_mday = 4;
159                 a.tm_mon = 2;
160                 a.tm_year = 81;
161
162                 dcp::LocalTime b ("1985-05-23T00:00:00");
163                 BOOST_CHECK (day_less_than_or_equal(a, b));
164         }
165
166         {
167                 /* years equal, other parts less */
168                 struct tm a;
169                 a.tm_mday = 4;
170                 a.tm_mon = 2;
171                 a.tm_year = 81;
172
173                 dcp::LocalTime b ("1981-05-10T00:00:00");
174                 BOOST_CHECK (day_less_than_or_equal(a, b));
175         }
176
177         {
178                 /* year and month equal, day less */
179                 struct tm a;
180                 a.tm_mday = 4;
181                 a.tm_mon = 2;
182                 a.tm_year = 81;
183
184                 dcp::LocalTime b ("1981-03-12T00:00:00");
185                 BOOST_CHECK (day_less_than_or_equal(a, b));
186         }
187
188         {
189                 /* year and month equal, day less */
190                 struct tm a;
191                 a.tm_mday = 1;
192                 a.tm_mon = 2;
193                 a.tm_year = 81;
194
195                 dcp::LocalTime b ("1981-03-04T00:00:00");
196                 BOOST_CHECK (day_less_than_or_equal(a, b));
197         }
198
199         {
200                 /* a one day later than b */
201                 struct tm a;
202                 a.tm_mday = 5;
203                 a.tm_mon = 2;
204                 a.tm_year = 81;
205
206                 dcp::LocalTime b ("1981-03-04T00:00:00");
207                 BOOST_CHECK (!day_less_than_or_equal(a, b));
208         }
209
210         {
211                 /* year and month same, day much later */
212                 struct tm a;
213                 a.tm_mday = 22;
214                 a.tm_mon = 2;
215                 a.tm_year = 81;
216
217                 dcp::LocalTime b ("1981-03-04T00:00:00");
218                 BOOST_CHECK (!day_less_than_or_equal(a, b));
219         }
220
221         {
222                 /* year same, month and day later */
223                 struct tm a;
224                 a.tm_mday = 22;
225                 a.tm_mon = 5;
226                 a.tm_year = 81;
227
228                 dcp::LocalTime b ("1981-02-04T00:00:00");
229                 BOOST_CHECK (!day_less_than_or_equal(a, b));
230         }
231
232         {
233                 /* all later */
234                 struct tm a;
235                 a.tm_mday = 22;
236                 a.tm_mon = 5;
237                 a.tm_year = 99;
238
239                 dcp::LocalTime b ("1981-02-04T00:00:00");
240                 BOOST_CHECK (!day_less_than_or_equal(a, b));
241         }
242 }
243
244 BOOST_AUTO_TEST_CASE (day_greater_than_or_equal_test)
245 {
246         {
247                 /* equal */
248                 struct tm a;
249                 a.tm_mday = 5;
250                 a.tm_mon = 3;
251                 a.tm_year = 78;
252
253                 dcp::LocalTime b ("1978-04-05T00:00:00");
254                 BOOST_CHECK (day_greater_than_or_equal(a, b));
255         }
256
257         {
258                 /* every part of a less than b */
259                 struct tm a;
260                 a.tm_mday = 4;
261                 a.tm_mon = 2;
262                 a.tm_year = 81;
263
264                 dcp::LocalTime b ("1985-05-23T00:00:00");
265                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
266         }
267
268         {
269                 /* years equal, other parts less */
270                 struct tm a;
271                 a.tm_mday = 4;
272                 a.tm_mon = 2;
273                 a.tm_year = 81;
274
275                 dcp::LocalTime b ("1981-05-10T00:00:00");
276                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
277         }
278
279         {
280                 /* year and month equal, day less */
281                 struct tm a;
282                 a.tm_mday = 4;
283                 a.tm_mon = 2;
284                 a.tm_year = 81;
285
286                 dcp::LocalTime b ("1981-03-12T00:00:00");
287                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
288         }
289
290         {
291                 /* year and month equal, day less */
292                 struct tm a;
293                 a.tm_mday = 1;
294                 a.tm_mon = 2;
295                 a.tm_year = 81;
296
297                 dcp::LocalTime b ("1981-03-04T00:00:00");
298                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
299         }
300
301         {
302                 /* a one day later than b */
303                 struct tm a;
304                 a.tm_mday = 5;
305                 a.tm_mon = 2;
306                 a.tm_year = 81;
307
308                 dcp::LocalTime b ("1981-03-04T00:00:00");
309                 BOOST_CHECK (day_greater_than_or_equal(a, b));
310         }
311
312         {
313                 /* year and month same, day much later */
314                 struct tm a;
315                 a.tm_mday = 22;
316                 a.tm_mon = 2;
317                 a.tm_year = 81;
318
319                 dcp::LocalTime b ("1981-03-04T00:00:00");
320                 BOOST_CHECK (day_greater_than_or_equal(a, b));
321         }
322
323         {
324                 /* year same, month and day later */
325                 struct tm a;
326                 a.tm_mday = 22;
327                 a.tm_mon = 5;
328                 a.tm_year = 81;
329
330                 dcp::LocalTime b ("1981-02-04T00:00:00");
331                 BOOST_CHECK (day_greater_than_or_equal(a, b));
332         }
333
334         {
335                 /* all later */
336                 struct tm a;
337                 a.tm_mday = 22;
338                 a.tm_mon = 5;
339                 a.tm_year = 99;
340
341                 dcp::LocalTime b ("1981-02-04T00:00:00");
342                 BOOST_CHECK (day_greater_than_or_equal(a, b));
343         }
344 }
345
346 BOOST_AUTO_TEST_CASE (unique_string_test)
347 {
348         list<string> existing;
349         for (int i = 0; i < 16; i++) {
350                 string s;
351                 BOOST_CHECK_NO_THROW (s = dcp::unique_string(existing, "foo"));
352                 BOOST_CHECK (find(existing.begin(), existing.end(), s) == existing.end());
353                 existing.push_back (s);
354         }
355 }