Supporters update.
[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/filesystem.h>
32 #include <dcp/util.h>
33 #include <boost/test/unit_test.hpp>
34 #include <boost/filesystem.hpp>
35
36
37 /** Basic test of Zipper working normally */
38 BOOST_AUTO_TEST_CASE (zipper_test1)
39 {
40         boost::system::error_code ec;
41         boost::filesystem::remove ("build/test/zipped.zip", ec);
42
43         Zipper zipper ("build/test/zipped.zip");
44         zipper.add ("foo.txt", "1234567890");
45         zipper.add ("bar.txt", "xxxxxxCCCCbbbbbbb1");
46         zipper.close ();
47
48         /* Make sure we aren't in a UNC current working directory otherwise the use of cmd.exe
49          * in system() below will fail.
50          */
51         boost::filesystem::current_path(dcp::filesystem::unfix_long_path(boost::filesystem::current_path()));
52
53         boost::filesystem::remove_all ("build/test/zipper_test1", ec);
54 #ifdef DCPOMATIC_WINDOWS
55         /* unzip on windows crashes every so often (with a return code -1073740940, for some reason)
56          * so try using the built-in tar which can unzip things.
57          */
58         boost::filesystem::create_directories ("build/test/zipper_test1");
59         int const r = system ("tar -xf build\\test\\zipped.zip -C build\\test\\zipper_test1");
60 #else
61         int const r = system ("unzip build/test/zipped.zip -d build/test/zipper_test1");
62 #endif
63         BOOST_REQUIRE_EQUAL (r, 0);
64
65         BOOST_CHECK_EQUAL (dcp::file_to_string("build/test/zipper_test1/foo.txt"), "1234567890");
66         BOOST_CHECK_EQUAL (dcp::file_to_string("build/test/zipper_test1/bar.txt"), "xxxxxxCCCCbbbbbbb1");
67 }
68
69
70 /** Test failure when trying to overwrite a file */
71 BOOST_AUTO_TEST_CASE (zipper_test2, * boost::unit_test::depends_on("zipper_test1"))
72 {
73         BOOST_CHECK_THROW (Zipper("build/test/zipped.zip"), FileError);
74 }
75