Untested interop DCP/KDM support.
[libdcp.git] / test / recovery_test.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "picture_asset_writer.h"
21
22 /* Check that recovery from a partially-written MXF works */
23 BOOST_AUTO_TEST_CASE (recovery)
24 {
25         Kumu::libdcp_test = true;
26
27         string const picture = "test/data/32x32_red_square.j2c";
28         int const size = boost::filesystem::file_size (picture);
29         uint8_t* data = new uint8_t[size];
30         {
31                 FILE* f = fopen (picture.c_str(), "rb");
32                 BOOST_CHECK (f);
33                 fread (data, 1, size, f);
34                 fclose (f);
35         }
36
37 #ifdef LIBDCP_POSIX
38         /* XXX: fix this posix-only stuff */
39         Kumu::ResetTestRNG ();
40 #endif  
41         
42         boost::filesystem::remove_all ("build/test/baz");
43         boost::filesystem::create_directories ("build/test/baz");
44         shared_ptr<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset ("build/test/baz", "video1.mxf", 24, libdcp::Size (32, 32)));
45         shared_ptr<libdcp::PictureAssetWriter> writer = mp->start_write (false, false);
46
47         int written_size = 0;
48         for (int i = 0; i < 24; ++i) {
49                 libdcp::FrameInfo info = writer->write (data, size);
50                 written_size = info.size;
51         }
52
53         writer->finalize ();
54         writer.reset ();
55
56         boost::filesystem::copy_file ("build/test/baz/video1.mxf", "build/test/baz/video2.mxf");
57         boost::filesystem::resize_file ("build/test/baz/video2.mxf", 16384 + 353 * 11);
58
59         {
60                 FILE* f = fopen ("build/test/baz/video2.mxf", "r+");
61                 rewind (f);
62                 char zeros[256];
63                 memset (zeros, 0, 256);
64                 fwrite (zeros, 1, 256, f);
65                 fclose (f);
66         }
67
68 #ifdef LIBDCP_POSIX     
69         Kumu::ResetTestRNG ();
70 #endif  
71
72         mp.reset (new libdcp::MonoPictureAsset ("build/test/baz", "video2.mxf", 24, libdcp::Size (32, 32)));
73         writer = mp->start_write (true, false);
74
75         writer->write (data, size);
76
77         for (int i = 1; i < 4; ++i) {
78                 writer->fake_write (written_size);
79         }
80
81         for (int i = 4; i < 24; ++i) {
82                 writer->write (data, size);
83         }
84         
85         writer->finalize ();
86 }