Replace std::list with std::vector in the API.
[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 "openjpeg_image.h"
50 #include "rgb_xyz.h"
51 #include "colour_conversion.h"
52 #include <boost/test/unit_test.hpp>
53 #include <boost/scoped_array.hpp>
54 #include <iostream>
55
56 using std::list;
57 using std::vector;
58 using std::string;
59 using std::shared_ptr;
60 using boost::scoped_array;
61
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)
64 {
65         shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain (boost::filesystem::path ("openssl")));
66
67         boost::filesystem::path work_dir = "build/test/round_trip_test";
68         boost::filesystem::create_directory (work_dir);
69
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 ());
75         }
76         writer->finalize ();
77
78         dcp::Key key;
79
80         asset_A->set_key (key);
81
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)));
85         cpl->add (reel);
86
87         dcp::LocalTime start;
88         start.set_year (start.year() + 1);
89         dcp::LocalTime end;
90         end.set_year (end.year() + 2);
91
92         /* A KDM using our certificate chain's leaf key pair */
93         dcp::DecryptedKDM kdm_A (
94                 cpl,
95                 key,
96                 start,
97                 end,
98                 "libdcp",
99                 "test",
100                 "2012-07-17T04:45:18+00:00"
101                 );
102
103         boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
104
105         kdm_A.encrypt(signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml (kdm_file);
106
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 ());
109
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);
118                 ++i;
119                 ++j;
120         }
121
122         /* Reload the picture asset */
123         shared_ptr<dcp::MonoPictureAsset> asset_B (
124                 new dcp::MonoPictureAsset (work_dir / "video.mxf")
125                 );
126
127         BOOST_CHECK (!kdm_B.keys().empty ());
128         asset_B->set_key (kdm_B.keys().front().key());
129
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 ();
132
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);
135
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);
138
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);
142 }