Debian Codegraph @

contrib/python-zstandard/tests/test_decompressor_content_dict_chain.py

raw
1 import unittest
2
3 import zstandard as zstd
4
5
6 class TestDecompressor_content_dict_chain(unittest.TestCase):
7 def test_bad_inputs_simple(self):
8 dctx = zstd.ZstdDecompressor()
9
10 with self.assertRaises(TypeError):
11 dctx.decompress_content_dict_chain(b"foo")
12
13 with self.assertRaises(TypeError):
14 dctx.decompress_content_dict_chain((b"foo", b"bar"))
15
16 with self.assertRaisesRegex(ValueError, "empty input chain"):
17 dctx.decompress_content_dict_chain([])
18
19 with self.assertRaisesRegex(ValueError, "chunk 0 must be bytes"):
20 dctx.decompress_content_dict_chain(["foo"])
21
22 with self.assertRaisesRegex(ValueError, "chunk 0 must be bytes"):
23 dctx.decompress_content_dict_chain([True])
24
25 with self.assertRaisesRegex(
26 ValueError, "chunk 0 is too small to contain a zstd frame"
27 ):
28 dctx.decompress_content_dict_chain([zstd.FRAME_HEADER])
29
30 with self.assertRaisesRegex(
31 ValueError, "chunk 0 is not a valid zstd frame"
32 ):
33 dctx.decompress_content_dict_chain([b"foo" * 8])
34
35 no_size = zstd.ZstdCompressor(write_content_size=False).compress(
36 b"foo" * 64
37 )
38
39 with self.assertRaisesRegex(
40 ValueError, "chunk 0 missing content size in frame"
41 ):
42 dctx.decompress_content_dict_chain([no_size])
43
44 # Corrupt first frame.
45 frame = zstd.ZstdCompressor().compress(b"foo" * 64)
46 frame = frame[0:12] + frame[15:]
47 with self.assertRaisesRegex(
48 zstd.ZstdError, "chunk 0 did not decompress full frame"
49 ):
50 dctx.decompress_content_dict_chain([frame])
51
52 def test_bad_subsequent_input(self):
53 initial = zstd.ZstdCompressor().compress(b"foo" * 64)
54
55 dctx = zstd.ZstdDecompressor()
56
57 with self.assertRaisesRegex(ValueError, "chunk 1 must be bytes"):
58 dctx.decompress_content_dict_chain([initial, "foo"])
59
60 with self.assertRaisesRegex(ValueError, "chunk 1 must be bytes"):
61 dctx.decompress_content_dict_chain([initial, None])
62
63 with self.assertRaisesRegex(
64 ValueError, "chunk 1 is too small to contain a zstd frame"
65 ):
66 dctx.decompress_content_dict_chain([initial, zstd.FRAME_HEADER])
67
68 with self.assertRaisesRegex(
69 ValueError, "chunk 1 is not a valid zstd frame"
70 ):
71 dctx.decompress_content_dict_chain([initial, b"foo" * 8])
72
73 no_size = zstd.ZstdCompressor(write_content_size=False).compress(
74 b"foo" * 64
75 )
76
77 with self.assertRaisesRegex(
78 ValueError, "chunk 1 missing content size in frame"
79 ):
80 dctx.decompress_content_dict_chain([initial, no_size])
81
82 # Corrupt second frame.
83 cctx = zstd.ZstdCompressor(
84 dict_data=zstd.ZstdCompressionDict(b"foo" * 64)
85 )
86 frame = cctx.compress(b"bar" * 64)
87 frame = frame[0:12] + frame[15:]
88
89 with self.assertRaisesRegex(
90 zstd.ZstdError, "chunk 1 did not decompress full frame"
91 ):
92 dctx.decompress_content_dict_chain([initial, frame])
93
94 def test_simple(self):
95 original = [
96 b"foo" * 64,
97 b"foobar" * 64,
98 b"baz" * 64,
99 b"foobaz" * 64,
100 b"foobarbaz" * 64,
101 ]
102
103 chunks = []
104 chunks.append(zstd.ZstdCompressor().compress(original[0]))
105 for i, chunk in enumerate(original[1:]):
106 d = zstd.ZstdCompressionDict(original[i])
107 cctx = zstd.ZstdCompressor(dict_data=d)
108 chunks.append(cctx.compress(chunk))
109
110 for i in range(1, len(original)):
111 chain = chunks[0:i]
112 expected = original[i - 1]
113 dctx = zstd.ZstdDecompressor()
114 decompressed = dctx.decompress_content_dict_chain(chain)
115 self.assertEqual(decompressed, expected)