c3f6ef7281140769220ab20489d190a44869942c
[dcpomatic.git] / test / zipper_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  test/zipper_test.cc
22  *  @brief Test Zipper class.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/zipper.h"
27 #include "lib/exceptions.h"
28 #include "test.h"
29 #include <dcp/util.h>
30 #include <boost/test/unit_test.hpp>
31 #include <boost/filesystem.hpp>
32
33 /** Basic test of Zipper working normally */
34 BOOST_AUTO_TEST_CASE (zipper_test1)
35 {
36         boost::system::error_code ec;
37         boost::filesystem::remove ("build/test/zipped.zip", ec);
38
39         Zipper zipper ("build/test/zipped.zip");
40         zipper.add ("foo.txt", "1234567890");
41         zipper.add ("bar.txt", "xxxxxxCCCCbbbbbbb1");
42         zipper.close ();
43
44         boost::filesystem::remove_all ("build/test/zipper_test1", ec);
45 #ifdef DCPOMATIC_WINDOWS
46         /* unzip on windows crashes every so often (with a return code -1073740940, for some reason)
47          * so try using the built-in tar which can unzip things.
48          */
49         boost::filesystem::create_directories ("build/test/zipper_test1");
50         int const r = system ("tar -xf build\\test\\zipped.zip -C build\\test\\zipper_test1");
51 #else
52         int const r = system ("unzip build/test/zipped.zip -d build/test/zipper_test1");
53 #endif
54         BOOST_REQUIRE_EQUAL (r, 0);
55
56         BOOST_CHECK_EQUAL (dcp::file_to_string("build/test/zipper_test1/foo.txt"), "1234567890");
57         BOOST_CHECK_EQUAL (dcp::file_to_string("build/test/zipper_test1/bar.txt"), "xxxxxxCCCCbbbbbbb1");
58 }
59
60
61 /** Test failure when trying to overwrite a file */
62 BOOST_AUTO_TEST_CASE (zipper_test2, * boost::unit_test::depends_on("zipper_test1"))
63 {
64         BOOST_CHECK_THROW (Zipper("build/test/zipped.zip"), FileError);
65 }
66