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