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>
58 using std::make_shared;
59 using std::shared_ptr;
62 using boost::scoped_array;
65 /** Build an encrypted picture asset and a KDM for it and check that the KDM can be decrypted */
66 BOOST_AUTO_TEST_CASE (round_trip_test)
68 auto signer = make_shared<dcp::CertificateChain>(boost::filesystem::path("openssl"), 10 * 365);
70 boost::filesystem::path work_dir = "build/test/round_trip_test";
71 boost::filesystem::create_directory (work_dir);
73 auto asset_A = make_shared<dcp::MonoPictureAsset>(dcp::Fraction (24, 1), dcp::Standard::SMPTE);
74 auto writer = asset_A->start_write(work_dir / "video.mxf", dcp::PictureAsset::Behaviour::MAKE_NEW);
75 dcp::ArrayData j2c ("test/data/flat_red.j2c");
76 for (int i = 0; i < 24; ++i) {
77 writer->write (j2c.data (), j2c.size ());
83 asset_A->set_key (key);
85 auto cpl = make_shared<dcp::CPL>("A Test DCP", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
86 auto reel = make_shared<dcp::Reel>();
87 reel->add (make_shared<dcp::ReelMonoPictureAsset>(asset_A, 0));
91 start.set_year (start.year() + 1);
93 end.set_year (end.year() + 2);
95 /* A KDM using our certificate chain's leaf key pair */
96 dcp::DecryptedKDM kdm_A (
103 "2012-07-17T04:45:18+00:00"
106 boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
108 kdm_A.encrypt(signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, 0).as_xml (kdm_file);
110 /* Reload the KDM, using our private key to decrypt it */
111 dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (dcp::file_to_string (kdm_file)), signer->key().get ());
113 /* Check that the decrypted KDMKeys are the same as the ones we started with */
114 BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
115 auto keys_A = kdm_A.keys ();
116 auto keys_B = kdm_B.keys ();
117 auto i = keys_A.begin();
118 auto j = keys_B.begin();
119 while (i != keys_A.end ()) {
120 BOOST_CHECK (*i == *j);
125 /* Reload the picture asset */
126 auto asset_B = make_shared<dcp::MonoPictureAsset>(work_dir / "video.mxf");
128 BOOST_CHECK (!kdm_B.keys().empty ());
129 asset_B->set_key (kdm_B.keys().front().key());
131 auto xyz_A = asset_A->start_read()->get_frame(0)->xyz_image ();
132 auto 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);