示例#1
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"")
示例#2
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 = (
            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()
        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(raw_packet))
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, raw_packet)
示例#3
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)
示例#4
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('').
        """

        # 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"D" * (uut.packet_size + 1)

        # 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[:uut.packet_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.packet_size)
        self.assertEqual(expected_server_receive, server.received)
        self.assertEqual(received, data_to_send[:-1])
示例#5
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()

        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"")
示例#6
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)
示例#7
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"")
示例#8
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 = 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.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)