Remove submodule of test/private as submodules don't seem to work too well with git...
[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 <boost/test/unit_test.hpp>
21 #include <boost/filesystem.hpp>
22 #include "mono_picture_asset_writer.h"
23 #include "mono_picture_asset.h"
24 #include "KM_util.h"
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::libdcp_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<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset ("build/test/baz", "video1.mxf"));
52         mp->set_edit_rate (24);
53         mp->set_size (libdcp::Size (32, 32));
54         shared_ptr<libdcp::PictureAssetWriter> writer = mp->start_write (false);
55
56         int written_size = 0;
57         for (int i = 0; i < 24; ++i) {
58                 libdcp::FrameInfo info = writer->write (data, size);
59                 written_size = info.size;
60         }
61
62         writer->finalize ();
63         writer.reset ();
64
65         boost::filesystem::copy_file ("build/test/baz/video1.mxf", "build/test/baz/video2.mxf");
66         boost::filesystem::resize_file ("build/test/baz/video2.mxf", 16384 + 353 * 11);
67
68         {
69                 FILE* f = fopen ("build/test/baz/video2.mxf", "r+");
70                 rewind (f);
71                 char zeros[256];
72                 memset (zeros, 0, 256);
73                 fwrite (zeros, 1, 256, f);
74                 fclose (f);
75         }
76
77 #ifdef LIBDCP_POSIX     
78         Kumu::ResetTestRNG ();
79 #endif  
80
81         mp.reset (new libdcp::MonoPictureAsset ("build/test/baz", "video2.mxf"));
82         mp->set_edit_rate (24);
83         mp->set_size (libdcp::Size (32, 32));
84         writer = mp->start_write (true);
85
86         writer->write (data, size);
87
88         for (int i = 1; i < 4; ++i) {
89                 writer->fake_write (written_size);
90         }
91
92         for (int i = 4; i < 24; ++i) {
93                 writer->write (data, size);
94         }
95         
96         writer->finalize ();
97 }