| 1 | import importlib.util |
| 2 | import io |
| 3 | from pathlib import Path |
| 4 | import stat |
| 5 | import unittest |
| 6 | import zipfile |
| 7 | |
| 8 | |
| 9 | DIRECTORY = Path(__file__).resolve().parent |
| 10 | SPEC = importlib.util.spec_from_file_location("zipelo_fixture_factory", DIRECTORY / "fixture_factory.py") |
| 11 | FACTORY = importlib.util.module_from_spec(SPEC) |
| 12 | SPEC.loader.exec_module(FACTORY) |
| 13 | |
| 14 | |
| 15 | class SafetyFixtureTests(unittest.TestCase): |
| 16 | def test_samples_match_reproducible_generator(self): |
| 17 | for name, expected in FACTORY.build_samples().items(): |
| 18 | with self.subTest(name=name): |
| 19 | path = DIRECTORY / "fixtures" / name |
| 20 | self.assertFalse(path.is_symlink()) |
| 21 | self.assertEqual(path.read_bytes(), expected) |
| 22 | self.assertLessEqual(path.stat().st_size, FACTORY.MAX_SAMPLE_BYTES) |
| 23 | |
| 24 | def test_valid_samples_have_bounded_content_without_extraction(self): |
| 25 | for name, data in FACTORY.build_samples().items(): |
| 26 | if name in ("bad-crc.zip", "truncated.zip"): |
| 27 | continue |
| 28 | with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(data)) as zipped: |
| 29 | self.assertLessEqual(len(zipped.infolist()), 2) |
| 30 | self.assertLessEqual(sum(item.file_size for item in zipped.infolist()), 128) |
| 31 | self.assertIsNone(zipped.testzip()) |
| 32 | |
| 33 | def test_normal_sample_preserves_unicode_and_empty_file(self): |
| 34 | with zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["normal.zip"])) as zipped: |
| 35 | self.assertEqual(zipped.read("hello.txt"), FACTORY.PAYLOAD) |
| 36 | self.assertEqual(zipped.read("한글/빈파일.txt"), b"") |
| 37 | |
| 38 | def test_traversal_samples_retain_unsafe_names_as_metadata(self): |
| 39 | samples = FACTORY.build_samples() |
| 40 | for name, entry in ( |
| 41 | ("traversal.zip", "../escape.txt"), |
| 42 | ("backslash-traversal.zip", "..\\escape.txt"), |
| 43 | ): |
| 44 | with self.subTest(name=name), zipfile.ZipFile(io.BytesIO(samples[name])) as zipped: |
| 45 | self.assertEqual(zipped.namelist(), [entry]) |
| 46 | |
| 47 | def test_link_sample_is_archive_metadata_not_a_live_link(self): |
| 48 | data = FACTORY.build_samples()["symlink.zip"] |
| 49 | with zipfile.ZipFile(io.BytesIO(data)) as zipped: |
| 50 | entry = zipped.infolist()[0] |
| 51 | self.assertTrue(stat.S_ISLNK(entry.external_attr >> 16)) |
| 52 | self.assertEqual(zipped.read(entry), b"../sentinel.txt") |
| 53 | |
| 54 | def test_case_collision_contains_distinct_entries(self): |
| 55 | with zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["case-collision.zip"])) as zipped: |
| 56 | names = zipped.namelist() |
| 57 | self.assertEqual(len(set(names)), 2) |
| 58 | self.assertEqual(len({name.casefold() for name in names}), 1) |
| 59 | |
| 60 | def test_parent_collision_has_file_then_child(self): |
| 61 | with zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["parent-collision.zip"])) as zipped: |
| 62 | self.assertEqual(zipped.namelist(), ["folder", "folder/child.txt"]) |
| 63 | self.assertTrue(all(not item.is_dir() for item in zipped.infolist())) |
| 64 | |
| 65 | def test_corrupt_sample_fails_crc(self): |
| 66 | with zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["bad-crc.zip"])) as zipped: |
| 67 | self.assertEqual(zipped.testzip(), "hello.txt") |
| 68 | |
| 69 | def test_truncated_sample_has_no_valid_central_directory(self): |
| 70 | with self.assertRaises(zipfile.BadZipFile): |
| 71 | zipfile.ZipFile(io.BytesIO(FACTORY.build_samples()["truncated.zip"])) |
| 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | unittest.main() |