Bump waf to 2.0.27.
[libdcp.git] / test / encryption_test.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "metadata.h"
21 #include "certificate.h"
22 #include "dcp.h"
23 #include "certificate_chain.h"
24 #include "cpl.h"
25 #include "mono_picture_asset.h"
26 #include "picture_asset_writer.h"
27 #include "sound_asset_writer.h"
28 #include "sound_asset.h"
29 #include "reel.h"
30 #include "test.h"
31 #include "file.h"
32 #include "subtitle_asset.h"
33 #include "reel_mono_picture_asset.h"
34 #include "reel_sound_asset.h"
35 #include "encrypted_kdm.h"
36 #include "decrypted_kdm.h"
37 #include <asdcp/KM_util.h>
38 #include <sndfile.h>
39 #include <boost/test/unit_test.hpp>
40 #include <boost/shared_ptr.hpp>
41
42 using std::vector;
43 using std::string;
44 using boost::shared_ptr;
45
46 /** Load a certificate chain from build/test/data/ *.pem and then build
47  *  an encrypted DCP and a KDM using it.
48  */
49 BOOST_AUTO_TEST_CASE (encryption_test)
50 {
51         boost::filesystem::remove_all ("build/test/signer");
52         boost::filesystem::create_directory ("build/test/signer");
53
54         Kumu::cth_test = true;
55
56         dcp::MXFMetadata mxf_metadata;
57         mxf_metadata.company_name = "OpenDCP";
58         mxf_metadata.product_name = "OpenDCP";
59         mxf_metadata.product_version = "0.0.25";
60
61         dcp::XMLMetadata xml_metadata;
62         xml_metadata.annotation_text = "A Test DCP";
63         xml_metadata.issuer = "OpenDCP 0.0.25";
64         xml_metadata.creator = "OpenDCP 0.0.25";
65         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
66
67         boost::filesystem::remove_all ("build/test/DCP/encryption_test");
68         boost::filesystem::create_directories ("build/test/DCP/encryption_test");
69         dcp::DCP d ("build/test/DCP/encryption_test");
70
71         /* Use test/ref/crypt so this test is repeatable */
72         shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain ());
73         signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
74         signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
75         signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
76         signer->set_key (dcp::file_to_string ("test/ref/crypt/leaf.key"));
77
78         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
79
80         dcp::Key key;
81
82         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
83         mp->set_metadata (mxf_metadata);
84         mp->set_key (key);
85
86         shared_ptr<dcp::PictureAssetWriter> writer = mp->start_write ("build/test/DCP/encryption_test/video.mxf", false);
87         dcp::File j2c ("test/data/32x32_red_square.j2c");
88         for (int i = 0; i < 24; ++i) {
89                 writer->write (j2c.data (), j2c.size ());
90         }
91         writer->finalize ();
92
93         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
94         ms->set_metadata (mxf_metadata);
95         ms->set_key (key);
96         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/encryption_test/audio.mxf");
97
98         SF_INFO info;
99         info.format = 0;
100         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
101         BOOST_CHECK (sndfile);
102         float buffer[4096*6];
103         float* channels[1];
104         channels[0] = buffer;
105         while (1) {
106                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
107                 sound_writer->write (channels, N);
108                 if (N < 4096) {
109                         break;
110                 }
111         }
112
113         sound_writer->finalize ();
114
115         cpl->add (shared_ptr<dcp::Reel> (new dcp::Reel (
116                                                  shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
117                                                  shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
118                                                  shared_ptr<dcp::ReelSubtitleAsset> ()
119                                                  )));
120         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
121         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
122         cpl->set_metadata (xml_metadata);
123
124         d.add (cpl);
125
126         xml_metadata.annotation_text = "Created by libdcp";
127         d.write_xml (dcp::SMPTE, xml_metadata, signer);
128
129         dcp::DecryptedKDM kdm (
130                 cpl,
131                 key,
132                 dcp::LocalTime ("2013-01-01T00:00:00+00:00"),
133                 dcp::LocalTime ("2017-01-08T00:00:00+00:00"),
134                 "libdcp",
135                 "test",
136                 "2012-07-17T04:45:18+00:00"
137                 );
138
139         kdm.encrypt (signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml ("build/test/encryption_test.kdm.xml");
140
141         int r = system (
142                 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/encryption_test.kdm.xml "
143                 "> build/test/xmllint.log 2>&1 < /dev/null"
144                 );
145
146 #ifdef LIBDCP_POSIX
147         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
148 #else
149         BOOST_CHECK_EQUAL (r, 0);
150 #endif
151
152         r = system ("xmlsec1 verify "
153                 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
154                 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
155                 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
156                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
157                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
158                     "build/test/encryption_test.kdm.xml > build/test/xmlsec1.log 2>&1 < /dev/null");
159
160 #ifdef LIBDCP_POSIX
161         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
162 #else
163         BOOST_CHECK_EQUAL (r, 0);
164 #endif
165 }