team-ljyzipelo

공개
125줄6.2 KB
1import hashlib
2import importlib.util
3import io
4import json
5from pathlib import Path
6import stat
7import struct
8import unittest
9import zipfile
10import zlib
11
12
13DIRECTORY = Path(__file__).resolve().parent
14SPEC = importlib.util.spec_from_file_location("zipelo_extraction_factory", DIRECTORY / "fixture_factory.py")
15FACTORY = importlib.util.module_from_spec(SPEC)
16SPEC.loader.exec_module(FACTORY)
17
18
19class ExtractionFixtureTests(unittest.TestCase):
20 def test_samples_and_manifest_are_reproducible(self):
21 samples = FACTORY.build_samples()
22 self.assertEqual(22, len(samples))
23 self.assertEqual(set(samples), {path.name for path in (DIRECTORY / "fixtures").iterdir()})
24 for name, data in samples.items():
25 with self.subTest(name=name):
26 path = DIRECTORY / "fixtures" / name
27 self.assertFalse(path.is_symlink())
28 self.assertEqual(data, path.read_bytes())
29 self.assertLessEqual(len(data), FACTORY.MAX_SAMPLE_BYTES)
30 self.assertEqual(FACTORY.build_manifest(samples), json.loads((DIRECTORY / "fixtures.json").read_text()))
31
32 def test_normal_contents_and_hashes_for_both_methods(self):
33 samples = FACTORY.build_samples()
34 manifest = FACTORY.build_manifest(samples)
35 for name in ("normal-stored.zip", "normal-deflated.zip"):
36 with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(samples[name])) as zipped:
37 self.assertEqual(4, len(zipped.infolist()))
38 self.assertTrue(zipped.getinfo("nested/").is_dir())
39 for entry, expected in manifest["normal_files"].items():
40 data = zipped.read(entry)
41 self.assertEqual(expected["bytes"], len(data))
42 self.assertEqual(expected["sha256"], hashlib.sha256(data).hexdigest())
43
44 def test_unsafe_names_are_metadata_only(self):
45 samples = FACTORY.build_samples()
46 expected = {
47 "traversal.zip": "../escape.txt",
48 "backslash-traversal.zip": "..\\escape.txt",
49 "absolute-posix.zip": "/escape.txt",
50 "absolute-drive.zip": "C:/escape.txt",
51 "absolute-unc.zip": "//server/share/escape.txt",
52 }
53 for name, entry in expected.items():
54 with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(samples[name])) as zipped:
55 self.assertEqual([entry], zipped.namelist())
56
57 def test_conflicts_and_os_names_remain_distinct(self):
58 samples = FACTORY.build_samples()
59 expected = {
60 "duplicate.zip": ["same.txt", "same.txt"],
61 "file-before-child.zip": ["node", "node/child"],
62 "child-before-file.zip": ["node/child", "node"],
63 "case-distinct.zip": ["Readme.txt", "README.TXT"],
64 "linux-names.zip": ["CON", "name:stream"],
65 }
66 for name, entries in expected.items():
67 with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(samples[name])) as zipped:
68 self.assertEqual(entries, zipped.namelist())
69 self.assertIsNone(zipped.testzip())
70
71 def test_link_metadata_without_live_filesystem_links(self):
72 samples = FACTORY.build_samples()
73 with zipfile.ZipFile(io.BytesIO(samples["symlink.zip"])) as zipped:
74 self.assertTrue(stat.S_ISLNK(zipped.infolist()[0].external_attr >> 16))
75 with zipfile.ZipFile(io.BytesIO(samples["special-fifo.zip"])) as zipped:
76 self.assertTrue(stat.S_ISFIFO(zipped.infolist()[0].external_attr >> 16))
77 with zipfile.ZipFile(io.BytesIO(samples["unix-hardlink.zip"])) as zipped:
78 item = zipped.infolist()[0]
79 self.assertTrue(stat.S_ISREG(item.external_attr >> 16))
80 tag, size = struct.unpack_from("<HH", item.extra)
81 self.assertEqual(0x000D, tag)
82 self.assertEqual(12 + len(b"../sentinel.txt"), size)
83 self.assertEqual(b"../sentinel.txt", item.extra[16:])
84 self.assertEqual(0, item.file_size)
85
86 def test_crc_damage_is_independent_of_lengths(self):
87 samples = FACTORY.build_samples()
88 for name in ("bad-payload-crc.zip", "bad-header-crc.zip"):
89 with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(samples[name])) as zipped:
90 self.assertEqual(len(FACTORY.PAYLOAD), zipped.infolist()[0].file_size)
91 self.assertEqual("payload.txt", zipped.testzip())
92
93 def test_length_damage_preserves_decoded_payload_and_crc(self):
94 samples = FACTORY.build_samples()
95 for name, declared in (("length-smaller.zip", len(FACTORY.PAYLOAD) - 1),
96 ("length-larger.zip", len(FACTORY.PAYLOAD) + 1)):
97 data = samples[name]
98 central = data.index(b"PK\x01\x02")
99 with self.subTest(name=name):
100 self.assertEqual(declared, struct.unpack_from("<I", data, 22)[0])
101 self.assertEqual(declared, struct.unpack_from("<I", data, central + 24)[0])
102 compressed = struct.unpack_from("<I", data, 18)[0]
103 start = 30 + sum(struct.unpack_from("<HH", data, 26))
104 decoded = zlib.decompress(data[start:start + compressed], -15)
105 self.assertEqual(FACTORY.PAYLOAD, decoded)
106 self.assertNotEqual(declared, len(decoded))
107 self.assertEqual(zlib.crc32(decoded), struct.unpack_from("<I", data, 14)[0])
108 self.assertEqual(zlib.crc32(decoded), struct.unpack_from("<I", data, central + 16)[0])
109
110 def test_header_disagreement_and_truncation_are_separate(self):
111 samples = FACTORY.build_samples()
112 data = samples["local-central-length.zip"]
113 central = data.index(b"PK\x01\x02")
114 self.assertNotEqual(struct.unpack_from("<I", data, 22)[0], struct.unpack_from("<I", data, central + 24)[0])
115 with self.assertRaises(zipfile.BadZipFile):
116 zipfile.ZipFile(io.BytesIO(samples["truncated.zip"]))
117
118 def test_cumulative_sample_needs_aggregate_budget(self):
119 with zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["cumulative.zip"])) as zipped:
120 self.assertEqual([8, 8], [item.file_size for item in zipped.infolist()])
121 self.assertEqual(16, sum(len(zipped.read(item)) for item in zipped.infolist()))
122
123
124if __name__ == "__main__":
125 unittest.main()