import libxml2 ip6_pat1 = \ '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}' \ + '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|' \ + '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}' \ + '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))' \ + '(%[\p{N}\p{L}]+)?' ip6_pat2 = \ '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|' \ + '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)' \ + '(%.+)?' ip6_re1 = libxml2.regexpCompile(ip6_pat1) ip6_re2 = libxml2.regexpCompile(ip6_pat2) pfx_pat1 = \ '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}' \ + '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|' \ + '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}' \ + '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))' \ + '(/(([0-9])|([0-9]{2})|(1[0-1][0-9])|(12[0-8])))' pfx_pat2 = \ '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|' \ + '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)' \ + '(/.+)' pfx_re1 = libxml2.regexpCompile(pfx_pat1) pfx_re2 = libxml2.regexpCompile(pfx_pat2) ip6_good = ['ABCD:EF01:2345:6789:ABCD:EF01:2345:6789', '2001:DB8:0:0:8:800:200C:417A', 'FF01:0:0:0:0:0:0:101', '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:0', '2001:DB8::8:800:200C:417A', 'FF01::101', '::1', '::', '0:0:0:0:0:0:13.1.68.3', '0:0:0:0:0:FFFF:129.144.52.38', '::13.1.68.3', '::FFFF:129.144.52.38', '1:2:3:4:5:6:7::', '::1:2:3:4:5:6:7', '::1%eth0', '::1%42', '::1%en1'] ip6_bad = ['::1::', '::FFFF:1.2.3.4.5', '1:2:3:4:5:6:7:8:9' '::1%', '::1%*', '::1%-1'] pfx_good = ['ABCD:EF01:2345:6789:ABCD:EF01:2345:6789/48', '2001:DB8:0:0:8:800:200C:417A/0', 'FF01:0:0:0:0:0:0:101/128', '0:0:0:0:0:0:0:1/1', '0:0:0:0:0:0:0:0/123', '2001:DB8::8:800:200C:417A/8', 'FF01::101/64', '::1/64', '::/64', '0:0:0:0:0:0:13.1.68.3/64', '0:0:0:0:0:FFFF:129.144.52.38/64', '::13.1.68.3/64', '::FFFF:129.144.52.38/64', '1:2:3:4:5:6:7::/64', '::1:2:3:4:5:6:7/64'] pfx_bad = ['::1::/48', '::FFFF:1.2.3.4.5/48', '1:2:3:4:5:6:7:8:9/48', '::1/-1', '::1/a', '::1/129', '::1/1234', '::1/+12' ] for ip in ip6_good: if ip6_re1.regexpExec(ip) != 1: print "** ipv6 pattern #1 fails on %s" % ip if ip6_re2.regexpExec(ip) != 1: print "** ipv6 pattern #2 fails on %s" % ip for ip in ip6_bad: if ip6_re1.regexpExec(ip) != 1: continue if ip6_re2.regexpExec(ip) != 1: continue print "** ipv6 pattern fail to reject %s" % ip for ip in pfx_good: if pfx_re1.regexpExec(ip) != 1: print "** pfx pattern #1 fails on %s" % ip if pfx_re2.regexpExec(ip) != 1: print "** pfx pattern #2 fails on %s" % ip for ip in pfx_bad: if pfx_re1.regexpExec(ip) != 1: continue if pfx_re2.regexpExec(ip) != 1: continue print "** pfx pattern fail to reject %s" % ip