gzip_test.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. #include <cstddef>
  7. #include <string>
  8. #include <boost/iostreams/copy.hpp>
  9. #include <boost/iostreams/device/array.hpp>
  10. #include <boost/iostreams/device/back_inserter.hpp>
  11. #include <boost/iostreams/filter/gzip.hpp>
  12. #include <boost/iostreams/filter/test.hpp>
  13. #include <boost/iostreams/filtering_stream.hpp>
  14. #include <boost/ref.hpp>
  15. #include <boost/range/iterator_range.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/test/unit_test.hpp>
  18. #include "detail/sequence.hpp"
  19. #include "detail/verification.hpp"
  20. using namespace boost;
  21. using namespace boost::iostreams;
  22. using namespace boost::iostreams::test;
  23. namespace io = boost::iostreams;
  24. using boost::unit_test::test_suite;
  25. struct gzip_alloc : std::allocator<char> {
  26. gzip_alloc() { }
  27. gzip_alloc(const gzip_alloc& other) { }
  28. template<typename T>
  29. gzip_alloc(const std::allocator<T>& other) { }
  30. };
  31. void compression_test()
  32. {
  33. text_sequence data;
  34. // Test compression and decompression with metadata
  35. for (int i = 0; i < 4; ++i) {
  36. gzip_params params;
  37. if (i & 1) {
  38. params.file_name = "original file name";
  39. }
  40. if (i & 2) {
  41. params.comment = "detailed file description";
  42. }
  43. gzip_compressor out(params);
  44. gzip_decompressor in;
  45. BOOST_CHECK(
  46. test_filter_pair( boost::ref(out),
  47. boost::ref(in),
  48. std::string(data.begin(), data.end()) )
  49. );
  50. BOOST_CHECK(in.file_name() == params.file_name);
  51. BOOST_CHECK(in.comment() == params.comment);
  52. }
  53. // Test compression and decompression with custom allocator
  54. BOOST_CHECK(
  55. test_filter_pair( basic_gzip_compressor<gzip_alloc>(),
  56. basic_gzip_decompressor<gzip_alloc>(),
  57. std::string(data.begin(), data.end()) )
  58. );
  59. }
  60. void multiple_member_test()
  61. {
  62. text_sequence data;
  63. std::vector<char> temp, dest;
  64. // Write compressed data to temp, twice in succession
  65. filtering_ostream out;
  66. out.push(gzip_compressor());
  67. out.push(io::back_inserter(temp));
  68. io::copy(make_iterator_range(data), out);
  69. out.push(io::back_inserter(temp));
  70. io::copy(make_iterator_range(data), out);
  71. // Read compressed data from temp into dest
  72. filtering_istream in;
  73. in.push(gzip_decompressor());
  74. in.push(array_source(&temp[0], temp.size()));
  75. io::copy(in, io::back_inserter(dest));
  76. // Check that dest consists of two copies of data
  77. BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
  78. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
  79. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
  80. dest.clear();
  81. io::copy(
  82. array_source(&temp[0], temp.size()),
  83. io::compose(gzip_decompressor(), io::back_inserter(dest)));
  84. // Check that dest consists of two copies of data
  85. BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
  86. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
  87. BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
  88. }
  89. void array_source_test()
  90. {
  91. std::string data = "simple test string.";
  92. std::string encoded;
  93. filtering_ostream out;
  94. out.push(gzip_compressor());
  95. out.push(io::back_inserter(encoded));
  96. io::copy(make_iterator_range(data), out);
  97. std::string res;
  98. io::array_source src(encoded.data(),encoded.length());
  99. io::copy(io::compose(io::gzip_decompressor(), src), io::back_inserter(res));
  100. BOOST_CHECK_EQUAL(data, res);
  101. }
  102. #if defined(BOOST_MSVC)
  103. # pragma warning(push)
  104. # pragma warning(disable:4309) // Truncation of constant value
  105. #endif
  106. void header_test()
  107. {
  108. // This test is in response to https://svn.boost.org/trac/boost/ticket/5908
  109. // which describes a problem parsing gzip headers with extra fields as
  110. // defined in RFC 1952 (http://www.ietf.org/rfc/rfc1952.txt).
  111. // The extra field data used here is characteristic of the tabix file
  112. // format (http://samtools.sourceforge.net/tabix.shtml).
  113. const char header_bytes[] = {
  114. static_cast<char>(gzip::magic::id1),
  115. static_cast<char>(gzip::magic::id2),
  116. gzip::method::deflate, // Compression Method: deflate
  117. gzip::flags::extra | gzip::flags::name | gzip::flags::comment, // flags
  118. '\x22', '\x9c', '\xf3', '\x4e', // 4 byte modification time (little endian)
  119. gzip::extra_flags::best_compression, // XFL
  120. gzip::os_unix, // OS
  121. 6, 0, // 2 byte length of extra field (little endian, 6 bytes)
  122. 'B', 'C', 2, 0, 0, 0, // 6 bytes worth of extra field data
  123. 'a', 'b', 'c', 0, // original filename, null terminated
  124. 'n', 'o', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, // comment
  125. };
  126. size_t sz = sizeof(header_bytes)/sizeof(header_bytes[0]);
  127. boost::iostreams::detail::gzip_header hdr;
  128. for (size_t i = 0; i < sz; ++i) {
  129. hdr.process(header_bytes[i]);
  130. // Require that we are done at the last byte, not before.
  131. if (i == sz-1)
  132. BOOST_REQUIRE(hdr.done());
  133. else
  134. BOOST_REQUIRE(!hdr.done());
  135. }
  136. BOOST_CHECK_EQUAL("abc", hdr.file_name());
  137. BOOST_CHECK_EQUAL("no comment", hdr.comment());
  138. BOOST_CHECK_EQUAL(0x4ef39c22, hdr.mtime());
  139. BOOST_CHECK_EQUAL(gzip::os_unix, hdr.os());
  140. }
  141. #if defined(BOOST_MSVC)
  142. # pragma warning(pop)
  143. #endif
  144. void empty_file_test()
  145. {
  146. // This test is in response to https://svn.boost.org/trac/boost/ticket/5237
  147. // The previous implementation of gzip_compressor only wrote the gzip file
  148. // header when the first bytes of uncompressed input were processed, causing
  149. // incorrect behavior for empty files
  150. BOOST_CHECK(
  151. test_filter_pair( gzip_compressor(),
  152. gzip_decompressor(),
  153. std::string() )
  154. );
  155. }
  156. void multipart_test()
  157. {
  158. // This test verifies that the gzip_decompressor properly handles a file
  159. // that was written in multiple parts using Z_FULL_FLUSH, and in particular
  160. // handles the CRC properly when one of those parts is empty.
  161. const char multipart_file[] = {
  162. '\x1f', '\x8b', '\x08', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02', '\xff', '\xf2', '\xc9',
  163. '\xcc', '\x4b', '\x55', '\x30', '\xe4', '\xf2', '\x01', '\x51', '\x46', '\x10', '\xca', '\x98',
  164. '\x0b', '\x00', '\x00', '\x00', '\xff', '\xff', '\x03', '\x00', '\xdb', '\xa7', '\x83', '\xc9',
  165. '\x15', '\x00', '\x00', '\x00', '\x1f', '\x8b', '\x08', '\x00', '\x00', '\x00', '\x00', '\x00',
  166. '\x02', '\xff', '\xf2', '\xc9', '\xcc', '\x4b', '\x55', '\x30', '\xe1', '\xf2', '\x01', '\x51',
  167. '\xa6', '\x10', '\xca', '\x8c', '\x0b', '\x00', '\x00', '\x00', '\xff', '\xff', '\x03', '\x00',
  168. '\x41', '\xe3', '\xcc', '\xaa', '\x15', '\x00', '\x00', '\x00', '\x1f', '\x8b', '\x08', '\x00',
  169. '\x00', '\x00', '\x00', '\x00', '\x02', '\xff', '\x02', '\x00', '\x00', '\x00', '\xff', '\xff',
  170. '\x03', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x1f', '\x8b',
  171. '\x08', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02', '\xff', '\xf2', '\xc9', '\xcc', '\x4b',
  172. '\x55', '\x30', '\xe7', '\xf2', '\x01', '\x51', '\x16', '\x10', '\xca', '\x92', '\x0b', '\x00',
  173. '\x00', '\x00', '\xff', '\xff', '\x03', '\x00', '\x2b', '\xac', '\xd3', '\xf5', '\x15', '\x00',
  174. '\x00', '\x00'
  175. };
  176. filtering_istream in;
  177. std::string line;
  178. in.push(gzip_decompressor());
  179. in.push(io::array_source(multipart_file, sizeof(multipart_file)));
  180. // First part
  181. std::getline(in, line);
  182. BOOST_CHECK_EQUAL("Line 1", line);
  183. std::getline(in, line);
  184. BOOST_CHECK_EQUAL("Line 2", line);
  185. std::getline(in, line);
  186. BOOST_CHECK_EQUAL("Line 3", line);
  187. // Second part immediately follows
  188. std::getline(in, line);
  189. BOOST_CHECK_EQUAL("Line 4", line);
  190. std::getline(in, line);
  191. BOOST_CHECK_EQUAL("Line 5", line);
  192. std::getline(in, line);
  193. BOOST_CHECK_EQUAL("Line 6", line);
  194. // Then an empty part, followed by one last 3-line part.
  195. std::getline(in, line);
  196. BOOST_CHECK_EQUAL("Line 7", line);
  197. std::getline(in, line);
  198. BOOST_CHECK_EQUAL("Line 8", line);
  199. std::getline(in, line);
  200. BOOST_CHECK_EQUAL("Line 9", line);
  201. // Check for gzip errors too.
  202. BOOST_CHECK(!in.bad());
  203. }
  204. test_suite* init_unit_test_suite(int, char* [])
  205. {
  206. test_suite* test = BOOST_TEST_SUITE("gzip test");
  207. test->add(BOOST_TEST_CASE(&compression_test));
  208. test->add(BOOST_TEST_CASE(&multiple_member_test));
  209. test->add(BOOST_TEST_CASE(&array_source_test));
  210. test->add(BOOST_TEST_CASE(&header_test));
  211. test->add(BOOST_TEST_CASE(&empty_file_test));
  212. test->add(BOOST_TEST_CASE(&multipart_test));
  213. return test;
  214. }