Files
WoW-Hybrid-gateway/tests/codec_protocol_archive_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

83 lines
5.7 KiB
Python

#!/usr/bin/env python3
import hashlib,subprocess,sys,tempfile,unittest
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
from build_forever_create import protocol_archive
class ProtocolArchiveTest(unittest.TestCase):
def test_regular_archive_identity(self):
with tempfile.TemporaryDirectory() as t:
p=Path(t)/'matching.a';p.write_bytes(b'!<arch>\nfixture')
selected,meta=protocol_archive(p)
self.assertEqual(selected,p.resolve());self.assertEqual(meta['sha256'],hashlib.sha256(p.read_bytes()).hexdigest());self.assertEqual(meta['bytes'],15)
def test_refuse_missing_directory_bad_magic_and_symlink(self):
with tempfile.TemporaryDirectory() as t:
root=Path(t);p=root/'bad.a';p.write_bytes(b'not-library');link=root/'alias.a';link.symlink_to(p)
for path in [root/'missing',root,p,link]:
with self.subTest(path=path.name),self.assertRaises(ValueError):protocol_archive(path)
def test_compiled_record_layout_must_match_protocol_archive(self):
root=Path(__file__).resolve().parents[1]
with tempfile.TemporaryDirectory() as t:
tmp=Path(t);include=root/'include';consumer=tmp/'consumer.cpp'
consumer.write_text('#include "gateway/object_updates.hpp"\n'
'int main(){ gateway::require_legacy_object_update_layout(nullptr); }\n')
# Compile the real protocol producer with the same optimized/linker
# section options used by the codec and exercise the actual symbol.
obj=tmp/'object_updates.o';archive=tmp/'matching.a';binary=tmp/'matching'
subprocess.run(['c++','-std=c++20','-O2','-Werror','-ffunction-sections',
'-fdata-sections','-I'+str(include),'-c',
str(root/'src/object_updates.cpp'),'-o',str(obj)],check=True,capture_output=True)
subprocess.run(['ar','rcs',str(archive),str(obj)],check=True,capture_output=True)
command=['clang++','-std=c++20','-O2','-Werror','-I'+str(include),
str(consumer),str(archive),'-Wl,--gc-sections,--no-undefined','-o',str(binary)]
subprocess.run(command,check=True,capture_output=True)
subprocess.run([str(binary)],check=True,capture_output=True)
# Build a producer with the source-transport member added or
# removed, as when the protocol archive and serializer used
# different record definitions. The consumer must fail to link.
stale=tmp/'stale/gateway';stale.mkdir(parents=True)
header=(include/'gateway/object_updates.hpp').read_text()
transport=' std::optional<SourceTransportSnapshot> source_transport;\n'
movement=' std::optional<GroundMovement> movement;\n'
self.assertEqual(header.count(movement),1)
if transport in header:
self.assertEqual(header.count(transport),1)
stale_header=header.replace(transport,'')
else:
stale_header=header.replace(movement,movement+' std::optional<uint64_t> source_transport;\n')
(stale/'object_updates.hpp').write_text(stale_header)
shim=tmp/'stale.cpp';shim.write_text(
'#include "gateway/object_updates.hpp"\n'
'namespace gateway { void require_legacy_object_update_layout('
'LegacyObjectUpdateLayout*) noexcept {} }\n')
stale_obj=tmp/'stale.o';stale_archive=tmp/'stale.a'
subprocess.run(['c++','-std=c++20','-O2','-Werror','-I'+str(tmp/'stale'),
'-I'+str(include),'-c',str(shim),'-o',str(stale_obj)],check=True,capture_output=True)
subprocess.run(['ar','rcs',str(stale_archive),str(stale_obj)],check=True,capture_output=True)
mismatched=subprocess.run(['clang++','-std=c++20','-O2','-Werror','-I'+str(include),
str(consumer),str(stale_archive),'-Wl,--gc-sections,--no-undefined',
'-o',str(tmp/'mismatched')],capture_output=True,text=True)
self.assertNotEqual(mismatched.returncode,0)
self.assertIn('require_legacy_object_update_layout',mismatched.stderr)
# Native translation units opt into the shared guard, including
# units that instantiate the record only through a destructor.
# Unrelated users of the public header must not acquire a link
# dependency on the protocol archive.
native=tmp/'native.cpp';native.write_text(
'#include "object_update_layout_guard.hpp"\nint main(){}\n')
native_command=['clang++','-std=c++20','-O2','-Werror','-I'+str(root/'native'),
'-I'+str(include),str(native)]
suffix=['-Wl,--gc-sections,--no-undefined','-o',str(tmp/'native')]
subprocess.run(native_command+[str(archive)]+suffix,check=True,capture_output=True)
bad_native=subprocess.run(native_command+[str(stale_archive)]+suffix,
capture_output=True,text=True)
self.assertNotEqual(bad_native.returncode,0)
self.assertIn('require_legacy_object_update_layout',bad_native.stderr)
unrelated=tmp/'unrelated.cpp';unrelated.write_text(
'#include "gateway/object_updates.hpp"\nint main(){}\n')
subprocess.run(['clang++','-std=c++20','-O2','-Werror','-I'+str(include),
str(unrelated),'-Wl,--gc-sections,--no-undefined',
'-o',str(tmp/'unrelated')],check=True,capture_output=True)
if __name__=='__main__':unittest.main()