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