示例#1
0
class MiscRadiusPacketTestCase(unittest.TestCase):
	"""Test misc.packetToStr, misc.authPacketToStr and acctPacketToStr"""
	def setUp(self):
		attributes = {
			'NAS-IP-Address' : ['127.0.0.1'],
			'User-Name' : ['testuser'],
			'User-Password' : ['testpassword'],
		}
		dict = Dictionary(dictFile)
		self.srv = Client(server = "127.0.0.1", dict = dict)
		self.auth_pkt = self.srv.CreateAuthPacket(code = packet.AccessRequest, **attributes)
		self.acct_pkt = self.srv.CreateAcctPacket(code = packet.AccountingRequest, **attributes)
	
	
	def testPacketToStr(self):
		output = misc.packetToStr(self.auth_pkt)
		self.failUnless(isinstance(output, types.StringTypes))
		self.failUnless(output != '')
	
	
	def testAuthPacketToStr(self):
		output = misc.authPacketToStr(self.auth_pkt)
		self.failUnless(isinstance(output, types.StringTypes))
		self.failUnless(output != '')
	
	
	def testAcctPacketToStr(self):
		output = misc.acctPacketToStr(self.acct_pkt)
		self.failUnless(isinstance(output, types.StringTypes))
		self.failUnless(output != '')
示例#2
0
	def setUp(self):
		attributes = {
			'NAS-IP-Address' : ['127.0.0.1'],
			'User-Name' : ['testuser'],
			'User-Password' : ['testpassword'],
		}
		dict = Dictionary(dictFile)
		self.srv = Client(server = "127.0.0.1", dict = dict)
		self.auth_pkt = self.srv.CreateAuthPacket(code = packet.AccessRequest, **attributes)
		self.acct_pkt = self.srv.CreateAcctPacket(code = packet.AccountingRequest, **attributes)
示例#3
0
class RadClient(object):
    """Describes interface to RADIUS server. Don't mix this with
		pyrad.client.Client. RadClient encapsulates pyrad's Client and makes it
		easier to send and receive radius packets,
	"""
    hexSymbols = '0123456789abcdef'

    def __init__(self,
                 dictFile="./dictionary",
                 host='127.0.0.1',
                 authport=1812,
                 acctport=1813,
                 secret='testing123',
                 retries=3,
                 timeout=5):
        """Create new radius client instance.
			Input: (str) path to radius dictionary file;
				(str) radius server address;
				(int) radius server authorization message port;
				(int) radius server accounting message port;
				(str) secret password known by radius server and client only;
				(int) packet send retries
				(int) packet send timeout
		"""
        self.dictFile = dictFile
        self.host = host
        self.authport = authport
        self.acctport = acctport
        self.secret = secret

        # parse dictionaries and create radius server descriptor instance
        self.dict = Dictionary(dictFile)
        self.srv = Client(server=host,
                          secret=secret,
                          dict=self.dict,
                          authport=authport,
                          acctport=acctport)

        self.srv.retries = retries
        self.srv.timeout = timeout

    def getAuthPacket(self, attributes={}):
        """Get RADIUS authorization packet.
			Input: (dict) attributes for RADIUS server.
			Output: (pyrad.packet.AcctPacket) RADIUS authorization packet.
		"""
        req = self.srv.CreateAuthPacket(code=packet.AccessRequest,
                                        **attributes)
        if 'User-Password' in attributes:
            req["User-Password"] = req.PwCrypt(attributes['User-Password'][0])
        return req

    def getAcctPacket(self, attributes={}):
        """Get RADIUS accounting packet.
			Input: (dict) attributes for RADIUS server.
			Output: (pyrad.packet.AcctPacket) RADIUS accounting packet.
		"""
        req = self.srv.CreateAcctPacket(code=packet.AccountingRequest,
                                        **attributes)
        return req

    def sendPacket(self, req):
        """Send packet to RADIUS server.
			Raises exception upon error.
			Input: (pyrad.packet.Packet) RADIUS packet
			Output: RADIUS server response
		"""
        try:
            reply = self.srv.SendPacket(req)
            return reply
        except Timeout:
            raise RadClientError("RADIUS server does not reply")
        except socket.error, error:
            raise RadClientError("Network error: " + error[1])
(cliOptions, args) = cliParser.parse_args()

# get options
testTime = cliOptions.testTime
testAuth = not cliOptions.noAuth
testAcct = not cliOptions.noAcct
testMixed = not cliOptions.noMixed
host = cliOptions.serverHost

# parse dictionaries
print '--- Parsing dictionary files ---'
dict = dictionary.Dictionary(main_config['PATHS']['dictionary_file'])
# start server itself
authport = main_config["SERVER"]["auth_port"]
acctport = main_config["SERVER"]["acct_port"]
srv = Client(server=host, secret="testing123", dict=dict)

### run tests ###
totalAuthRequests = 0
totalAcctRequests = 0
totalMixedRequests = 0

if testAuth:
    startTime = time.time()
    endTime = startTime + testTime
    while (time.time() < endTime):
        req = getAuthPacket(srv)
        sendPacket(srv, req)
        totalAuthRequests += 1

if testAcct: