87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Prove the source Toy rows reach both build 70334 hotfix reply paths."""
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path[:0] = [str(ROOT / 'ops'), str(ROOT / 'tests')]
|
|
from build_source_toy_compat import INPUT, OUTPUT, build
|
|
from native_codec_driver import MODES, load, serialize
|
|
|
|
|
|
def run(codec):
|
|
os.environ['GW_TEST_CODEC_PATH'] = str(codec)
|
|
component = json.loads(INPUT.read_text())
|
|
push, header, pages = build()
|
|
assert OUTPUT.read_text() == header
|
|
assert len(pages) == 1 and pages[0]['count'] == 72
|
|
expected = {row['id']: (bytes.fromhex(row['wire_hex']), row['sha256'])
|
|
for row in component['records']}
|
|
assert len(expected) == 72
|
|
for wire, digest in expected.values():
|
|
assert hashlib.sha256(wire).hexdigest() == digest
|
|
|
|
library = load()
|
|
available = serialize(library, MODES.index('--native-client-initialization'),
|
|
struct.pack('<IIQ', 0x01000031, 0, 0))
|
|
count = struct.unpack_from('<I', available)[0]
|
|
offset = 4
|
|
available_body = None
|
|
for _ in range(count):
|
|
opcode, size = struct.unpack_from('<II', available, offset)
|
|
offset += 8
|
|
body = available[offset:offset + size]
|
|
offset += size
|
|
if opcode == 0x4B0001:
|
|
available_body = body
|
|
assert offset == len(available) and available_body is not None
|
|
realm, push_count = struct.unpack_from('<II', available_body)
|
|
assert realm == 0x01000031 and len(available_body) == 8 + 8 * push_count
|
|
advertised = {struct.unpack_from('<II', available_body, 8 + 8 * i)[0]
|
|
for i in range(push_count)}
|
|
assert push in advertised
|
|
|
|
reply = serialize(library, MODES.index('--native-hotfix-reply'),
|
|
struct.pack('<4I', 70334, 0, 1, push))
|
|
row_count = struct.unpack_from('<I', reply)[0]
|
|
assert row_count == len(expected)
|
|
offset = 4
|
|
metadata = []
|
|
for index in range(1, row_count + 1):
|
|
push_id, unique, table, ident, size, status = struct.unpack_from('<IIIIIB', reply, offset)
|
|
offset += 21
|
|
assert (push_id, unique, table, size, status) == (push, index, 0x7B03245C, 14, 0x20)
|
|
metadata.append(ident)
|
|
body_size = struct.unpack_from('<I', reply, offset)[0]
|
|
offset += 4
|
|
body = reply[offset:]
|
|
assert len(body) == body_size == row_count * 14
|
|
offset = 0
|
|
seen = set()
|
|
for ident in metadata:
|
|
wire = body[offset:offset + 14]
|
|
offset += 14
|
|
assert ident in expected and wire == expected[ident][0] and ident not in seen
|
|
seen.add(ident)
|
|
assert offset == len(body) and seen == set(expected)
|
|
|
|
for ident, (raw, _) in expected.items():
|
|
db_reply = serialize(library, MODES.index('--native-db-reply-missing'),
|
|
struct.pack('<III', 0x7B03245C, ident, 12345))
|
|
assert struct.unpack_from('<III', db_reply) == (0x7B03245C, ident, 12345)
|
|
assert db_reply[12] == 0x20
|
|
assert struct.unpack_from('<I', db_reply, 13)[0] == len(raw)
|
|
assert db_reply[17:] == raw
|
|
|
|
print('PASS: 72 Toy rows advertised, bulk-served, and returned byte-exact by DBReply.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 2:
|
|
raise SystemExit('usage: source_toy_hotfix_fixture.py CODEC_LIBRARY')
|
|
run(Path(sys.argv[1]))
|