2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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.
11 libdcp 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.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
20 #include "certificate.h"
21 #include "decrypted_kdm.h"
22 #include "encrypted_kdm.h"
23 #include "certificate_chain.h"
24 #include "mono_picture_asset.h"
25 #include "sound_asset.h"
29 #include "mono_picture_frame.h"
30 #include "certificate_chain.h"
31 #include "mono_picture_asset_writer.h"
32 #include "mono_picture_asset_reader.h"
33 #include "reel_picture_asset.h"
34 #include "reel_mono_picture_asset.h"
36 #include "openjpeg_image.h"
38 #include "colour_conversion.h"
39 #include <boost/test/unit_test.hpp>
40 #include <boost/scoped_array.hpp>
45 using boost::shared_ptr;
46 using boost::scoped_array;
48 /** Build an encrypted picture asset and a KDM for it and check that the KDM can be decrypted */
49 BOOST_AUTO_TEST_CASE (round_trip_test)
51 shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain ("openssl"));
53 boost::filesystem::path work_dir = "build/test/round_trip_test";
54 boost::filesystem::create_directory (work_dir);
56 shared_ptr<dcp::MonoPictureAsset> asset_A (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
57 shared_ptr<dcp::PictureAssetWriter> writer = asset_A->start_write (work_dir / "video.mxf", dcp::SMPTE, false);
58 dcp::File j2c ("test/data/32x32_red_square.j2c");
59 for (int i = 0; i < 24; ++i) {
60 writer->write (j2c.data (), j2c.size ());
66 asset_A->set_key (key);
68 shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
69 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
70 reel->add (shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (asset_A, 0)));
73 /* A KDM using our certificate chain's leaf key pair */
74 dcp::DecryptedKDM kdm_A (
77 dcp::LocalTime ("2013-01-01T00:00:00+00:00"),
78 dcp::LocalTime ("2013-01-08T00:00:00+00:00"),
81 "2012-07-17T04:45:18+00:00"
84 boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
86 kdm_A.encrypt(signer, signer->leaf(), vector<dcp::Certificate>(), dcp::MODIFIED_TRANSITIONAL_1).as_xml (kdm_file);
88 /* Reload the KDM, using our private key to decrypt it */
89 dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (dcp::file_to_string (kdm_file)), signer->key().get ());
91 /* Check that the decrypted KDMKeys are the same as the ones we started with */
92 BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
93 list<dcp::DecryptedKDMKey> keys_A = kdm_A.keys ();
94 list<dcp::DecryptedKDMKey> keys_B = kdm_B.keys ();
95 list<dcp::DecryptedKDMKey>::const_iterator i = keys_A.begin();
96 list<dcp::DecryptedKDMKey>::const_iterator j = keys_B.begin();
97 while (i != keys_A.end ()) {
98 BOOST_CHECK (*i == *j);
103 /* Reload the picture asset */
104 shared_ptr<dcp::MonoPictureAsset> asset_B (
105 new dcp::MonoPictureAsset (work_dir / "video.mxf")
108 BOOST_CHECK (!kdm_B.keys().empty ());
109 asset_B->set_key (kdm_B.keys().front().key());
111 shared_ptr<dcp::OpenJPEGImage> xyz_A = asset_A->start_read()->get_frame(0)->xyz_image ();
112 shared_ptr<dcp::OpenJPEGImage> xyz_B = asset_B->start_read()->get_frame(0)->xyz_image ();
114 scoped_array<uint8_t> frame_A (new uint8_t[xyz_A->size().width * xyz_A->size().height * 4]);
115 dcp::xyz_to_rgba (xyz_A, dcp::ColourConversion::srgb_to_xyz(), frame_A.get());
117 scoped_array<uint8_t> frame_B (new uint8_t[xyz_B->size().width * xyz_B->size().height * 4]);
118 dcp::xyz_to_rgba (xyz_B, dcp::ColourConversion::srgb_to_xyz(), frame_B.get());
120 BOOST_CHECK_EQUAL (xyz_A->size().width, xyz_B->size().width);
121 BOOST_CHECK_EQUAL (xyz_A->size().height, xyz_B->size().height);
122 BOOST_CHECK_EQUAL (memcmp (frame_A.get(), frame_B.get(), xyz_A->size().width * xyz_A->size().height * 4), 0);