253efac17e31d52cd46a9743e65c96f8ec35dc7a
[libdcp.git] / test / decryption_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 "kdm.h"
21 #include "picture_frame.h"
22 #include "argb_frame.h"
23
24 using boost::dynamic_pointer_cast;
25
26 static shared_ptr<const libdcp::ARGBFrame>
27 get_frame (libdcp::DCP const & dcp)
28 {
29         shared_ptr<const libdcp::Reel> reel = dcp.cpls().front()->reels().front ();
30         shared_ptr<const libdcp::PictureAsset> picture = reel->main_picture ();
31         BOOST_CHECK (picture);
32
33         shared_ptr<const libdcp::MonoPictureAsset> mono_picture = dynamic_pointer_cast<const libdcp::MonoPictureAsset> (picture);
34         shared_ptr<const libdcp::MonoPictureFrame> j2k_frame = mono_picture->get_frame (0);
35         return j2k_frame->argb_frame ();
36 }
37
38 /** Decrypt an encrypted test DCP and check that its first frame is the same as the unencrypted version */
39 BOOST_AUTO_TEST_CASE (decryption_test)
40 {
41         boost::filesystem::path plaintext_path = test_corpus;
42         plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
43         libdcp::DCP plaintext (plaintext_path.string ());
44         plaintext.read ();
45         BOOST_CHECK_EQUAL (plaintext.encrypted (), false);
46
47         boost::filesystem::path encrypted_path = test_corpus;
48         encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
49         libdcp::DCP encrypted (encrypted_path.string ());
50         encrypted.read ();
51         BOOST_CHECK_EQUAL (encrypted.encrypted (), true);
52
53         libdcp::KDM kdm (
54                 "test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml",
55                 "test/data/private.key"
56                 );
57
58         encrypted.add_kdm (kdm);
59
60         shared_ptr<const libdcp::ARGBFrame> plaintext_frame = get_frame (plaintext);
61         shared_ptr<const libdcp::ARGBFrame> encrypted_frame = get_frame (encrypted);
62
63         /* Check that plaintext and encrypted are the same */
64         BOOST_CHECK_EQUAL (plaintext_frame->stride(), encrypted_frame->stride());
65         BOOST_CHECK_EQUAL (plaintext_frame->size().width, encrypted_frame->size().width);
66         BOOST_CHECK_EQUAL (plaintext_frame->size().height, encrypted_frame->size().height);
67         BOOST_CHECK_EQUAL (memcmp (plaintext_frame->data(), encrypted_frame->data(), plaintext_frame->stride() * plaintext_frame->size().height), 0);
68 }
69
70 /** Load in a KDM that didn't work at first */
71 BOOST_AUTO_TEST_CASE (failing_kdm_test)
72 {
73         libdcp::KDM kdm (
74                 "test/data/target.pem.crt.de5d4eba-e683-41ca-bdda-aa4ad96af3f4.kdm.xml",
75                 "test/data/private.key"
76                 );
77 }