Use locked_sstream. Replace once parse_stream with parse_memory.
[libdcp.git] / test / kdm_test.cc
1 /*
2     Copyright (C) 2013-2014 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 "encrypted_kdm.h"
21 #include "decrypted_kdm.h"
22 #include "util.h"
23 #include <libxml++/libxml++.h>
24 #include <boost/test/unit_test.hpp>
25
26 using std::list;
27 using boost::shared_ptr;
28
29 /** Check reading and decryption of a KDM */
30 BOOST_AUTO_TEST_CASE (kdm_test)
31 {
32         dcp::DecryptedKDM kdm (
33                 dcp::EncryptedKDM (
34                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
35                         ),
36                 dcp::file_to_string ("test/data/private.key")
37                 );
38
39         list<dcp::DecryptedKDMKey> keys = kdm.keys ();
40
41         BOOST_CHECK_EQUAL (keys.size(), 2);
42
43         BOOST_CHECK_EQUAL (keys.front().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
44         BOOST_CHECK_EQUAL (keys.front().id(), "4ac4f922-8239-4831-b23b-31426d0542c4");
45         BOOST_CHECK_EQUAL (keys.front().key().hex(), "8a2729c3e5b65c45d78305462104c3fb");
46
47         BOOST_CHECK_EQUAL (keys.back().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
48         BOOST_CHECK_EQUAL (keys.back().id(), "73baf5de-e195-4542-ab28-8a465f7d4079");
49         BOOST_CHECK_EQUAL (keys.back().key().hex(), "5327fb7ec2e807bd57059615bf8a169d");
50 }
51
52 /** Check that we can read in a KDM and then write it back out again the same */
53 BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
54 {
55         dcp::EncryptedKDM kdm (
56                 dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
57                 );
58
59         shared_ptr<xmlpp::DomParser> parser (new xmlpp::DomParser ());
60         parser->parse_memory (kdm.as_xml ());
61         parser->get_document()->write_to_file_formatted ("build/kdm.xml", "UTF-8");
62         int const r = system (
63                 "xmldiff -c test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml build/kdm.xml"
64                 );
65
66 #ifdef LIBDCP_WINDOWS
67         BOOST_CHECK_EQUAL (r, 0);
68 #else
69         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
70 #endif
71 }