|
1 | 1 | import os |
2 | 2 | import shutil |
| 3 | +import tempfile |
3 | 4 | from pathlib import Path |
4 | 5 | from unittest.mock import Mock, patch |
5 | 6 |
|
6 | 7 | import pytest |
| 8 | +from hypothesis import assume, given |
| 9 | +from hypothesis import strategies as st |
7 | 10 | from tests._zipfile_shim import reload_zipfile |
8 | 11 |
|
9 | 12 | from config.config_manager import LIBRARY_BASE_PATH, Config |
| 13 | +from handler.filesystem.base_handler import ( |
| 14 | + LANGUAGES_BY_SHORTCODE, |
| 15 | + REGIONS_BY_SHORTCODE, |
| 16 | +) |
10 | 17 | from handler.filesystem.roms_handler import ( |
11 | 18 | FileHash, |
12 | 19 | FSRomsHandler, |
@@ -1546,3 +1553,99 @@ def test_extract_chd_hash_max_sha1(self, tmp_path): |
1546 | 1553 | assert result |
1547 | 1554 | assert result == "f" * 40 |
1548 | 1555 | assert len(result) == 40 |
| 1556 | + |
| 1557 | + |
| 1558 | +KNOWN_REGION_NAMES = frozenset(REGIONS_BY_SHORTCODE.values()) |
| 1559 | +KNOWN_LANGUAGE_NAMES = frozenset(LANGUAGES_BY_SHORTCODE.values()) |
| 1560 | + |
| 1561 | +region_code = st.sampled_from(sorted(REGIONS_BY_SHORTCODE)) |
| 1562 | +language_code = st.sampled_from(sorted(LANGUAGES_BY_SHORTCODE)) |
| 1563 | + |
| 1564 | + |
| 1565 | +class TestParseTagsProperties: |
| 1566 | + """Property-based tests for FSRomsHandler.parse_tags.""" |
| 1567 | + |
| 1568 | + handler = FSRomsHandler() |
| 1569 | + |
| 1570 | + @given(st.text()) |
| 1571 | + def test_never_raises_on_arbitrary_input(self, fs_name: str): |
| 1572 | + self.handler.parse_tags(fs_name) |
| 1573 | + |
| 1574 | + @given(st.text()) |
| 1575 | + def test_is_deterministic(self, fs_name: str): |
| 1576 | + assert self.handler.parse_tags(fs_name) == self.handler.parse_tags(fs_name) |
| 1577 | + |
| 1578 | + @given(st.lists(region_code), st.lists(language_code)) |
| 1579 | + def test_known_codes_map_to_known_names(self, regions, languages): |
| 1580 | + fs_name = "Game" |
| 1581 | + for code in regions: |
| 1582 | + fs_name += f"({code})" |
| 1583 | + for code in languages: |
| 1584 | + fs_name += f"({code})" |
| 1585 | + fs_name += ".rom" |
| 1586 | + |
| 1587 | + parsed = self.handler.parse_tags(fs_name) |
| 1588 | + |
| 1589 | + assert set(parsed.regions) <= KNOWN_REGION_NAMES |
| 1590 | + assert set(parsed.languages) <= KNOWN_LANGUAGE_NAMES |
| 1591 | + # Every supplied code resolves to its mapped full name. |
| 1592 | + assert {REGIONS_BY_SHORTCODE[c] for c in regions} <= set(parsed.regions) |
| 1593 | + assert {LANGUAGES_BY_SHORTCODE[c] for c in languages} <= set(parsed.languages) |
| 1594 | + |
| 1595 | + |
| 1596 | +def _chd_header_bytes(version: int, sha1: bytes) -> bytes: |
| 1597 | + """Build a 124-byte CHD header with the given version and combined SHA1.""" |
| 1598 | + header = bytearray(124) |
| 1599 | + header[0:8] = b"MComprHD" |
| 1600 | + header[12:16] = version.to_bytes(4, "big") |
| 1601 | + header[84:104] = sha1 |
| 1602 | + return bytes(header) |
| 1603 | + |
| 1604 | + |
| 1605 | +def _run_extract(data: bytes) -> str: |
| 1606 | + fd, path = tempfile.mkstemp(suffix=".chd") |
| 1607 | + try: |
| 1608 | + with os.fdopen(fd, "wb") as f: |
| 1609 | + f.write(data) |
| 1610 | + return extract_chd_hash(Path(path)) |
| 1611 | + finally: |
| 1612 | + os.unlink(path) |
| 1613 | + |
| 1614 | + |
| 1615 | +class TestExtractCHDHashProperties: |
| 1616 | + """Property-based tests for extract_chd_hash.""" |
| 1617 | + |
| 1618 | + @given(sha1=st.binary(min_size=20, max_size=20)) |
| 1619 | + def test_valid_v5_returns_embedded_sha1(self, sha1: bytes): |
| 1620 | + result = _run_extract(_chd_header_bytes(5, sha1)) |
| 1621 | + assert result == sha1.hex() |
| 1622 | + assert len(result) == 40 |
| 1623 | + |
| 1624 | + @given( |
| 1625 | + version=st.integers(min_value=0, max_value=2**32 - 1), |
| 1626 | + sha1=st.binary(min_size=20, max_size=20), |
| 1627 | + ) |
| 1628 | + def test_non_v5_version_returns_empty(self, version: int, sha1: bytes): |
| 1629 | + assume(version != 5) |
| 1630 | + assert _run_extract(_chd_header_bytes(version, sha1)) == "" |
| 1631 | + |
| 1632 | + @given(data=st.binary(min_size=16)) |
| 1633 | + def test_wrong_signature_returns_empty(self, data: bytes): |
| 1634 | + assume(data[:8] != b"MComprHD") |
| 1635 | + assert _run_extract(data) == "" |
| 1636 | + |
| 1637 | + @given(data=st.binary(max_size=15)) |
| 1638 | + def test_too_short_for_signature_returns_empty(self, data: bytes): |
| 1639 | + assert _run_extract(data) == "" |
| 1640 | + |
| 1641 | + @given( |
| 1642 | + sha1=st.binary(min_size=20, max_size=20), |
| 1643 | + truncate_len=st.integers(min_value=16, max_value=103), |
| 1644 | + ) |
| 1645 | + def test_truncated_before_sha1_returns_empty(self, sha1: bytes, truncate_len: int): |
| 1646 | + truncated = _chd_header_bytes(5, sha1)[:truncate_len] |
| 1647 | + assert _run_extract(truncated) == "" |
| 1648 | + |
| 1649 | + @given(data=st.binary()) |
| 1650 | + def test_never_raises_on_arbitrary_bytes(self, data: bytes): |
| 1651 | + _run_extract(data) |
0 commit comments