Fixes for the bad distros.
[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
27 /** Test dcp::base64_decode */
28 BOOST_AUTO_TEST_CASE (base64_decode_test)
29 {
30         int const N = 256;
31
32         ifstream f ("test/data/base64_test");
33         BOOST_CHECK (f.good ());
34         string s;
35         while (f.good ()) {
36                 string l;
37                 getline (f, l);
38                 s += l;
39         }
40
41         ifstream g ("test/ref/base64_test_decoded", std::ios::binary);
42         BOOST_CHECK (g.good ());
43         unsigned char ref_decoded[N];
44         for (int i = 0; i < N; ++i) {
45                 char c;
46                 g.get (c);
47                 ref_decoded[i] = static_cast<unsigned char> (c);
48         }
49
50         unsigned char decoded[N];
51         int const r = dcp::base64_decode (s, decoded, N);
52         BOOST_CHECK_EQUAL (r, N);
53
54         for (int i = 0; i < N; ++i) {
55                 BOOST_CHECK_EQUAL (decoded[i], ref_decoded[i]);
56         }
57 }
58
59 /** Test dcp::content_kind_from_string */
60 BOOST_AUTO_TEST_CASE (content_kind_test)
61 {
62         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("feature"), dcp::FEATURE);
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 ("Short"), dcp::SHORT);
66         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("trailer"), dcp::TRAILER);
67         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("test"), dcp::TEST);
68         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("transitional"), dcp::TRANSITIONAL);
69         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("rating"), dcp::RATING);
70         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("teaser"), dcp::TEASER);
71         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("policy"), dcp::POLICY);
72         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("psa"), dcp::PUBLIC_SERVICE_ANNOUNCEMENT);
73         BOOST_CHECK_EQUAL (dcp::content_kind_from_string ("advertisement"), dcp::ADVERTISEMENT);
74 }
75
76 /** Test dcp::relative_to_root */
77 BOOST_AUTO_TEST_CASE (relative_to_root_test)
78 {
79         {
80                 boost::filesystem::path root = "a";
81                 root /= "b";
82
83                 boost::filesystem::path file = "a";
84                 file /= "b";
85                 file /= "c";
86
87                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
88                 BOOST_CHECK (rel);
89                 BOOST_CHECK_EQUAL (rel.get(), boost::filesystem::path ("c"));
90         }
91
92         {
93                 boost::filesystem::path root = "a";
94                 root /= "b";
95                 root /= "c";
96
97                 boost::filesystem::path file = "a";
98                 file /= "b";
99
100                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
101                 BOOST_CHECK (!rel);
102         }
103
104         {
105                 boost::filesystem::path root = "a";
106
107                 boost::filesystem::path file = "a";
108                 file /= "b";
109                 file /= "c";
110
111                 boost::optional<boost::filesystem::path> rel = dcp::relative_to_root (root, file);
112                 BOOST_CHECK (rel);
113
114                 boost::filesystem::path check = "b";
115                 check /= "c";
116                 BOOST_CHECK_EQUAL (rel.get(), check);
117         }
118 }
119
120 /** Test private_key_fingerprint() */
121 BOOST_AUTO_TEST_CASE (private_key_fingerprint_test)
122 {
123         BOOST_CHECK_EQUAL (dcp::private_key_fingerprint (dcp::file_to_string ("test/data/private.key")), "Jdz1bFpCcKI7R16Ccx9JHYytag0=");
124 }