MXF -> Asset in lots of places.
[libdcp.git] / test / decryption_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 "dcp.h"
21 #include "mono_picture_frame.h"
22 #include "cpl.h"
23 #include "decrypted_kdm.h"
24 #include "encrypted_kdm.h"
25 #include "mono_picture_asset.h"
26 #include "reel_picture_asset.h"
27 #include "reel.h"
28 #include "test.h"
29 #include "xyz_image.h"
30 #include "rgb_xyz.h"
31 #include "colour_conversion.h"
32 #include <boost/test/unit_test.hpp>
33 #include <boost/scoped_array.hpp>
34
35 using std::pair;
36 using std::make_pair;
37 using boost::dynamic_pointer_cast;
38 using boost::shared_ptr;
39 using boost::scoped_array;
40
41 pair<uint8_t*, dcp::Size>
42 get_frame (dcp::DCP const & dcp)
43 {
44         shared_ptr<const dcp::Reel> reel = dcp.cpls().front()->reels().front ();
45         shared_ptr<dcp::PictureAsset> picture = reel->main_picture()->asset ();
46         BOOST_CHECK (picture);
47
48         shared_ptr<const dcp::MonoPictureAsset> mono_picture = dynamic_pointer_cast<const dcp::MonoPictureAsset> (picture);
49         shared_ptr<const dcp::MonoPictureFrame> j2k_frame = mono_picture->get_frame (0);
50         shared_ptr<dcp::XYZImage> xyz = j2k_frame->xyz_image();
51
52         uint8_t* argb = new uint8_t[xyz->size().width * xyz->size().height * 4];
53         dcp::xyz_to_rgba (j2k_frame->xyz_image(), dcp::ColourConversion::srgb_to_xyz(), argb);
54         return make_pair (argb, xyz->size ());
55 }
56
57 /** Decrypt an encrypted test DCP and check that its first frame is the same as the unencrypted version */
58 BOOST_AUTO_TEST_CASE (decryption_test)
59 {
60         boost::filesystem::path plaintext_path = private_test;
61         plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
62         dcp::DCP plaintext (plaintext_path.string ());
63         plaintext.read ();
64         BOOST_CHECK_EQUAL (plaintext.encrypted (), false);
65
66         boost::filesystem::path encrypted_path = private_test;
67         encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
68         dcp::DCP encrypted (encrypted_path.string ());
69         encrypted.read ();
70         BOOST_CHECK_EQUAL (encrypted.encrypted (), true);
71
72         dcp::DecryptedKDM kdm (
73                 dcp::EncryptedKDM (
74                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
75                         ),
76                 dcp::file_to_string ("test/data/private.key")
77                 );
78         
79         encrypted.add (kdm);
80
81         pair<uint8_t *, dcp::Size> plaintext_frame = get_frame (plaintext);
82         pair<uint8_t *, dcp::Size> encrypted_frame = get_frame (encrypted);
83
84         /* Check that plaintext and encrypted are the same */
85
86         BOOST_CHECK_EQUAL (plaintext_frame.second, encrypted_frame.second);
87         
88         BOOST_CHECK_EQUAL (
89                 memcmp (
90                         plaintext_frame.first,
91                         encrypted_frame.first,
92                         plaintext_frame.second.width * plaintext_frame.second.height * 4
93                         ),
94                 0
95                 );
96
97         delete[] plaintext_frame.first;
98         delete[] encrypted_frame.first;
99 }
100
101 /** Load in a KDM that didn't work at first */
102 BOOST_AUTO_TEST_CASE (failing_kdm_test)
103 {
104         dcp::DecryptedKDM kdm (
105                 dcp::EncryptedKDM (dcp::file_to_string ("test/data/target.pem.crt.de5d4eba-e683-41ca-bdda-aa4ad96af3f4.kdm.xml")),
106                 dcp::file_to_string ("test/data/private.key")
107                 );
108 }