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 != '')
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])