2 Copyright (C) 2013-2019 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/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
34 #include "certificate.h"
35 #include "decrypted_kdm.h"
36 #include "encrypted_kdm.h"
37 #include "certificate_chain.h"
38 #include "mono_picture_asset.h"
39 #include "sound_asset.h"
43 #include "mono_picture_frame.h"
44 #include "certificate_chain.h"
45 #include "mono_picture_asset_writer.h"
46 #include "mono_picture_asset_reader.h"
47 #include "reel_picture_asset.h"
48 #include "reel_mono_picture_asset.h"
50 #include "openjpeg_image.h"
52 #include "colour_conversion.h"
53 #include <boost/test/unit_test.hpp>
54 #include <boost/scoped_array.hpp>
60 using std::shared_ptr;
61 using boost::scoped_array;
63 /** Build an encrypted picture asset and a KDM for it and check that the KDM can be decrypted */
64 BOOST_AUTO_TEST_CASE (round_trip_test)
66 shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain (boost::filesystem::path ("openssl")));
68 boost::filesystem::path work_dir = "build/test/round_trip_test";
69 boost::filesystem::create_directory (work_dir);
71 shared_ptr<dcp::MonoPictureAsset> asset_A (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
72 shared_ptr<dcp::PictureAssetWriter> writer = asset_A->start_write (work_dir / "video.mxf", false);
73 dcp::File j2c ("test/data/flat_red.j2c");
74 for (int i = 0; i < 24; ++i) {
75 writer->write (j2c.data (), j2c.size ());
81 asset_A->set_key (key);
83 shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
84 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
85 reel->add (shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (asset_A, 0)));
89 start.set_year (start.year() + 1);
91 end.set_year (end.year() + 2);
93 /* A KDM using our certificate chain's leaf key pair */
94 dcp::DecryptedKDM kdm_A (
101 "2012-07-17T04:45:18+00:00"
104 boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
106 kdm_A.encrypt(signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml (kdm_file);
108 /* Reload the KDM, using our private key to decrypt it */
109 dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (dcp::file_to_string (kdm_file)), signer->key().get ());
111 /* Check that the decrypted KDMKeys are the same as the ones we started with */
112 BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
113 list<dcp::DecryptedKDMKey> keys_A = kdm_A.keys ();
114 list<dcp::DecryptedKDMKey> keys_B = kdm_B.keys ();
115 list<dcp::DecryptedKDMKey>::const_iterator i = keys_A.begin();
116 list<dcp::DecryptedKDMKey>::const_iterator j = keys_B.begin();
117 while (i != keys_A.end ()) {
118 BOOST_CHECK (*i == *j);
123 /* Reload the picture asset */
124 shared_ptr<dcp::MonoPictureAsset> asset_B (
125 new dcp::MonoPictureAsset (work_dir / "video.mxf")
128 BOOST_CHECK (!kdm_B.keys().empty ());
129 asset_B->set_key (kdm_B.keys().front().key());
131 shared_ptr<dcp::OpenJPEGImage> xyz_A = asset_A->start_read()->get_frame(0)->xyz_image ();
132 shared_ptr<dcp::OpenJPEGImage> xyz_B = asset_B->start_read()->get_frame(0)->xyz_image ();
134 scoped_array<uint8_t> frame_A (new uint8_t[xyz_A->size().width * xyz_A->size().height * 4]);
135 dcp::xyz_to_rgba (xyz_A, dcp::ColourConversion::srgb_to_xyz(), frame_A.get(), xyz_A->size().width * 4);
137 scoped_array<uint8_t> frame_B (new uint8_t[xyz_B->size().width * xyz_B->size().height * 4]);
138 dcp::xyz_to_rgba (xyz_B, dcp::ColourConversion::srgb_to_xyz(), frame_B.get(), xyz_B->size().width * 4);
140 BOOST_CHECK_EQUAL (xyz_A->size().width, xyz_B->size().width);
141 BOOST_CHECK_EQUAL (xyz_A->size().height, xyz_B->size().height);
142 BOOST_CHECK_EQUAL (memcmp (frame_A.get(), frame_B.get(), xyz_A->size().width * xyz_A->size().height * 4), 0);