Working decryption via KDM.
[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 <tiffio.h>
21 #include "kdm.h"
22 #include "picture_frame.h"
23 #include "argb_frame.h"
24
25 using boost::dynamic_pointer_cast;
26
27 BOOST_AUTO_TEST_CASE (decryption_test)
28 {
29         boost::filesystem::path plaintext_path = test_corpus;
30         plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
31         libdcp::DCP plaintext (plaintext_path.string ());
32         plaintext.read ();
33         BOOST_CHECK_EQUAL (plaintext.encrypted (), false);
34
35         boost::filesystem::path encrypted_path = test_corpus;
36         encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
37         libdcp::DCP encrypted (encrypted_path.string ());
38         encrypted.read ();
39         BOOST_CHECK_EQUAL (encrypted.encrypted (), true);
40
41         libdcp::KDM kdm (
42                 "test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml",
43                 "test/data/private.key"
44                 );
45
46         encrypted.add_kdm (kdm);
47
48         shared_ptr<const libdcp::Reel> encrypted_reel = encrypted.cpls().front()->reels().front ();
49         shared_ptr<const libdcp::PictureAsset> encrypted_picture = encrypted_reel->main_picture ();
50         BOOST_CHECK (encrypted_picture);
51
52         shared_ptr<const libdcp::MonoPictureAsset> encrypted_mono_picture = dynamic_pointer_cast<const libdcp::MonoPictureAsset> (encrypted_picture);
53         shared_ptr<const libdcp::MonoPictureFrame> j2k_frame = encrypted_mono_picture->get_frame (0);
54
55         shared_ptr<const libdcp::ARGBFrame> argb_frame = j2k_frame->argb_frame ();
56
57
58
59         uint8_t* tiff_frame = new uint8_t[1998 * 3 * 1080];
60         
61         uint8_t* t = tiff_frame;
62         uint8_t* r = argb_frame->data ();
63         for (int y = 0; y < 1080; ++y) {
64                 for (int x = 0; x < 1998; ++x) {
65                         /* Our data is first-byte blue, second-byte green, third-byte red, fourth-byte alpha,
66                            so we need to twiddle here.
67                         */
68                         
69                         t[0] = r[2]; // red
70                         t[1] = r[1]; // green
71                         t[2] = r[0]; // blue
72                         t += 3;
73                         r += 4;
74                 }
75         }
76         
77         TIFF* output = TIFFOpen ("foo.tiff", "w");
78         BOOST_CHECK (output);
79         
80         TIFFSetField (output, TIFFTAG_IMAGEWIDTH, 1998);
81         TIFFSetField (output, TIFFTAG_IMAGELENGTH, 1080);
82         TIFFSetField (output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
83         TIFFSetField (output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
84         TIFFSetField (output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
85         TIFFSetField (output, TIFFTAG_BITSPERSAMPLE, 8);
86         TIFFSetField (output, TIFFTAG_SAMPLESPERPIXEL, 3);
87         
88         BOOST_CHECK (TIFFWriteEncodedStrip (output, 0, tiff_frame, 1998 * 1080 * 3));
89         TIFFClose (output);
90
91         delete[] tiff_frame;
92 }