feat: 落实决策216/219/227取价锚点与公式口径,增加商业版权益和源码完整性校验

This commit is contained in:
lianjianwei2024
2026-10-27 12:16:47 +08:00
parent 5d9eaf774b
commit 380a39f8db
36 changed files with 18821 additions and 879 deletions

View File

@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
"""version_gate 强制更新门禁测试(2026-10-27,《企业和个人版.md》5.9)。
覆盖:版本比较、开发态旁路、无缓存 fail-open、两段式拦截(GET 页/POST JSON)、
缓存落盘回读、拉取失败沿用旧缓存、放行清单、status 形状、非法信息文件拒收。
"""
import json
import os
import sys
from types import SimpleNamespace
import pytest
HERE = os.path.dirname(os.path.abspath(__file__))
SERVER_DIR = os.path.join(os.path.dirname(HERE), "server")
if SERVER_DIR not in sys.path:
sys.path.insert(0, SERVER_DIR)
import version_gate as vg # noqa: E402
def make_build(app="1.2.0", url="https://releases.example/prd.json", **kw):
return SimpleNamespace(APP_VERSION=app, VERSION_INFO_URL=url, **kw)
@pytest.fixture(autouse=True)
def fresh(tmp_path):
"""每个用例独立 data_dir + 复位全局状态,不起后台线程。"""
vg._cache = None
vg._cfg.update({"app_version": None, "info_url": None,
"interval": vg.DEFAULT_INTERVAL, "data_dir": str(tmp_path)})
yield vg
vg._cache = None
INFO = {"latest_version": "1.3.0", "min_required_version": "1.2.5",
"release_notes": "修复", "package_url": "https://x/pkg.zip", "sha256": "ab"}
def ok_get(url):
return dict(INFO)
# ---------------------------------------------------------------- 版本比较
def test_vkey():
assert vg.vkey("1.2.10") == (1, 2, 10)
assert vg.vkey("v1.3") == (1, 3)
assert vg.vkey("2.0.0") > vg.vkey("1.9.9")
assert vg.vkey("junk") == ()
assert vg.vkey(None) == ()
# ---------------------------------------------------------------- 开发态旁路
def test_dev_bypass_no_build(tmp_path):
vg.init(str(tmp_path), build=None, start_thread=False)
assert vg.effective() is False
assert vg.intercept("GET", "/admin.html") is None
ok, info = vg.check()
assert ok and info.get("bypass") == "dev"
def test_dev_bypass_version_dev(tmp_path):
vg.init(str(tmp_path), build=make_build(app="dev"), start_thread=False)
assert vg.effective() is False
# ---------------------------------------------------------------- fail-open:无缓存不拦
def test_no_cache_never_blocks(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.0.0"), start_thread=False)
assert vg.intercept("GET", "/admin.html") is None # 从未拉到且无快照 → 不拦
def test_bad_min_required_fail_open(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.0.0"), start_thread=False)
vg._save_cache({"latest_version": "9", "min_required_version": "x.y",
"fetched_at": "t"}) # 最低要求解析失败
assert vg.intercept("GET", "/admin.html") is None # 视为 0,不拦
# ---------------------------------------------------------------- 两段式拦截
def test_forced_blocks_pages_and_api(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.2.0"), start_thread=False)
vg.fetch_once(http_get=ok_get) # min_required=1.2.5 > 1.2.0
g = vg.intercept("GET", "/admin.html")
assert g and g["kind"] == "html" and "升级" in g["text"]
p = vg.intercept("POST", "/api/session/bill/op")
assert p and p["kind"] == "json" and p["code"] == 403
assert p["obj"]["error"]["code"] == "UPDATE_REQUIRED" # API 绝不回 HTML
def test_ok_version_passes(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.3.0"), start_thread=False)
vg.fetch_once(http_get=ok_get) # 1.3.0 >= 1.2.5
assert vg.intercept("GET", "/admin.html") is None
assert vg.intercept("POST", "/api/session/save") is None
def test_always_paths_pass_even_when_forced(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.0.0"), start_thread=False)
vg.fetch_once(http_get=ok_get)
assert vg.intercept("GET", "/api/license/status") is None
assert vg.intercept("GET", "/api/version") is None
assert vg.intercept("GET", "/api/version_info") is None
assert vg.intercept("POST", "/api/license/activate") is None
# ---------------------------------------------------------------- 拉取与缓存
def test_fetch_persists_cache(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.3.0"), start_thread=False)
assert vg.fetch_once(http_get=ok_get) is True
fp = os.path.join(str(tmp_path), vg.CACHE_NAME)
d = json.load(open(fp, encoding="utf-8"))
assert d["min_required_version"] == "1.2.5" and d["fetched_at"]
# 重启(重新 init/load)后缓存仍生效
vg._cache = None
vg.load_cache()
assert vg.status()["min_required"] == "1.2.5"
def test_fetch_failure_keeps_old_cache(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.2.0"), start_thread=False)
assert vg.fetch_once(http_get=ok_get) is True
def dead(url):
raise OSError("断网")
assert vg.fetch_once(http_get=dead) is False # 失败静默
assert vg.intercept("GET", "/admin.html") is not None # 旧缓存仍判强制
def test_fetch_rejects_malformed_info(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.2.0"), start_thread=False)
for bad in ({"latest_version": 3}, {"min_required_version": None}, [1, 2], "x"):
assert vg.fetch_once(http_get=lambda u, b=bad: b) is False
assert vg._cache is None # 非法信息绝不落缓存
# ---------------------------------------------------------------- status
def test_status_shape(tmp_path):
vg.init(str(tmp_path), build=make_build(app="1.2.8"), start_thread=False)
vg.fetch_once(http_get=ok_get)
s = vg.status()
assert s["current"] == "1.2.8" and s["latest"] == "1.3.0"
assert s["min_required"] == "1.2.5" and s["forced"] is False
assert s["update_available"] is True # 1.2.8 < 1.3.0 有新版
assert s["package_url"] == INFO["package_url"]