Debian Codegraph @

contrib/python-zstandard/tests/common.py

raw
1 import io
2 import os
3
4 from typing import List
5
6
7 class NonClosingBytesIO(io.BytesIO):
8 """BytesIO that saves the underlying buffer on close().
9
10 This allows us to access written data after close().
11 """
12
13 def __init__(self, *args, **kwargs):
14 super(NonClosingBytesIO, self).__init__(*args, **kwargs)
15 self._saved_buffer = None
16
17 def close(self):
18 self._saved_buffer = self.getvalue()
19 return super(NonClosingBytesIO, self).close()
20
21 def getvalue(self):
22 if self.closed:
23 return self._saved_buffer
24 else:
25 return super(NonClosingBytesIO, self).getvalue()
26
27
28 class CustomBytesIO(io.BytesIO):
29 def __init__(self, *args, **kwargs):
30 self._flush_count = 0
31 self._read_count = 0
32 self._write_count = 0
33 self.flush_exception = None
34 self.read_exception = None
35 self.write_exception = None
36 super(CustomBytesIO, self).__init__(*args, **kwargs)
37
38 def flush(self):
39 self._flush_count += 1
40
41 if self.flush_exception:
42 raise self.flush_exception
43
44 return super(CustomBytesIO, self).flush()
45
46 def read(self, *args):
47 self._read_count += 1
48
49 if self.read_exception:
50 raise self.read_exception
51
52 return super(CustomBytesIO, self).read(*args)
53
54 def write(self, data):
55 self._write_count += 1
56
57 if self.write_exception:
58 raise self.write_exception
59
60 return super(CustomBytesIO, self).write(data)
61
62
63 _source_files = [] # type: List[bytes]
64
65
66 def random_input_data():
67 """Obtain the raw content of source files.
68
69 This is used for generating "random" data to feed into fuzzing, since it is
70 faster than random content generation.
71 """
72 if _source_files:
73 return _source_files
74
75 for root, dirs, files in os.walk(os.path.dirname(__file__)):
76 # We filter out __pycache__ because there is a race between another
77 # process writing cache files and us reading them.
78 dirs[:] = list(sorted(d for d in dirs if d != "__pycache__"))
79
80 for f in sorted(files):
81 try:
82 with open(os.path.join(root, f), "rb") as fh:
83 data = fh.read()
84 # Exclude large files because it can cause us to easily exceed
85 # deadlines during fuzz testing.
86 if data and len(data) < 131072:
87 _source_files.append(data)
88 except OSError:
89 pass
90
91 # Also add some actual random data.
92 _source_files.append(os.urandom(100))
93 _source_files.append(os.urandom(1000))
94 _source_files.append(os.urandom(10000))
95 _source_files.append(os.urandom(100000))
96 _source_files.append(os.urandom(1000000))
97
98 return _source_files
99
100
101 def get_optimal_dict_size_heuristically(src):
102 return sum(len(ch) for ch in src) // 100
103
104
105 def generate_samples():
106 inputs = [
107 b"foo" * 32,
108 b"bar" * 16,
109 b"abcdef" * 64,
110 b"sometext" * 128,
111 b"baz" * 512,
112 ]
113
114 samples = []
115
116 for i in range(128):
117 samples.append(inputs[i % 5])
118 samples.append(inputs[i % 5] * (i + 3))
119 samples.append(inputs[-(i % 5)] * (i + 2))
120
121 return samples