Line data Source code
1 : /* 2 : Zipios -- a small C++ library that provides easy access to .zip files. 3 : 4 : Copyright (C) 2000-2007 Thomas Sondergaard 5 : Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved 6 : 7 : This library is free software; you can redistribute it and/or 8 : modify it under the terms of the GNU Lesser General Public 9 : License as published by the Free Software Foundation; either 10 : version 2.1 of the License, or (at your option) any later version. 11 : 12 : This library is distributed in the hope that it will be useful, 13 : but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 : Lesser General Public License for more details. 16 : 17 : You should have received a copy of the GNU Lesser General Public 18 : License along with this library; if not, write to the Free Software 19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 : */ 21 : 22 : /** \file 23 : * 24 : * Zipios unit tests for the BackBuffer class. 25 : */ 26 : 27 : #include "catch_main.hpp" 28 : 29 : #include <src/backbuffer.hpp> 30 : #include <zipios/zipiosexceptions.hpp> 31 : 32 : #include <fstream> 33 : 34 : 35 3 : CATCH_SCENARIO("BackBuffer read a file", "[BackBuffer]") 36 : { 37 3 : zipios_test::safe_chdir cwd(SNAP_CATCH2_NAMESPACE::g_tmp_dir()); 38 : 39 3 : CATCH_GIVEN("a binary file of 256 bytes") 40 : { 41 : // create a file of 256 bytes 42 6 : zipios_test::auto_unlink_t auto_unlink("file256.bin", true); 43 : { 44 3 : std::ofstream os("file256.bin", std::ios::out | std::ios::binary); 45 771 : for(int i(0); i < 256; ++i) 46 : { 47 768 : os << static_cast<char>(i); 48 : } 49 3 : } 50 : 51 3 : CATCH_WHEN("setting up a backbuffer") 52 : { 53 3 : std::ifstream is("file256.bin", std::ios::in | std::ios::binary); 54 3 : zipios::BackBuffer back_buffer(is, zipios::VirtualSeeker(), 16); 55 : 56 3 : CATCH_THEN("we can read the file going backward") 57 : { 58 1 : ssize_t read_pointer(0); 59 17 : for(int i(0), j(256 - 16); i < 256; i += 16, j -= 16) 60 : { 61 16 : back_buffer.readChunk(read_pointer); 62 16 : CATCH_REQUIRE(read_pointer == i + 16); 63 272 : for(int k(0); k < 16; ++k) 64 : { 65 256 : CATCH_REQUIRE(back_buffer[k] == j + k); 66 : } 67 : } 68 3 : } 69 : 70 3 : CATCH_THEN("try with an invalid chunk size") 71 : { 72 18 : for(int i(-16); i <= 0; ++i) 73 : { 74 51 : CATCH_REQUIRE_THROWS_AS([&](){ 75 : zipios::BackBuffer bb(is, zipios::VirtualSeeker(), i); 76 : }(), zipios::InvalidException); 77 : } 78 3 : } 79 : 80 3 : CATCH_THEN("we close the file and we get an exception") 81 : { 82 1 : is.close(); 83 : // first time the seek fails 84 3 : CATCH_REQUIRE_THROWS_AS([&](){ 85 : zipios::BackBuffer bb(is); 86 : }(), zipios::IOException); 87 : // second time the file is marked as invalid 88 3 : CATCH_REQUIRE_THROWS_AS([&](){ 89 : zipios::BackBuffer bb(is); 90 : }(), zipios::InvalidException); 91 3 : } 92 6 : } 93 6 : } 94 3 : } 95 : 96 : 97 : // Local Variables: 98 : // mode: cpp 99 : // indent-tabs-mode: nil 100 : // c-basic-offset: 4 101 : // tab-width: 4 102 : // End: 103 : 104 : // vim: ts=4 sw=4 et