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