Fix strange Windows build error introduced in 6c37cc1979b2a01205a888c4c98f3334685ee8dd
[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 "stream_operators.h"
37 #include <boost/test/unit_test.hpp>
38 #include <fstream>
39
40 using std::ifstream;
41 using std::string;
42 using std::vector;
43
44 /** Test dcp::base64_decode */
45 BOOST_AUTO_TEST_CASE (base64_decode_test)
46 {
47         int const N = 256;
48
49         ifstream f ("test/data/base64_test");
50         BOOST_CHECK (f.good ());
51         string s;
52         while (f.good ()) {
53                 string l;
54                 getline (f, l);
55                 s += l;
56         }
57
58         ifstream g ("test/ref/base64_test_decoded", std::ios::binary);
59         BOOST_CHECK (g.good ());
60         unsigned char ref_decoded[N];
61         for (int i = 0; i < N; ++i) {
62                 char c;
63                 g.get (c);
64                 ref_decoded[i] = static_cast<unsigned char> (c);
65         }
66
67         unsigned char decoded[N];
68         int const r = dcp::base64_decode (s, decoded, N);
69         BOOST_CHECK_EQUAL (r, N);
70
71         for (int i = 0; i < N; ++i) {
72                 BOOST_CHECK_EQUAL (decoded[i], ref_decoded[i]);
73         }
74 }
75
76 /** Test dcp::content_kind_from_string */
77 BOOST_AUTO_TEST_CASE (content_kind_test)
78 {
79         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("feature"), dcp::ContentKind::FEATURE);
80         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Feature"), dcp::ContentKind::FEATURE);
81         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("FeaturE"), dcp::ContentKind::FEATURE);
82         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Short"), dcp::ContentKind::SHORT);
83         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("trailer"), dcp::ContentKind::TRAILER);
84         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("test"), dcp::ContentKind::TEST);
85         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("transitional"), dcp::ContentKind::TRANSITIONAL);
86         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("rating"), dcp::ContentKind::RATING);
87         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("teaser"), dcp::ContentKind::TEASER);
88         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("policy"), dcp::ContentKind::POLICY);
89         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("psa"), dcp::ContentKind::PUBLIC_SERVICE_ANNOUNCEMENT);
90         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("advertisement"), dcp::ContentKind::ADVERTISEMENT);
91 }
92
93 /** Test dcp::relative_to_root */
94 BOOST_AUTO_TEST_CASE (relative_to_root_test)
95 {
96         {
97                 boost::filesystem::path root = "a";
98                 root /= "b";
99
100                 boost::filesystem::path file = "a";
101                 file /= "b";
102                 file /= "c";
103
104                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
105                 BOOST_CHECK (rel);
106                 BOOST_CHECK_EQUAL (rel.get(), boost::filesystem::path ("c"));
107         }
108
109         {
110                 boost::filesystem::path root = "a";
111                 root /= "b";
112                 root /= "c";
113
114                 boost::filesystem::path file = "a";
115                 file /= "b";
116
117                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
118                 BOOST_CHECK (!rel);
119         }
120
121         {
122                 boost::filesystem::path root = "a";
123
124                 boost::filesystem::path file = "a";
125                 file /= "b";
126                 file /= "c";
127
128                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
129                 BOOST_CHECK (rel);
130
131                 boost::filesystem::path check = "b";
132                 check /= "c";
133                 BOOST_CHECK_EQUAL (rel.get(), check);
134         }
135 }
136
137 /** Test private_key_fingerprint() */
138 BOOST_AUTO_TEST_CASE (private_key_fingerprint_test)
139 {
140         BOOST_CHECK_EQUAL (dcp::private_key_fingerprint (dcp::file_to_string ("test/data/private.key")), "Jdz1bFpCcKI7R16Ccx9JHYytag0=");
141 }
142
143 BOOST_AUTO_TEST_CASE (day_less_than_or_equal_test)
144 {
145         {
146                 /* equal */
147                 dcp::LocalTime a ("1978-04-05T00:00:00");
148                 dcp::LocalTime b ("1978-04-05T00:00:00");
149                 BOOST_CHECK (day_less_than_or_equal(a, b));
150         }
151
152         {
153                 /* every part of a less than b */
154                 dcp::LocalTime a ("1981-02-04T00:00:00");
155                 dcp::LocalTime b ("1985-05-23T00:00:00");
156                 BOOST_CHECK (day_less_than_or_equal(a, b));
157         }
158
159         {
160                 /* years equal, other parts less */
161                 dcp::LocalTime a ("1981-03-02T00:00:00");
162                 dcp::LocalTime b ("1981-05-10T00:00:00");
163                 BOOST_CHECK (day_less_than_or_equal(a, b));
164         }
165
166         {
167                 /* year and month equal, day less */
168                 dcp::LocalTime a ("1981-03-09T00:00:00");
169                 dcp::LocalTime b ("1981-03-12T00:00:00");
170                 BOOST_CHECK (day_less_than_or_equal(a, b));
171         }
172
173         {
174                 /* a one day later than b */
175                 dcp::LocalTime a ("1981-03-05T00:00:00");
176                 dcp::LocalTime b ("1981-03-04T00:00:00");
177                 BOOST_CHECK (!day_less_than_or_equal(a, b));
178         }
179
180         {
181                 /* year and month same, day much later */
182                 dcp::LocalTime a ("1981-03-22T00:00:00");
183                 dcp::LocalTime b ("1981-03-04T00:00:00");
184                 BOOST_CHECK (!day_less_than_or_equal(a, b));
185         }
186
187         {
188                 /* year same, month and day later */
189                 dcp::LocalTime a ("1981-06-22T00:00:00");
190                 dcp::LocalTime b ("1981-02-04T00:00:00");
191                 BOOST_CHECK (!day_less_than_or_equal(a, b));
192         }
193
194         {
195                 /* all later */
196                 dcp::LocalTime a ("1999-06-22T00:00:00");
197                 dcp::LocalTime b ("1981-02-04T00:00:00");
198                 BOOST_CHECK (!day_less_than_or_equal(a, b));
199         }
200 }
201
202 BOOST_AUTO_TEST_CASE (day_greater_than_or_equal_test)
203 {
204         {
205                 /* equal */
206                 dcp::LocalTime a ("1978-04-05T00:00:00");
207                 dcp::LocalTime b ("1978-04-05T00:00:00");
208                 BOOST_CHECK (day_greater_than_or_equal(a, b));
209         }
210
211         {
212                 /* every part of a less than b */
213                 dcp::LocalTime a ("1981-03-04T00:00:00");
214                 dcp::LocalTime b ("1985-05-23T00:00:00");
215                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
216         }
217
218         {
219                 /* years equal, other parts less */
220                 dcp::LocalTime a ("1981-02-05T00:00:00");
221                 dcp::LocalTime b ("1981-05-10T00:00:00");
222                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
223         }
224
225         {
226                 /* year and month equal, day less */
227                 dcp::LocalTime a ("1981-03-04T00:00:00");
228                 dcp::LocalTime b ("1981-03-12T00:00:00");
229                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
230         }
231
232         {
233                 /* year and month equal, day less */
234                 dcp::LocalTime a ("1981-03-01T00:00:00");
235                 dcp::LocalTime b ("1981-03-04T00:00:00");
236                 BOOST_CHECK (!day_greater_than_or_equal(a, b));
237         }
238
239         {
240                 /* a one day later than b */
241                 dcp::LocalTime a ("1981-03-05T00:00:00");
242                 dcp::LocalTime b ("1981-03-04T00:00:00");
243                 BOOST_CHECK (day_greater_than_or_equal(a, b));
244         }
245
246         {
247                 /* year and month same, day much later */
248                 dcp::LocalTime a ("1981-03-22T00:00:00");
249                 dcp::LocalTime b ("1981-03-04T00:00:00");
250                 BOOST_CHECK (day_greater_than_or_equal(a, b));
251         }
252
253         {
254                 /* year same, month and day later */
255                 dcp::LocalTime a ("1981-05-22T00:00:00");
256                 dcp::LocalTime b ("1981-02-04T00:00:00");
257                 BOOST_CHECK (day_greater_than_or_equal(a, b));
258         }
259
260         {
261                 /* all later */
262                 dcp::LocalTime a ("1999-06-22T00:00:00");
263                 dcp::LocalTime b ("1981-02-04T00:00:00");
264                 BOOST_CHECK (day_greater_than_or_equal(a, b));
265         }
266 }
267
268 BOOST_AUTO_TEST_CASE (unique_string_test)
269 {
270         vector<string> existing;
271         for (int i = 0; i < 16; i++) {
272                 string s;
273                 BOOST_CHECK_NO_THROW (s = dcp::unique_string(existing, "foo"));
274                 BOOST_CHECK (find(existing.begin(), existing.end(), s) == existing.end());
275                 existing.push_back (s);
276         }
277 }