def test_socks4_receive_data(request_reply_code: bytes) -> None: conn = SOCKS4Connection(user_id=b"socks") reply = conn.receive_data(b"".join([ b"\x00", request_reply_code, (8080).to_bytes(2, byteorder="big"), b"\x7f\x00\x00\x01", ])) assert reply == SOCKS4Reply(reply_code=SOCKS4ReplyCode(request_reply_code), port=8080, addr="127.0.0.1")
def test_socks4_connection_request(command: SOCKS4Command) -> None: conn = SOCKS4Connection(user_id=b"socks") request = SOCKS4Request.from_address(command=command, address=("127.0.0.1", 8080)) conn.send(request) data = conn.data_to_send() assert len(data) == 9 + 5 assert data[0:1] == b"\x04" assert data[1:2] == command assert data[2:4] == (8080).to_bytes(2, byteorder="big") assert data[4:8] == b"\x7f\x00\x00\x01" assert data[8:13] == b"socks" assert data[13] == 0
def test_SOCKS4A_connection_request(command: SOCKS4Command) -> None: conn = SOCKS4Connection(user_id=b"socks") request = SOCKS4ARequest.from_address(command=command, address=("proxy.example.com", 8080)) conn.send(request) data = conn.data_to_send() assert len(data) == 32 assert data[0:1] == b"\x04" assert data[1:2] == command assert data[2:4] == (8080).to_bytes(2, byteorder="big") assert data[4:8] == b"\x00\x00\x00\xFF" assert data[8:14] == b"socks\x00" assert data[14:] == b"proxy.example.com\x00"
def test_socks4_receive_malformed_data(data: bytes) -> None: conn = SOCKS4Connection(user_id=b"socks") with pytest.raises(ProtocolError): conn.receive_data(data)