Split test file up a bit.
[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 /* Check that recovery from a partially-written MXF works */
21 BOOST_AUTO_TEST_CASE (recovery)
22 {
23         Kumu::libdcp_test = true;
24
25         string const picture = "test/data/32x32_red_square.j2c";
26         int const size = boost::filesystem::file_size (picture);
27         uint8_t* data = new uint8_t[size];
28         {
29                 FILE* f = fopen (picture.c_str(), "rb");
30                 BOOST_CHECK (f);
31                 fread (data, 1, size, f);
32                 fclose (f);
33         }
34
35 #ifdef LIBDCP_POSIX
36         /* XXX: fix this posix-only stuff */
37         Kumu::ResetTestRNG ();
38 #endif  
39         
40         boost::filesystem::remove_all ("build/test/baz");
41         boost::filesystem::create_directories ("build/test/baz");
42         shared_ptr<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset ("build/test/baz", "video1.mxf", 24, libdcp::Size (32, 32)));
43         shared_ptr<libdcp::MonoPictureAssetWriter> writer = mp->start_write (false);
44
45         int written_size = 0;
46         for (int i = 0; i < 24; ++i) {
47                 libdcp::FrameInfo info = writer->write (data, size);
48                 written_size = info.size;
49         }
50
51         writer->finalize ();
52         writer.reset ();
53
54         boost::filesystem::copy_file ("build/test/baz/video1.mxf", "build/test/baz/video2.mxf");
55         boost::filesystem::resize_file ("build/test/baz/video2.mxf", 16384 + 353 * 11);
56
57         {
58                 FILE* f = fopen ("build/test/baz/video2.mxf", "r+");
59                 rewind (f);
60                 char zeros[256];
61                 memset (zeros, 0, 256);
62                 fwrite (zeros, 1, 256, f);
63                 fclose (f);
64         }
65
66 #ifdef LIBDCP_POSIX     
67         Kumu::ResetTestRNG ();
68 #endif  
69
70         mp.reset (new libdcp::MonoPictureAsset ("build/test/baz", "video2.mxf", 24, libdcp::Size (32, 32)));
71         writer = mp->start_write (true);
72
73         writer->write (data, size);
74
75         for (int i = 1; i < 4; ++i) {
76                 writer->fake_write (written_size);
77         }
78
79         for (int i = 4; i < 24; ++i) {
80                 writer->write (data, size);
81         }
82         
83         writer->finalize ();
84 }