e300d82a5a04b8b481861ebb8ca47458b22ac791
[dcpomatic.git] / test / kdm_cli_test.cc
1 /*
2     Copyright (C) 2022 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
22 #include "lib/cinema.h"
23 #include "lib/config.h"
24 #include "lib/kdm_cli.h"
25 #include "lib/screen.h"
26 #include "lib/trusted_device.h"
27 #include "test.h"
28 #include <boost/algorithm/string/predicate.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/test/unit_test.hpp>
31 #include <iostream>
32
33
34 using std::list;
35 using std::string;
36 using std::vector;
37 using boost::optional;
38
39
40 optional<string>
41 run(vector<string> const& args, vector<string>& output)
42 {
43         std::vector<char*> argv(args.size());
44         for (auto i = 0U; i < args.size(); ++i) {
45                 argv[i] = const_cast<char*>(args[i].c_str());
46         }
47
48         auto error = kdm_cli(args.size(), argv.data(), [&output](string s) { output.push_back(s); });
49         if (error) {
50                 std::cout << *error << "\n";
51         }
52
53         return error;
54 }
55
56
57 BOOST_AUTO_TEST_CASE (kdm_cli_test_certificate)
58 {
59         vector<string> args = {
60                 "kdm_cli",
61                 "--verbose",
62                 "--valid-from", "now",
63                 "--valid-duration", "2 weeks",
64                 "--certificate", "test/data/cert.pem",
65                 "-S", "my great screen",
66                 "-o", "build/test",
67                 "test/data/dkdm.xml"
68         };
69
70         boost::filesystem::path const kdm_filename = "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV__my_great_screen.xml";
71         boost::system::error_code ec;
72         boost::filesystem::remove(kdm_filename, ec);
73
74         vector<string> output;
75         auto error = run(args, output);
76         BOOST_CHECK (!error);
77
78         BOOST_CHECK(boost::filesystem::exists(kdm_filename));
79 }
80
81
82 static
83 void
84 setup_test_config()
85 {
86         auto config = Config::instance();
87         auto const cert = dcp::Certificate(dcp::file_to_string("test/data/cert.pem"));
88
89         auto cinema_a = std::make_shared<Cinema>("Dean's Screens", list<string>(), "", 0, 0);
90         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 1", "", cert, boost::none, std::vector<TrustedDevice>()));
91         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 2", "", cert, boost::none, std::vector<TrustedDevice>()));
92         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 3", "", cert, boost::none, std::vector<TrustedDevice>()));
93         config->add_cinema(cinema_a);
94
95         auto cinema_b = std::make_shared<Cinema>("Floyd's Celluloid", list<string>(), "", 0, 0);
96         cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Foo", "", cert, boost::none, std::vector<TrustedDevice>()));
97         cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Bar", "", cert, boost::none, std::vector<TrustedDevice>()));
98         config->add_cinema(cinema_b);
99 }
100
101
102 BOOST_AUTO_TEST_CASE(kdm_cli_select_cinema)
103 {
104         ConfigRestorer cr;
105
106         setup_test_config();
107
108         vector<boost::filesystem::path> kdm_filenames = {
109                 "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Foo.xml",
110                 "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Bar.xml"
111         };
112
113         for (auto path: kdm_filenames) {
114                 boost::system::error_code ec;
115                 boost::filesystem::remove(path, ec);
116         }
117
118         vector<string> args = {
119                 "kdm_cli",
120                 "--verbose",
121                 "--valid-from", "now",
122                 "--valid-duration", "2 weeks",
123                 "-c", "Floyd's Celluloid",
124                 "-o", "build/test",
125                 "test/data/dkdm.xml"
126         };
127
128         vector<string> output;
129         auto error = run(args, output);
130         BOOST_CHECK(!error);
131
132         BOOST_REQUIRE_EQUAL(output.size(), 2U);
133         BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
134         BOOST_CHECK_EQUAL(output[1], "Wrote 2 KDM files to build/test");
135
136         for (auto path: kdm_filenames) {
137                 BOOST_CHECK(boost::filesystem::exists(path));
138         }
139 }
140
141
142 BOOST_AUTO_TEST_CASE(kdm_cli_select_screen)
143 {
144         ConfigRestorer cr;
145
146         setup_test_config();
147
148         boost::filesystem::path kdm_filename = "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Deans_Screens_Screen_2.xml";
149
150         boost::system::error_code ec;
151         boost::filesystem::remove(kdm_filename, ec);
152
153         vector<string> args = {
154                 "kdm_cli",
155                 "--verbose",
156                 "--valid-from", "now",
157                 "--valid-duration", "2 weeks",
158                 "-c", "Dean's Screens",
159                 "-S", "Screen 2",
160                 "-o", "build/test",
161                 "test/data/dkdm.xml"
162         };
163
164         vector<string> output;
165         auto error = run(args, output);
166         BOOST_CHECK(!error);
167
168         BOOST_REQUIRE_EQUAL(output.size(), 2U);
169         BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
170         BOOST_CHECK_EQUAL(output[1], "Wrote 1 KDM files to build/test");
171
172         BOOST_CHECK(boost::filesystem::exists(kdm_filename));
173 }
174
175