96563ee81f19837188387b9123b709c42944dbb1
[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 "picture_asset.h"
26 #include "sound_asset.h"
27 #include "reel.h"
28 #include "test.h"
29 #include "cpl.h"
30 #include "picture_frame.h"
31 #include "argb_frame.h"
32
33 using boost::shared_ptr;
34
35 /* Build an encrypted picture MXF and a KDM for it and check that the KDM can be decrypted */
36 BOOST_AUTO_TEST_CASE (round_trip_test)
37 {
38         libdcp::CertificateChain chain;
39         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/ca.self-signed.pem"))));
40         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/intermediate.signed.pem"))));
41         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("build/test/signer/leaf.signed.pem"))));
42
43         shared_ptr<libdcp::Signer> signer (
44                 new libdcp::Signer (
45                         chain,
46                         "test/data/signer.key"
47                         )
48                 );
49
50         boost::filesystem::path work_dir = "build/test/round_trip_test";
51         boost::filesystem::create_directory (work_dir);
52
53         libdcp::MXFMetadata mxf_metadata;
54
55         shared_ptr<libdcp::MonoPictureAsset> asset_A (
56                 new libdcp::MonoPictureAsset (j2c, work_dir, "video.mxf", 0, 24, 24, libdcp::Size (32, 32), false, mxf_metadata)
57                 );
58
59         libdcp::Key key;
60
61         asset_A->set_key (key);
62
63         shared_ptr<libdcp::CPL> cpl (new libdcp::CPL (work_dir, "A Test DCP", libdcp::FEATURE, 24, 24));
64         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (asset_A, shared_ptr<libdcp::SoundAsset> (), shared_ptr<libdcp::SubtitleAsset> ())));
65
66         /* A KDM using our certificate chain's leaf key pair */
67         libdcp::KDM kdm_A (
68                 cpl,
69                 signer,
70                 signer->certificates().leaf(),
71                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
72                 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
73                 "libdcp",
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.as_xml (kdm_file);
80
81         /* Reload the KDM, using our private key to decrypt it */
82         libdcp::KDM kdm_B (kdm_file, "build/test/signer/leaf.key");
83
84         /* Reload the picture MXF */
85         shared_ptr<libdcp::MonoPictureAsset> asset_B (
86                 new libdcp::MonoPictureAsset (work_dir, "video.mxf")
87                 );
88
89         asset_B->set_key (kdm_B.keys().front().key());
90
91         shared_ptr<libdcp::ARGBFrame> frame_A = asset_A->get_frame(0)->argb_frame ();
92         shared_ptr<libdcp::ARGBFrame> frame_B = asset_B->get_frame(0)->argb_frame ();
93         BOOST_CHECK_EQUAL (frame_A->size().width, frame_B->size().width);
94         BOOST_CHECK_EQUAL (frame_A->size().height, frame_B->size().height);
95         BOOST_CHECK_EQUAL (memcmp (frame_A->data(), frame_B->data(), frame_A->size().width * frame_A->size().height), 0);
96 }