Quite large reworking of signer/cert handling.
[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 "decrypted_kdm.h"
22 #include "encrypted_kdm.h"
23 #include "signer.h"
24 #include "mono_picture_mxf.h"
25 #include "sound_mxf.h"
26 #include "reel.h"
27 #include "test.h"
28 #include "cpl.h"
29 #include "mono_picture_frame.h"
30 #include "argb_frame.h"
31 #include "certificate_chain.h"
32 #include "mono_picture_mxf_writer.h"
33 #include "reel_picture_asset.h"
34 #include "reel_mono_picture_asset.h"
35 #include "file.h"
36 #include <boost/test/unit_test.hpp>
37 #include <iostream>
38
39 using std::list;
40 using boost::shared_ptr;
41
42 /* Build an encrypted picture MXF and a KDM for it and check that the KDM can be decrypted */
43 BOOST_AUTO_TEST_CASE (round_trip_test)
44 {
45         shared_ptr<dcp::Signer> signer (new dcp::Signer ("openssl"));
46
47         boost::filesystem::path work_dir = "build/test/round_trip_test";
48         boost::filesystem::create_directory (work_dir);
49
50         shared_ptr<dcp::MonoPictureMXF> mxf_A (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
51         shared_ptr<dcp::PictureMXFWriter> writer = mxf_A->start_write (work_dir / "video.mxf", dcp::SMPTE, false);
52         dcp::File j2c ("test/data/32x32_red_square.j2c");
53         for (int i = 0; i < 24; ++i) {
54                 writer->write (j2c.data (), j2c.size ());
55         }
56         writer->finalize ();
57
58         dcp::Key key;
59
60         mxf_A->set_key (key);
61
62         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
63         shared_ptr<dcp::Reel> reel (new dcp::Reel ());
64         reel->add (shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mxf_A, 0)));
65         cpl->add (reel);
66
67         /* A KDM using our certificate chain's leaf key pair */
68         dcp::DecryptedKDM kdm_A (
69                 cpl,
70                 dcp::LocalTime ("2013-01-01T00:00:00+00:00"),
71                 dcp::LocalTime ("2013-01-08T00:00:00+00:00"),
72                 "libdcp",
73                 "test",
74                 "2012-07-17T04:45:18+00:00"
75                 );
76
77         boost::filesystem::path const kdm_file = work_dir / "kdm.xml";
78
79         kdm_A.encrypt(signer, signer->certificates().leaf(), dcp::MODIFIED_TRANSITIONAL_1).as_xml (kdm_file);
80
81         /* Reload the KDM, using our private key to decrypt it */
82         dcp::DecryptedKDM kdm_B (dcp::EncryptedKDM (kdm_file), signer->key ());
83
84         /* Check that the decrypted KDMKeys are the same as the ones we started with */
85         BOOST_CHECK_EQUAL (kdm_A.keys().size(), kdm_B.keys().size());
86         list<dcp::DecryptedKDMKey> keys_A = kdm_A.keys ();
87         list<dcp::DecryptedKDMKey> keys_B = kdm_B.keys ();
88         list<dcp::DecryptedKDMKey>::const_iterator i = keys_A.begin();
89         list<dcp::DecryptedKDMKey>::const_iterator j = keys_B.begin();
90         while (i != keys_A.end ()) {
91                 BOOST_CHECK (*i == *j);
92                 ++i;
93                 ++j;
94         }
95
96         /* Reload the picture MXF */
97         shared_ptr<dcp::MonoPictureMXF> mxf_B (
98                 new dcp::MonoPictureMXF (work_dir / "video.mxf")
99                 );
100
101         BOOST_CHECK (!kdm_B.keys().empty ());
102         mxf_B->set_key (kdm_B.keys().front().key());
103
104         shared_ptr<dcp::ARGBFrame> frame_A = mxf_A->get_frame(0)->argb_frame ();
105         shared_ptr<dcp::ARGBFrame> frame_B = mxf_B->get_frame(0)->argb_frame ();
106         BOOST_CHECK_EQUAL (frame_A->size().width, frame_B->size().width);
107         BOOST_CHECK_EQUAL (frame_A->size().height, frame_B->size().height);
108         BOOST_CHECK_EQUAL (memcmp (frame_A->data(), frame_B->data(), frame_A->size().width * frame_A->size().height), 0);
109 }