예제 #1
0
    def test_coa(self):
        request = packet.CoARequest(SECRET_KEY)
        request.attributes.extend(
            ('User-Password', 'rejected-attribute'),
            ('Error-Cause', 'rejected-attribute')
        )
        with self.assertRaises(packet.PacketError):
            encoded_ = bytes(request)
        request.attributes.extend(
            ('NAS-Identifier', 'test-nas'),
        )
        encoded_ = bytes(request)
        decoded_request = packet.decode_request(SECRET_KEY, encoded_)

        self.assertEqual(request.identifier, decoded_request.identifier)
        self.assertEqual(request.code, decoded_request.code)
        self.assertNotIn('User-Password', decoded_request.attributes)
        self.assertNotIn('Error-Cause', decoded_request.attributes)

        response = decoded_request.create_reply(packet.COA_ACK)
        encoded_ = bytes(response)
        decoded_response = packet.decode_response(SECRET_KEY, encoded_, decoded_request)

        self.assertEqual(request.identifier, decoded_response.identifier)
        self.assertIsInstance(decoded_response, packet.CoaACK)

        response = decoded_request.create_reply(packet.COA_NACK)
        encoded_ = bytes(response)
        decoded_response = packet.decode_response(SECRET_KEY, encoded_, decoded_request)

        self.assertEqual(request.identifier, decoded_response.identifier)
        self.assertIsInstance(decoded_response, packet.CoaNACK)
예제 #2
0
    def test_access_challenge(self):
        request = packet.AccessRequest(SECRET_KEY)
        request.attributes.extend(
            ('User-Name', 'username'),
            ('User-Password', 'password'),
            ('NAS-Identifier', 'nas-id')
        )

        encoded_ = bytes(request)
        decoded_request = packet.decode_request(SECRET_KEY, encoded_)

        self.assertEqual(request, decoded_request)

        response = decoded_request.create_reply(packet.ACCESS_CHALLENGE)
        response.attributes.add('Reply-Message', 'ACCESS_CHALANGE')

        encoded_ = bytes(response)
        decoded_response = packet.decode_response(SECRET_KEY, encoded_, request=request)
        self.assertEqual(response, decoded_response)


        response = decoded_request.create_reply(packet.ACCESS_CHALLENGE)
        response.attributes.add('User-Name', 'username')
        with self.assertRaises(packet.PacketError):
            bytes(response)
예제 #3
0
    def test_accounting_response(self):
        request = packet.AccountingRequest(SECRET_KEY)
        request.attributes.extend(
            ('User-Name', 'username'),
            ('NAS-Identifier', 'nas-id')
        )
        with self.assertRaises(packet.PacketError) as ctx:
            bytes(request)

        request = packet.AccountingRequest(SECRET_KEY)
        request.attributes = (
            ('User-Name', 'username'),
            ('NAS-Identifier', 'nas-id'),
            ('Acct-Status-Type', 'Start'),
            ('Acct-Session-Id', 'session_id')
        )

        encoded_ = bytes(request)
        decoded_request = packet.decode_request(SECRET_KEY, encoded_)


        self.assertEqual(request, decoded_request)

        response = decoded_request.create_reply()

        self.assertIsInstance(response, packet.AccountingResponse)
        self.assertEqual(response.code, packet.ACCOUNTING_RESPONSE)
        self.assertEqual(decoded_request.identifier, response.identifier)

        encoded_ = bytes(response)

        decoded_response = packet.decode_response(SECRET_KEY, encoded_, request=request)
        self.assertEqual(response, decoded_response)
예제 #4
0
    def datagram_received(self, data, remote_address):
        """
        This method will be called when client receive data from server
        It trying to "close" the future, that was created when client send request to server

        Params:
            data(bytes) - data received from server
            remote_address(pair) - IP-address and port of server
        """

        try:
            packet_class = packet.get_packet_class(data)
            packet_identifier = packet.get_packet_identifier(data)
        except packet.PacketError:
            return

        # If identifier of response in set of identifiers that we are sent,
        # get future for this identifier
        if packet_identifier in self.__futures.keys():
            future, request, start_time = self.__futures.get(packet_identifier)
            # If future is done early, that is a duplicate packet
            if future.done():
                exec_time = (self.loop.time() - start_time) * LOOP_TIME_MULTIPLEX
                logger.debug("Got RADIUS response from {} by {:.4f}ms, but it is duplicate, ignore it".format(
                    remote_address,
                    exec_time
                ))
                return
            # Trying to verify response from server
            try:
                response_packet = packet.decode_response(request.secret_key, data, request=request)
            except packet.PacketError as e:
                exec_time = (self.loop.time() - start_time) * LOOP_TIME_MULTIPLEX
                logger.warning("Got response from {} by {:.4f}ms, but it is not valid ({!s}), ignore it".format(
                    remote_address,
                    exec_time, e
                ))
                future.set_exception(packet.PacketError("Validation Error"))
            except Exception as e:
                exec_time = (self.loop.time() - start_time) * LOOP_TIME_MULTIPLEX
                logger.warning("Got {!s} from {} by {:.4f}ms, but it is not valid ({!s}), ignore it".format(
                    response_packet, remote_address,
                    exec_time, e
                ))
                future.set_exception(packet.PacketError("Validation Error"))
            else:
                exec_time = (self.loop.time() - start_time) * LOOP_TIME_MULTIPLEX
                local_addr, local_port, remote_addr, remote_port = self.get_enpoint_addresses()
                logger.debug("Got {!s} from {}:{} by {:.4f} ms".format(
                    response_packet,
                    remote_addr, remote_port,
                    exec_time
                ))
                future.set_result((response_packet, exec_time))
        else:
            # Got response packet with identifier that dont sent, ignore it
            logger.warning("Got RADIUS response from {}, but we are not send request with this ID, ignore it".format(
                remote_address
            ))
예제 #5
0
    def test_access_accept(self):
        attributes = packet.AttributesSet(
            ('User-Name', 'username'),
            ('User-Password', 'password'),
            ('NAS-Identifier', 'nas-id')
        )
        request = packet.AccessRequest(SECRET_KEY)
        request.attributes = attributes

        encoded_ = bytes(request)
        decoded_request = packet.decode_request(SECRET_KEY, encoded_)

        self.assertEqual(request, decoded_request)

        response = decoded_request.create_reply(packet.ACCESS_ACCEPT)
        encoded_ = bytes(response)

        decoded_response = packet.decode_response(SECRET_KEY, encoded_, request=request)

        self.assertEqual(response, decoded_response)