Ticket #5908: iostreams-gzip_hdr_test.patch

File iostreams-gzip_hdr_test.patch, 2.4 KB (added by typedef.struct@…, 11 years ago)

second patch adding unit test to prevent future regressions.

  • libs/iostreams/test/gzip_test.cpp

     
    55
    66// See http://www.boost.org/libs/iostreams for documentation.
    77
     8#include <cstddef>
    89#include <string>
    910#include <boost/iostreams/copy.hpp>
    1011#include <boost/iostreams/device/array.hpp>
     
    111112    BOOST_CHECK_EQUAL(data, res);
    112113}
    113114
     115void 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        gzip::magic::id1,
     124        gzip::magic::id2,
     125        gzip::method::deflate, // Compression Method: deflate
     126        gzip::flags::extra | gzip::flags::name | gzip::flags::comment, // flags
     127        0x22, 0x9c, 0xf3, 0x4e, // 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
    114154test_suite* init_unit_test_suite(int, char* [])
    115155{
    116156    test_suite* test = BOOST_TEST_SUITE("gzip test");
    117157    test->add(BOOST_TEST_CASE(&compression_test));
    118158    test->add(BOOST_TEST_CASE(&multiple_member_test));
    119159    test->add(BOOST_TEST_CASE(&array_source_test));
     160    test->add(BOOST_TEST_CASE(&header_test));
    120161    return test;
    121162}