Esempio n. 1
0
    def test_send_relayed_without_interface_id(self):
        multicast_socket = MockSocket(AF_INET6, IPPROTO_UDP, All_DHCP_Relay_Agents_and_Servers, SERVER_PORT, 42, 1608)
        link_local_socket = MockSocket(AF_INET6, IPPROTO_UDP, 'fe80::1%eth0', SERVER_PORT, 42, 1608)

        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8::1'))

        # Start with a clean parse and then change interface-id
        new_message = Message.parse(relayed_advertise_packet)[1]
        interface_id_option = new_message.inner_relay_message.get_option_of_type(InterfaceIdOption)
        new_message.inner_relay_message.options.remove(interface_id_option)

        outgoing_message = RelayReplyMessage(hop_count=0,
                                             link_address=IPv6Address('2001:db8::1'),
                                             peer_address=IPv6Address('fe80::babe'),
                                             options=[
                                                 InterfaceIdOption(interface_id=b'eth0'),
                                                 RelayMessageOption(relayed_message=new_message)
                                             ])

        with self.assertLogs(level=logging.DEBUG) as logged:
            listening_socket.send_reply(outgoing_message)

        log_output = '\n'.join(logged.output)
        self.assertRegex(log_output, r'Sent AdvertiseMessage')
        self.assertRegex(log_output, r'to fe80::3631:c4ff:fe3c:b2f1')
        self.assertRegex(log_output, r"via relay fe80::babe")
Esempio n. 2
0
    def test_send_unwrapped(self):
        multicast_socket = MockSocket(AF_INET6, IPPROTO_UDP, All_DHCP_Relay_Agents_and_Servers, SERVER_PORT, 42, 1608)
        link_local_socket = MockSocket(AF_INET6, IPPROTO_UDP, 'fe80::1%eth0', SERVER_PORT, 42, 1608)

        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8::1'))

        with self.assertRaisesRegex(ValueError, r'has to be wrapped'):
            listening_socket.send_reply(advertise_message)
Esempio n. 3
0
    def test_failed_send_relayed(self):
        multicast_socket = MockSocket(AF_INET6, IPPROTO_UDP, All_DHCP_Relay_Agents_and_Servers, SERVER_PORT, 42, 1608)
        link_local_socket = MockSocket(AF_INET6, IPPROTO_UDP, 'fe80::1%eth0', SERVER_PORT, 42, 1608)
        link_local_socket.pretend_sendto_fails = True

        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8::1'))

        outgoing_message = RelayReplyMessage(hop_count=0,
                                             link_address=IPv6Address('2001:db8::1'),
                                             peer_address=IPv6Address('fe80::babe'),
                                             options=[
                                                 InterfaceIdOption(interface_id=b'eth0'),
                                                 RelayMessageOption(relayed_message=relayed_advertise_message)
                                             ])

        with self.assertLogs(level=logging.DEBUG) as logged:
            success = listening_socket.send_reply(outgoing_message)
            self.assertFalse(success)

        # Nothing should be sent from a multicast socket
        with self.assertRaises(IndexError):
            multicast_socket.read_from_outgoing_queue()

        # It must be on the link local socket
        sent_packet, recipient = link_local_socket.read_from_outgoing_queue()
        self.assertNotEqual(sent_packet, relayed_advertise_packet)

        log_output = '\n'.join(logged.output)
        self.assertRegex(log_output, r'AdvertiseMessage')
        self.assertRegex(log_output, r'to fe80::3631:c4ff:fe3c:b2f1')
        self.assertRegex(log_output, r"via Fa2/3 of relay fe80::babe")
Esempio n. 4
0
    def test_send_empty_wrapper(self):
        multicast_socket = MockSocket(AF_INET6, IPPROTO_UDP, All_DHCP_Relay_Agents_and_Servers, SERVER_PORT, 42, 1608)
        link_local_socket = MockSocket(AF_INET6, IPPROTO_UDP, 'fe80::1%eth0', SERVER_PORT, 42, 1608)

        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8::1'))

        outgoing_message = RelayReplyMessage(hop_count=0,
                                             link_address=IPv6Address('2001:db8::1'),
                                             peer_address=IPv6Address('fe80::babe'),
                                             options=[
                                                 InterfaceIdOption(interface_id=b'eth0'),
                                             ])

        with self.assertRaisesRegex(ValueError, r'not contain a message'):
            listening_socket.send_reply(outgoing_message)
Esempio n. 5
0
    def test_send_badly_wrapped(self):
        multicast_socket = MockSocket(AF_INET6, IPPROTO_UDP, All_DHCP_Relay_Agents_and_Servers, SERVER_PORT, 42, 1608)
        link_local_socket = MockSocket(AF_INET6, IPPROTO_UDP, 'fe80::1%eth0', SERVER_PORT, 42, 1608)

        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8::1'))

        with self.assertRaisesRegex(ValueError, r'link-address does not match'):
            listening_socket.send_reply(relayed_advertise_message)

        # Fix the link-address so the test continues to the interface-id
        # noinspection PyTypeChecker
        listening_socket = ListeningSocket('eth0', multicast_socket, link_local_socket,
                                           global_address=IPv6Address('2001:db8:ffff:1::1'))

        with self.assertRaisesRegex(ValueError, r'interface-id in the reply does not match'):
            listening_socket.send_reply(relayed_advertise_message)