示例#1
0
 def test_optional_args_port(self):
     """
     Given: No preconditions.
     When: Constructing SocketConnections with:
           protocol types in ['raw-l2', 'raw-l3'] and
           no port argument.
     Then: Constructor raises no exception.
     """
     SocketConnection(host='127.0.0.1', proto='raw-l2')
     SocketConnection(host='127.0.0.1', proto='raw-l3')
示例#2
0
    def test_tcp_client(self):
        """
        Given: A SocketConnection 'tcp' object and a TCP server.
        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 = six.binary_type(b'uuddlrlrba')

        # Given
        server = MiniTestServer()
        server.bind()

        t = threading.Thread(target=server.serve_once)
        t.daemon = True
        t.start()

        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.isAlive())

        # Then
        self.assertEqual(send_result, len(data_to_send))
        self.assertEqual(data_to_send, server.received)
        self.assertEqual(received, server.data_to_send)
示例#3
0
    def test_tcp_client_timeout(self):
        """
        Given: A SocketConnection 'tcp' object and a TCP server, set not to respond.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent works as expected, and recv() returns bytes('') after timing out.
        """
        data_to_send = b"uuddlrlrba"

        # Given
        server = MiniTestServer(stay_silent=True)
        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, b"")
示例#4
0
    def test_raw_l2(self):
        """
        Test 'raw' protocol with the loopback interface 'lo'.

        Given: A SocketConnection 'raw-l2' object.
          and: A raw UDP/IP/Ethernet packet.
          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 length of payload.
         and: The server receives the raw packet data from send().
         and: SocketConnection.recv() returns bytes('').
        """
        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
        t.start()

        # When
        uut.open()
        send_result = uut.send(data=raw_packet)
        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(expected_server_receive))
        self.assertEqual(raw_packet, server.received)
        self.assertEqual(received, b"")
    def test_tcp_client_timeout(self):
        """
        Given: A SocketConnection 'tcp' object and a TCP server, set not to respond.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent works as expected, and recv() returns bytes('') after timing out.
        """
        data_to_send = bytes('uuddlrlrba')

        # Given
        server = MiniTestServer(stay_silent=True)
        server.bind()

        t = threading.Thread(target=server.serve_once)
        t.daemon = True
        t.start()

        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.isAlive())

        # Then
        self.assertEqual(send_result, len(data_to_send))
        self.assertEqual(data_to_send, server.received)
        self.assertEqual(received, bytes(''))
    def test_raw_l3(self):
        """
        Test 'raw' protocol with the loopback interface 'lo'.

        Given: A SocketConnection 'raw-l3' object.
          and: A raw UDP/IP packet.
          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 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 = bytes(
            '"Imprudent marriages!" roared Michael. "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()

        uut = SocketConnection(host="lo",
                               port=socket_connection.ETH_P_IP,
                               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="\x7F\x00\x00\x01",
                               dst_ip="\x7F\x00\x00\x01")
        expected_server_receive = '\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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, len(raw_packet))
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, bytes(''))
示例#7
0
 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')
    def test_raw_l2(self):
        """
        Test 'raw' protocol with the loopback interface 'lo'.

        Given: A SocketConnection 'raw-l2' object.
          and: A raw UDP/IP/Ethernet packet.
          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 length of payload.
         and: The server receives the raw packet data from send().
         and: SocketConnection.recv() returns bytes('').
        """
        data_to_send = bytes('"Imagination does not breed insanity. Exactly what does breed insanity is reason.'
                             ' Poets do not go mad; but chess-players do. Mathematicians go mad, and cashiers;'
                             ' but creative artists very seldom. "')

        # Given
        server = MiniTestServer(proto='raw', host='lo')
        server.data_to_send = "GKC"
        server.bind()

        uut = SocketConnection(host="lo", port=socket_connection.ETH_P_IP, proto='raw-l2')
        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="\x7F\x00\x00\x01",
                        dst_ip="\x7F\x00\x00\x01"),
                src_mac="\x00" * 6,
                dst_mac="\xff" * 6)
        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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, len(expected_server_receive))
        self.assertEqual(raw_packet, server.received)
        self.assertEqual(received, bytes(''))
