Allow reading of certificate chains from strings.
[libdcp.git] / test / recovery_test.cc
1 /*
2     Copyright (C) 2012-2013 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 "mono_picture_asset_writer.h"
21 #include "mono_picture_asset.h"
22 #include <asdcp/KM_util.h>
23 #include <boost/test/unit_test.hpp>
24 #include <boost/filesystem.hpp>
25
26 using std::string;
27 using boost::shared_ptr;
28
29 /** Check that recovery from a partially-written MXF works */
30 BOOST_AUTO_TEST_CASE (recovery)
31 {
32         Kumu::cth_test = true;
33
34         string const picture = "test/data/32x32_red_square.j2c";
35         int const size = boost::filesystem::file_size (picture);
36         uint8_t* data = new uint8_t[size];
37         {
38                 FILE* f = fopen (picture.c_str(), "rb");
39                 BOOST_CHECK (f);
40                 fread (data, 1, size, f);
41                 fclose (f);
42         }
43
44 #ifdef LIBDCP_POSIX
45         /* XXX: fix this posix-only stuff */
46         Kumu::ResetTestRNG ();
47 #endif
48
49         boost::filesystem::remove_all ("build/test/baz");
50         boost::filesystem::create_directories ("build/test/baz");
51         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
52         shared_ptr<dcp::PictureAssetWriter> writer = mp->start_write ("build/test/baz/video1.mxf", dcp::SMPTE, false);
53
54         int written_size = 0;
55         for (int i = 0; i < 24; ++i) {
56                 dcp::FrameInfo info = writer->write (data, size);
57                 BOOST_CHECK_EQUAL (info.hash, "cb90485a97ea5f7555cedc8a7afd473b");
58                 written_size = info.size;
59         }
60
61         writer->finalize ();
62         writer.reset ();
63
64         boost::filesystem::copy_file ("build/test/baz/video1.mxf", "build/test/baz/video2.mxf");
65         boost::filesystem::resize_file ("build/test/baz/video2.mxf", 16384 + 353 * 11);
66
67         {
68                 FILE* f = fopen ("build/test/baz/video2.mxf", "rb+");
69                 rewind (f);
70                 char zeros[256];
71                 memset (zeros, 0, 256);
72                 fwrite (zeros, 1, 256, f);
73                 fclose (f);
74         }
75
76 #ifdef LIBDCP_POSIX
77         Kumu::ResetTestRNG ();
78 #endif
79
80         mp.reset (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
81         writer = mp->start_write ("build/test/baz/video2.mxf", dcp::SMPTE, true);
82
83         writer->write (data, size);
84
85         for (int i = 1; i < 4; ++i) {
86                 writer->fake_write (written_size);
87         }
88
89         for (int i = 4; i < 24; ++i) {
90                 writer->write (data, size);
91         }
92
93         writer->finalize ();
94 }