Files
easy-tdx/tests/unit/test_decode_errors.py
T
GitHubandClaude Opus 4.7 50491f9aae feat!: rename project from xmtdx to easy-tdx
- Package directory: src/xmtdx/ -> src/easy_tdx/
- Import path: from easy_tdx import ...
- pip install easy-tdx
- Add LICENSE (MIT) with upstream attribution (pytdx, xmtdx)
- Add NOTICE with detailed attribution
- Update all examples, tests, scripts, docs
- Bump version to 1.0.0

BREAKING CHANGE: import path changed from `xmtdx` to `easy_tdx`

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 23:21:21 +08:00

52 lines
1.6 KiB
Python

"""坏包与解码异常回归测试。"""
from __future__ import annotations
from pathlib import Path
import pytest
from easy_tdx.codec.frame import FrameHeader, decompress_body
from easy_tdx.commands.company_info import GetCompanyInfoCategoryCmd
from easy_tdx.commands.security_count import GetSecurityCountCmd
from easy_tdx.commands.xdxr_info import GetXdxrInfoCmd
from easy_tdx.exceptions import TdxDecodeError
from easy_tdx.models.enums import Market
FIXTURES = Path(__file__).parent.parent / "fixtures"
def _load_hex(name: str) -> bytes:
return bytes.fromhex((FIXTURES / f"{name}.hex").read_text().strip())
def test_security_count_truncated_raises_tdxdecodeerror() -> None:
with pytest.raises(TdxDecodeError):
GetSecurityCountCmd(Market.SH).parse_response(b"")
def test_company_info_category_truncated_raises_tdxdecodeerror() -> None:
body = _load_hex("company_info_category")
cmd = GetCompanyInfoCategoryCmd(Market.SH, "600000")
with pytest.raises(TdxDecodeError):
cmd.parse_response(body[:-10])
def test_xdxr_info_truncated_raises_tdxdecodeerror() -> None:
body = _load_hex("xdxr_info")
cmd = GetXdxrInfoCmd(Market.SH, "600000")
with pytest.raises(TdxDecodeError):
cmd.parse_response(body[:-10])
def test_frame_bad_zlib_raises_tdxdecodeerror() -> None:
with pytest.raises(TdxDecodeError):
decompress_body(FrameHeader(0, 0, 0, 4, 8), b"xxxx")
def test_frame_unzipsize_mismatch_raises_tdxdecodeerror() -> None:
with pytest.raises(TdxDecodeError):
decompress_body(FrameHeader(0, 0, 0, 3, 4), b"abc")