File: test_zlibutil.py

package info (click to toggle)
python-pyutil 3.3.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 884 kB
  • sloc: python: 7,198; makefile: 6
file content (81 lines) | stat: -rw-r--r-- 3,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# -*- coding: utf-8-with-signature-unix; fill-column: 77 -*-
# -*- indent-tabs-mode: nil -*-

#  This file is part of pyutil; see README.rst for licensing terms.

import unittest

from pyutil import randutil

from pyutil import zlibutil

class Accumulator:
    def __init__(self):
        self.buf = b''

    def write(self, str):
        self.buf += str

def make_decomp(realdecomp):
    def decomp(str, maxlen, maxmem):
        d = Accumulator()
        realdecomp(str, d, maxlen, maxmem)
        return d.buf
    return decomp

def genrandstr(strlen):
    return randutil.insecurerandstr(strlen)

def genbombstr(strlen):
    return b'0' * strlen

MAXMEM=65*2**20

class ZlibTestCase(unittest.TestCase):
    def _help_test(self, genstring, decomp, strlen):
        s = genstring(strlen)
        cs = zlibutil.zlib.compress(s)
        s2 = decomp(cs, maxlen=strlen, maxmem=strlen*2**3 + zlibutil.MINMAXMEM)
        self.assertTrue(s == s2)
        s2 = decomp(cs, maxlen=strlen, maxmem=strlen*2**6 + zlibutil.MINMAXMEM)
        self.assertTrue(s == s2)
        self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=strlen*2**3 + zlibutil.MINMAXMEM)

    def _help_test_inplace_minmaxmem(self, genstring, decomp, strlen):
        s = genstring(strlen)
        cs = zlibutil.zlib.compress(s)
        s2 = decomp(cs, maxlen=strlen, maxmem=zlibutil.MINMAXMEM)
        self.assertTrue(s == s2)
        self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=zlibutil.MINMAXMEM)

    def _help_test_inplace(self, genstring, decomp, strlen):
        # ### XXX self.assertRaises(UnsafeDecompressError, decomp, zlib.compress(genstring(strlen)), maxlen=strlen, maxmem=strlen-1)
        s = genstring(strlen)
        cs = zlibutil.zlib.compress(s)
        s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM))
        self.assertTrue(s == s2)
        s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen*2**6, zlibutil.MINMAXMEM))
        self.assertTrue(s == s2)
        s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen-1, zlibutil.MINMAXMEM))
        self.assertTrue(s == s2)
        s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen/2, zlibutil.MINMAXMEM))
        self.assertTrue(s == s2)
        self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM))

    def testem(self):
        # for strlen in [2**1, 2**2, 2**10, 2**14, 2**21]: # a *real* test ought to include 2**21, which exercises different cases re: maxmem.  But it takes too long.
        for strlen in [2, 3, 4, 99,]:
            # print "strlen: %s\n" % (strlen,)
            for decomp in [zlibutil.decompress, make_decomp(zlibutil.decompress_to_fileobj), make_decomp(zlibutil.decompress_to_spool),]:
                # print "decomp: %s\n" % (decomp,)
                for genstring in [genrandstr, genbombstr,]:
                    # print "genstring: %s\n" % (genstring,)
                    self._help_test(genstring, decomp, strlen)

            for decomp in [make_decomp(zlibutil.decompress_to_spool),]:
                # print "decomp: %s\n" % (decomp,)
                for genstring in [genrandstr, genbombstr,]:
                    # print "genstring: %s\n" % (genstring,)
                    self._help_test_inplace(genstring, decomp, strlen)
                    self._help_test_inplace_minmaxmem(genstring, decomp, strlen)