"""合成私人暂存的排他发布、重复写入、字节、权限与清理边界。 任务中断后的恢复由任务运行集成套件验证;本文件不声明覆盖任务恢复。 """ import hashlib import os import uuid import pytest from muse.基础设施.受控文件 import 受控文件, 受控文件错误 @pytest.mark.case_id( "NC-private-bytes-atomic", environment="离线临时目录", given="独立私人目录与中文原文字节", when="按不透明对象和哈希重复发布、读取再清理", then=["字节和0600权限保留,异哈希拒绝,清理只删除该对象"], contract="docs/系统架构/新版设计/接口契约/原文证据生命周期.md", ) def test_内容哈希排他发布和重复写入保持字节__a80001(tmp_path) -> None: 管理器 = 受控文件(tmp_path / "私人暂存") 身份 = str(uuid.uuid4()) 原文 = '中文"引号"\n换行\\反斜杠'.encode() 哈希 = hashlib.sha256(原文).hexdigest() 管理器.写入(身份, 哈希, 原文) 管理器.写入(身份, 哈希, 原文) assert 管理器.读取(身份, 哈希) == 原文 assert (管理器.根 / 身份 / (哈希 + ".bin")).stat().st_mode & 0o777 == 0o600 with pytest.raises(受控文件错误): 管理器.写入(身份, 哈希, b"different") 管理器.删除对象(身份) assert not (管理器.根 / 身份).exists() @pytest.mark.case_id( "NC-private-no-symlink", environment="离线临时目录", given="本对象含指向其他资料的软链接", when="读取与清理该对象", then=["拒绝软链接与越界身份,其他资料不变"], contract="docs/系统架构/新版设计/接口契约/原文证据生命周期.md", ) def test_软链接和未知资料不会被清理或跟随__a80002(tmp_path) -> None: 管理器 = 受控文件(tmp_path / "私人暂存") 身份 = str(uuid.uuid4()) 原文 = b"synthetic" 哈希 = hashlib.sha256(原文).hexdigest() 管理器.写入(身份, 哈希, 原文) 外部 = tmp_path / "其他资料" 外部.write_bytes(b"preserve") (管理器.根 / 身份 / (哈希 + ".bin")).unlink() os.symlink(外部, 管理器.根 / 身份 / (哈希 + ".bin")) with pytest.raises(受控文件错误): 管理器.读取(身份, 哈希) with pytest.raises(受控文件错误): 管理器.删除对象(身份) assert 外部.read_bytes() == b"preserve" with pytest.raises(受控文件错误): 管理器.读取("../其他资料", 哈希)