Fix edition font UUID rebinding and Protocols translation layout

This commit is contained in:
Kai Tan
2026-10-10 00:42:37 +08:00
parent 26da365cb2
commit 3e2093dba8
12 changed files with 522 additions and 4 deletions
+40
View File
@@ -142,3 +142,43 @@ def test_headless_can_render_a_new_cover_without_a_source_counterpart(tmp_path):
assert result['sampled_documents']==[cover_page]
assert len(result['images'])==1 and result['images'][0]['book']=='translated'
assert (evidence/result['images'][0]['image']).exists()
def test_new_edition_uuid_keeps_obfuscated_font_readable_and_audited(tmp_path):
from src.edition.fonts import decoded_fonts
import uuid
source,cfg,image,review,_ = fixture(tmp_path)
old_id = 'urn:uuid:' + str(uuid.uuid4())
plain = b'\x00\x01\x00\x00' + bytes(range(256)) * 8
key = uuid.UUID(old_id.removeprefix('urn:uuid:')).bytes
encoded = bytes(byte ^ key[i % 16] if i < 1024 else byte for i,byte in enumerate(plain))
font_source = tmp_path/'fonts.epub'
with zipfile.ZipFile(source) as z, zipfile.ZipFile(font_source,'w') as target:
for info in z.infolist():
data = z.read(info.filename)
if info.filename == 'EPUB/package.opf':
data = data.replace(b'id="old-id">9780000000001', b'id="old-id">'+old_id.encode())
data = data.replace(b'</manifest>', b'<item id="font" href="fonts/face.ttf" media-type="font/ttf"/><item id="css" href="style.css" media-type="text/css"/></manifest>')
target.writestr(info,data)
target.writestr('EPUB/fonts/face.ttf',encoded)
target.writestr('EPUB/style.css', '@font-face {font-family: Face; src:url("fonts/face.ttf")}')
target.writestr('META-INF/encryption.xml', '<encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="http://ns.adobe.com/pdf/enc#RC"/><CipherData><CipherReference URI="EPUB/fonts/face.ttf"/></CipherData></EncryptedData></encryption>')
config=json.loads(cfg.read_text());config['source']['identifier']=old_id;cfg.write_text(json.dumps(config))
out=tmp_path/'new-fonts.epub';record=localize(font_source,out,cfg,'target_only',image,review)
assert record['identifier'] != old_id
assert audit_edition(out)['issues'] == []
assert audit_pair(font_source,out)[0]['new_issues'] == []
with zipfile.ZipFile(out) as z:
opf,root=read_package(out)
assert list(decoded_fonts(z,opf,root).values()) == [plain]
assert z.read('EPUB/fonts/face.ttf') == encoded
from src.qa.headless import archive_server
from urllib.request import urlopen
with archive_server({'edition': out}) as url:
with urlopen(url + '/edition/' + record['font_bindings'][0]['active_path']) as response:
assert response.read() == plain
damaged=tmp_path/'damaged-fonts.epub'
active=record['font_bindings'][0]['active_path']
with zipfile.ZipFile(out) as z,zipfile.ZipFile(damaged,'w') as target:
for info in z.infolist(): target.writestr(info,b'broken' if info.filename==active else z.read(info.filename))
assert any('font_binding' in issue for issue in audit_edition(damaged)['issues'])