X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fdecryption_test.cc;h=8f3dbff711295205cbe7c407f5d73ba137aef108;hb=refs%2Fheads%2Fmain;hp=0974b85d02a4076a59c37d669c56f1404612cd34;hpb=fe99eae4b95c5fcf2f3730efad4a18d0cb5c29bc;p=libdcp.git diff --git a/test/decryption_test.cc b/test/decryption_test.cc index 0974b85d..8f3dbff7 100644 --- a/test/decryption_test.cc +++ b/test/decryption_test.cc @@ -1,73 +1,109 @@ /* - Copyright (C) 2013-2014 Carl Hetherington + Copyright (C) 2013-2019 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of libdcp. + + libdcp 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. - This program is distributed in the hope that it will be useful, + libdcp 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 this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - + along with libdcp. If not, see . + + In addition, as a special exception, the copyright holders give + permission to link the code of portions of this program with the + OpenSSL library under certain conditions as described in each + individual source file, and distribute linked combinations + including the two. + + You must obey the GNU General Public License in all respects + for all of the code used other than OpenSSL. If you modify + file(s) with this exception, you may extend this exception to your + version of the file(s), but you are not obligated to do so. If you + do not wish to do so, delete this exception statement from your + version. If you delete this exception statement from all source + files in the program, then also delete it here. */ -#include "dcp.h" -#include "mono_picture_frame.h" +#include "colour_conversion.h" #include "cpl.h" +#include "dcp.h" #include "decrypted_kdm.h" #include "encrypted_kdm.h" #include "mono_picture_asset.h" -#include "reel_picture_asset.h" +#include "mono_picture_asset_reader.h" +#include "mono_picture_frame.h" +#include "openjpeg_image.h" +#include "picture_asset_writer.h" #include "reel.h" -#include "test.h" -#include "xyz_image.h" +#include "reel_file_asset.h" +#include "reel_mono_picture_asset.h" +#include "reel_picture_asset.h" +#include "reel_sound_asset.h" +#include "reel_smpte_subtitle_asset.h" #include "rgb_xyz.h" -#include "colour_conversion.h" +#include "smpte_subtitle_asset.h" +#include "sound_asset.h" +#include "sound_asset_writer.h" +#include "stream_operators.h" +#include "test.h" #include #include -using std::pair; + +#ifndef M_PI +#define M_PI (3.14159265358979323846) +#endif + + +using std::dynamic_pointer_cast; using std::make_pair; -using boost::dynamic_pointer_cast; -using boost::shared_ptr; +using std::map; +using std::pair; +using std::shared_ptr; +using std::string; +using std::vector; +using boost::optional; using boost::scoped_array; -pair + +pair, dcp::Size> get_frame (dcp::DCP const & dcp) { - shared_ptr reel = dcp.cpls().front()->reels().front (); - shared_ptr picture = reel->main_picture()->asset (); + auto reel = dcp.cpls().front()->reels()[0]; + auto picture = reel->main_picture()->asset(); BOOST_CHECK (picture); - shared_ptr mono_picture = dynamic_pointer_cast (picture); - shared_ptr j2k_frame = mono_picture->get_frame (0); - shared_ptr xyz = j2k_frame->xyz_image(); + auto mono_picture = dynamic_pointer_cast(picture); + auto reader = mono_picture->start_read(); + auto j2k_frame = reader->get_frame(0); + auto xyz = j2k_frame->xyz_image(); - uint8_t* argb = new uint8_t[xyz->size().width * xyz->size().height * 4]; - dcp::xyz_to_rgba (j2k_frame->xyz_image(), dcp::ColourConversion::srgb_to_xyz(), argb); + std::vector argb(xyz->size().width * xyz->size().height * 4); + dcp::xyz_to_rgba(j2k_frame->xyz_image(), dcp::ColourConversion::srgb_to_xyz(), argb.data(), xyz->size().width * 4); return make_pair (argb, xyz->size ()); } /** Decrypt an encrypted test DCP and check that its first frame is the same as the unencrypted version */ -BOOST_AUTO_TEST_CASE (decryption_test) +BOOST_AUTO_TEST_CASE (decryption_test1) { - boost::filesystem::path plaintext_path = private_test; + auto plaintext_path = private_test; plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV"; dcp::DCP plaintext (plaintext_path.string ()); plaintext.read (); - BOOST_CHECK_EQUAL (plaintext.encrypted (), false); + BOOST_CHECK_EQUAL (plaintext.any_encrypted(), false); - boost::filesystem::path encrypted_path = private_test; + auto encrypted_path = private_test; encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV"; dcp::DCP encrypted (encrypted_path.string ()); encrypted.read (); - BOOST_CHECK_EQUAL (encrypted.encrypted (), true); + BOOST_CHECK_EQUAL (encrypted.any_encrypted(), true); dcp::DecryptedKDM kdm ( dcp::EncryptedKDM ( @@ -75,27 +111,24 @@ BOOST_AUTO_TEST_CASE (decryption_test) ), dcp::file_to_string ("test/data/private.key") ); - + encrypted.add (kdm); - pair plaintext_frame = get_frame (plaintext); - pair encrypted_frame = get_frame (encrypted); + auto plaintext_frame = get_frame(plaintext); + auto encrypted_frame = get_frame(encrypted); /* Check that plaintext and encrypted are the same */ BOOST_CHECK_EQUAL (plaintext_frame.second, encrypted_frame.second); - + BOOST_CHECK_EQUAL ( memcmp ( - plaintext_frame.first, - encrypted_frame.first, + plaintext_frame.first.data(), + encrypted_frame.first.data(), plaintext_frame.second.width * plaintext_frame.second.height * 4 ), 0 ); - - delete[] plaintext_frame.first; - delete[] encrypted_frame.first; } /** Load in a KDM that didn't work at first */ @@ -103,6 +136,108 @@ BOOST_AUTO_TEST_CASE (failing_kdm_test) { dcp::DecryptedKDM kdm ( dcp::EncryptedKDM (dcp::file_to_string ("test/data/target.pem.crt.de5d4eba-e683-41ca-bdda-aa4ad96af3f4.kdm.xml")), - dcp::file_to_string ("test/data/private.key") + dcp::file_to_string("test/data/failing-private.key") ); } + + +/** Make an encrypted SMPTE DCP with picture, sound and subs and check that the keys get distributed to the assets + * when we read it back in. + */ +BOOST_AUTO_TEST_CASE (decryption_test2) +{ + boost::filesystem::path dir = "build/test/decryption_test2"; + boost::filesystem::create_directory(dir); + + auto context_id = dcp::make_uuid(); + dcp::Key key; + + auto picture_asset = std::make_shared(dcp::Fraction(24, 1), dcp::Standard::SMPTE); + picture_asset->set_key (key); + picture_asset->set_context_id (context_id); + auto picture_writer = picture_asset->start_write(dir / "picture.mxf", dcp::PictureAsset::Behaviour::MAKE_NEW); + dcp::ArrayData picture("test/data/flat_red.j2c"); + for (int i = 0; i < 24; ++i) { + picture_writer->write(picture); + } + picture_writer->finalize(); + + auto sound_asset = std::make_shared(dcp::Fraction(24, 1), 48000, 2, dcp::LanguageTag("en-GB"), dcp::Standard::SMPTE); + sound_asset->set_key (key); + sound_asset->set_context_id (context_id); + auto sound_writer = sound_asset->start_write(dir / "sound.mxf", {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED); + std::array left; + std::array right; + for (int i = 0; i < 48000; ++i) { + left[i] = sin (2 * M_PI * i * 440 / 48000) * 0.25; + right[i] = sin (2 * M_PI * i * 880 / 48000) * 0.25; + } + std::array audio; + audio[0] = left.data(); + audio[1] = right.data(); + sound_writer->write(audio.data(), 2, 48000); + sound_writer->finalize (); + + auto subs_asset = std::make_shared(); + subs_asset->set_key (key); + subs_asset->set_context_id (context_id); + subs_asset->add(std::make_shared( + optional(), + false, false, false, + dcp::Colour(255, 255, 255), + 42, + 1, + dcp::Time(), + dcp::Time(0, 0, 5, 0, 24), + 0.5, dcp::HAlign::CENTER, + 0.5, dcp::VAlign::CENTER, + 0, + dcp::Direction::LTR, + "Hello world", + dcp::Effect::NONE, + dcp::Colour(0, 0, 0), + dcp::Time(), dcp::Time(), 0, std::vector() + )); + subs_asset->write (dir / "subs.mxf"); + + auto reel = std::make_shared(); + auto reel_picture_asset = std::make_shared(picture_asset, 0); + auto reel_sound_asset = std::make_shared(sound_asset, 0); + auto reel_subs_asset = std::make_shared(subs_asset, dcp::Fraction(24, 1), 120, 0); + reel->add(reel_picture_asset); + reel->add(reel_sound_asset); + reel->add(reel_subs_asset); + + auto cpl = std::make_shared("My film", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE); + cpl->add(reel); + + dcp::DCP dcp (dir); + dcp.add (cpl); + dcp.write_xml (); + + map, dcp::Key> keys; + keys[reel_picture_asset] = key; + keys[reel_sound_asset] = key; + keys[reel_subs_asset] = key; + + dcp::DecryptedKDM kdm (cpl->id(), keys, dcp::LocalTime(), dcp::LocalTime(), "foo", "bar", "baz"); + + dcp::DCP dcp_read (dir); + dcp_read.read (); + dcp_read.add (kdm); + + BOOST_REQUIRE_EQUAL (dcp_read.cpls().size(), 1U); + auto cpl_read = dcp_read.cpls()[0]; + BOOST_REQUIRE_EQUAL (cpl_read->reels().size(), 1U); + auto reel_read = cpl_read->reels()[0]; + + BOOST_REQUIRE (reel_read->main_picture()); + BOOST_CHECK (reel_read->main_picture()->asset()->key()); + BOOST_REQUIRE (reel_read->main_sound()); + BOOST_CHECK (reel_read->main_sound()->asset()->key()); + BOOST_REQUIRE (reel_read->main_subtitle()); + auto smpte_sub = dynamic_pointer_cast(reel_read->main_subtitle()->asset()); + BOOST_REQUIRE (smpte_sub); + BOOST_CHECK (smpte_sub->key()); +} +