Merge master.
[libdcp.git] / test / round_trip_test.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "certificates.h"
21 #include "kdm.h"
22 #include "signer.h"
23 #include "mono_picture_mxf.h"
24 #include "sound_mxf.h"
25 #include "reel.h"
26 #include "test.h"
27 #include "cpl.h"
28 #include "mono_picture_frame.h"
29 #include "argb_frame.h"
30 #include "signer_chain.h"
31 #include "mono_picture_mxf_writer.h"
32 #include "reel_picture_asset.h"
33 #include "reel_mono_picture_asset.h"
34 #include "file.h"
35 #include <boost/test/unit_test.hpp>
36 #include <iostream>
37
38 using std::list;
39 using boost::shared_ptr;
40
41 /* Build an encrypted picture MXF and a KDM for it and check that the KDM can be decrypted */
42 BOOST_AUTO_TEST_CASE (round_trip_test)
43 {
44         boost::filesystem::remove_all ("build/test/signer");
45         boost::filesystem::create_directory ("build/test/signer");
46         dcp::make_signer_chain ("build/test/signer", "openssl");
47         
48         dcp::CertificateChain chain;
49         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/ca.self-signed.pem"))));
50         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/intermediate.signed.pem"))));
51         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/leaf.signed.pem"))));
52
53         shared_ptr<dcp::Signer> signer (
54                 new dcp::Signer (
55                         chain,
56                         "test/data/signer.key"
57                         )
58                 );
59
60         boost::filesystem::path work_dir = "build/test/round_trip_test";
61         boost::filesystem::create_directory (work_dir);
62
63         shared_ptr<dcp::MonoPictureMXF> mxf_A (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
64         shared_ptr<dcp::PictureMXFWriter> writer = mxf_A->start_write (work_dir / "video.mxf", dcp::SMPTE, false);
65         dcp::File j2c ("test/data/32x32_red_square.j2c");
66         for (int i = 0; i < 24; ++i) {
67                 writer->write (j2c.data (), j2c.size ());
68         }
69         writer->finalize ();
70
71         dcp::Key key;
72
73         mxf_A->set_key (key);
74
75         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
76         shared_ptr<dcp::Reel> reel (new dcp::Reel ());
77         reel->add (shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mxf_A, 0)));
78         cpl->add (reel);
79
80         /* A KDM using our certificate chain's leaf key pair */
81         dcp::KDM kdm_A (
82                 cpl,
83                 signer,
84                 signer->certificates().leaf(),
85                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
86                 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
87                 "libdcp",
88                 "2012-07-17T04:45:18+00:00"
89                 );
90
91         boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
92
93         kdm_A.as_xml (kdm_file);
94
95         /* Reload the KDM, using our private key to decrypt it */
96         dcp::KDM kdm_B (kdm_file, "build/test/signer/leaf.key");
97
98         /* Check that the decrypted KDMKeys are the same as the ones we started with */
99         BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
100         list<dcp::KDMKey> keys_A = kdm_A.keys ();
101         list<dcp::KDMKey> keys_B = kdm_B.keys ();
102         list<dcp::KDMKey>::const_iterator i = keys_A.begin();
103         list<dcp::KDMKey>::const_iterator j = keys_B.begin();
104         while (i != keys_A.end ()) {
105                 BOOST_CHECK (*i == *j);
106                 ++i;
107                 ++j;
108         }
109
110         /* Reload the picture MXF */
111         shared_ptr<dcp::MonoPictureMXF> mxf_B (
112                 new dcp::MonoPictureMXF (work_dir / "video.mxf")
113                 );
114
115         BOOST_CHECK (!kdm_B.keys().empty ());
116         mxf_B->set_key (kdm_B.keys().front().key());
117
118         shared_ptr<dcp::ARGBFrame> frame_A = mxf_A->get_frame(0)->argb_frame ();
119         shared_ptr<dcp::ARGBFrame> frame_B = mxf_B->get_frame(0)->argb_frame ();
120         BOOST_CHECK_EQUAL (frame_A->size().width, frame_B->size().width);
121         BOOST_CHECK_EQUAL (frame_A->size().height, frame_B->size().height);
122         BOOST_CHECK_EQUAL (memcmp (frame_A->data(), frame_B->data(), frame_A->size().width * frame_A->size().height), 0);
123 }