示例#1
0
 def test_decompress_garbage_in_header(self):
     """rzlib handle garbage just like zlib."""
     data = os.urandom(102400)
     rdecompressor = rzlib.decompressobj()
     decompressor = rzlib.decompressobj()
     for do in (decompressor, rdecompressor):
         self.assertRaises(zlib.error, do.decompress, data)
示例#2
0
    def test_set_state(self):
        """set_state works as expected."""
        do = rzlib.decompressobj()
        # change the state to a unfinished decompress
        data = os.urandom(1024 * 64)
        deflated_data = zlib.compress(data)
        do.decompress(deflated_data[:len(deflated_data) / 2])
        new_do = rzlib.decompressobj()
        new_do.set_state(do.get_state())
        fnames = [f[0]for f in rzlib.InflateState._fields_]
        code_fields = [field[0] for field in rzlib.Code._fields_]
        new_state = new_do._c_do.zst.state.contents
        old_state = do._c_do.zst.state.contents
        for fname in fnames:
            # pointers needs special care
            # check Code pointers
            if fname in ['next', 'distcode', 'lencode']:
                new_code = getattr(new_state, fname).contents
                old_code = getattr(old_state, fname).contents
                for f in code_fields:
                    self.assertEqual(getattr(new_code, f),
                                     getattr(old_code, f))
                continue
            # check the window
            elif fname == 'window':
                if new_state.write == 0 and old_state.write == 0:
                    # the write index is 0, skip the window comparsion.
                    continue
                new_window = ctypes.string_at(new_state.window,
                                              new_state.whave)
                old_window = ctypes.string_at(old_state.window,
                                              old_state.whave)
                self.assertEqual(new_window, old_window, 'not equal')
                continue
            elif fname == 'head':
                self.assertFalse(new_state.head)
                self.assertFalse(old_state.head)
                continue
            # check arrays
            elif fname in ['lens', 'codes', 'work']:
                array = getattr(new_state, fname)
                new_array_content = ctypes.string_at(ctypes.pointer(array),
                                                     ctypes.sizeof(array))
                array = getattr(new_state, fname)
                old_array_content = ctypes.string_at(ctypes.pointer(array),
                                                     ctypes.sizeof(array))
                self.assertEqual(new_array_content, old_array_content)
                continue
            # compare simple types
            self.assertEqual(getattr(new_state, fname),
                             getattr(old_state, fname))

        fnames = [f[0]for f in rzlib.ResumableZStream._fields_]
        for fname in fnames:
            # skip data pointers, functions and the inflate state
            if fname in ['next_in', 'next_out', 'zalloc',
                         'zfree', 'opaque', 'state']:
                continue
            self.assertEqual(getattr(new_do._c_do.zst, fname),
                             getattr(do._c_do.zst, fname))
示例#3
0
 def test_set_state_bad_version(self):
     """set_state with a different version fails."""
     do = rzlib.decompressobj()
     state = cPickle.loads(do.get_state())
     state['zlib_version'] = 'hola'
     pickled_state = cPickle.dumps(state)
     do1 = rzlib.decompressobj()
     self.assertRaises(rzlib.VersionError, do1.set_state, pickled_state)
示例#4
0
 def test_initial_state(self):
     """get/set initial state work."""
     do = rzlib.decompressobj()
     # get  works
     old = do.get_state()
     # set works
     do1 = rzlib.decompressobj()
     do1.set_state(old)
     self.assertEqual(old, do1.get_state())
示例#5
0
    def test_decompress_garbage_in_middle(self):
        """rzlib handle garbage just like zlib."""
        data = os.urandom(102400)
        deflated_data = zlib.compress(data)

        rdecompressor = rzlib.decompressobj()
        decompressor = rzlib.decompressobj()
        for do in (decompressor, rdecompressor):
            data_size = len(deflated_data)
            part = data_size / 3
            do.decompress(deflated_data[:part])
            self.assertRaises(zlib.error, do.decompress, data[part:part * 2])
