Changeset 1057

Show
Ignore:
Timestamp:
09/09/07 19:18:54 (15 months ago)
Author:
wichert
Message:

Full test coverage for packet.py

Location:
trunk/pyrad
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/pyrad/packet.py

    r1056 r1057  
    123123 
    124124        def _EncodeKey(self, key): 
    125                 if type(key)!=types.StringType: 
     125                if not isinstance(key, str):  
    126126                        return key 
    127127 
     
    289289                        return (26, data) 
    290290 
    291                 try: 
    292                         (vendor, type, length)=struct.unpack("!LBB", data[:6])[0:3] 
    293                 except struct.error: 
    294                         raise PacketError, "Vender attribute header is corrupt" 
     291                (vendor, type, length)=struct.unpack("!LBB", data[:6])[0:3] 
    295292                # Another sanity check 
    296293                if len(data)!=length+4: 
     
    496493                return hash==self.authenticator 
    497494 
     495 
    498496        def RequestPacket(self): 
    499497                """Create a ready-to-transmit authentication request packet 
  • trunk/pyrad/tests/testPacket.py

    r1054 r1057  
    243243 
    244244 
     245    def testDecodePacketWithPartialAttributes(self): 
     246        try: 
     247            self.packet.DecodePacket("\x01\x02\x00\x151234567890123456\x00") 
     248        except packet.PacketError, e: 
     249            self.failUnless("header is corrupt" in str(e)) 
     250        else: 
     251            self.fail() 
     252 
     253 
    245254    def testDecodePacketWithoutAttributes(self): 
    246255        self.packet.DecodePacket("\x01\x02\x00\x141234567890123456") 
     
    286295 
    287296 
     297    def testEncodeKeyValues(self): 
     298        self.assertEqual(self.packet._EncodeKeyValues(1, "1234"), (1, "1234")) 
     299 
     300 
     301    def testEncodeKey(self): 
     302        self.assertEqual(self.packet._EncodeKey(1), 1) 
     303 
     304 
     305    def testAddAttribute(self): 
     306        self.packet.AddAttribute(1, 1) 
     307        self.assertEqual(self.packet.data[1], [1]) 
     308        self.packet.AddAttribute(1, 1) 
     309        self.assertEqual(self.packet.data[1], [1, 1]) 
     310 
     311 
     312 
    288313class AuthPacketConstructionTests(PacketConstructionTests): 
    289314    klass = packet.AuthPacket 
     
    294319 
    295320 
     321 
     322class AuthPacketTests(unittest.TestCase): 
     323 
     324    def setUp(self): 
     325        self.path=os.path.join(home, "tests", "data") 
     326        self.dict=Dictionary(os.path.join(self.path, "full")) 
     327        self.packet=packet.AuthPacket(id=0, secret="secret", 
     328                authenticator="01234567890ABCDEF", dict=self.dict) 
     329 
     330 
     331    def testCreateReply(self): 
     332        reply=self.packet.CreateReply(Test_Integer=10) 
     333        self.assertEqual(reply.code, packet.AccessAccept) 
     334        self.assertEqual(reply.id, self.packet.id) 
     335        self.assertEqual(reply.secret, self.packet.secret) 
     336        self.assertEqual(reply.authenticator, self.packet.authenticator) 
     337        self.assertEqual(reply["Test-Integer"], [10]) 
     338 
     339 
     340    def testRequestPacket(self): 
     341        self.assertEqual(self.packet.RequestPacket(), 
     342                "\x01\x00\x00\x1401234567890ABCDE") 
     343 
     344    def testRequestPacketCreatesAuthenticator(self): 
     345        self.packet.authenticator=None 
     346        self.packet.RequestPacket() 
     347        self.failUnless(self.packet.authenticator is not None) 
     348 
     349 
     350    def testRequestPacketCreatesID(self): 
     351        self.packet.id=None 
     352        self.packet.RequestPacket() 
     353        self.failUnless(self.packet.id is not None) 
     354 
     355 
     356    def testPwCryptEmptyPassword(self): 
     357        self.assertEqual(self.packet.PwCrypt(""), "") 
     358 
     359 
     360    def testPwCryptPassword(self): 
     361        self.assertEqual(self.packet.PwCrypt("Simplon"),  
     362                "\xd3U;\xb23\r\x11\xba\x07\xe3\xa8*\xa8x\x14\x01") 
     363 
     364 
     365    def testPwCryptSetsAuthenticator(self): 
     366        self.packet.authenticator=None 
     367        self.packet.PwCrypt("") 
     368        self.failUnless(self.packet.authenticator is not None) 
     369 
     370 
     371    def testPwDecryptEmptyPassword(self): 
     372        self.assertEqual(self.packet.PwDecrypt(""), "") 
     373 
     374 
     375    def testPwDecryptPassword(self): 
     376        self.assertEqual(self.packet.PwDecrypt( 
     377                "\xd3U;\xb23\r\x11\xba\x07\xe3\xa8*\xa8x\x14\x01"), 
     378                "Simplon") 
     379 
     380 
     381 
    296382class AcctPacketConstructionTests(PacketConstructionTests): 
    297383    klass = packet.AcctPacket 
     
    301387        self.assertEqual(pkt.code, packet.AccountingRequest) 
    302388 
     389 
     390    def testConstructorRawPacket(self): 
     391        raw="\x00\x00\x00\x14\xb0\x5e\x4b\xfb\xcc\x1c" \ 
     392            "\x8c\x8e\xc4\x72\xac\xea\x87\x45\x63\xa7" 
     393        pkt=self.klass(packet=raw) 
     394        self.assertEqual(pkt.raw_packet, raw) 
     395 
     396 
     397class AcctPacketTests(unittest.TestCase): 
     398 
     399    def setUp(self): 
     400        self.path=os.path.join(home, "tests", "data") 
     401        self.dict=Dictionary(os.path.join(self.path, "full")) 
     402        self.packet=packet.AcctPacket(id=0, secret="secret", 
     403                authenticator="01234567890ABCDEF", dict=self.dict) 
     404 
     405 
     406    def testCreateReply(self): 
     407        reply=self.packet.CreateReply(Test_Integer=10) 
     408        self.assertEqual(reply.code, packet.AccountingResponse) 
     409        self.assertEqual(reply.id, self.packet.id) 
     410        self.assertEqual(reply.secret, self.packet.secret) 
     411        self.assertEqual(reply.authenticator, self.packet.authenticator) 
     412        self.assertEqual(reply["Test-Integer"], [10]) 
     413 
     414 
     415    def testVerifyAcctRequest(self): 
     416        rawpacket=self.packet.RequestPacket() 
     417        pkt=packet.AcctPacket(secret="secret", packet=rawpacket) 
     418        self.assertEqual(pkt.VerifyAcctRequest(), True) 
     419 
     420        pkt.secret="different" 
     421        self.assertEqual(pkt.VerifyAcctRequest(), False) 
     422        pkt.secret="secret" 
     423 
     424        pkt.raw_packet="X" + pkt.raw_packet[1:] 
     425        self.assertEqual(pkt.VerifyAcctRequest(), False) 
     426 
     427 
     428    def testRequestPacket(self): 
     429        self.assertEqual(self.packet.RequestPacket(), 
     430            "\x04\x00\x00\x14\x95\xdf\x90\xccbn\xfb\x15G!\x13\xea\xfa>6\x0f") 
     431 
     432 
     433    def testRequestPacketSetsId(self): 
     434        self.packet.id=None 
     435        self.packet.RequestPacket() 
     436        self.failUnless(self.packet.id is not None) 
     437