Partial tidy-up of KDM classes.
[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_writer.h"
30 #include "sound_mxf.h"
31 #include "reel.h"
32 #include "test.h"
33 #include "file.h"
34 #include "signer_chain.h"
35 #include "subtitle_content.h"
36 #include "reel_mono_picture_asset.h"
37 #include "reel_sound_asset.h"
38 #include <sndfile.h>
39 #include <boost/test/unit_test.hpp>
40 #include <boost/shared_ptr.hpp>
41
42 using boost::shared_ptr;
43
44 /* Load a certificate chain from build/test/data/ *.pem and then build
45    an encrypted DCP and a KDM using it.
46 */
47 BOOST_AUTO_TEST_CASE (encryption_test)
48 {
49         boost::filesystem::remove_all ("build/test/signer");
50         boost::filesystem::create_directory ("build/test/signer");
51         dcp::make_signer_chain ("build/test/signer", "openssl");
52         
53         Kumu::libdcp_test = true;
54
55         dcp::MXFMetadata mxf_metadata;
56         mxf_metadata.company_name = "OpenDCP";
57         mxf_metadata.product_name = "OpenDCP";
58         mxf_metadata.product_version = "0.0.25";
59
60         dcp::XMLMetadata xml_metadata;
61         xml_metadata.issuer = "OpenDCP 0.0.25";
62         xml_metadata.creator = "OpenDCP 0.0.25";
63         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
64         
65         boost::filesystem::remove_all ("build/test/DCP/bar");
66         boost::filesystem::create_directories ("build/test/DCP/bar");
67         dcp::DCP d ("build/test/DCP/bar");
68
69         /* Use test/ref/crypt so this test is repeatable */
70         dcp::CertificateChain chain;
71         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("test/ref/crypt/ca.self-signed.pem"))));
72         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("test/ref/crypt/intermediate.signed.pem"))));
73         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("test/ref/crypt/leaf.signed.pem"))));
74
75         shared_ptr<dcp::Signer> signer (
76                 new dcp::Signer (
77                         chain,
78                         "test/ref/crypt/leaf.key"
79                         )
80                 );
81
82         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
83
84         dcp::Key key;
85         
86         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
87         mp->set_metadata (mxf_metadata);
88         mp->set_key (key);
89
90         shared_ptr<dcp::PictureMXFWriter> writer = mp->start_write ("build/test/DCP/bar/video.mxf", dcp::SMPTE, false);
91         dcp::File j2c ("test/data/32x32_red_square.j2c");
92         for (int i = 0; i < 24; ++i) {
93                 writer->write (j2c.data (), j2c.size ());
94         }
95         writer->finalize ();
96
97         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
98         ms->set_key (key);
99         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/bar/audio.mxf", dcp::SMPTE);
100         
101         SF_INFO info;
102         info.format = 0;
103         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
104         BOOST_CHECK (sndfile);
105         float buffer[4096*6];
106         float* channels[1];
107         channels[0] = buffer;
108         while (1) {
109                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
110                 sound_writer->write (channels, N);
111                 if (N < 4096) {
112                         break;
113                 }
114         }
115         
116         sound_writer->finalize ();      
117
118         cpl->add (shared_ptr<dcp::Reel> (new dcp::Reel (
119                                                  shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
120                                                  shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
121                                                  shared_ptr<dcp::ReelSubtitleAsset> ()
122                                                  )));
123         d.add (cpl);
124         d.write_xml (dcp::SMPTE, xml_metadata, signer);
125
126         dcp::KDM kdm (
127                 cpl,
128                 signer,
129                 signer->certificates().leaf(),
130                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
131                 boost::posix_time::time_from_string ("2017-01-08 00:00:00"),
132                 "libdcp",
133                 "2012-07-17T04:45:18+00:00"
134                 );
135
136         kdm.as_xml ("build/test/bar.kdm.xml");
137         
138         int r = system (
139                 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/bar.kdm.xml "
140                 "> build/test/xmllint.log 2>&1 < /dev/null"
141                 );
142
143 #ifdef DCPOMATIC_POSIX  
144         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
145 #else
146         BOOST_CHECK_EQUAL (r, 0);
147 #endif  
148                 
149         r = system ("xmlsec1 verify "
150                 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
151                 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
152                 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
153                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
154                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
155                     "build/test/bar.kdm.xml > build/test/xmlsec1.log 2>&1 < /dev/null");
156         
157 #ifdef DCPOMATIC_POSIX  
158         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
159 #else
160         BOOST_CHECK_EQUAL (r, 0);
161 #endif  
162 }