Move verify API away from strings towards error codes.
[libdcp.git] / test / verify_test.cc
index a036073050688d6d742ac5bd8998fbe2cc764f34..a4ed162d562bfa5c0d9a3ceeb82b40051a1dd8a9 100644 (file)
 */
 
 #include "verify.h"
+#include "util.h"
+#include "compose.hpp"
 #include <boost/test/unit_test.hpp>
+#include <boost/algorithm/string.hpp>
 #include <cstdio>
+#include <iostream>
 
 using std::list;
 using std::pair;
@@ -56,18 +60,29 @@ progress (float)
 
 }
 
-BOOST_AUTO_TEST_CASE (verify_test1)
+static vector<boost::filesystem::path>
+setup (int n)
 {
-       boost::filesystem::remove_all ("build/test/verify_test1");
-       boost::filesystem::create_directory ("build/test/verify_test1");
+       boost::filesystem::remove_all (dcp::String::compose("build/test/verify_test%1", n));
+       boost::filesystem::create_directory (dcp::String::compose("build/test/verify_test%1", n));
        for (boost::filesystem::directory_iterator i("test/ref/DCP/dcp_test1"); i != boost::filesystem::directory_iterator(); ++i) {
-               boost::filesystem::copy_file (i->path(), "build/test/verify_test1" / i->path().filename());
+               boost::filesystem::copy_file (i->path(), dcp::String::compose("build/test/verify_test%1", n) / i->path().filename());
        }
 
        vector<boost::filesystem::path> directories;
-       directories.push_back ("build/test/verify_test1");
+       directories.push_back (dcp::String::compose("build/test/verify_test%1", n));
+       return directories;
+
+}
+
+/* Check DCP as-is (should be OK) */
+BOOST_AUTO_TEST_CASE (verify_test1)
+{
+       vector<boost::filesystem::path> directories = setup (1);
        list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
 
+       boost::filesystem::path const cpl_file = "build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
+
        list<pair<string, optional<boost::filesystem::path> > >::const_iterator st = stages.begin();
        BOOST_CHECK_EQUAL (st->first, "Checking DCP");
        BOOST_REQUIRE (st->second);
@@ -75,7 +90,7 @@ BOOST_AUTO_TEST_CASE (verify_test1)
        ++st;
        BOOST_CHECK_EQUAL (st->first, "Checking CPL");
        BOOST_REQUIRE (st->second);
-       BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical("build/test/verify_test1/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml"));
+       BOOST_CHECK_EQUAL (st->second.get(), boost::filesystem::canonical(cpl_file));
        ++st;
        BOOST_CHECK_EQUAL (st->first, "Checking reel");
        BOOST_REQUIRE (!st->second);
@@ -91,24 +106,99 @@ BOOST_AUTO_TEST_CASE (verify_test1)
        BOOST_REQUIRE (st == stages.end());
 
        BOOST_CHECK_EQUAL (notes.size(), 0);
