Fix failure to verify when the XSD/DTD path has a space in it.
[libdcp.git] / test / recovery_test.cc
1 /*
2     Copyright (C) 2012-2019 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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "mono_picture_asset_writer.h"
35 #include "mono_picture_asset.h"
36 #include <asdcp/KM_util.h>
37 #include <boost/test/unit_test.hpp>
38 #include <boost/filesystem.hpp>
39
40 using std::string;
41 using boost::shared_ptr;
42
43 /** Check that recovery from a partially-written MXF works */
44 BOOST_AUTO_TEST_CASE (recovery)
45 {
46         Kumu::cth_test = true;
47
48         string const picture = "test/data/32x32_red_square.j2c";
49         int const size = boost::filesystem::file_size (picture);
50         uint8_t* data = new uint8_t[size];
51         {
52                 FILE* f = fopen (picture.c_str(), "rb");
53                 BOOST_CHECK (f);
54                 fread (data, 1, size, f);
55                 fclose (f);
56         }
57
58 #ifdef LIBDCP_POSIX
59         /* XXX: fix this posix-only stuff */
60         Kumu::ResetTestRNG ();
61 #endif
62
63         boost::filesystem::remove_all ("build/test/baz");
64         boost::filesystem::create_directories ("build/test/baz");
65         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
66         shared_ptr<dcp::PictureAssetWriter> writer = mp->start_write ("build/test/baz/video1.mxf", false);
67
68         int written_size = 0;
69         for (int i = 0; i < 24; ++i) {
70                 dcp::FrameInfo info = writer->write (data, size);
71                 BOOST_CHECK_EQUAL (info.hash, "cb90485a97ea5f7555cedc8a7afd473b");
72                 written_size = info.size;
73         }
74
75         writer->finalize ();
76         writer.reset ();
77
78         boost::filesystem::copy_file ("build/test/baz/video1.mxf", "build/test/baz/video2.mxf");
79         boost::filesystem::resize_file ("build/test/baz/video2.mxf", 16384 + 353 * 11);
80
81         {
82                 FILE* f = fopen ("build/test/baz/video2.mxf", "rb+");
83                 rewind (f);
84                 char zeros[256];
85                 memset (zeros, 0, 256);
86                 fwrite (zeros, 1, 256, f);
87                 fclose (f);
88         }
89
90 #ifdef LIBDCP_POSIX
91         Kumu::ResetTestRNG ();
92 #endif
93
94         mp.reset (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
95         writer = mp->start_write ("build/test/baz/video2.mxf", true);
96
97         writer->write (data, size);
98
99         for (int i = 1; i < 4; ++i) {
100                 writer->fake_write (written_size);
101         }
102
103         for (int i = 4; i < 24; ++i) {
104                 writer->write (data, size);
105         }
106
107         writer->finalize ();
108 }