Various test fixes.
[libdcp.git] / test / encryption_test.cc
1 /*
2     Copyright (C) 2013-2014 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 "kdm.h"
21 #include "KM_util.h"
22 #include "metadata.h"
23 #include "certificates.h"
24 #include "dcp.h"
25 #include "signer.h"
26 #include "cpl.h"
27 #include "mono_picture_mxf.h"
28 #include "picture_mxf_writer.h"
29 #include "sound_mxf.h"
30 #include "reel.h"
31 #include "test.h"
32 #include "file.h"
33 #include "signer_chain.h"
34 #include "subtitle_content.h"
35 #include "reel_mono_picture_asset.h"
36 #include "reel_sound_asset.h"
37 #include <boost/test/unit_test.hpp>
38 #include <boost/shared_ptr.hpp>
39
40 using boost::shared_ptr;
41
42 /* Load a certificate chain from build/test/data/ *.pem and then build
43    an encrypted DCP and a KDM using it.
44 */
45 BOOST_AUTO_TEST_CASE (encryption_test)
46 {
47         boost::filesystem::remove_all ("build/test/signer");
48         boost::filesystem::create_directory ("build/test/signer");
49         dcp::make_signer_chain ("build/test/signer", "openssl");
50         
51         Kumu::libdcp_test = true;
52
53         dcp::MXFMetadata mxf_metadata;
54         mxf_metadata.company_name = "OpenDCP";
55         mxf_metadata.product_name = "OpenDCP";
56         mxf_metadata.product_version = "0.0.25";
57
58         dcp::XMLMetadata xml_metadata;
59         xml_metadata.issuer = "OpenDCP 0.0.25";
60         xml_metadata.creator = "OpenDCP 0.0.25";
61         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
62         
63         boost::filesystem::remove_all ("build/test/DCP/bar");
64         boost::filesystem::create_directories ("build/test/DCP/bar");
65         dcp::DCP d ("build/test/DCP/bar");
66
67         dcp::CertificateChain chain;
68         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/ca.self-signed.pem"))));
69         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/intermediate.signed.pem"))));
70         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/leaf.signed.pem"))));
71
72         shared_ptr<dcp::Signer> signer (
73                 new dcp::Signer (
74                         chain,
75                         "build/test/signer/leaf.key"
76                         )
77                 );
78
79         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
80
81         dcp::Key key;
82         
83         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
84         mp->set_progress (&d.Progress);
85         mp->set_metadata (mxf_metadata);
86         mp->set_key (key);
87
88         shared_ptr<dcp::PictureMXFWriter> writer = mp->start_write ("build/test/DCP/bar/video.mxf", dcp::SMPTE, false);
89         dcp::File j2c ("test/data/32x32_red_square.j2c");
90         for (int i = 0; i < 24; ++i) {
91                 writer->write (j2c.data (), j2c.size ());
92         }
93         writer->finalize ();
94
95         cpl->add (shared_ptr<dcp::Reel> (new dcp::Reel (
96                                                  shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
97                                                  shared_ptr<dcp::ReelSoundAsset> (),
98                                                  shared_ptr<dcp::ReelSubtitleAsset> ()
99                                                  )));
100         d.add (cpl);
101         d.write_xml (dcp::SMPTE, xml_metadata, signer);
102
103         dcp::KDM kdm (
104                 cpl,
105                 signer,
106                 signer->certificates().leaf(),
107                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
108                 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
109                 "libdcp",
110                 "2012-07-17T04:45:18+00:00"
111                 );
112
113         kdm.as_xml ("build/test/bar.kdm.xml");
114         system ("xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/bar.kdm.xml");
115         system ("xmlsec1 verify "
116                 "--pubkey-cert-pem build/test/signer/leaf.signed.pem "
117                 "--trusted-pem build/test/signer/intermediate.signed.pem "
118                 "--trusted-pem build/test/signer/ca.self-signed.pem "
119                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
120                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
121                 "build/test/bar.kdm.xml");
122 }