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"
49 #include "openjpeg_image.h"
51 #include "colour_conversion.h"
52 #include <boost/test/unit_test.hpp>
53 #include <boost/scoped_array.hpp>
59 using std::shared_ptr;
60 using boost::scoped_array;
62 /** Build an encrypted picture asset and a KDM for it and check that the KDM can be decrypted */
63 BOOST_AUTO_TEST_CASE (round_trip_test)
65 shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain (boost::filesystem::path ("openssl")));
67 boost::filesystem::path work_dir = "build/test/round_trip_test";
68 boost::filesystem::create_directory (work_dir);
70 shared_ptr<dcp::MonoPictureAsset> asset_A (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
71 shared_ptr<dcp::PictureAssetWriter> writer = asset_A->start_write (work_dir / "video.mxf", false);
72 dcp::ArrayData j2c ("test/data/flat_red.j2c");
73 for (int i = 0; i < 24; ++i) {
74 writer->write (j2c.data (), j2c.size ());
80 asset_A->set_key (key);
82 shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
83 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
84 reel->add (shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (asset_A, 0)));
88 start.set_year (start.year() + 1);
90 end.set_year (end.year() + 2);
92 /* A KDM using our certificate chain's leaf key pair */
93 dcp::DecryptedKDM kdm_A (
100 "2012-07-17T04:45:18+00:00"
103 boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
105 kdm_A.encrypt(signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml (kdm_file);
107 /* Reload the KDM, using our private key to decrypt it */
108 dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (dcp::file_to_string (kdm_file)), signer->key().get ());
110 /* Check that the decrypted KDMKeys are the same as the ones we started with */
111 BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
112 auto keys_A = kdm_A.keys ();
113 auto keys_B = kdm_B.keys ();
114 auto i = keys_A.begin();
115 auto j = keys_B.begin();
116 while (i != keys_A.end ()) {
117 BOOST_CHECK (*i == *j);
122 /* Reload the picture asset */
123 shared_ptr<dcp::MonoPictureAsset> asset_B (
124 new dcp::MonoPictureAsset (work_dir / "video.mxf")
127 BOOST_CHECK (!kdm_B.keys().empty ());
128 asset_B->set_key (kdm_B.keys().front().key());
130 shared_ptr<dcp::OpenJPEGImage> xyz_A = asset_A->start_read()->get_frame(0)->xyz_image ();
131 shared_ptr<dcp::OpenJPEGImage> xyz_B = asset_B->start_read()->get_frame(0)->xyz_image ();
133 scoped_array<uint8_t> frame_A (new uint8_t[xyz_A->size().width * xyz_A->size().height * 4]);
134 dcp::xyz_to_rgba (xyz_A, dcp::ColourConversion::srgb_to_xyz(), frame_A.get(), xyz_A->size().width * 4);
136 scoped_array<uint8_t> frame_B (new uint8_t[xyz_B->size().width * xyz_B->size().height * 4]);
137 dcp::xyz_to_rgba (xyz_B, dcp::ColourConversion::srgb_to_xyz(), frame_B.get(), xyz_B->size().width * 4);
139 BOOST_CHECK_EQUAL (xyz_A->size().width, xyz_B->size().width);
140 BOOST_CHECK_EQUAL (xyz_A->size().height, xyz_B->size().height);
141 BOOST_CHECK_EQUAL (memcmp (frame_A.get(), frame_B.get(), xyz_A->size().width * xyz_A->size().height * 4), 0);