2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <boost/test/unit_test.hpp>
22 #include "certificates.h"
25 #include "mono_picture_asset.h"
26 #include "sound_asset.h"
30 #include "mono_picture_frame.h"
31 #include "argb_frame.h"
32 #include "signer_chain.h"
35 using boost::shared_ptr;
37 /* Build an encrypted picture MXF and a KDM for it and check that the KDM can be decrypted */
38 BOOST_AUTO_TEST_CASE (round_trip_test)
40 boost::filesystem::remove_all ("build/test/signer");
41 boost::filesystem::create_directory ("build/test/signer");
42 libdcp::make_signer_chain ("build/test/signer", "openssl");
44 libdcp::CertificateChain chain;
45 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/ca.self-signed.pem"))));
46 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/intermediate.signed.pem"))));
47 chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/leaf.signed.pem"))));
49 shared_ptr<libdcp::Signer> signer (
52 "test/data/signer.key"
56 boost::filesystem::path work_dir = "build/test/round_trip_test";
57 boost::filesystem::create_directory (work_dir);
59 shared_ptr<libdcp::MonoPictureAsset> asset_A (new libdcp::MonoPictureAsset (work_dir, "video.mxf"));
60 asset_A->set_edit_rate (24);
61 asset_A->set_intrinsic_duration (24);
62 asset_A->set_size (libdcp::Size (32, 32));
63 asset_A->create (j2c);
67 asset_A->set_key (key);
69 shared_ptr<libdcp::CPL> cpl (new libdcp::CPL (work_dir, "A Test DCP", libdcp::FEATURE, 24, 24));
70 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (asset_A, shared_ptr<libdcp::SoundAsset> (), shared_ptr<libdcp::SubtitleAsset> ())));
72 /* A KDM using our certificate chain's leaf key pair */
76 signer->certificates().leaf(),
77 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
78 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
80 "2012-07-17T04:45:18+00:00"
83 boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
85 kdm_A.as_xml (kdm_file);
87 /* Reload the KDM, using our private key to decrypt it */
88 libdcp::KDM kdm_B (kdm_file, "build/test/signer/leaf.key");
90 /* Check that the decrypted KDMKeys are the same as the ones we started with */
91 BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
92 list<libdcp::KDMKey> keys_A = kdm_A.keys ();
93 list<libdcp::KDMKey> keys_B = kdm_B.keys ();
94 list<libdcp::KDMKey>::const_iterator i = keys_A.begin();
95 list<libdcp::KDMKey>::const_iterator j = keys_B.begin();
96 while (i != keys_A.end ()) {
97 BOOST_CHECK (*i == *j);
102 /* Reload the picture MXF */
103 shared_ptr<libdcp::MonoPictureAsset> asset_B (
104 new libdcp::MonoPictureAsset (work_dir, "video.mxf")
107 asset_B->set_key (kdm_B.keys().front().key());
109 shared_ptr<libdcp::ARGBFrame> frame_A = asset_A->get_frame(0)->argb_frame ();
110 shared_ptr<libdcp::ARGBFrame> frame_B = asset_B->get_frame(0)->argb_frame ();
111 BOOST_CHECK_EQUAL (frame_A->size().width, frame_B->size().width);
112 BOOST_CHECK_EQUAL (frame_A->size().height, frame_B->size().height);
113 BOOST_CHECK_EQUAL (memcmp (frame_A->data(), frame_B->data(), frame_A->size().width * frame_A->size().height), 0);