swaroop: store whole signer/decryption chains and private keys encrypted by machine...
[dcpomatic.git] / test / util_test.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  test/util_test.cc
22  *  @brief Test various utility methods.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/util.h"
27 #include "lib/cross.h"
28 #include "lib/exceptions.h"
29 #include <dcp/certificate_chain.h>
30 #include <boost/test/unit_test.hpp>
31
32 using std::string;
33 using std::vector;
34 using boost::shared_ptr;
35
36 BOOST_AUTO_TEST_CASE (digest_head_tail_test)
37 {
38         vector<boost::filesystem::path> p;
39         p.push_back ("test/data/digest.test");
40         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "57497ef84a0487f2bb0939a1f5703912");
41
42         p.push_back ("test/data/digest.test2");
43         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "5a3a89857b931755ae728a518224a05c");
44
45         p.clear ();
46         p.push_back ("test/data/digest.test3");
47         p.push_back ("test/data/digest.test");
48         p.push_back ("test/data/digest.test2");
49         p.push_back ("test/data/digest.test4");
50         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "52ccf111e4e72b58bb7b2aaa6bd45ea5");
51
52         p.clear ();
53         p.push_back ("foobar");
54         BOOST_CHECK_THROW (digest_head_tail (p, 1024), OpenFileError);
55 }
56
57 BOOST_AUTO_TEST_CASE (timecode_test)
58 {
59         DCPTime t = DCPTime::from_seconds (2 * 60 * 60 + 4 * 60 + 31) + DCPTime::from_frames (19, 24);
60         BOOST_CHECK_EQUAL (t.timecode (24), "02:04:31:19");
61 }
62
63 BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test)
64 {
65         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1), "1s");
66         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2), "2s");
67         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (60), "1m");
68         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1.5 * 60), "1m 30s");
69         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 60), "2m");
70         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (17 * 60 + 20), "17m");
71         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1 * 3600), "1h");
72         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1h 40m");
73         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600), "2h");
74         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600 - 1), "2h");
75         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14h");
76 }
77
78 BOOST_AUTO_TEST_CASE (time_to_hmsf_test)
79 {
80         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(12, 24), 24), "0:00:00.12");
81         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(24, 24), 24), "0:00:01.0");
82         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(32, 24), 24), "0:00:01.8");
83         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_seconds(92), 24), "0:01:32.0");
84         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_seconds(2 * 60 * 60 + 92), 24), "2:01:32.0");
85 }
86
87 BOOST_AUTO_TEST_CASE (tidy_for_filename_test)
88 {
89         BOOST_CHECK_EQUAL (tidy_for_filename ("fish\\chips"), "fish_chips");
90         BOOST_CHECK_EQUAL (tidy_for_filename ("fish:chips\\"), "fish_chips_");
91         BOOST_CHECK_EQUAL (tidy_for_filename ("fish/chips\\"), "fish_chips_");
92         BOOST_CHECK_EQUAL (tidy_for_filename ("abcdefghï"), "abcdefghï");
93 }
94
95 #ifdef DCPOMATIC_VARIANT_SWAROOP
96 BOOST_AUTO_TEST_CASE (swaroop_chain_test)
97 {
98         shared_ptr<dcp::CertificateChain> cc (
99                 new dcp::CertificateChain (
100                         openssl_path(),
101                         "dcpomatic.com",
102                         "dcpomatic.com",
103                         ".dcpomatic.smpte-430-2.ROOT",
104                         ".dcpomatic.smpte-430-2.INTERMEDIATE",
105                         "CS.dcpomatic.smpte-430-2.LEAF"
106                         )
107                 );
108
109         write_swaroop_chain (cc, "build/test/swaroop_chain");
110         shared_ptr<dcp::CertificateChain> back = read_swaroop_chain ("build/test/swaroop_chain");
111
112         BOOST_CHECK (cc->root_to_leaf() == back->root_to_leaf());
113 }
114
115 #endif