示例#6
0
    def test_decompress(self):
        """rzlib produces the same results as zlib."""
        results = []
        data = os.urandom(102400)
        deflated_data = zlib.compress(data)

        rdecompressor = rzlib.decompressobj()
        decompressor = rzlib.decompressobj()
        for do in (decompressor, rdecompressor):
            inflated_data = do.decompress(deflated_data)
            inflated_data += do.flush()
            results.append(inflated_data)
            self.assertEqual(inflated_data, data)

        self.assertEqual(*results)
示例#7
0
    def test_decompressmaxlen(self, flush=False):
        """Check a decompression object with max_length specified."""
        data = HAMLET_SCENE * 128
        co = zlib.compressobj()
        bufs = []
        for i in range(0, len(data), 256):
            bufs.append(co.compress(data[i:i + 256]))
        bufs.append(co.flush())
        combuf = ''.join(bufs)
        self.assertEqual(data, zlib.decompress(combuf),
                         'compressed data failure')

        dco = rzlib.decompressobj()
        bufs = []
        cb = combuf
        while cb:
            max_length = 1 + len(cb) // 10
            chunk = dco.decompress(cb, max_length)
            self.failIf(len(chunk) > max_length,
                        'chunk too big (%d>%d)' % (len(chunk), max_length))
            bufs.append(chunk)
            cb = dco.unconsumed_tail
        if flush:
            bufs.append(dco.flush())
        else:
            while chunk:
                chunk = dco.decompress('', max_length)
                self.failIf(len(chunk) > max_length,
                            'chunk too big (%d>%d)' % (len(chunk), max_length))
                bufs.append(chunk)
        self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
示例#8
0
 def test_baddecompresscopy(self):
     """Test copying a compression object in an inconsistent state."""
     data = zlib.compress(HAMLET_SCENE)
     d = rzlib.decompressobj()
     d.decompress(data)
     d.flush()
     self.assertRaises(ValueError, d.copy)
示例#9
0
    def test_decompimax(self, source=None, cx=256, dcx=64):
        """Compress in steps, decompress in length-restricted steps."""
        source = source or HAMLET_SCENE
        # Check a decompression object with max_length specified
        data = source * 128
        co = zlib.compressobj()
        bufs = []
        for i in range(0, len(data), cx):
            bufs.append(co.compress(data[i:i + cx]))
        bufs.append(co.flush())
        combuf = ''.join(bufs)
        self.assertEqual(data, zlib.decompress(combuf),
                         'compressed data failure')

        dco = rzlib.decompressobj()
        bufs = []
        cb = combuf
        while cb:
            # max_length = 1 + len(cb)//10
            chunk = dco.decompress(cb, dcx)
            self.failIf(len(chunk) > dcx,
                        'chunk too big (%d>%d)' % (len(chunk), dcx))
            bufs.append(chunk)
            cb = dco.unconsumed_tail
        bufs.append(dco.flush())
        self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
示例#10
0
    def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
        """Compress object in steps, decompress object in steps."""
        source = source or HAMLET_SCENE
        data = source * 128
        co = zlib.compressobj()
        bufs = []
        for i in range(0, len(data), cx):
            bufs.append(co.compress(data[i:i + cx]))
        bufs.append(co.flush())
        combuf = ''.join(bufs)

        self.assertEqual(data, zlib.decompress(combuf))

        dco = rzlib.decompressobj()
        bufs = []
        for i in range(0, len(combuf), dcx):
            bufs.append(dco.decompress(combuf[i:i + dcx]))
            self.assertEqual('', dco.unconsumed_tail,
                             "(A) uct should be '': not %d long" % len(
                                 dco.unconsumed_tail))
        if flush:
            bufs.append(dco.flush())
        else:
            while True:
                chunk = dco.decompress('')
                if chunk:
                    bufs.append(chunk)
                else:
                    break
        self.assertEqual('', dco.unconsumed_tail,
                         "(B) uct should be '': not %d long" % len(
                             dco.unconsumed_tail))
        self.assertEqual(data, ''.join(bufs))
