54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Force the real client to reject the invalid group before fixture trailer writes.
|
|
No sleeps, synthetic connection faults, retries, or changed production deadlines.
|
|
"""
|
|
import select
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
def check_rejection_schedule(driver, fixture):
|
|
fragmented = fixture.fragmented
|
|
boundaries = []
|
|
|
|
def rejection_boundary(sock, data):
|
|
# challenge status3 + B32 + generator-width/generator/modulus-width3
|
|
# + modulus32. The real client rejects a zero modulus at this point.
|
|
end = 70
|
|
if len(data) >= end and data[:3] == bytes(3) and data[35:38] == b'\1\7\x20' and data[38:70] == bytes(32):
|
|
fragmented(sock, data[:end])
|
|
assert sock.recv(1) == b'', 'client did not reject the invalid modulus'
|
|
boundaries.append(end)
|
|
if len(data) > end:
|
|
# The original fixture writes after rejection. Let the real
|
|
# TCP peer report RST before the next byte, rather than relying
|
|
# on a lucky thread timeslice to expose EPIPE.
|
|
sock.sendall(data[end:end + 1])
|
|
poll = select.poll()
|
|
poll.register(sock, select.POLLERR)
|
|
events = poll.poll(int(sock.gettimeout() * 1000))
|
|
assert any(flags & select.POLLERR for _, flags in events), 'closed peer did not report the trailer write'
|
|
fragmented(sock, data[end + 1:])
|
|
return
|
|
fragmented(sock, data)
|
|
|
|
fixture.fragmented = rejection_boundary
|
|
try:
|
|
with tempfile.TemporaryDirectory(prefix='gateway-rejection-schedule-') as tmp:
|
|
directory = Path(tmp)
|
|
(directory / 'password').write_bytes(fixture.PASSWORD + b'\n')
|
|
(directory / 'version').write_bytes(fixture.VERSION)
|
|
(directory / 'addons').write_bytes(fixture.ADDONS)
|
|
fixture.run_case(driver, directory, 'wrong-group', discover=True,
|
|
auth_host=True, ipv4=True)
|
|
finally:
|
|
fixture.fragmented = fragmented
|
|
assert boundaries == [70], 'required rejection boundary was not exercised'
|
|
print('invalid-group rejection boundary and fixture lifetime PASS')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import loopback_fixture
|
|
check_rejection_schedule(sys.argv[1], loopback_fixture)
|