Example #1
0
def _eq_zformat(expected,
                output,
                mtime=0,
                expected_basename=None,
                in_size=None):
    """Compares the file formats of data in the given streams.
    """
    if in_size is None:
        expected.seek(-4, os.SEEK_END)
        in_size = struct.unpack("<I", expected.read(4))[0]
        expected.seek(0)

    # ID1,ID2,CM
    asserting.eq_bytes(expected.read(3), output.read(3))

    # flags
    flags = ord(expected.read(1)) & 0xff
    if expected_basename is None:
        flags = flags - compressor.FNAME
    eq_(flags, ord(output.read(1)) & 0xff)

    # mtime
    eq_(mtime, struct.unpack("<I", output.read(4))[0])
    expected.read(4)

    # XFL and OS
    asserting.eq_bytes(expected.read(2), output.read(2))

    # FEXTRA header
    info_len = 10
    xlen_bytes = expected.read(2)
    asserting.eq_bytes(xlen_bytes, output.read(2))

    xlen = ord(xlen_bytes[0]) & 0xff + 256 * (ord(xlen_bytes[1]) & 0xff)
    num_chunks = in_size // compressor.CHUNK_LENGTH
    if in_size % compressor.CHUNK_LENGTH:
        num_chunks += 1
    eq_(num_chunks, (xlen - info_len) // 2)

    asserting.eq_bytes(expected.read(info_len), output.read(info_len))
    expected.seek(xlen - info_len, os.SEEK_CUR)
    output.seek(xlen - info_len, os.SEEK_CUR)

    # FNAME
    fname = _read_cstring(expected)
    if expected_basename is not None:
        eq_(expected_basename, _read_cstring(output))

    # zstream
    _eq_zstream(expected, output)

    # tail
    asserting.eq_bytes(expected.read(8), output.read(8))
Example #2
0
def _eq_zformat(expected, output, mtime=0, expected_basename=None, in_size=None):
    """Compares the file formats of data in the given streams.
    """
    if in_size is None:
        expected.seek(-4, os.SEEK_END)
        in_size = struct.unpack("<I", expected.read(4))[0]
        expected.seek(0)

    # ID1,ID2,CM
    asserting.eq_bytes(expected.read(3), output.read(3))

    # flags
    flags = ord(expected.read(1)) & 0xff
    if expected_basename is None:
        flags = flags - compressor.FNAME
    eq_(flags, ord(output.read(1)) & 0xff)

    # mtime
    eq_(mtime, struct.unpack("<I", output.read(4))[0])
    expected.read(4)

    # XFL and OS
    asserting.eq_bytes(expected.read(2), output.read(2))

    # FEXTRA header
    info_len = 10
    xlen_bytes = expected.read(2)
    asserting.eq_bytes(xlen_bytes, output.read(2))

    xlen = ord(xlen_bytes[0]) & 0xff + 256 * (ord(xlen_bytes[1]) & 0xff)
    num_chunks = in_size // compressor.CHUNK_LENGTH
    if in_size % compressor.CHUNK_LENGTH:
        num_chunks += 1
    eq_(num_chunks, (xlen - info_len) // 2)

    asserting.eq_bytes(expected.read(info_len), output.read(info_len))
    expected.seek(xlen - info_len, os.SEEK_CUR)
    output.seek(xlen - info_len, os.SEEK_CUR)

    # FNAME
    fname = _read_cstring(expected)
    if expected_basename is not None:
        eq_(expected_basename, _read_cstring(output))

    # zstream
    _eq_zstream(expected, output)

    # tail
    asserting.eq_bytes(expected.read(8), output.read(8))
Example #3
0
def _eq_zstream(expected, produced):
    """Compares the zstreams.
    Their decompressed bytes are compared.
    The compressed bytes differ, because of the different
    flushing used in the Python zlib and dictzip.
    """
    import zlib
    deobj = zlib.decompressobj(-zlib.MAX_WBITS)
    expected_data = deobj.decompress(expected.read())
    expected.seek(-len(deobj.unused_data), os.SEEK_CUR)

    deobj = zlib.decompressobj(-zlib.MAX_WBITS)
    got = deobj.decompress(produced.read())
    produced.seek(-len(deobj.unused_data), os.SEEK_CUR)
    asserting.eq_bytes(expected_data, got)
Example #4
0
def _eq_zstream(expected, produced):
    """Compares the zstreams.
    Their decompressed bytes are compared.
    The compressed bytes differ, because of the different
    flushing used in the Python zlib and dictzip.
    """
    import zlib
    deobj = zlib.decompressobj(-zlib.MAX_WBITS)
    expected_data = deobj.decompress(expected.read())
    expected.seek(-len(deobj.unused_data), os.SEEK_CUR)

    deobj = zlib.decompressobj(-zlib.MAX_WBITS)
    got = deobj.decompress(produced.read())
    produced.seek(-len(deobj.unused_data), os.SEEK_CUR)
    asserting.eq_bytes(expected_data, got)
Example #5
0
 def readline(self, size=-1):
     expected = self.expected_input.readline(size)
     got = self.input.readline(size)
     asserting.eq_bytes(expected, got)
     return got