示例#11
0
 def test_get_state_with_error(self):
     """get/set state fails if there is an error in the state."""
     do = rzlib.decompressobj()
     try:
         do.decompress("Ho")
     except zlib.error:
         pass
     self.assertRaises(ValueError, do.get_state)
示例#12
0
 def test_resumable_decompress(self):
     """Resuming decompress has the same result as zlib."""
     results = []
     chunksz = 1024
     data = os.urandom(1024 * 64)
     deflated_data = zlib.compress(data)
     results.append(zlib.decompress(deflated_data))
     start = 0
     state = None
     rdata = ''
     for j, i in enumerate(range(0, len(deflated_data), chunksz)):
         if state is None:
             do2 = rzlib.decompressobj()
         else:
             do2 = rzlib.decompressobj(state)
         rdata += do2.decompress(deflated_data[start:i])
         state = do2.get_state()
         start = i
     # if there are some bytes left, decompress them.
     if start < len(deflated_data):
         rdata += do2.decompress(deflated_data[start:])
     results.append(rdata)
     self.assertEqual(results[0], results[1], "inflated data doesn't match")
示例#13
0
    def test_get_state(self):
        """get/set state work."""
        do = rzlib.decompressobj()
        state = cPickle.loads(do.get_state())
        fnames = [f[0]for f in rzlib.InflateState._fields_]
        for fname in fnames:
            self.assertTrue(fname in state['state'],
                            '%s not in InflateState' % fname)

        fnames = [f[0]for f in rzlib.ResumableZStream._fields_]
        for fname in fnames:
            if fname in ['next_in', 'next_out', 'zalloc', 'zfree', 'opaque']:
                continue
            self.assertTrue(fname in state,
                            '%s not in ResumableZStream' % fname)
示例#14
0
 def test_copy(self):
     """Copied decompressobj have the same state."""
     data = "Thoughtcrime does not entail death: thoughtcrime is death."
     data = os.urandom(102400)
     deflated_data = zlib.compress(data)
     do = rzlib.decompressobj()
     do.decompress(deflated_data[:len(deflated_data) / 2])
     state = cPickle.loads(do.get_state())
     copy = cPickle.loads(do.copy().get_state())
     for k, v in state.iteritems():
         if k == 'state':
             for k1, v1 in v.iteritems():
                 self.assertEqual(v1, copy[k][k1],
                                  "%s -> %r != %r" % (k1, v1, copy[k][k1]))
             continue
         self.assertEqual(v, copy[k],
                          "%s -> %r != %r" % (k, v, copy[k]))
示例#15
0
    def test_decompresscopy(self):
        """Test copying a decompression object."""
        data = HAMLET_SCENE
        comp = zlib.compress(data)

        d0 = rzlib.decompressobj()
        bufs0 = []
        bufs0.append(d0.decompress(comp[:32]))

        d1 = d0.copy()
        bufs1 = bufs0[:]

        bufs0.append(d0.decompress(comp[32:]))
        s0 = ''.join(bufs0)

        bufs1.append(d1.decompress(comp[32:]))
        s1 = ''.join(bufs1)

        self.assertEqual(s0, s1)
        self.assertEqual(s0, data)
示例#16
0
 def test_get_state_version(self):
     """get/set state work."""
     do = rzlib.decompressobj()
     state = cPickle.loads(do.get_state())
     self.assertEqual(state['zlib_version'], zlib.ZLIB_VERSION)
示例#17
0
 def test_maxlenmisc(self):
     """Misc tests of max_length."""
     dco = rzlib.decompressobj()
     self.assertRaises(ValueError, dco.decompress, "", -1)
     self.assertEqual('', dco.unconsumed_tail)
示例#18
0
 def test_decompressobj_badflush(self):
     """Verify failure on calling decompressobj.flush with bad params."""
     self.assertRaises(ValueError, rzlib.decompressobj().flush, 0)
     self.assertRaises(ValueError, rzlib.decompressobj().flush, -1)