44 lines
1.7 KiB
Python
44 lines
1.7 KiB
Python
"""合成私人暂存的字节、权限与清理边界。"""
|
|
|
|
import hashlib
|
|
import os
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
from muse.基础设施.受控文件 import 受控文件, 受控文件错误
|
|
|
|
|
|
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()
|
|
|
|
|
|
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(受控文件错误):
|
|
管理器.读取("../其他资料", 哈希)
|