1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5 
6 // See http://www.boost.org/libs/iostreams for documentation.
7 
8 #include <cstddef>
9 #include <string>
10 #include <boost/iostreams/copy.hpp>
11 #include <boost/iostreams/device/array.hpp>
12 #include <boost/iostreams/device/back_inserter.hpp>
13 #include <boost/iostreams/filter/gzip.hpp>
14 #include <boost/iostreams/filter/test.hpp>
15 #include <boost/iostreams/filtering_stream.hpp>
16 #include <boost/ref.hpp>
17 #include <boost/range/iterator_range.hpp>
18 #include <boost/test/test_tools.hpp>
19 #include <boost/test/unit_test.hpp>
20 #include "detail/sequence.hpp"
21 #include "detail/verification.hpp"
22 
23 using namespace boost;
24 using namespace boost::iostreams;
25 using namespace boost::iostreams::test;
26 namespace io = boost::iostreams;
27 using boost::unit_test::test_suite;
28 
29 struct gzip_alloc : std::allocator<char> { };
30 
compression_test()31 void compression_test()
32 {
33     text_sequence      data;
34 
35     // Test compression and decompression with metadata
36     for (int i = 0; i < 4; ++i) {
37         gzip_params params;
38         if (i & 1) {
39             params.file_name = "original file name";
40         }
41         if (i & 2) {
42             params.comment = "detailed file description";
43         }
44         gzip_compressor    out(params);
45         gzip_decompressor  in;
46         BOOST_CHECK(
47             test_filter_pair( boost::ref(out),
48                               boost::ref(in),
49                               std::string(data.begin(), data.end()) )
50         );
51         BOOST_CHECK(in.file_name() == params.file_name);
52         BOOST_CHECK(in.comment() == params.comment);
53     }
54 
55     // Test compression and decompression with custom allocator
56     BOOST_CHECK(
57         test_filter_pair( basic_gzip_compressor<gzip_alloc>(),
58                           basic_gzip_decompressor<gzip_alloc>(),
59                           std::string(data.begin(), data.end()) )
60     );
61 }
62 
multiple_member_test()63 void multiple_member_test()
64 {
65     text_sequence      data;
66     std::vector<char>  temp, dest;
67 
68     // Write compressed data to temp, twice in succession
69     filtering_ostream out;
70     out.push(gzip_compressor());
71     out.push(io::back_inserter(temp));
72     io::copy(make_iterator_range(data), out);
73     out.push(io::back_inserter(temp));
74     io::copy(make_iterator_range(data), out);
75 
76     // Read compressed data from temp into dest
77     filtering_istream in;
78     in.push(gzip_decompressor());
79     in.push(array_source(&temp[0], temp.size()));
80     io::copy(in, io::back_inserter(dest));
81 
82     // Check that dest consists of two copies of data
83     BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
84     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
85     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
86 
87     dest.clear();
88     io::copy(
89         array_source(&temp[0], temp.size()),
90         io::compose(gzip_decompressor(), io::back_inserter(dest)));
91 
92     // Check that dest consists of two copies of data
93     BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
94     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
95     BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
96 }
97 
array_source_test()98 void array_source_test()
99 {
100     std::string data = "simple test string.";
101     std::string encoded;
102 
103     filtering_ostream out;
104     out.push(gzip_compressor());
105     out.push(io::back_inserter(encoded));
106     io::copy(make_iterator_range(data), out);
107 
108     std::string res;
109     io::array_source src(encoded.data(),encoded.length());
110     io::copy(io::compose(io::gzip_decompressor(), src), io::back_inserter(res));
111 
112     BOOST_CHECK_EQUAL(data, res);
113 }
114 
header_test()115 void header_test()
116 {
117     // This test is in response to https://svn.boost.org/trac/boost/ticket/5908
118     // which describes a problem parsing gzip headers with extra fields as
119     // defined in RFC 1952 (http://www.ietf.org/rfc/rfc1952.txt).
120     // The extra field data used here is characteristic of the tabix file
121     // format (http://samtools.sourceforge.net/tabix.shtml).
122     const char header_bytes[] = {
123         static_cast<char>(gzip::magic::id1),
124         static_cast<char>(gzip::magic::id2),
125         gzip::method::deflate, // Compression Method: deflate
126         gzip::flags::extra | gzip::flags::name | gzip::flags::comment, // flags
127         '\x22', '\x9c', '\xf3', '\x4e', // 4 byte modification time (little endian)
128         gzip::extra_flags::best_compression, // XFL
129         gzip::os_unix, // OS
130         6, 0, // 2 byte length of extra field (little endian, 6 bytes)
131         'B', 'C', 2, 0, 0, 0, // 6 bytes worth of extra field data
132         'a', 'b', 'c', 0, // original filename, null terminated
133         'n', 'o', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, // comment
134     };
135     size_t sz = sizeof(header_bytes)/sizeof(header_bytes[0]);
136 
137     boost::iostreams::detail::gzip_header hdr;
138     for (size_t i = 0; i < sz; ++i) {
139         hdr.process(header_bytes[i]);
140 
141         // Require that we are done at the last byte, not before.
142         if (i == sz-1)
143             BOOST_REQUIRE(hdr.done());
144         else
145             BOOST_REQUIRE(!hdr.done());
146     }
147 
148     BOOST_CHECK_EQUAL("abc", hdr.file_name());
149     BOOST_CHECK_EQUAL("no comment", hdr.comment());
150     BOOST_CHECK_EQUAL(0x4ef39c22, hdr.mtime());
151     BOOST_CHECK_EQUAL(gzip::os_unix, hdr.os());
152 }
153 
init_unit_test_suite(int,char * [])154 test_suite* init_unit_test_suite(int, char* [])
155 {
156     test_suite* test = BOOST_TEST_SUITE("gzip test");
157     test->add(BOOST_TEST_CASE(&compression_test));
158     test->add(BOOST_TEST_CASE(&multiple_member_test));
159     test->add(BOOST_TEST_CASE(&array_source_test));
160     test->add(BOOST_TEST_CASE(&header_test));
161     return test;
162 }
163