Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
data_to_send = (
b'"Imagination does not breed insanity.'
b"Exactly what does breed insanity is reason."
b" Poets do not go mad; but chess-players do."
b"Mathematicians go mad, and cashiers;"
b' but creative artists very seldom. "'
)
# Given
server = MiniTestServer(proto="raw", host="lo")
server.data_to_send = "GKC"
server.bind()
# noinspection PyDeprecation
uut = SocketConnection(host="lo", proto="raw-l2", recv_timeout=0.1)
uut.logger = logging.getLogger("SulleyUTLogger")
# Assemble packet...
raw_packet = ethernet_frame(
payload=ip_packet(
payload=udp_packet(payload=data_to_send, src_port=server.active_port + 1, dst_port=server.active_port),
src_ip=b"\x7F\x00\x00\x01",
dst_ip=b"\x7F\x00\x00\x01",
),
src_mac=b"\x00" * 6,
dst_mac=b"\xff" * 6,
)
expected_server_receive = raw_packet
t = threading.Thread(target=functools.partial(server.receive_until, expected_server_receive))
t.daemon = True
When: Constructing SocketConnections with:
protocol types in [default, 'udp', 'tcp', 'ssl', 'raw-l2', 'raw-l3] and
no host argument.
Then: Constructor raises exception.
"""
# This method tests bad argument lists. Therefore we ignore
# PyArgumentList inspections.
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5)
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="tcp")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="udp")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="ssl")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="raw-l2")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="raw-l3")
def test_required_args_port(self):
"""
Given: No preconditions.
When: Constructing SocketConnections with:
protocol types in [default, 'udp', 'tcp', 'ssl'] and
no port argument.
Then: Constructor raises exception.
"""
with self.assertRaises(Exception):
SocketConnection(host="127.0.0.1")
with self.assertRaises(Exception):
SocketConnection(host="127.0.0.1", proto="tcp")
with self.assertRaises(Exception):
SocketConnection(host="127.0.0.1", proto="udp")
with self.assertRaises(Exception):
SocketConnection(host="127.0.0.1", proto="ssl")
Given: A SocketConnection 'raw-l2' object.
and: A raw UDP/IP/Ethernet packet of RAW_L2_MAX_PAYLOAD bytes.
and: A server socket created with AF_PACKET, SOCK_RAW, configured to respond.
When: Calling SocketConnection.open(), .send() with the valid UDP packet, .recv(), and .close()
Then: send() returns RAW_L2_MAX_PAYLOAD.
and: The server receives the raw packet data from send().
and: SocketConnection.recv() returns bytes('').
"""
# Given
server = MiniTestServer(proto="raw", host="lo")
server.data_to_send = "GKC"
server.bind()
# noinspection PyDeprecation
uut = SocketConnection(host="lo", proto="raw-l2", recv_timeout=0.1)
uut.logger = logging.getLogger("SulleyUTLogger")
data_to_send = b"1" * uut.max_send_size
# Assemble packet...
raw_packet = data_to_send
expected_server_receive = raw_packet
t = threading.Thread(target=functools.partial(server.receive_until, expected_server_receive))
t.daemon = True
t.start()
# When
uut.open()
send_result = uut.send(data=raw_packet)
received = uut.recv(10000)
uut.close()
When: Calling SocketConnection.open(), .send(), .recv(), and .close()
Then: send() returns RAW_L3_MAX_PAYLOAD.
and: Sent and received data is as expected.
"""
data_to_send = b"uuddlrlrba"
# Given
server = MiniTestServer()
server.bind()
t = threading.Thread(target=server.serve_once)
t.daemon = True
t.start()
# noinspection PyDeprecation
uut = SocketConnection(host=socket.gethostname(), port=server.active_port, proto="tcp")
uut.logger = logging.getLogger("SulleyUTLogger")
# When
uut.open()
send_result = uut.send(data=data_to_send)
received = uut.recv(10000)
uut.close()
# Wait for the other thread to terminate
t.join(THREAD_WAIT_TIMEOUT)
self.assertFalse(t.is_alive())
# Then
self.assertEqual(send_result, len(data_to_send))
self.assertEqual(data_to_send, server.received)
self.assertEqual(received, server.data_to_send)
data_to_send = helpers.str_to_bytes(
'"Never drink because you need it, for this is rational drinking, and the '
"way to death and hell. But drink because you do not need it, for this is "
'irrational drinking, and the ancient health of the world."'
)
# Given
server = MiniTestServer(proto="udp", host="")
server.bind()
t = threading.Thread(target=server.serve_once)
t.daemon = True
t.start()
# noinspection PyDeprecation
uut = SocketConnection(
host=broadcast_addr,
port=server.active_port,
proto="udp",
bind=("", server.active_port + 1),
udp_broadcast=True,
)
uut.logger = logging.getLogger("BoofuzzUTLogger")
# When
uut.open()
send_result = uut.send(data=data_to_send)
received = uut.recv(10000)
uut.close()
# Wait for the other thread to terminate
t.join(THREAD_WAIT_TIMEOUT)
Then: send() returns length of payload.
and: The server receives the raw packet data from send(), with an Ethernet header appended.
and: SocketConnection.recv() returns bytes('').
"""
data_to_send = (
b'"Imprudent marriages!" roared Michael. '
b'"And pray where in earth or heaven are there any prudent marriages?""'
)
# Given
server = MiniTestServer(proto="raw", host="lo")
server.data_to_send = "GKC"
server.bind()
# noinspection PyDeprecation
uut = SocketConnection(host="lo", proto="raw-l3")
uut.logger = logging.getLogger("SulleyUTLogger")
# Assemble packet...
raw_packet = ip_packet(
payload=udp_packet(payload=data_to_send, src_port=server.active_port + 1, dst_port=server.active_port),
src_ip=b"\x7F\x00\x00\x01",
dst_ip=b"\x7F\x00\x00\x01",
)
expected_server_receive = b"\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00" + raw_packet
t = threading.Thread(target=functools.partial(server.receive_until, expected_server_receive))
t.daemon = True
t.start()
# When
uut.open()
Given: A SocketConnection 'raw-l3' object.
and: A raw UDP/IP packet of RAW_L3_MAX_PAYLOAD bytes.
and: A server socket created with AF_PACKET, SOCK_RAW, configured to respond.
When: Calling SocketConnection.open(), .send() with the valid UDP packet, .recv(), and .close()
Then: send() returns RAW_L3_MAX_PAYLOAD bytes.
and: The server receives the raw packet data from send(), with an Ethernet header appended.
and: SocketConnection.recv() returns bytes('').
"""
# Given
server = MiniTestServer(proto="raw", host="lo")
server.data_to_send = "GKC"
server.bind()
# noinspection PyDeprecation
uut = SocketConnection(host="lo", proto="raw-l3")
uut.logger = logging.getLogger("SulleyUTLogger")
data_to_send = b"0" * uut.packet_size
# Assemble packet...
raw_packet = data_to_send
expected_server_receive = b"\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00" + raw_packet
t = threading.Thread(target=functools.partial(server.receive_until, expected_server_receive))
t.daemon = True
t.start()
# When
uut.open()
send_result = uut.send(data=raw_packet)
received = uut.recv(10000)
uut.close()
Given: A SocketConnection 'raw-l2' object.
and: A raw UDP/IP/Ethernet packet of RAW_L2_MAX_PAYLOAD + 1 bytes.
and: A server socket created with AF_PACKET, SOCK_RAW, configured to respond.
When: Calling SocketConnection.open(), .send() with the valid UDP packet, .recv(), and .close()
Then: send() returns RAW_L2_MAX_PAYLOAD.
and: The server receives the first RAW_L2_MAX_PAYLOAD bytes of raw packet data from send().
and: SocketConnection.recv() returns bytes('').
"""
# Given
server = MiniTestServer(proto="raw", host="lo")
server.data_to_send = "GKC"
server.bind()
# noinspection PyDeprecation
uut = SocketConnection(host="lo", proto="raw-l2", recv_timeout=0.1)
uut.logger = logging.getLogger("SulleyUTLogger")
data_to_send = b"F" * (uut.max_send_size + 1)
# Assemble packet...
raw_packet = data_to_send
expected_server_receive = raw_packet[: uut.max_send_size]
t = threading.Thread(target=functools.partial(server.receive_until, expected_server_receive))
t.daemon = True
t.start()
# When
uut.open()
send_result = uut.send(data=raw_packet)
received = uut.recv(10000)
uut.close()
def test_required_args_host(self):
"""
Given: No preconditions.
When: Constructing SocketConnections with:
protocol types in [default, 'udp', 'tcp', 'ssl', 'raw-l2', 'raw-l3] and
no host argument.
Then: Constructor raises exception.
"""
# This method tests bad argument lists. Therefore we ignore
# PyArgumentList inspections.
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5)
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="tcp")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="udp")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="ssl")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="raw-l2")
with self.assertRaises(Exception):
# noinspection PyArgumentList
SocketConnection(port=5, proto="raw-l3")