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-2019 Made to Order Software Corporation
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 "tests.hpp"
28 :
29 : #include "src/backbuffer.hpp"
30 : #include "zipios/zipiosexceptions.hpp"
31 :
32 : #include <fstream>
33 :
34 :
35 4 : SCENARIO("BackBuffer read a file", "[BackBuffer]")
36 : {
37 6 : GIVEN("a binary file of 256 bytes")
38 : {
39 : // create a file of 256 bytes
40 6 : zipios_test::auto_unlink_t auto_unlink("file256.bin");
41 : {
42 6 : std::ofstream os("file256.bin", std::ios::out | std::ios::binary);
43 771 : for(int i(0); i < 256; ++i)
44 : {
45 768 : os << static_cast<char>(i);
46 : }
47 : }
48 :
49 6 : WHEN("setting up a backbuffer")
50 : {
51 6 : std::ifstream is("file256.bin", std::ios::in | std::ios::binary);
52 6 : zipios::BackBuffer back_buffer(is, zipios::VirtualSeeker(), 16);
53 :
54 6 : THEN("we can read the file going backward")
55 : {
56 1 : ssize_t read_pointer(0);
57 17 : for(int i(0), j(256 - 16); i < 256; i += 16, j -= 16)
58 : {
59 16 : back_buffer.readChunk(read_pointer);
60 16 : REQUIRE(read_pointer == i + 16);
61 272 : for(int k(0); k < 16; ++k)
62 : {
63 256 : REQUIRE(back_buffer[k] == j + k);
64 : }
65 : }
66 : }
67 :
68 6 : THEN("try with an invalid chunk size")
69 : {
70 18 : for(int i(-16); i <= 0; ++i)
71 : {
72 34 : REQUIRE_THROWS_AS([&](){
73 : zipios::BackBuffer bb(is, zipios::VirtualSeeker(), i);
74 : }(), zipios::InvalidException);
75 : }
76 : }
77 :
78 6 : THEN("we close the file and we get an exception")
79 : {
80 1 : is.close();
81 : // first time the seek fails
82 2 : REQUIRE_THROWS_AS([&](){
83 : zipios::BackBuffer bb(is);
84 : }(), zipios::IOException);
85 : // second time the file is marked as invalid
86 2 : REQUIRE_THROWS_AS([&](){
87 : zipios::BackBuffer bb(is);
88 : }(), zipios::InvalidException);
89 : }
90 : }
91 : }
92 6 : }
93 :
94 :
95 : // Local Variables:
96 : // mode: cpp
97 : // indent-tabs-mode: nil
98 : // c-basic-offset: 4
99 : // tab-width: 4
100 : // End:
101 :
102 : // vim: ts=4 sw=4 et
|