def test_unpack_addr(): addr = ("232.32.9.86", 49238) assert unpack_addr(pack_addr(addr))[0] == addr addr = ("google.com", 80) assert unpack_addr(pack_addr(addr))[0] == addr with pytest.raises(Exception): unpack_addr(b"\x02")
def test_addr_reader(): addrs = ( ("127.0.0.1", 8000), ("example.com", 7999), ("1050::5:600:300c:326b", 8889), ) for addr in addrs: addr_bytes = pack_addr(addr) addr_parser = addr_reader.parser() length = len(addr_bytes) // 2 addr_parser.send(addr_bytes[:length]) assert not addr_parser.has_result tail = os.urandom(10) addr_parser.send(addr_bytes[length:] + tail) assert addr_parser.has_result new_addr, _ = addr_parser.get_result() assert new_addr == addr
def test_pack_addr(): assert pack_addr(("127.0.0.1", 8080)) == b"\x01\x7f\x00\x00\x01\x1f\x90" ipv6 = "1050:0:0:0:5:600:300c:326b" data = pack_addr((ipv6, 80)) assert ip_address(unpack_addr(data)[0][0]) == ip_address(ipv6)