Rename read_interop_subtitle_test -> interop_subtitle_test.
[libdcp.git] / test / util_test.cc
1 /*
2     Copyright (C) 2013 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
20 #include <fstream>
21 #include <boost/test/unit_test.hpp>
22 #include "util.h"
23
24 using std::ifstream;
25 using std::string;
26 using std::list;
27
28 /** Test dcp::base64_decode */
29 BOOST_AUTO_TEST_CASE (base64_decode_test)
30 {
31         int const N = 256;
32
33         ifstream f ("test/data/base64_test");
34         BOOST_CHECK (f.good ());
35         string s;
36         while (f.good ()) {
37                 string l;
38                 getline (f, l);
39                 s += l;
40         }
41
42         ifstream g ("test/ref/base64_test_decoded", std::ios::binary);
43         BOOST_CHECK (g.good ());
44         unsigned char ref_decoded[N];
45         for (int i = 0; i < N; ++i) {
46                 char c;
47                 g.get (c);
48                 ref_decoded[i] = static_cast<unsigned char> (c);
49         }
50
51         unsigned char decoded[N];
52         int const r = dcp::base64_decode (s, decoded, N);
53         BOOST_CHECK_EQUAL (r, N);
54
55         for (int i = 0; i < N; ++i) {
56                 BOOST_CHECK_EQUAL (decoded[i], ref_decoded[i]);
57         }
58 }
59
60 /** Test dcp::content_kind_from_string */
61 BOOST_AUTO_TEST_CASE (content_kind_test)
62 {
63         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("feature"), dcp::FEATURE);
64         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Feature"), dcp::FEATURE);
65         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("FeaturE"), dcp::FEATURE);
66         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("Short"), dcp::SHORT);
67         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("trailer"), dcp::TRAILER);
68         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("test"), dcp::TEST);
69         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("transitional"), dcp::TRANSITIONAL);
70         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("rating"), dcp::RATING);
71         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("teaser"), dcp::TEASER);
72         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("policy"), dcp::POLICY);
73         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("psa"), dcp::PUBLIC_SERVICE_ANNOUNCEMENT);
74         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("advertisement"), dcp::ADVERTISEMENT);
75 }
76
77 /** Test dcp::relative_to_root */
78 BOOST_AUTO_TEST_CASE (relative_to_root_test)
79 {
80         {
81                 boost::filesystem::path root = "a";
82                 root /= "b";
83
84                 boost::filesystem::path file = "a";
85                 file /= "b";
86                 file /= "c";
87
88                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
89                 BOOST_CHECK (rel);
90                 BOOST_CHECK_EQUAL (rel.get(), boost::filesystem::path ("c"));
91         }
92
93         {
94                 boost::filesystem::path root = "a";
95                 root /= "b";
96                 root /= "c";
97
98                 boost::filesystem::path file = "a";
99                 file /= "b";
100
101                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
102                 BOOST_CHECK (!rel);
103         }
104
105         {
106                 boost::filesystem::path root = "a";
107
108                 boost::filesystem::path file = "a";
109                 file /= "b";
110                 file /= "c";
111
112                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
113                 BOOST_CHECK (rel);
114
115                 boost::filesystem::path check = "b";
116                 check /= "c";
117                 BOOST_CHECK_EQUAL (rel.get(), check);
118         }
119 }
120
121 /** Test private_key_fingerprint() */
122 BOOST_AUTO_TEST_CASE (private_key_fingerprint_test)
123 {
124         BOOST_CHECK_EQUAL (dcp::private_key_fingerprint (dcp::file_to_string ("test/data/private.key")), "Jdz1bFpCcKI7R16Ccx9JHYytag0=");
125 }
126
127 BOOST_AUTO_TEST_CASE (unique_string_test)
128 {
129         list<string> existing;
130         for (int i = 0; i < 16; i++) {
131                 string s;
132                 BOOST_CHECK_NO_THROW (s = dcp::unique_string(existing, "foo"));
133                 BOOST_CHECK (find(existing.begin(), existing.end(), s) == existing.end());
134                 existing.push_back (s);
135         }
136 }