+}
+
+/* Corrupt the MXFs and check that this is spotted */
+BOOST_AUTO_TEST_CASE (verify_test2)
+{
+       vector<boost::filesystem::path> directories = setup (2);
 
-       FILE* mod = fopen("build/test/verify_test1/video.mxf", "r+b");
+       FILE* mod = fopen("build/test/verify_test2/video.mxf", "r+b");
        BOOST_REQUIRE (mod);
        fseek (mod, 4096, SEEK_SET);
        int x = 42;
        fwrite (&x, sizeof(x), 1, mod);
        fclose (mod);
 
-       mod = fopen("build/test/verify_test1/audio.mxf", "r+b");
+       mod = fopen("build/test/verify_test2/audio.mxf", "r+b");
        BOOST_REQUIRE (mod);
        fseek (mod, 4096, SEEK_SET);
        BOOST_REQUIRE (fwrite (&x, sizeof(x), 1, mod) == 1);
        fclose (mod);
 
-       notes = dcp::verify (directories, &stage, &progress);
-       BOOST_CHECK_EQUAL (notes.size(), 2);
+       list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
+
+       BOOST_REQUIRE_EQUAL (notes.size(), 2);
        BOOST_CHECK_EQUAL (notes.front().type(), dcp::VerificationNote::VERIFY_ERROR);
-       BOOST_CHECK_EQUAL (notes.front().note(), "Picture asset hash is incorrect");
+       BOOST_CHECK_EQUAL (notes.front().code(), dcp::VerificationNote::PICTURE_HASH_INCORRECT);
        BOOST_CHECK_EQUAL (notes.back().type(), dcp::VerificationNote::VERIFY_ERROR);
-       BOOST_CHECK_EQUAL (notes.back().note(), "Sound asset hash is incorrect");
+       BOOST_CHECK_EQUAL (notes.back().code(), dcp::VerificationNote::SOUND_HASH_INCORRECT);
+}
+
+/* Corrupt the hashes in the PKL and check that the disagreement between CPL and PKL is spotted */
+BOOST_AUTO_TEST_CASE (verify_test3)
+{
+       vector<boost::filesystem::path> directories = setup (3);
+
+       boost::filesystem::path const pkl_file = "build/test/verify_test3/pkl_74e205d0-d145-42d2-8c49-7b55d058ca55.xml";
+
+       string pkl = dcp::file_to_string (pkl_file);
+       boost::algorithm::replace_all (pkl, "<Hash>", "<Hash>x");
+       FILE* f = fopen(pkl_file.string().c_str(), "w");
+       fwrite(pkl.c_str(), pkl.length(), 1, f);
+       fclose(f);
+
+       list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
+
+       BOOST_REQUIRE_EQUAL (notes.size(), 3);
+       list<dcp::VerificationNote>::const_iterator i = notes.begin();
+       BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
+       BOOST_CHECK_EQUAL (i->code(), dcp::VerificationNote::CPL_HASH_INCORRECT);
+       ++i;
+       BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
+       BOOST_CHECK_EQUAL (i->code(), dcp::VerificationNote::PKL_CPL_PICTURE_HASHES_DISAGREE);
+       ++i;
+       BOOST_CHECK_EQUAL (i->type(), dcp::VerificationNote::VERIFY_ERROR);
+       BOOST_CHECK_EQUAL (i->code(), dcp::VerificationNote::PKL_CPL_SOUND_HASHES_DISAGREE);
+       ++i;
+}
+
+/* Corrupt the ContentKind in the CPL */
+BOOST_AUTO_TEST_CASE (verify_test4)
+{
+       vector<boost::filesystem::path> directories = setup (4);
+
+       boost::filesystem::path const cpl_file = "build/test/verify_test4/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
+
+       string cpl = dcp::file_to_string (cpl_file);
+       boost::algorithm::replace_all (cpl, "<ContentKind>", "<ContentKind>x");
+       FILE* f = fopen(cpl_file.string().c_str(), "w");
+       fwrite(cpl.c_str(), cpl.length(), 1, f);
+       fclose(f);
+
+       list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
+
+       BOOST_REQUIRE_EQUAL (notes.size(), 1);
+       BOOST_CHECK_EQUAL (notes.front().code(), dcp::VerificationNote::GENERAL_READ);
+       BOOST_CHECK_EQUAL (*notes.front().note(), "Bad content kind 'xfeature'");
+}
+
+/* FrameRate */
+BOOST_AUTO_TEST_CASE (verify_test5)
+{
+       vector<boost::filesystem::path> directories = setup (5);
+
+       boost::filesystem::path const cpl_file = "build/test/verify_test5/cpl_81fb54df-e1bf-4647-8788-ea7ba154375b.xml";
+
+       string cpl = dcp::file_to_string (cpl_file);
+       boost::algorithm::replace_all (cpl, "<FrameRate>24 1", "<FrameRate>99 1");
+       FILE* f = fopen(cpl_file.string().c_str(), "w");
+       fwrite(cpl.c_str(), cpl.length(), 1, f);
+       fclose(f);
+
+       list<dcp::VerificationNote> notes = dcp::verify (directories, &stage, &progress);
+
+       BOOST_REQUIRE_EQUAL (notes.size(), 2);
+       BOOST_CHECK_EQUAL (notes.front().code(), dcp::VerificationNote::CPL_HASH_INCORRECT);
+       BOOST_CHECK_EQUAL (notes.back().code(), dcp::VerificationNote::INVALID_PICTURE_FRAME_RATE);
 }