File: 111_ipv6.t

package info (click to toggle)
libregexp-common-perl 2017060201-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: perl: 17,868; makefile: 2
file content (146 lines) | stat: -rwxr-xr-x 5,642 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl

use strict;
use warnings;
no  warnings 'syntax';

use Regexp::Common;

my $count = 0;
my $PAT;

END {print "1..$count\n"}

sub try {
    $PAT     = shift;
    my $name = shift;
    print "# $name\n";
}

sub pass {
    my $address = shift;
    my $r = $address =~ /^$PAT/ && $address eq $&;
    printf "%s %d  # Matching %s\n", $r ? "ok" : "not ok", ++ $count, $address;
}

sub fail {
    my $address = shift;
    my $r = $address !~ /^$PAT/ || $address ne $&;
    printf "%s %d  # Failing %s\n", $r ? "ok" : "not ok", ++ $count, $address;
}

sub match {
    my $address = $_ [0];
    my @matches = @_;

    my $r = $address =~ /^$PAT/ && $address eq $&;
    printf "%s %d  # Matching %s\n", $r ? "ok" : "not ok", ++ $count, $address;

    if (!$r) {
        for my $i (0 .. @matches) {
            printf "not ok %d  # SKIP\n" => ++ $count;
        }
        return;
    }

    #
    # Correct number of matches?
    #
    printf "%s %d     # Number of matches\n" =>
           @matches == @- - 1 ? "ok" : "not ok", ++ $count;

    for (my $i = 0; $i < @matches; $i ++) {
        no strict 'refs';
        my $matched = ${$i + 1};
        printf "%s %d     # \$%d eq '%s'\n" =>
               $matched eq $matches [$i] ? "ok" : "not ok",
               ++ $count, $i + 1, $matches [$i];
    }
}



try $RE {net} {IPv6} => '$RE {net} {IPv6}';

pass "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
pass "2001:db8:85a3:0:0:8a2e:370:7334";               # Leading 0's removed
pass "2001:DB8:85A3:0:0:8A2E:370:7334";               # Upper case allowed
pass "2001:Db8:85A3:0:0:8a2E:370:7334";               # Mixed case allowed
pass "2001:db8:85a3::8a2e:370:7334";                  # Contractions
pass "2001:db8::8a2e:370:7334";
pass "2001::8a2e:370:7334";
pass "::8a2e:370:7334";
pass "::370:7334";
pass "::7334";
pass "::";

fail "2001:0db8:85a3:0000:0000:8a2e:0370:7334:1234";  # Too many parts
fail "2001:0db8:85a3:0000:0000:8a2e:0370";            # Not enough parts
fail "20013:db8:85a3:0:0:8a2e:370:7334";              # Part too long
fail "2001:0db8:85a3:0000:0000:8a2e:0370:7334:";      # Trailing separator
fail ":2001:0db8:85a3:0000:0000:8a2e:0370:7334";      # Leading separator
fail "2001:db8:85a3:0::8a2e:370:7334";                # Only one unit removed
fail "2001::8a2e:370::7334";                          # Two contractions
fail "2001:::8a2e:370:7334";                          # Three separators
fail "2001.db8.85a3.0.0.8a2e.370.7334";               # Wrong separator

try $RE {net} {IPv6} {-style => "hex"} => '$RE {net} {IPv6} {-style => "hex"}';

pass "2001:db8:85a3:0:0:8a2e:370:7334";               # Lower case allowed
fail "2001:DB8:85A3:0:0:8A2E:370:7334";               # Upper case not allowed
fail "2001:Db8:85A3:0:0:8a2E:370:7334";               # Mixed case not allowed

try $RE {net} {IPv6} {-style => "HEX"} => '$RE {net} {IPv6} {-style => "HEX"}';

fail "2001:db8:85a3:0:0:8a2e:370:7334";               # Lower case allowed
pass "2001:DB8:85A3:0:0:8A2E:370:7334";               # Upper case not allowed
fail "2001:Db8:85A3:0:0:8a2E:370:7334";               # Mixed case not allowed

try $RE {net} {IPv6} {-sep => "[.]"} => '$RE {net} {IPv6} {-sep => "[.]"}';

pass "2001.db8.85a3.0.0.8a2e.370.7334";               # Lower case allowed
pass "2001.DB8.85A3.0.0.8A2E.370.7334";               # Upper case allowed
fail "2001:db8:85a3:0:0:8a2e:370:7334";               # Fail on default sep


try $RE {net} {IPv6} {-keep} => '$RE {net} {IPv6} {-keep}';

match  "2001:0db8:85a3:0000:0000:8a2e:0370:7334" =>
       "2001", "0db8", "85a3", "0000", "0000", "8a2e", "0370", "7334";
match  "2001:0db8:85a3:0:0:8a2e:0370:7334"       =>
       "2001", "0db8", "85a3",    "0",    "0", "8a2e", "0370", "7334";
match  "2001:db8:85a3:0:0:8a2e:370:7334"         =>
       "2001",  "db8", "85a3",    "0",    "0", "8a2e",  "370", "7334";
match  "2001:db8:85a3::8a2e:370:7334"            =>
       "2001",  "db8", "85a3",     "",     "", "8a2e",  "370", "7334";
match  "2001:db8::8a2e:370:7334"                 =>
       "2001",  "db8",     "",     "",     "", "8a2e",  "370", "7334";
match  "2001::8a2e:370:7334"                     =>
       "2001",     "",     "",     "",     "", "8a2e",  "370", "7334";
match  "::8a2e:370:7334"                         =>
           "",     "",     "",     "",     "", "8a2e",  "370", "7334";
match  "::370:7334"                              =>
           "",     "",     "",     "",     "",     "",  "370", "7334";
match  "::7334"                                  =>
           "",     "",     "",     "",     "",     "",     "", "7334";
match  "::"                                      =>
           "",     "",     "",     "",     "",     "",     "",     "";

fail "2001:0db8:85a3:0000:0000:8a2e:0370:7334:1234";  # Too many parts
fail "2001:0db8:85a3:0000:0000:8a2e:0370";            # Not enough parts
fail "20013:db8:85a3:0:0:8a2e:370:7334";              # Part too long
fail "2001:0db8:85a3:0000:0000:8a2e:0370:7334:";      # Trailing separator
fail ":2001:0db8:85a3:0000:0000:8a2e:0370:7334";      # Leading separator
fail "2001:db8:85a3:0::8a2e:370:7334";                # Only one unit removed
fail "2001::8a2e:370::7334";                          # Two contractions
fail "2001:::8a2e:370:7334";                          # Three separators
fail "2001.db8.85a3.0.0.8a2e.370.7334";               # Wrong separator


try $RE {net} {IPv6} {-style => 'HEX'} {-sep => '[.]'} {-keep} =>
 q [$RE {net} {IPv6} {-style => 'HEX'} {-sep => '[.]'} {-keep}];

match  "2001.DB8.85A3..8A2E.370.7334"         =>
       "2001",  "DB8", "85A3",     "",     "", "8A2E",  "370", "7334";

__END__