Try to support different KDM formulations.
[libdcp.git] / test / encryption_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 <boost/test/unit_test.hpp>
21 #include "kdm.h"
22 #include "KM_util.h"
23 #include "metadata.h"
24 #include "certificates.h"
25 #include "dcp.h"
26 #include "signer.h"
27 #include "cpl.h"
28 #include "mono_picture_asset.h"
29 #include "sound_asset.h"
30 #include "reel.h"
31 #include "test.h"
32 #include "signer_chain.h"
33
34 using boost::shared_ptr;
35
36 /* Load a certificate chain from build/test/data/ *.pem and then build
37    an encrypted DCP and a KDM using it.
38 */
39 BOOST_AUTO_TEST_CASE (encryption)
40 {
41         boost::filesystem::remove_all ("build/test/signer");
42         boost::filesystem::create_directory ("build/test/signer");
43         libdcp::make_signer_chain ("build/test/signer", "openssl");
44         
45         Kumu::libdcp_test = true;
46
47         libdcp::MXFMetadata mxf_metadata;
48         mxf_metadata.company_name = "OpenDCP";
49         mxf_metadata.product_name = "OpenDCP";
50         mxf_metadata.product_version = "0.0.25";
51
52         libdcp::XMLMetadata xml_metadata;
53         xml_metadata.issuer = "OpenDCP 0.0.25";
54         xml_metadata.creator = "OpenDCP 0.0.25";
55         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
56         
57         boost::filesystem::remove_all ("build/test/DCP/bar");
58         boost::filesystem::create_directories ("build/test/DCP/bar");
59         libdcp::DCP d ("build/test/DCP/bar");
60
61         /* Use test/ref/crypt so this test is repeatable */
62         libdcp::CertificateChain chain;
63         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("test/ref/crypt/ca.self-signed.pem"))));
64         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("test/ref/crypt/intermediate.signed.pem"))));
65         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (boost::filesystem::path ("test/ref/crypt/leaf.signed.pem"))));
66
67         shared_ptr<libdcp::Signer> signer (
68                 new libdcp::Signer (
69                         chain,
70                         "test/ref/crypt/leaf.key"
71                         )
72                 );
73
74         shared_ptr<libdcp::CPL> cpl (new libdcp::CPL ("build/test/DCP/bar", "A Test DCP", libdcp::FEATURE, 24, 24));
75
76         libdcp::Key key;
77         
78         shared_ptr<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset ("build/test/DCP/bar", "video.mxf"));
79         mp->set_progress (&d.Progress);
80         mp->set_edit_rate (24);
81         mp->set_intrinsic_duration (24);
82         mp->set_duration (24);
83         mp->set_size (libdcp::Size (32, 32));
84         mp->set_metadata (mxf_metadata);
85         mp->set_key (key);
86         mp->create (j2c);
87
88         shared_ptr<libdcp::SoundAsset> ms (new libdcp::SoundAsset ("build/test/DCP/bar", "audio.mxf"));
89         ms->set_progress (&d.Progress);
90         ms->set_edit_rate (24);
91         ms->set_intrinsic_duration (24);
92         mp->set_duration (24);
93         ms->set_channels (2);
94         ms->set_metadata (mxf_metadata);
95         ms->set_key (key);
96         ms->create (wav);
97         
98         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (mp, ms, shared_ptr<libdcp::SubtitleAsset> ())));
99         d.add_cpl (cpl);
100
101         d.write_xml (false, xml_metadata, signer);
102
103         boost::filesystem::path cpl_path = boost::filesystem::path ("build/test/DCP/bar") / (cpl->id() + "_cpl.xml");
104
105         libdcp::KDM kdm (
106                 cpl_path,
107                 signer,
108                 signer->certificates().leaf(),
109                 key,
110                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
111                 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
112                 "libdcp",
113                 "2012-07-17T04:45:18+00:00",
114                 libdcp::KDM::MODIFIED_TRANSITIONAL_1
115                 );
116
117         kdm.as_xml ("build/test/bar.kdm.xml");
118         
119         int r = system (
120                 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/bar.kdm.xml "
121                 "> build/test/xmllint.log 2>&1 < /dev/null"
122                 );
123
124 #ifdef DCPOMATIC_POSIX  
125         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
126 #else
127         BOOST_CHECK_EQUAL (r, 0);
128 #endif  
129                 
130         r = system ("xmlsec1 verify "
131                 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
132                 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
133                 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
134                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
135                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
136                     "build/test/bar.kdm.xml > build/test/xmlsec1.log 2>&1 < /dev/null");
137         
138 #ifdef DCPOMATIC_POSIX  
139         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
140 #else
141         BOOST_CHECK_EQUAL (r, 0);
142 #endif  
143 }