/* Copyright (C) 2022 Carl Hetherington This file is part of DCP-o-matic. DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with DCP-o-matic. If not, see . */ #include "lib/cinema.h" #include "lib/config.h" #include "lib/content_factory.h" #include "lib/film.h" #include "lib/kdm_cli.h" #include "lib/screen.h" #include "lib/trusted_device.h" #include "test.h" #include #include #include #include using std::string; using std::vector; using boost::optional; optional run(vector const& args, vector& output) { std::vector argv(args.size()); for (auto i = 0U; i < args.size(); ++i) { argv[i] = const_cast(args[i].c_str()); } auto error = kdm_cli(args.size(), argv.data(), [&output](string s) { output.push_back(s); }); if (error) { std::cout << *error << "\n"; } return error; } BOOST_AUTO_TEST_CASE (kdm_cli_test_certificate) { vector args = { "kdm_cli", "--verbose", "--valid-from", "now", "--valid-duration", "2 weeks", "--certificate", "test/data/cert.pem", "-S", "my great screen", "-o", "build/test", "test/data/dkdm.xml" }; 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"; boost::system::error_code ec; boost::filesystem::remove(kdm_filename, ec); vector output; auto error = run(args, output); BOOST_CHECK (!error); BOOST_CHECK(boost::filesystem::exists(kdm_filename)); } static void setup_test_config() { auto config = Config::instance(); auto const cert = dcp::Certificate(dcp::file_to_string("test/data/cert.pem")); auto cinema_a = std::make_shared("Dean's Screens", vector(), "", 0, 0); cinema_a->add_screen(std::make_shared("Screen 1", "", cert, boost::none, std::vector())); cinema_a->add_screen(std::make_shared("Screen 2", "", cert, boost::none, std::vector())); cinema_a->add_screen(std::make_shared("Screen 3", "", cert, boost::none, std::vector())); config->add_cinema(cinema_a); auto cinema_b = std::make_shared("Floyd's Celluloid", vector(), "", 0, 0); cinema_b->add_screen(std::make_shared("Foo", "", cert, boost::none, std::vector())); cinema_b->add_screen(std::make_shared("Bar", "", cert, boost::none, std::vector())); config->add_cinema(cinema_b); } BOOST_AUTO_TEST_CASE(kdm_cli_select_cinema) { ConfigRestorer cr; setup_test_config(); vector kdm_filenames = { "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Foo.xml", "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Bar.xml" }; for (auto path: kdm_filenames) { boost::system::error_code ec; boost::filesystem::remove(path, ec); } vector args = { "kdm_cli", "--verbose", "--valid-from", "now", "--valid-duration", "2 weeks", "-c", "Floyd's Celluloid", "-o", "build/test", "test/data/dkdm.xml" }; vector output; auto error = run(args, output); BOOST_CHECK(!error); BOOST_REQUIRE_EQUAL(output.size(), 2U); BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from")); BOOST_CHECK_EQUAL(output[1], "Wrote 2 KDM files to build/test"); for (auto path: kdm_filenames) { BOOST_CHECK(boost::filesystem::exists(path)); } } BOOST_AUTO_TEST_CASE(kdm_cli_select_screen) { ConfigRestorer cr; setup_test_config(); 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"; boost::system::error_code ec; boost::filesystem::remove(kdm_filename, ec); vector args = { "kdm_cli", "--verbose", "--valid-from", "now", "--valid-duration", "2 weeks", "-c", "Dean's Screens", "-S", "Screen 2", "-o", "build/test", "test/data/dkdm.xml" }; vector output; auto error = run(args, output); BOOST_CHECK(!error); BOOST_REQUIRE_EQUAL(output.size(), 2U); BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from")); BOOST_CHECK_EQUAL(output[1], "Wrote 1 KDM files to build/test"); BOOST_CHECK(boost::filesystem::exists(kdm_filename)); } BOOST_AUTO_TEST_CASE(kdm_cli_specify_cinemas_file) { ConfigRestorer cr; setup_test_config(); vector args = { "kdm_cli", "--cinemas-file", "test/data/cinemas.xml", "--list-cinemas" }; vector output; auto const error = run(args, output); BOOST_CHECK(!error); BOOST_REQUIRE_EQUAL(output.size(), 3U); BOOST_CHECK_EQUAL(output[0], "stinking dump ()"); BOOST_CHECK_EQUAL(output[1], "classy joint ()"); BOOST_CHECK_EQUAL(output[2], "Great ()"); } BOOST_AUTO_TEST_CASE(kdm_cli_specify_cert) { boost::filesystem::path kdm_filename = "build/test/KDM_KDMCLI__.xml"; boost::system::error_code ec; boost::filesystem::remove(kdm_filename, ec); auto film = new_test_film2("kdm_cli_specify_cert", content_factory("test/data/flat_red.png")); film->set_encrypted(true); film->set_name("KDMCLI"); film->set_use_isdcf_name(false); make_and_verify_dcp(film); vector args = { "kdm_cli", "--valid-from", "2024-01-01 10:10:10", "--valid-duration", "2 weeks", "-C", "test/data/cert.pem", "-o", "build/test", "build/test/kdm_cli_specify_cert" }; vector output; auto error = run(args, output); BOOST_CHECK(!error); BOOST_CHECK(output.empty()); BOOST_CHECK(boost::filesystem::exists(kdm_filename)); }