Try to fix build on some platforms.
[libdcp.git] / test / kdm_test.cc
index 4051a44ada972639bddb78883903cd37a9705b15..c007a7fa8ef6ce2bd211a2098f1530ac83a6ddf0 100644 (file)
@@ -1,30 +1,31 @@
 /*
     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
+    libdcp is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <boost/test/unit_test.hpp>
-#include <libxml++/libxml++.h>
 #include "encrypted_kdm.h"
 #include "decrypted_kdm.h"
 #include "util.h"
+#include <libcxml/cxml.h>
+#include <libxml++/libxml++.h>
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
 
 using std::list;
-using std::stringstream;
 using boost::shared_ptr;
 
 /** Check reading and decryption of a KDM */
@@ -38,7 +39,7 @@ BOOST_AUTO_TEST_CASE (kdm_test)
                );
 
        list<dcp::DecryptedKDMKey> keys = kdm.keys ();
-       
+
        BOOST_CHECK_EQUAL (keys.size(), 2);
 
        BOOST_CHECK_EQUAL (keys.front().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
@@ -58,17 +59,70 @@ BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
                );
 
        shared_ptr<xmlpp::DomParser> parser (new xmlpp::DomParser ());
-       stringstream s;
-       s << kdm.as_xml ();
-       parser->parse_stream (s);
+       parser->parse_memory (kdm.as_xml ());
        parser->get_document()->write_to_file_formatted ("build/kdm.xml", "UTF-8");
        int const r = system (
                "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"
                );
 
-#ifdef LIBDCP_WINDOWS  
+#ifdef LIBDCP_WINDOWS
        BOOST_CHECK_EQUAL (r, 0);
-#else  
+#else
        BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
-#endif 
+#endif
+}
+
+/** Test some of the utility methods of DecryptedKDM */
+BOOST_AUTO_TEST_CASE (decrypted_kdm_test)
+{
+       uint8_t* data = new uint8_t[16];
+       uint8_t* p = data;
+       dcp::DecryptedKDM::put_uuid (&p, "8971c838-d0c3-405d-bc57-43afa9d91242");
+
+       BOOST_CHECK_EQUAL (data[0], 0x89);
+       BOOST_CHECK_EQUAL (data[1], 0x71);
+       BOOST_CHECK_EQUAL (data[2], 0xc8);
+       BOOST_CHECK_EQUAL (data[3], 0x38);
+       BOOST_CHECK_EQUAL (data[4], 0xd0);
+       BOOST_CHECK_EQUAL (data[5], 0xc3);
+       BOOST_CHECK_EQUAL (data[6], 0x40);
+       BOOST_CHECK_EQUAL (data[7], 0x5d);
+       BOOST_CHECK_EQUAL (data[8], 0xbc);
+       BOOST_CHECK_EQUAL (data[9], 0x57);
+       BOOST_CHECK_EQUAL (data[10], 0x43);
+       BOOST_CHECK_EQUAL (data[11], 0xaf);
+       BOOST_CHECK_EQUAL (data[12], 0xa9);
+       BOOST_CHECK_EQUAL (data[13], 0xd9);
+       BOOST_CHECK_EQUAL (data[14], 0x12);
+       BOOST_CHECK_EQUAL (data[15], 0x42);
+
+       p = data;
+       BOOST_CHECK_EQUAL (dcp::DecryptedKDM::get_uuid (&p), "8971c838-d0c3-405d-bc57-43afa9d91242");
+
+       delete[] data;
+}
+
+/** Check that <KeyType> tags have the scope attribute.
+ *  Wolfgang Woehl believes this is compulsory and I am more-or-less inclined to agree.
+ */
+BOOST_AUTO_TEST_CASE (kdm_key_type_scope)
+{
+       dcp::EncryptedKDM kdm (
+               dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
+               );
+
+       cxml::Document doc;
+       doc.read_string (kdm.as_xml ());
+
+       list<cxml::NodePtr> typed_key_ids = doc.node_child("AuthenticatedPublic")->
+               node_child("RequiredExtensions")->
+               node_child("KDMRequiredExtensions")->
+               node_child("KeyIdList")->
+               node_children("TypedKeyId");
+
+       BOOST_FOREACH (cxml::NodePtr i, typed_key_ids) {
+               BOOST_FOREACH (cxml::NodePtr j, i->node_children("KeyType")) {
+                       BOOST_CHECK (j->string_attribute("scope") == "http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
+               }
+       }
 }