Add scope to KeyType tags in KDMs.
[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 <libcxml/cxml.h>
24 #include <libxml++/libxml++.h>
25 #include <boost/test/unit_test.hpp>
26 #include <boost/foreach.hpp>
27
28 using std::list;
29 using boost::shared_ptr;
30
31 /** Check reading and decryption of a KDM */
32 BOOST_AUTO_TEST_CASE (kdm_test)
33 {
34         dcp::DecryptedKDM kdm (
35                 dcp::EncryptedKDM (
36                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
37                         ),
38                 dcp::file_to_string ("test/data/private.key")
39                 );
40
41         list<dcp::DecryptedKDMKey> keys = kdm.keys ();
42
43         BOOST_CHECK_EQUAL (keys.size(), 2);
44
45         BOOST_CHECK_EQUAL (keys.front().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
46         BOOST_CHECK_EQUAL (keys.front().id(), "4ac4f922-8239-4831-b23b-31426d0542c4");
47         BOOST_CHECK_EQUAL (keys.front().key().hex(), "8a2729c3e5b65c45d78305462104c3fb");
48
49         BOOST_CHECK_EQUAL (keys.back().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
50         BOOST_CHECK_EQUAL (keys.back().id(), "73baf5de-e195-4542-ab28-8a465f7d4079");
51         BOOST_CHECK_EQUAL (keys.back().key().hex(), "5327fb7ec2e807bd57059615bf8a169d");
52 }
53
54 /** Check that we can read in a KDM and then write it back out again the same */
55 BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
56 {
57         dcp::EncryptedKDM kdm (
58                 dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
59                 );
60
61         shared_ptr<xmlpp::DomParser> parser (new xmlpp::DomParser ());
62         parser->parse_memory (kdm.as_xml ());
63         parser->get_document()->write_to_file_formatted ("build/kdm.xml", "UTF-8");
64         int const r = system (
65                 "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"
66                 );
67
68 #ifdef LIBDCP_WINDOWS
69         BOOST_CHECK_EQUAL (r, 0);
70 #else
71         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
72 #endif
73 }
74
75 /** Test some of the utility methods of DecryptedKDM */
76 BOOST_AUTO_TEST_CASE (decrypted_kdm_test)
77 {
78         uint8_t* data = new uint8_t[16];
79         uint8_t* p = data;
80         dcp::DecryptedKDM::put_uuid (&p, "8971c838-d0c3-405d-bc57-43afa9d91242");
81
82         BOOST_CHECK_EQUAL (data[0], 0x89);
83         BOOST_CHECK_EQUAL (data[1], 0x71);
84         BOOST_CHECK_EQUAL (data[2], 0xc8);
85         BOOST_CHECK_EQUAL (data[3], 0x38);
86         BOOST_CHECK_EQUAL (data[4], 0xd0);
87         BOOST_CHECK_EQUAL (data[5], 0xc3);
88         BOOST_CHECK_EQUAL (data[6], 0x40);
89         BOOST_CHECK_EQUAL (data[7], 0x5d);
90         BOOST_CHECK_EQUAL (data[8], 0xbc);
91         BOOST_CHECK_EQUAL (data[9], 0x57);
92         BOOST_CHECK_EQUAL (data[10], 0x43);
93         BOOST_CHECK_EQUAL (data[11], 0xaf);
94         BOOST_CHECK_EQUAL (data[12], 0xa9);
95         BOOST_CHECK_EQUAL (data[13], 0xd9);
96         BOOST_CHECK_EQUAL (data[14], 0x12);
97         BOOST_CHECK_EQUAL (data[15], 0x42);
98
99         p = data;
100         BOOST_CHECK_EQUAL (dcp::DecryptedKDM::get_uuid (&p), "8971c838-d0c3-405d-bc57-43afa9d91242");
101
102         delete[] data;
103 }
104
105 /** Check that <KeyType> tags have the scope attribute.
106  *  Wolfgang Woehl believes this is compulsory and I am more-or-less inclined to agree.
107  */
108 BOOST_AUTO_TEST_CASE (kdm_key_type_scope)
109 {
110         dcp::EncryptedKDM kdm (
111                 dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
112                 );
113
114         cxml::Document doc;
115         doc.read_string (kdm.as_xml ());
116
117         list<cxml::NodePtr> typed_key_ids = doc.node_child("AuthenticatedPublic")->
118                 node_child("RequiredExtensions")->
119                 node_child("KDMRequiredExtensions")->
120                 node_child("KeyIdList")->
121                 node_children("TypedKeyId");
122
123         BOOST_FOREACH (cxml::NodePtr i, typed_key_ids) {
124                 BOOST_FOREACH (cxml::NodePtr j, i->node_children("KeyType")) {
125                         BOOST_CHECK (j->string_attribute("scope") == "http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
126                 }
127         }
128 }