Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2013
1,371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import re
  4. import sys
  5.  
  6. passed_tests = 0
  7. failed_tests = 0
  8. num_tests = 0
  9.  
  10. def ipv6test(result, ip):
  11.     """Validates IPv6 addresses.
  12.     """
  13.     global passed_tests
  14.     global failed_tests
  15.     global num_tests
  16.  
  17.     # Regex from: http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python/319293#319293
  18.     pattern = re.compile(r"""^
  19.                               \s* # Leading whitespace
  20.                               # Zero-width lookaheads to reject too many quartets
  21.                               (?:
  22.                                  # 6 quartets, ending IPv4 address; no wildcards
  23.                                  (?:[0-9a-f]{1,4}(?::(?!:))){6}
  24.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  25.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  26.                                |
  27.                                  # 0-5 quartets, wildcard, ending IPv4 address
  28.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,4}[0-9a-f]{1,4})?
  29.                                  (?:::(?!:))
  30.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  31.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  32.                                |
  33.                                  # 0-4 quartets, wildcard, 0-1 quartets, ending IPv4 address
  34.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,3}[0-9a-f]{1,4})?
  35.                                  (?:::(?!:))
  36.                                  (?:[0-9a-f]{1,4}(?::(?!:)))?
  37.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  38.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  39.                                |
  40.                                  # 0-3 quartets, wildcard, 0-2 quartets, ending IPv4 address
  41.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,2}[0-9a-f]{1,4})?
  42.                                  (?:::(?!:))
  43.                                  (?:[0-9a-f]{1,4}(?::(?!:))){0,2}
  44.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  45.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  46.                                |
  47.                                  # 0-2 quartets, wildcard, 0-3 quartets, ending IPv4 address
  48.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,1}[0-9a-f]{1,4})?
  49.                                  (?:::(?!:))
  50.                                  (?:[0-9a-f]{1,4}(?::(?!:))){0,3}
  51.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  52.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  53.                                |
  54.                                  # 0-1 quartets, wildcard, 0-4 quartets, ending IPv4 address
  55.                                  (?:[0-9a-f]{1,4}){0,1}
  56.                                  (?:::(?!:))
  57.                                  (?:[0-9a-f]{1,4}(?::(?!:))){0,4}
  58.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  59.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  60.                                |
  61.                                  # wildcard, 0-5 quartets, ending IPv4 address
  62.                                  (?:::(?!:))
  63.                                  (?:[0-9a-f]{1,4}(?::(?!:))){0,5}
  64.                                       (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)
  65.                                  (?:\.(?:25[0-4]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}
  66.                                |
  67.                                  # 8 quartets; no wildcards
  68.                                  (?:[0-9a-f]{1,4}(?::(?!:))){7}[0-9a-f]{1,4}
  69.                                |
  70.                                  # 0-7 quartets, wildcard
  71.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,6}[0-9a-f]{1,4})?
  72.                                  (?:::(?!:))
  73.                                |
  74.                                  # 0-6 quartets, wildcard, 0-1 quartets
  75.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,5}[0-9a-f]{1,4})?
  76.                                  (?:::(?!:))
  77.                                  (?:[0-9a-f]{1,4})?
  78.                                |
  79.                                  # 0-5 quartets, wildcard, 0-2 quartets
  80.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,4}[0-9a-f]{1,4})?
  81.                                  (?:::(?!:))
  82.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,1}[0-9a-f]{1,4})?
  83.                                |
  84.                                  # 0-4 quartets, wildcard, 0-3 quartets
  85.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,3}[0-9a-f]{1,4})?
  86.                                  (?:::(?!:))
  87.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,2}[0-9a-f]{1,4})?
  88.                                |
  89.                                  # 0-3 quartets, wildcard, 0-4 quartets
  90.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,2}[0-9a-f]{1,4})?
  91.                                  (?:::(?!:))
  92.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,3}[0-9a-f]{1,4})?
  93.                                |
  94.                                  # 0-2 quartets, wildcard, 0-5 quartets
  95.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,1}[0-9a-f]{1,4})?
  96.                                  (?:::(?!:))
  97.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,4}[0-9a-f]{1,4})?
  98.                                |
  99.                                  # 0-1 quartets, wildcard, 0-6 quartets
  100.                                  (?:[0-9a-f]{1,4})?
  101.                                  (?:::(?!:))
  102.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,5}[0-9a-f]{1,4})?
  103.                                |
  104.                                  # wildcard, 0-7 quartets
  105.                                  (?:::(?!:))
  106.                                  (?:(?:[0-9a-f]{1,4}(?::(?!:))){0,6}[0-9a-f]{1,4})?
  107.                               )
  108.                               (?:/(?:1(?:2[0-7]|[01]\d)|\d\d?))? # With an optional CIDR routing prefix (0-128)
  109.                               \s* # Trailing whitespace
  110.                              $""", re.VERBOSE | re.IGNORECASE | re.DOTALL)
  111.  
  112.     if result is True:
  113.         if pattern.match(ip) is not None:
  114.             passed_tests += 1
  115.             num_tests += 1
  116.         else:
  117.             print "Failed %s" % ip
  118.             failed_tests += 1
  119.             num_tests += 1
  120.     else:
  121.         if pattern.match(ip) is not None:
  122.             print "Passed %s" % ip
  123.             failed_tests += 1
  124.             num_tests += 1
  125.         else:
  126.             passed_tests += 1
  127.             num_tests += 1
  128.  
  129.  
  130.  
  131. if len(sys.argv[1:]) > 0:
  132.     for arg in sys.argv[1:]:
  133.         if not re.match(r"^--.*$", arg):
  134.             args = arg.split('=')
  135.  
  136.             if args[0].lower() == 'False'.lower() or args[0].lower() == 'Fail'.lower():
  137.                 ipv6test(False, args[1])
  138.  
  139.             elif args[0].lower() == 'True'.lower() or args[0].lower() == 'Pass'.lower():
  140.                 ipv6test(True, args[1])
  141.  
  142.             else:
  143.                 t = args[0]
  144.                 for a in args[1:]:
  145.                     t += " "
  146.                     t += a
  147.                 raise AttributeError("Unknown argument: " + arg + " (" + t + ")")
  148.  
  149.     print "Failed %s out of %s tests" % (failed_tests, num_tests)
  150.     sys.exit(0)
  151.  
  152.  
  153.  
  154. # Tests from http://download.dartware.com/thirdparty/test-ipv6-regex.pl
  155.  
  156. ipv6test(False,"");# empty string
  157. ipv6test(True,"::1");# loopback, compressed, non-routable
  158. ipv6test(True,"::");# unspecified, compressed, non-routable
  159. ipv6test(True,"0:0:0:0:0:0:0:1");# loopback, full
  160. ipv6test(True,"0:0:0:0:0:0:0:0");# unspecified, full
  161. ipv6test(True,"2001:DB8:0:0:8:800:200C:417A");# unicast, full
  162. ipv6test(True,"FF01:0:0:0:0:0:0:101");# multicast, full
  163. ipv6test(True,"2001:DB8::8:800:200C:417A");# unicast, compressed
  164. ipv6test(True,"FF01::101");# multicast, compressed
  165. ipv6test(False,"2001:DB8:0:0:8:800:200C:417A:221");# unicast, full
  166. ipv6test(False,"FF01::101::2");# multicast, compressed
  167. ipv6test(True,"fe80::217:f2ff:fe07:ed62");
  168.  
  169. ipv6test(True,"2001:0000:1234:0000:0000:C1C0:ABCD:0876");
  170. ipv6test(True,"3ffe:0b00:0000:0000:0001:0000:0000:000a");
  171. ipv6test(True,"FF02:0000:0000:0000:0000:0000:0000:0001");
  172. ipv6test(True,"0000:0000:0000:0000:0000:0000:0000:0001");
  173. ipv6test(True,"0000:0000:0000:0000:0000:0000:0000:0000");
  174. ipv6test(False,"02001:0000:1234:0000:0000:C1C0:ABCD:0876");# extra 0 not allowed!
  175. ipv6test(False,"2001:0000:1234:0000:00001:C1C0:ABCD:0876");# extra 0 not allowed!
  176. #ipv6test(True," 2001:0000:1234:0000:0000:C1C0:ABCD:0876");# leading space
  177. #ipv6test(True,"2001:0000:1234:0000:0000:C1C0:ABCD:0876");# trailing space
  178. #ipv6test(True," 2001:0000:1234:0000:0000:C1C0:ABCD:0876  ");# leading and trailing space
  179. ipv6test(False,"2001:0000:1234:0000:0000:C1C0:ABCD:0876  0");# junk after valid address
  180. ipv6test(False,"2001:0000:1234: 0000:0000:C1C0:ABCD:0876");# internal space
  181.  
  182. ipv6test(False,"3ffe:0b00:0000:0001:0000:0000:000a");# seven segments
  183. ipv6test(False,"FF02:0000:0000:0000:0000:0000:0000:0000:0001");# nine segments
  184. ipv6test(False,"3ffe:b00::1::a");# double "::"
  185. ipv6test(False,"::1111:2222:3333:4444:5555:6666::");# double "::"
  186. ipv6test(True,"2::10");
  187. ipv6test(True,"ff02::1");
  188. ipv6test(True,"fe80::");
  189. ipv6test(True,"2002::");
  190. ipv6test(True,"2001:db8::");
  191. ipv6test(True,"2001:0db8:1234::");
  192. ipv6test(True,"::ffff:0:0");
  193. ipv6test(True,"::1");
  194. ipv6test(True,"1:2:3:4:5:6:7:8");
  195. ipv6test(True,"1:2:3:4:5:6::8");
  196. ipv6test(True,"1:2:3:4:5::8");
  197. ipv6test(True,"1:2:3:4::8");
  198. ipv6test(True,"1:2:3::8");
  199. ipv6test(True,"1:2::8");
  200. ipv6test(True,"1::8");
  201. ipv6test(True,"1::2:3:4:5:6:7");
  202. ipv6test(True,"1::2:3:4:5:6");
  203. ipv6test(True,"1::2:3:4:5");
  204. ipv6test(True,"1::2:3:4");
  205. ipv6test(True,"1::2:3");
  206. ipv6test(True,"1::8");
  207. ipv6test(True,"::2:3:4:5:6:7:8");
  208. ipv6test(True,"::2:3:4:5:6:7");
  209. ipv6test(True,"::2:3:4:5:6");
  210. ipv6test(True,"::2:3:4:5");
  211. ipv6test(True,"::2:3:4");
  212. ipv6test(True,"::2:3");
  213. ipv6test(True,"::8");
  214. ipv6test(True,"1:2:3:4:5:6::");
  215. ipv6test(True,"1:2:3:4:5::");
  216. ipv6test(True,"1:2:3:4::");
  217. ipv6test(True,"1:2:3::");
  218. ipv6test(True,"1:2::");
  219. ipv6test(True,"1::");
  220. ipv6test(True,"1:2:3:4:5::7:8");
  221. ipv6test(False,"1:2:3::4:5::7:8");# Double "::"
  222. ipv6test(False,"12345::6:7:8");
  223. ipv6test(True,"1:2:3:4::7:8");
  224. ipv6test(True,"1:2:3::7:8");
  225. ipv6test(True,"1:2::7:8");
  226. ipv6test(True,"1::7:8");
  227.  
  228. # IPv4 addresses as dotted-quads
  229. ipv6test(True,"1:2:3:4:5:6:1.2.3.4");
  230. ipv6test(True,"1:2:3:4:5::1.2.3.4");
  231. ipv6test(True,"1:2:3:4::1.2.3.4");
  232. ipv6test(True,"1:2:3::1.2.3.4");
  233. ipv6test(True,"1:2::1.2.3.4");
  234. ipv6test(True,"1::1.2.3.4");
  235. ipv6test(True,"1:2:3:4::5:1.2.3.4");
  236. ipv6test(True,"1:2:3::5:1.2.3.4");
  237. ipv6test(True,"1:2::5:1.2.3.4");
  238. ipv6test(True,"1::5:1.2.3.4");
  239. ipv6test(True,"1::5:11.22.33.44");
  240. ipv6test(False,"1::5:400.2.3.4");
  241. ipv6test(False,"1::5:260.2.3.4");
  242. ipv6test(False,"1::5:256.2.3.4");
  243. ipv6test(False,"1::5:1.256.3.4");
  244. ipv6test(False,"1::5:1.2.256.4");
  245. ipv6test(False,"1::5:1.2.3.256");
  246. ipv6test(False,"1::5:300.2.3.4");
  247. ipv6test(False,"1::5:1.300.3.4");
  248. ipv6test(False,"1::5:1.2.300.4");
  249. ipv6test(False,"1::5:1.2.3.300");
  250. ipv6test(False,"1::5:900.2.3.4");
  251. ipv6test(False,"1::5:1.900.3.4");
  252. ipv6test(False,"1::5:1.2.900.4");
  253. ipv6test(False,"1::5:1.2.3.900");
  254. ipv6test(False,"1::5:300.300.300.300");
  255. ipv6test(False,"1::5:3000.30.30.30");
  256. ipv6test(False,"1::400.2.3.4");
  257. ipv6test(False,"1::260.2.3.4");
  258. ipv6test(False,"1::256.2.3.4");
  259. ipv6test(False,"1::1.256.3.4");
  260. ipv6test(False,"1::1.2.256.4");
  261. ipv6test(False,"1::1.2.3.256");
  262. ipv6test(False,"1::300.2.3.4");
  263. ipv6test(False,"1::1.300.3.4");
  264. ipv6test(False,"1::1.2.300.4");
  265. ipv6test(False,"1::1.2.3.300");
  266. ipv6test(False,"1::900.2.3.4");
  267. ipv6test(False,"1::1.900.3.4");
  268. ipv6test(False,"1::1.2.900.4");
  269. ipv6test(False,"1::1.2.3.900");
  270. ipv6test(False,"1::300.300.300.300");
  271. ipv6test(False,"1::3000.30.30.30");
  272. ipv6test(False,"::400.2.3.4");
  273. ipv6test(False,"::260.2.3.4");
  274. ipv6test(False,"::256.2.3.4");
  275. ipv6test(False,"::1.256.3.4");
  276. ipv6test(False,"::1.2.256.4");
  277. ipv6test(False,"::1.2.3.256");
  278. ipv6test(False,"::300.2.3.4");
  279. ipv6test(False,"::1.300.3.4");
  280. ipv6test(False,"::1.2.300.4");
  281. ipv6test(False,"::1.2.3.300");
  282. ipv6test(False,"::900.2.3.4");
  283. ipv6test(False,"::1.900.3.4");
  284. ipv6test(False,"::1.2.900.4");
  285. ipv6test(False,"::1.2.3.900");
  286. ipv6test(False,"::300.300.300.300");
  287. ipv6test(False,"::3000.30.30.30");
  288. ipv6test(True,"fe80::217:f2ff:254.7.237.98");
  289. ipv6test(True,"::ffff:192.168.1.26");
  290. ipv6test(False,"2001:1:1:1:1:1:255Z255X255Y255");# garbage instead of "." in IPv4
  291. ipv6test(False,"::ffff:192x168.1.26");# ditto
  292. ipv6test(True,"::ffff:192.168.1.1");
  293. ipv6test(True,"0:0:0:0:0:0:13.1.68.3");# IPv4-compatible IPv6 address, full, deprecated
  294. ipv6test(True,"0:0:0:0:0:FFFF:129.144.52.38");# IPv4-mapped IPv6 address, full
  295. ipv6test(True,"::13.1.68.3");# IPv4-compatible IPv6 address, compressed, deprecated
  296. ipv6test(True,"::FFFF:129.144.52.38");# IPv4-mapped IPv6 address, compressed
  297. ipv6test(True,"fe80:0:0:0:204:61ff:254.157.241.86");
  298. ipv6test(True,"fe80::204:61ff:254.157.241.86");
  299. ipv6test(True,"::ffff:12.34.56.78");
  300. ipv6test(False,"::ffff:2.3.4");
  301. ipv6test(False,"::ffff:257.1.2.3");
  302. ipv6test(False,"1.2.3.4");
  303.  
  304. ipv6test(False,"1.2.3.4:1111:2222:3333:4444::5555");  # Aeron
  305. ipv6test(False,"1.2.3.4:1111:2222:3333::5555");
  306. ipv6test(False,"1.2.3.4:1111:2222::5555");
  307. ipv6test(False,"1.2.3.4:1111::5555");
  308. ipv6test(False,"1.2.3.4::5555");
  309. ipv6test(False,"1.2.3.4::");
  310.  
  311. # Testing IPv4 addresses represented as dotted-quads
  312. # Leading zero's in IPv4 addresses not allowed: some systems treat the leading "0" in ".086" as the start of an octal number
  313. # Update: The BNF in RFC-3986 explicitly defines the dec-octet (for IPv4 addresses) not to have a leading zero
  314. ipv6test(False,"fe80:0000:0000:0000:0204:61ff:254.157.241.086");
  315. ipv6test(True,"::ffff:192.0.2.128");   # but this is OK, since there's a single digit
  316. ipv6test(False,"XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4");
  317. ipv6test(False,"1111:2222:3333:4444:5555:6666:00.00.00.00");
  318. ipv6test(False,"1111:2222:3333:4444:5555:6666:000.000.000.000");
  319. ipv6test(False,"1111:2222:3333:4444:5555:6666:256.256.256.256");
  320.  
  321. # Not testing address with subnet mask
  322. # ipv6test(True,"2001:0DB8:0000:CD30:0000:0000:0000:0000/60");# full, with prefix
  323. # ipv6test(True,"2001:0DB8::CD30:0:0:0:0/60");# compressed, with prefix
  324. # ipv6test(True,"2001:0DB8:0:CD30::/60");# compressed, with prefix #2
  325. # ipv6test(True,"::/128");# compressed, unspecified address type, non-routable
  326. # ipv6test(True,"::1/128");# compressed, loopback address type, non-routable
  327. # ipv6test(True,"FF00::/8");# compressed, multicast address type
  328. # ipv6test(True,"FE80::/10");# compressed, link-local unicast, non-routable
  329. # ipv6test(True,"FEC0::/10");# compressed, site-local unicast, deprecated
  330. # ipv6test(False,"124.15.6.89/60");# standard IPv4, prefix not allowed
  331.  
  332. ipv6test(True,"fe80:0000:0000:0000:0204:61ff:fe9d:f156");
  333. ipv6test(True,"fe80:0:0:0:204:61ff:fe9d:f156");
  334. ipv6test(True,"fe80::204:61ff:fe9d:f156");
  335. ipv6test(True,"::1");
  336. ipv6test(True,"fe80::");
  337. ipv6test(True,"fe80::1");
  338. ipv6test(False,":");
  339. ipv6test(True,"::ffff:c000:280");
  340.  
  341. # Aeron supplied these test cases
  342. ipv6test(False,"1111:2222:3333:4444::5555:");
  343. ipv6test(False,"1111:2222:3333::5555:");
  344. ipv6test(False,"1111:2222::5555:");
  345. ipv6test(False,"1111::5555:");
  346. ipv6test(False,"::5555:");
  347. ipv6test(False,":::");
  348. ipv6test(False,"1111:");
  349. ipv6test(False,":");
  350.  
  351. ipv6test(False,":1111:2222:3333:4444::5555");
  352. ipv6test(False,":1111:2222:3333::5555");
  353. ipv6test(False,":1111:2222::5555");
  354. ipv6test(False,":1111::5555");
  355. ipv6test(False,":::5555");
  356. ipv6test(False,":::");
  357.  
  358.  
  359. # Additional test cases
  360. # from http://rt.cpan.org/Public/Bug/Display.html?id=50693
  361.  
  362. ipv6test(True,"2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  363. ipv6test(True,"2001:db8:85a3:0:0:8a2e:370:7334");
  364. ipv6test(True,"2001:db8:85a3::8a2e:370:7334");
  365. ipv6test(True,"2001:0db8:0000:0000:0000:0000:1428:57ab");
  366. ipv6test(True,"2001:0db8:0000:0000:0000::1428:57ab");
  367. ipv6test(True,"2001:0db8:0:0:0:0:1428:57ab");
  368. ipv6test(True,"2001:0db8:0:0::1428:57ab");
  369. ipv6test(True,"2001:0db8::1428:57ab");
  370. ipv6test(True,"2001:db8::1428:57ab");
  371. ipv6test(True,"0000:0000:0000:0000:0000:0000:0000:0001");
  372. ipv6test(True,"::1");
  373. ipv6test(True,"::ffff:0c22:384e");
  374. ipv6test(True,"2001:0db8:1234:0000:0000:0000:0000:0000");
  375. ipv6test(True,"2001:0db8:1234:ffff:ffff:ffff:ffff:ffff");
  376. ipv6test(True,"2001:db8:a::123");
  377. ipv6test(True,"fe80::");
  378.  
  379. ipv6test(False,"123");
  380. ipv6test(False,"ldkfj");
  381. ipv6test(False,"2001::FFD3::57ab");
  382. ipv6test(False,"2001:db8:85a3::8a2e:37023:7334");
  383. ipv6test(False,"2001:db8:85a3::8a2e:370k:7334");
  384. ipv6test(False,"1:2:3:4:5:6:7:8:9");
  385. ipv6test(False,"1::2::3");
  386. ipv6test(False,"1:::3:4:5");
  387. ipv6test(False,"1:2:3::4:5:6:7:8:9");
  388.  
  389. # New from Aeron
  390. ipv6test(True,"1111:2222:3333:4444:5555:6666:7777:8888");
  391. ipv6test(True,"1111:2222:3333:4444:5555:6666:7777::");
  392. ipv6test(True,"1111:2222:3333:4444:5555:6666::");
  393. ipv6test(True,"1111:2222:3333:4444:5555::");
  394. ipv6test(True,"1111:2222:3333:4444::");
  395. ipv6test(True,"1111:2222:3333::");
  396. ipv6test(True,"1111:2222::");
  397. ipv6test(True,"1111::");
  398. # ipv6test(True,"::");     #duplicate
  399. ipv6test(True,"1111:2222:3333:4444:5555:6666::8888");
  400. ipv6test(True,"1111:2222:3333:4444:5555::8888");
  401. ipv6test(True,"1111:2222:3333:4444::8888");
  402. ipv6test(True,"1111:2222:3333::8888");
  403. ipv6test(True,"1111:2222::8888");
  404. ipv6test(True,"1111::8888");
  405. ipv6test(True,"::8888");
  406. ipv6test(True,"1111:2222:3333:4444:5555::7777:8888");
  407. ipv6test(True,"1111:2222:3333:4444::7777:8888");
  408. ipv6test(True,"1111:2222:3333::7777:8888");
  409. ipv6test(True,"1111:2222::7777:8888");
  410. ipv6test(True,"1111::7777:8888");
  411. ipv6test(True,"::7777:8888");
  412. ipv6test(True,"1111:2222:3333:4444::6666:7777:8888");
  413. ipv6test(True,"1111:2222:3333::6666:7777:8888");
  414. ipv6test(True,"1111:2222::6666:7777:8888");
  415. ipv6test(True,"1111::6666:7777:8888");
  416. ipv6test(True,"::6666:7777:8888");
  417. ipv6test(True,"1111:2222:3333::5555:6666:7777:8888");
  418. ipv6test(True,"1111:2222::5555:6666:7777:8888");
  419. ipv6test(True,"1111::5555:6666:7777:8888");
  420. ipv6test(True,"::5555:6666:7777:8888");
  421. ipv6test(True,"1111:2222::4444:5555:6666:7777:8888");
  422. ipv6test(True,"1111::4444:5555:6666:7777:8888");
  423. ipv6test(True,"::4444:5555:6666:7777:8888");
  424. ipv6test(True,"1111::3333:4444:5555:6666:7777:8888");
  425. ipv6test(True,"::3333:4444:5555:6666:7777:8888");
  426. ipv6test(True,"::2222:3333:4444:5555:6666:7777:8888");
  427. ipv6test(True,"1111:2222:3333:4444:5555:6666:123.123.123.123");
  428. ipv6test(True,"1111:2222:3333:4444:5555::123.123.123.123");
  429. ipv6test(True,"1111:2222:3333:4444::123.123.123.123");
  430. ipv6test(True,"1111:2222:3333::123.123.123.123");
  431. ipv6test(True,"1111:2222::123.123.123.123");
  432. ipv6test(True,"1111::123.123.123.123");
  433. ipv6test(True,"::123.123.123.123");
  434. ipv6test(True,"1111:2222:3333:4444::6666:123.123.123.123");
  435. ipv6test(True,"1111:2222:3333::6666:123.123.123.123");
  436. ipv6test(True,"1111:2222::6666:123.123.123.123");
  437. ipv6test(True,"1111::6666:123.123.123.123");
  438. ipv6test(True,"::6666:123.123.123.123");
  439. ipv6test(True,"1111:2222:3333::5555:6666:123.123.123.123");
  440. ipv6test(True,"1111:2222::5555:6666:123.123.123.123");
  441. ipv6test(True,"1111::5555:6666:123.123.123.123");
  442. ipv6test(True,"::5555:6666:123.123.123.123");
  443. ipv6test(True,"1111:2222::4444:5555:6666:123.123.123.123");
  444. ipv6test(True,"1111::4444:5555:6666:123.123.123.123");
  445. ipv6test(True,"::4444:5555:6666:123.123.123.123");
  446. ipv6test(True,"1111::3333:4444:5555:6666:123.123.123.123");
  447. ipv6test(True,"::2222:3333:4444:5555:6666:123.123.123.123");
  448.  
  449. # Playing with combinations of "0" and "::"
  450. # NB: these are all sytactically correct, but are bad form
  451. #   because "0" adjacent to "::" should be combined into "::"
  452. ipv6test(True,"::0:0:0:0:0:0:0");
  453. ipv6test(True,"::0:0:0:0:0:0");
  454. ipv6test(True,"::0:0:0:0:0");
  455. ipv6test(True,"::0:0:0:0");
  456. ipv6test(True,"::0:0:0");
  457. ipv6test(True,"::0:0");
  458. ipv6test(True,"::0");
  459. ipv6test(True,"0:0:0:0:0:0:0::");
  460. ipv6test(True,"0:0:0:0:0:0::");
  461. ipv6test(True,"0:0:0:0:0::");
  462. ipv6test(True,"0:0:0:0::");
  463. ipv6test(True,"0:0:0::");
  464. ipv6test(True,"0:0::");
  465. ipv6test(True,"0::");
  466.  
  467. # New invalid from Aeron
  468. # Invalid data
  469. ipv6test(False,"XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX");
  470.  
  471. # Too many components
  472. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:8888:9999");
  473. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:8888::");
  474. ipv6test(False,"::2222:3333:4444:5555:6666:7777:8888:9999");
  475.  
  476. # Too few components
  477. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777");
  478. ipv6test(False,"1111:2222:3333:4444:5555:6666");
  479. ipv6test(False,"1111:2222:3333:4444:5555");
  480. ipv6test(False,"1111:2222:3333:4444");
  481. ipv6test(False,"1111:2222:3333");
  482. ipv6test(False,"1111:2222");
  483. ipv6test(False,"1111");
  484.  
  485. # Missing :
  486. ipv6test(False,"11112222:3333:4444:5555:6666:7777:8888");
  487. ipv6test(False,"1111:22223333:4444:5555:6666:7777:8888");
  488. ipv6test(False,"1111:2222:33334444:5555:6666:7777:8888");
  489. ipv6test(False,"1111:2222:3333:44445555:6666:7777:8888");
  490. ipv6test(False,"1111:2222:3333:4444:55556666:7777:8888");
  491. ipv6test(False,"1111:2222:3333:4444:5555:66667777:8888");
  492. ipv6test(False,"1111:2222:3333:4444:5555:6666:77778888");
  493.  
  494. # Missing : intended for ::
  495. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:8888:");
  496. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:");
  497. ipv6test(False,"1111:2222:3333:4444:5555:6666:");
  498. ipv6test(False,"1111:2222:3333:4444:5555:");
  499. ipv6test(False,"1111:2222:3333:4444:");
  500. ipv6test(False,"1111:2222:3333:");
  501. ipv6test(False,"1111:2222:");
  502. ipv6test(False,"1111:");
  503. ipv6test(False,":");
  504. ipv6test(False,":8888");
  505. ipv6test(False,":7777:8888");
  506. ipv6test(False,":6666:7777:8888");
  507. ipv6test(False,":5555:6666:7777:8888");
  508. ipv6test(False,":4444:5555:6666:7777:8888");
  509. ipv6test(False,":3333:4444:5555:6666:7777:8888");
  510. ipv6test(False,":2222:3333:4444:5555:6666:7777:8888");
  511. ipv6test(False,":1111:2222:3333:4444:5555:6666:7777:8888");
  512.  
  513. # :::
  514. ipv6test(False,":::2222:3333:4444:5555:6666:7777:8888");
  515. ipv6test(False,"1111:::3333:4444:5555:6666:7777:8888");
  516. ipv6test(False,"1111:2222:::4444:5555:6666:7777:8888");
  517. ipv6test(False,"1111:2222:3333:::5555:6666:7777:8888");
  518. ipv6test(False,"1111:2222:3333:4444:::6666:7777:8888");
  519. ipv6test(False,"1111:2222:3333:4444:5555:::7777:8888");
  520. ipv6test(False,"1111:2222:3333:4444:5555:6666:::8888");
  521. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:::");
  522.  
  523. # Double ::");
  524. ipv6test(False,"::2222::4444:5555:6666:7777:8888");
  525. ipv6test(False,"::2222:3333::5555:6666:7777:8888");
  526. ipv6test(False,"::2222:3333:4444::6666:7777:8888");
  527. ipv6test(False,"::2222:3333:4444:5555::7777:8888");
  528. ipv6test(False,"::2222:3333:4444:5555:7777::8888");
  529. ipv6test(False,"::2222:3333:4444:5555:7777:8888::");
  530.  
  531. ipv6test(False,"1111::3333::5555:6666:7777:8888");
  532. ipv6test(False,"1111::3333:4444::6666:7777:8888");
  533. ipv6test(False,"1111::3333:4444:5555::7777:8888");
  534. ipv6test(False,"1111::3333:4444:5555:6666::8888");
  535. ipv6test(False,"1111::3333:4444:5555:6666:7777::");
  536.  
  537. ipv6test(False,"1111:2222::4444::6666:7777:8888");
  538. ipv6test(False,"1111:2222::4444:5555::7777:8888");
  539. ipv6test(False,"1111:2222::4444:5555:6666::8888");
  540. ipv6test(False,"1111:2222::4444:5555:6666:7777::");
  541.  
  542. ipv6test(False,"1111:2222:3333::5555::7777:8888");
  543. ipv6test(False,"1111:2222:3333::5555:6666::8888");
  544. ipv6test(False,"1111:2222:3333::5555:6666:7777::");
  545.  
  546. ipv6test(False,"1111:2222:3333:4444::6666::8888");
  547. ipv6test(False,"1111:2222:3333:4444::6666:7777::");
  548.  
  549. ipv6test(False,"1111:2222:3333:4444:5555::7777::");
  550.  
  551.  
  552. # Too many components"
  553. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4");
  554. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:1.2.3.4");
  555. ipv6test(False,"1111:2222:3333:4444:5555:6666::1.2.3.4");
  556. ipv6test(False,"::2222:3333:4444:5555:6666:7777:1.2.3.4");
  557. ipv6test(False,"1111:2222:3333:4444:5555:6666:1.2.3.4.5");
  558.  
  559. # Too few components
  560. ipv6test(False,"1111:2222:3333:4444:5555:1.2.3.4");
  561. ipv6test(False,"1111:2222:3333:4444:1.2.3.4");
  562. ipv6test(False,"1111:2222:3333:1.2.3.4");
  563. ipv6test(False,"1111:2222:1.2.3.4");
  564. ipv6test(False,"1111:1.2.3.4");
  565. ipv6test(False,"1.2.3.4");
  566.  
  567. # Missing :
  568. ipv6test(False,"11112222:3333:4444:5555:6666:1.2.3.4");
  569. ipv6test(False,"1111:22223333:4444:5555:6666:1.2.3.4");
  570. ipv6test(False,"1111:2222:33334444:5555:6666:1.2.3.4");
  571. ipv6test(False,"1111:2222:3333:44445555:6666:1.2.3.4");
  572. ipv6test(False,"1111:2222:3333:4444:55556666:1.2.3.4");
  573. ipv6test(False,"1111:2222:3333:4444:5555:66661.2.3.4");
  574.  
  575. # Missing .
  576. ipv6test(False,"1111:2222:3333:4444:5555:6666:255255.255.255");
  577. ipv6test(False,"1111:2222:3333:4444:5555:6666:255.255255.255");
  578. ipv6test(False,"1111:2222:3333:4444:5555:6666:255.255.255255");
  579.  
  580. # Missing : intended for ::
  581. ipv6test(False,":1.2.3.4");
  582. ipv6test(False,":6666:1.2.3.4");
  583. ipv6test(False,":5555:6666:1.2.3.4");
  584. ipv6test(False,":4444:5555:6666:1.2.3.4");
  585. ipv6test(False,":3333:4444:5555:6666:1.2.3.4");
  586. ipv6test(False,":2222:3333:4444:5555:6666:1.2.3.4");
  587. ipv6test(False,":1111:2222:3333:4444:5555:6666:1.2.3.4");
  588.  
  589. # :::
  590. ipv6test(False,":::2222:3333:4444:5555:6666:1.2.3.4");
  591. ipv6test(False,"1111:::3333:4444:5555:6666:1.2.3.4");
  592. ipv6test(False,"1111:2222:::4444:5555:6666:1.2.3.4");
  593. ipv6test(False,"1111:2222:3333:::5555:6666:1.2.3.4");
  594. ipv6test(False,"1111:2222:3333:4444:::6666:1.2.3.4");
  595. ipv6test(False,"1111:2222:3333:4444:5555:::1.2.3.4");
  596.  
  597. # Double ::
  598. ipv6test(False,"::2222::4444:5555:6666:1.2.3.4");
  599. ipv6test(False,"::2222:3333::5555:6666:1.2.3.4");
  600. ipv6test(False,"::2222:3333:4444::6666:1.2.3.4");
  601. ipv6test(False,"::2222:3333:4444:5555::1.2.3.4");
  602.  
  603. ipv6test(False,"1111::3333::5555:6666:1.2.3.4");
  604. ipv6test(False,"1111::3333:4444::6666:1.2.3.4");
  605. ipv6test(False,"1111::3333:4444:5555::1.2.3.4");
  606.  
  607. ipv6test(False,"1111:2222::4444::6666:1.2.3.4");
  608. ipv6test(False,"1111:2222::4444:5555::1.2.3.4");
  609.  
  610. ipv6test(False,"1111:2222:3333::5555::1.2.3.4");
  611.  
  612. # Missing parts
  613. ipv6test(False,"::.");
  614. ipv6test(False,"::..");
  615. ipv6test(False,"::...");
  616. ipv6test(False,"::1...");
  617. ipv6test(False,"::1.2..");
  618. ipv6test(False,"::1.2.3.");
  619. ipv6test(False,"::.2..");
  620. ipv6test(False,"::.2.3.");
  621. ipv6test(False,"::.2.3.4");
  622. ipv6test(False,"::..3.");
  623. ipv6test(False,"::..3.4");
  624. ipv6test(False,"::...4");
  625.  
  626. # Extra : in front
  627. ipv6test(False,":1111:2222:3333:4444:5555:6666:7777::");
  628. ipv6test(False,":1111:2222:3333:4444:5555:6666::");
  629. ipv6test(False,":1111:2222:3333:4444:5555::");
  630. ipv6test(False,":1111:2222:3333:4444::");
  631. ipv6test(False,":1111:2222:3333::");
  632. ipv6test(False,":1111:2222::");
  633. ipv6test(False,":1111::");
  634. ipv6test(False,":::");
  635. ipv6test(False,":1111:2222:3333:4444:5555:6666::8888");
  636. ipv6test(False,":1111:2222:3333:4444:5555::8888");
  637. ipv6test(False,":1111:2222:3333:4444::8888");
  638. ipv6test(False,":1111:2222:3333::8888");
  639. ipv6test(False,":1111:2222::8888");
  640. ipv6test(False,":1111::8888");
  641. ipv6test(False,":::8888");
  642. ipv6test(False,":1111:2222:3333:4444:5555::7777:8888");
  643. ipv6test(False,":1111:2222:3333:4444::7777:8888");
  644. ipv6test(False,":1111:2222:3333::7777:8888");
  645. ipv6test(False,":1111:2222::7777:8888");
  646. ipv6test(False,":1111::7777:8888");
  647. ipv6test(False,":::7777:8888");
  648. ipv6test(False,":1111:2222:3333:4444::6666:7777:8888");
  649. ipv6test(False,":1111:2222:3333::6666:7777:8888");
  650. ipv6test(False,":1111:2222::6666:7777:8888");
  651. ipv6test(False,":1111::6666:7777:8888");
  652. ipv6test(False,":::6666:7777:8888");
  653. ipv6test(False,":1111:2222:3333::5555:6666:7777:8888");
  654. ipv6test(False,":1111:2222::5555:6666:7777:8888");
  655. ipv6test(False,":1111::5555:6666:7777:8888");
  656. ipv6test(False,":::5555:6666:7777:8888");
  657. ipv6test(False,":1111:2222::4444:5555:6666:7777:8888");
  658. ipv6test(False,":1111::4444:5555:6666:7777:8888");
  659. ipv6test(False,":::4444:5555:6666:7777:8888");
  660. ipv6test(False,":1111::3333:4444:5555:6666:7777:8888");
  661. ipv6test(False,":::3333:4444:5555:6666:7777:8888");
  662. ipv6test(False,":::2222:3333:4444:5555:6666:7777:8888");
  663. ipv6test(False,":1111:2222:3333:4444:5555:6666:1.2.3.4");
  664. ipv6test(False,":1111:2222:3333:4444:5555::1.2.3.4");
  665. ipv6test(False,":1111:2222:3333:4444::1.2.3.4");
  666. ipv6test(False,":1111:2222:3333::1.2.3.4");
  667. ipv6test(False,":1111:2222::1.2.3.4");
  668. ipv6test(False,":1111::1.2.3.4");
  669. ipv6test(False,":::1.2.3.4");
  670. ipv6test(False,":1111:2222:3333:4444::6666:1.2.3.4");
  671. ipv6test(False,":1111:2222:3333::6666:1.2.3.4");
  672. ipv6test(False,":1111:2222::6666:1.2.3.4");
  673. ipv6test(False,":1111::6666:1.2.3.4");
  674. ipv6test(False,":::6666:1.2.3.4");
  675. ipv6test(False,":1111:2222:3333::5555:6666:1.2.3.4");
  676. ipv6test(False,":1111:2222::5555:6666:1.2.3.4");
  677. ipv6test(False,":1111::5555:6666:1.2.3.4");
  678. ipv6test(False,":::5555:6666:1.2.3.4");
  679. ipv6test(False,":1111:2222::4444:5555:6666:1.2.3.4");
  680. ipv6test(False,":1111::4444:5555:6666:1.2.3.4");
  681. ipv6test(False,":::4444:5555:6666:1.2.3.4");
  682. ipv6test(False,":1111::3333:4444:5555:6666:1.2.3.4");
  683. ipv6test(False,":::2222:3333:4444:5555:6666:1.2.3.4");
  684.  
  685. # Extra : at end
  686. ipv6test(False,"1111:2222:3333:4444:5555:6666:7777:::");
  687. ipv6test(False,"1111:2222:3333:4444:5555:6666:::");
  688. ipv6test(False,"1111:2222:3333:4444:5555:::");
  689. ipv6test(False,"1111:2222:3333:4444:::");
  690. ipv6test(False,"1111:2222:3333:::");
  691. ipv6test(False,"1111:2222:::");
  692. ipv6test(False,"1111:::");
  693. ipv6test(False,":::");
  694. ipv6test(False,"1111:2222:3333:4444:5555:6666::8888:");
  695. ipv6test(False,"1111:2222:3333:4444:5555::8888:");
  696. ipv6test(False,"1111:2222:3333:4444::8888:");
  697. ipv6test(False,"1111:2222:3333::8888:");
  698. ipv6test(False,"1111:2222::8888:");
  699. ipv6test(False,"1111::8888:");
  700. ipv6test(False,"::8888:");
  701. ipv6test(False,"1111:2222:3333:4444:5555::7777:8888:");
  702. ipv6test(False,"1111:2222:3333:4444::7777:8888:");
  703. ipv6test(False,"1111:2222:3333::7777:8888:");
  704. ipv6test(False,"1111:2222::7777:8888:");
  705. ipv6test(False,"1111::7777:8888:");
  706. ipv6test(False,"::7777:8888:");
  707. ipv6test(False,"1111:2222:3333:4444::6666:7777:8888:");
  708. ipv6test(False,"1111:2222:3333::6666:7777:8888:");
  709. ipv6test(False,"1111:2222::6666:7777:8888:");
  710. ipv6test(False,"1111::6666:7777:8888:");
  711. ipv6test(False,"::6666:7777:8888:");
  712. ipv6test(False,"1111:2222:3333::5555:6666:7777:8888:");
  713. ipv6test(False,"1111:2222::5555:6666:7777:8888:");
  714. ipv6test(False,"1111::5555:6666:7777:8888:");
  715. ipv6test(False,"::5555:6666:7777:8888:");
  716. ipv6test(False,"1111:2222::4444:5555:6666:7777:8888:");
  717. ipv6test(False,"1111::4444:5555:6666:7777:8888:");
  718. ipv6test(False,"::4444:5555:6666:7777:8888:");
  719. ipv6test(False,"1111::3333:4444:5555:6666:7777:8888:");
  720. ipv6test(False,"::3333:4444:5555:6666:7777:8888:");
  721. ipv6test(False,"::2222:3333:4444:5555:6666:7777:8888:");
  722.  
  723. # Additional cases: http://crisp.tweakblogs.net/blog/2031/ipv6-validation-%28and-caveats%29.html
  724. ipv6test(True,"0:a:b:c:d:e:f::");
  725. ipv6test(True,"::0:a:b:c:d:e:f"); # syntactically correct, but bad form (::0:... could be combined)
  726. ipv6test(True,"a:b:c:d:e:f:0::");
  727. ipv6test(False,"':10.0.0.1");
  728.  
  729. print "Failed %s out of %s tests" % (failed_tests, num_tests)
Advertisement
RAW Paste Data Copied
Advertisement