示例#9
0
    def test_udp_broadcast_client(self):
        """
        Given: A SocketConnection 'udp' object with udp_broadcast set, and a UDP server.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent and received data is as expected.
        """
        try:
            broadcast_addr = six.next(get_local_non_loopback_ipv4_addresses_info())["broadcast"]
        except StopIteration:
            assert False, TEST_ERR_NO_NON_LOOPBACK_IPV4

        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)
        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)
    def test_raw_l3_oversized(self):
        """
        Test 'raw-l3' max packet size.

        Given: A SocketConnection 'raw-l3' object.
          and: A raw UDP/IP packet of RAW_L3_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_L3_MAX_PAYLOAD.
         and: The server receives the raw packet data from send(), with an Ethernet header appended.
         and: SocketConnection.recv() returns bytes('').
        """
        data_to_send = bytes('D' * (RAW_L3_MAX_PAYLOAD + 1))

        # Given
        server = MiniTestServer(proto='raw', host='lo')
        server.data_to_send = "GKC"
        server.bind()

        uut = SocketConnection(host="lo",
                               port=socket_connection.ETH_P_IP,
                               proto='raw-l3')
        uut.logger = logging.getLogger("SulleyUTLogger")

        # Assemble packet...
        raw_packet = data_to_send
        expected_server_receive = '\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00' + raw_packet[:
                                                                                                          RAW_L3_MAX_PAYLOAD]

        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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, RAW_L3_MAX_PAYLOAD)
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, bytes(''))
示例#11
0
    def test_raw_l3(self):
        """
        Test 'raw' protocol with the loopback interface 'lo'.

        Given: A SocketConnection 'raw-l3' object.
          and: A raw UDP/IP packet.
          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 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 = bytes('"Imprudent marriages!" roared Michael. "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()

        uut = SocketConnection(host="lo", port=socket_connection.ETH_P_IP, 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="\x7F\x00\x00\x01",
                dst_ip="\x7F\x00\x00\x01")
        expected_server_receive = '\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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, len(raw_packet))
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, bytes(''))
示例#12
0
    def test_raw_l2_oversized(self):
        """
        Test 'raw-l2' oversized packet handling.

        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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.is_alive())

        # Then
        self.assertEqual(send_result, uut.max_send_size)
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, b"")
示例#13
0
 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')
示例#14
0
    def test_raw_l3_oversized(self):
        """
        Test 'raw-l3' max packet size.

        Given: A SocketConnection 'raw-l3' object.
          and: A raw UDP/IP packet of RAW_L3_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_L3_MAX_PAYLOAD.
         and: The server receives the raw packet data from send(), with an Ethernet header appended.
         and: SocketConnection.recv() returns bytes('').
        """
        data_to_send = bytes('D' * (RAW_L3_MAX_PAYLOAD + 1))

        # Given
        server = MiniTestServer(proto='raw', host='lo')
        server.data_to_send = "GKC"
        server.bind()

        uut = SocketConnection(host="lo", port=socket_connection.ETH_P_IP, proto='raw-l3')
        uut.logger = logging.getLogger("SulleyUTLogger")

        # Assemble packet...
        raw_packet = data_to_send
        expected_server_receive = '\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00' + raw_packet[
                                                                                               :RAW_L3_MAX_PAYLOAD]

        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()

        # Wait for the other thread to terminate
        t.join(THREAD_WAIT_TIMEOUT)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, RAW_L3_MAX_PAYLOAD)
        self.assertEqual(expected_server_receive,
                         server.received)
        self.assertEqual(received, bytes(''))
示例#15
0
    def test_udp_client(self):
        """
        Given: A SocketConnection 'udp' object and a UDP server.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent and received data is as expected.
        """
        data_to_send = (
            b'"Rum idea this is, that tidiness is a timid, quiet sort of thing;'
            b' why, tidiness is a toil for giants."'
        )

        # Given
        server = MiniTestServer(proto="udp")
        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="udp", bind=(socket.gethostname(), 0)
        )
        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)
    def test_udp_broadcast_client(self):
        """
        Given: A SocketConnection 'udp' object with udp_broadcast set, and a UDP server.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent and received data is as expected.
        """
        try:
            broadcast_addr = get_local_non_loopback_ipv4_addresses_info().next()['broadcast']
        except StopIteration:
            assert False, TEST_ERR_NO_NON_LOOPBACK_IPV4

        data_to_send = 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()

        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)
        self.assertFalse(t.isAlive())

        # Then
        self.assertEqual(send_result, len(data_to_send))
        self.assertEqual(data_to_send, server.received)
        self.assertEqual(received, server.data_to_send)
示例#17
0
    def test_udp_client(self):
        """
        Given: A SocketConnection 'udp' object and a UDP server.
        When: Calling SocketConnection.open(), .send(), .recv(), and .close()
        Then: send() returns length of payload.
         and: Sent and received data is as expected.
        """
        data_to_send = bytes('"Rum idea this is, that tidiness is a timid, quiet sort of thing;'
                             ' why, tidiness is a toil for giants."')

        # Given
        server = MiniTestServer(proto='udp')
        server.bind()

        t = threading.Thread(target=server.serve_once)
        t.daemon = True
        t.start()

        uut = SocketConnection(host=socket.gethostname(), port=server.active_port, proto='udp',
                               bind=(socket.gethostname(), 0))
        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.isAlive())

        # Then
        self.assertEqual(send_result, len(data_to_send))
        self.assertEqual(data_to_send, server.received)
        self.assertEqual(received, server.data_to_send)