Supporters update.
[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/content_factory.h"
25 #include "lib/film.h"
26 #include "lib/kdm_cli.h"
27 #include "lib/screen.h"
28 #include "lib/trusted_device.h"
29 #include "test.h"
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <boost/filesystem.hpp>
32 #include <boost/test/unit_test.hpp>
33 #include <iostream>
34
35
36 using std::string;
37 using std::vector;
38 using boost::optional;
39
40
41 optional<string>
42 run(vector<string> const& args, vector<string>& output)
43 {
44         std::vector<char*> argv(args.size());
45         for (auto i = 0U; i < args.size(); ++i) {
46                 argv[i] = const_cast<char*>(args[i].c_str());
47         }
48
49         auto error = kdm_cli(args.size(), argv.data(), [&output](string s) { output.push_back(s); });
50         if (error) {
51                 std::cout << *error << "\n";
52         }
53
54         return error;
55 }
56
57
58 BOOST_AUTO_TEST_CASE (kdm_cli_test_certificate)
59 {
60         vector<string> args = {
61                 "kdm_cli",
62                 "--verbose",
63                 "--valid-from", "now",
64                 "--valid-duration", "2 weeks",
65                 "--certificate", "test/data/cert.pem",
66                 "-S", "my great screen",
67                 "-o", "build/test",
68                 "test/data/dkdm.xml"
69         };
70
71         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";
72         boost::system::error_code ec;
73         boost::filesystem::remove(kdm_filename, ec);
74
75         vector<string> output;
76         auto error = run(args, output);
77         BOOST_CHECK (!error);
78
79         BOOST_CHECK(boost::filesystem::exists(kdm_filename));
80 }
81
82
83 static
84 void
85 setup_test_config()
86 {
87         auto config = Config::instance();
88         auto const cert = dcp::Certificate(dcp::file_to_string("test/data/cert.pem"));
89
90         auto cinema_a = std::make_shared<Cinema>("Dean's Screens", vector<string>(), "", 0, 0);
91         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 1", "", cert, boost::none, std::vector<TrustedDevice>()));
92         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 2", "", cert, boost::none, std::vector<TrustedDevice>()));
93         cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 3", "", cert, boost::none, std::vector<TrustedDevice>()));
94         config->add_cinema(cinema_a);
95
96         auto cinema_b = std::make_shared<Cinema>("Floyd's Celluloid", vector<string>(), "", 0, 0);
97         cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Foo", "", cert, boost::none, std::vector<TrustedDevice>()));
98         cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Bar", "", cert, boost::none, std::vector<TrustedDevice>()));
99         config->add_cinema(cinema_b);
100 }
101
102
103 BOOST_AUTO_TEST_CASE(kdm_cli_select_cinema)
104 {
105         ConfigRestorer cr;
106
107         setup_test_config();
108
109         vector<boost::filesystem::path> kdm_filenames = {
110                 "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Foo.xml",
111                 "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Bar.xml"
112         };
113
114         for (auto path: kdm_filenames) {
115                 boost::system::error_code ec;
116                 boost::filesystem::remove(path, ec);
117         }
118
119         vector<string> args = {
120                 "kdm_cli",
121                 "--verbose",
122                 "--valid-from", "now",
123                 "--valid-duration", "2 weeks",
124                 "-c", "Floyd's Celluloid",
125                 "-o", "build/test",
126                 "test/data/dkdm.xml"
127         };
128
129         vector<string> output;
130         auto error = run(args, output);
131         BOOST_CHECK(!error);
132
133         BOOST_REQUIRE_EQUAL(output.size(), 2U);
134         BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
135         BOOST_CHECK_EQUAL(output[1], "Wrote 2 KDM files to build/test");
136
137         for (auto path: kdm_filenames) {
138                 BOOST_CHECK(boost::filesystem::exists(path));
139         }
140 }
141
142
143 BOOST_AUTO_TEST_CASE(kdm_cli_select_screen)
144 {
145         ConfigRestorer cr;
146
147         setup_test_config();
148
149         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";
150
151         boost::system::error_code ec;
152         boost::filesystem::remove(kdm_filename, ec);
153
154         vector<string> args = {
155                 "kdm_cli",
156                 "--verbose",
157                 "--valid-from", "now",
158                 "--valid-duration", "2 weeks",
159                 "-c", "Dean's Screens",
160                 "-S", "Screen 2",
161                 "-o", "build/test",
162                 "test/data/dkdm.xml"
163         };
164
165         vector<string> output;
166         auto error = run(args, output);
167         BOOST_CHECK(!error);
168
169         BOOST_REQUIRE_EQUAL(output.size(), 2U);
170         BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
171         BOOST_CHECK_EQUAL(output[1], "Wrote 1 KDM files to build/test");
172
173         BOOST_CHECK(boost::filesystem::exists(kdm_filename));
174 }
175
176
177 BOOST_AUTO_TEST_CASE(kdm_cli_specify_cinemas_file)
178 {
179         ConfigRestorer cr;
180
181         setup_test_config();
182
183         vector<string> args = {
184                 "kdm_cli",
185                 "--cinemas-file",
186                 "test/data/cinemas.xml",
187                 "--list-cinemas"
188         };
189
190         vector<string> output;
191         auto const error = run(args, output);
192         BOOST_CHECK(!error);
193
194         BOOST_REQUIRE_EQUAL(output.size(), 3U);
195         BOOST_CHECK_EQUAL(output[0], "stinking dump ()");
196         BOOST_CHECK_EQUAL(output[1], "classy joint ()");
197         BOOST_CHECK_EQUAL(output[2], "Great ()");
198 }
199
200
201 BOOST_AUTO_TEST_CASE(kdm_cli_specify_cert)
202 {
203         boost::filesystem::path kdm_filename = "build/test/KDM_KDMCLI__.xml";
204
205         boost::system::error_code ec;
206         boost::filesystem::remove(kdm_filename, ec);
207
208         auto film = new_test_film2("kdm_cli_specify_cert", content_factory("test/data/flat_red.png"));
209         film->set_encrypted(true);
210         film->set_name("KDMCLI");
211         film->set_use_isdcf_name(false);
212         make_and_verify_dcp(film);
213
214         vector<string> args = {
215                 "kdm_cli",
216                 "--valid-from", "2024-01-01 10:10:10",
217                 "--valid-duration", "2 weeks",
218                 "-C", "test/data/cert.pem",
219                 "-o", "build/test",
220                 "build/test/kdm_cli_specify_cert"
221         };
222
223         vector<string> output;
224         auto error = run(args, output);
225         BOOST_CHECK(!error);
226
227         BOOST_CHECK(output.empty());
228         BOOST_CHECK(boost::filesystem::exists(kdm_filename));
229 }
230