闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
"""
|
|
tests/test_shipping_xfa_safe_regex.py
|
|
|
|
用途:锁定 XFA 安全正则模块——RE2 优先、restricted_re 子进程 fallback、禁止灾难回溯模式。
|
|
依赖:xfa_safe_regex。
|
|
逻辑:纯单测;不读 PDF;超时用例通过 monkeypatch 慢 worker,避免无限挂死。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.pure_unit
|
|
|
|
from app.services.shipping_notice.document_layout import xfa_safe_regex as safe_regex_mod
|
|
from app.services.shipping_notice.document_layout.xfa_safe_regex import (
|
|
SafeRegexError,
|
|
compile_safe_regex,
|
|
has_re2_engine,
|
|
search_safe,
|
|
validate_restricted_regex_pattern,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def force_restricted_re(monkeypatch: pytest.MonkeyPatch):
|
|
"""强制走 restricted_re 引擎,便于覆盖子进程匹配路径。"""
|
|
monkeypatch.setattr(safe_regex_mod, '_HAS_RE2', False)
|
|
|
|
|
|
def test_validate_rejects_forbidden_and_catastrophic_patterns():
|
|
"""命名捕获与嵌套量词必须拒绝。"""
|
|
with pytest.raises(SafeRegexError, match='forbidden'):
|
|
validate_restricted_regex_pattern(r'(?P<name>x)', max_length=128, max_groups=5)
|
|
with pytest.raises(SafeRegexError, match='catastrophic'):
|
|
validate_restricted_regex_pattern(r'(a+)+$', max_length=128, max_groups=5)
|
|
|
|
|
|
def test_compile_and_search_basic_capture():
|
|
"""可编译并捕获分组。"""
|
|
compiled = compile_safe_regex(r'^MPN:(.+)$', case_sensitive=True, max_length=128, max_groups=5)
|
|
matched = search_safe(compiled, 'MPN:ABC123', timeout_ms=50)
|
|
assert matched is not None
|
|
assert matched.group(1) == 'ABC123'
|
|
assert has_re2_engine() in (True, False)
|
|
|
|
|
|
def test_search_returns_none_when_no_match():
|
|
"""无匹配时返回 None。"""
|
|
compiled = compile_safe_regex(r'^MPN:(.+)$', case_sensitive=True, max_length=128, max_groups=5)
|
|
assert search_safe(compiled, 'NOPE', timeout_ms=50) is None
|
|
|
|
|
|
def test_case_insensitive_search():
|
|
"""不区分大小写时可命中。"""
|
|
compiled = compile_safe_regex(r'^mpn:(.+)$', case_sensitive=False, max_length=128, max_groups=5)
|
|
matched = search_safe(compiled, 'MPN:XYZ', timeout_ms=50)
|
|
assert matched is not None
|
|
assert matched.group(1) == 'XYZ'
|
|
|
|
|
|
def test_restricted_re_subprocess_basic_match(force_restricted_re):
|
|
"""restricted_re 子进程路径:基本捕获仍可用。"""
|
|
compiled = compile_safe_regex(r'^MPN:(.+)$', case_sensitive=True, max_length=128, max_groups=5)
|
|
assert compiled.engine == 'restricted_re'
|
|
matched = search_safe(compiled, 'MPN:ABC123', timeout_ms=500)
|
|
assert matched is not None
|
|
assert matched.group(1) == 'ABC123'
|
|
|
|
|
|
def test_restricted_re_subprocess_no_match(force_restricted_re):
|
|
"""restricted_re 子进程路径:无匹配时返回 None。"""
|
|
compiled = compile_safe_regex(r'^MPN:(.+)$', case_sensitive=True, max_length=128, max_groups=5)
|
|
assert search_safe(compiled, 'NOPE', timeout_ms=500) is None
|
|
|
|
|
|
def test_restricted_re_subprocess_timeout_kills_worker(force_restricted_re):
|
|
"""restricted_re 灾难回溯场景:超时抛 TimeoutError,且不等待完整回溯结束。"""
|
|
compiled = compile_safe_regex(r'(a|a)*X', case_sensitive=True, max_length=128, max_groups=5)
|
|
haystack = 'a' * 32 + 'Y'
|
|
started = time.monotonic()
|
|
with pytest.raises(TimeoutError, match='timed out'):
|
|
search_safe(compiled, haystack, timeout_ms=100)
|
|
elapsed = time.monotonic() - started
|
|
assert elapsed < 2.0
|
|
safe_regex_mod._destroy_restricted_pool()
|