github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/impacket/tests/misc/test_ip6_address.py (about) 1 import unittest 2 from binascii import hexlify 3 from impacket.IP6_Address import IP6_Address 4 5 def hexl(b): 6 return hexlify(b).decode('ascii') 7 8 class IP6AddressTests(unittest.TestCase): 9 def test_bin(self): 10 tests = (("A:B:C:D:E:F:1:2",'000a000b000c000d000e000f00010002', 11 "A:B:C:D:E:F:1:2"), 12 ("A:B:0:D:E:F:0:2",'000a000b0000000d000e000f00000002', 13 "A:B::D:E:F:0:2"), 14 ("A::BC:E:D",'000a000000000000000000bc000e000d', 15 "A::BC:E:D"), 16 ("A::BCD:EFFF:D",'000a00000000000000000bcdefff000d', 17 "A::BCD:EFFF:D"), 18 ("FE80:0000:0000:0000:020C:29FF:FE26:E251", 19 'fe80000000000000020c29fffe26e251', 20 "FE80::20C:29FF:FE26:E251"), 21 ("::",'00000000000000000000000000000000', 22 "::"), 23 ("1::",'00010000000000000000000000000000', 24 "1::"), 25 ("::2",'00000000000000000000000000000002', 26 "::2"), 27 ) 28 # print IP6_Address("A::BC:E:D").as_string(False) 29 for torig, thex, texp in tests: 30 ip = IP6_Address(torig) 31 byt = ip.as_bytes() 32 self.assertEqual(hexl(byt), thex) 33 self.assertEqual(ip.as_string(), texp) 34 35 if not hasattr(unittest.TestCase,'assertRaisesRegex'): 36 if hasattr(unittest.TestCase,'assertRaisesRegexp'): # PY2.7, PY3.1 37 assertRaisesRegex = unittest.TestCase.assertRaisesRegexp 38 else: # PY2.6 39 def assertRaisesRegex(self,ex,rx,*args): 40 # Just ignore the regex 41 return self.assertRaises(ex,rx,*args) 42 43 def test_malformed(self): 44 with self.assertRaisesRegex(Exception,r'address size'): 45 IP6_Address("ABCD:EFAB:1234:1234:1234:1234:1234:12345") 46 with self.assertRaisesRegex(Exception,r'triple colon'): 47 IP6_Address(":::") 48 with self.assertRaisesRegex(Exception,r'triple colon'): 49 IP6_Address("::::") 50 # Could also test other invalid inputs 51 #IP6_Address("AB:CD:EF") 52 #IP6_Address("12::34::56") 53 #IP6_Address("00BCDE::") 54 #IP6_Address("DEFG::") 55 # and how about these... 56 #IP6_Address("A::0XBC:D") 57 #IP6_Address("B:-123::") 58 #IP6_Address("B:56 ::-0xE") 59 60 if __name__=='__main__': 61 unittest.main(verbosity=1)