Merge branch 'master' of ssh://git.carlh.net/home/carl/git/libdcp
[libdcp.git] / test / round_trip_test.cc
1 /*
2     Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
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"
40 #include "reel.h"
41 #include "test.h"
42 #include "cpl.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 "file.h"
50 #include "openjpeg_image.h"
51 #include "rgb_xyz.h"
52 #include "colour_conversion.h"
53 #include <boost/test/unit_test.hpp>
54 #include <boost/scoped_array.hpp>
55 #include <iostream>
56
57 using std::list;
58 using std::vector;
59 using std::string;
60 using boost::shared_ptr;
61 using boost::scoped_array;
62
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)
65 {
66         shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain (boost::filesystem::path ("openssl")));
67
68         boost::filesystem::path work_dir = "build/test/round_trip_test";
69         boost::filesystem::create_directory (work_dir);
70
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/32x32_red_square.j2c");
74         for (int i = 0; i < 24; ++i) {
75                 writer->write (j2c.data (), j2c.size ());
76         }
77         writer->finalize ();
78
79         dcp::Key key;
80
81         asset_A->set_key (key);
82
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)));
86         cpl->add (reel);
87
88         /* A KDM using our certificate chain's leaf key pair */
89         dcp::DecryptedKDM kdm_A (
90                 cpl,
91                 key,
92                 dcp::LocalTime ("2013-01-01T00:00:00+00:00"),
93                 dcp::LocalTime ("2013-01-08T00:00:00+00:00"),
94                 "libdcp",
95                 "test",
96                 "2012-07-17T04:45:18+00:00"
97                 );
98
99         boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
100
101         kdm_A.encrypt(signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml (kdm_file);
102
103         /* Reload the KDM, using our private key to decrypt it */
104         dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (dcp::file_to_string (kdm_file)), signer->key().get ());
105
106         /* Check that the decrypted KDMKeys are the same as the ones we started with */
107         BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
108         list<dcp::DecryptedKDMKey> keys_A = kdm_A.keys ();
109         list<dcp::DecryptedKDMKey> keys_B = kdm_B.keys ();
110         list<dcp::DecryptedKDMKey>::const_iterator i = keys_A.begin();
111         list<dcp::DecryptedKDMKey>::const_iterator j = keys_B.begin();
112         while (i != keys_A.end ()) {
113                 BOOST_CHECK (*i == *j);
114                 ++i;
115                 ++j;
116         }
117
118         /* Reload the picture asset */
119         shared_ptr<dcp::MonoPictureAsset> asset_B (
120                 new dcp::MonoPictureAsset (work_dir / "video.mxf")
121                 );
122
123         BOOST_CHECK (!kdm_B.keys().empty ());
124         asset_B->set_key (kdm_B.keys().front().key());
125
126         shared_ptr<dcp::OpenJPEGImage> xyz_A = asset_A->start_read()->get_frame(0)->xyz_image ();
127         shared_ptr<dcp::OpenJPEGImage> xyz_B = asset_B->start_read()->get_frame(0)->xyz_image ();
128
129         scoped_array<uint8_t> frame_A (new uint8_t[xyz_A->size().width * xyz_A->size().height * 4]);
130         dcp::xyz_to_rgba (xyz_A, dcp::ColourConversion::srgb_to_xyz(), frame_A.get(), xyz_A->size().width * 4);
131
132         scoped_array<uint8_t> frame_B (new uint8_t[xyz_B->size().width * xyz_B->size().height * 4]);
133         dcp::xyz_to_rgba (xyz_B, dcp::ColourConversion::srgb_to_xyz(), frame_B.get(), xyz_B->size().width * 4);
134
135         BOOST_CHECK_EQUAL (xyz_A->size().width, xyz_B->size().width);
136         BOOST_CHECK_EQUAL (xyz_A->size().height, xyz_B->size().height);
137         BOOST_CHECK_EQUAL (memcmp (frame_A.get(), frame_B.get(), xyz_A->size().width * xyz_A->size().height * 4), 0);
138 }