File: IP.pm

package info (click to toggle)
libdata-validate-ip-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 1,069; sh: 23; makefile: 2
file content (395 lines) | stat: -rw-r--r-- 10,277 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package    # hide from PAUSE
    Test::Data::Validate::IP;

use strict;
use warnings;

use Data::Validate::IP;
use Exporter qw( import );
use Test::More 0.88;

## no critic (Modules::ProhibitAutomaticExportation)
our @EXPORT = 'run_tests';
## use critic

my $object = Data::Validate::IP->new();

my %ipv4_types = (
    private    => [qw(10.0.0.1 172.16.0.1 192.168.0.1)],
    public     => [qw(1.2.3.4 123.123.44.55 216.17.184.1)],
    loopback   => [qw(127.0.0.1)],
    testnet    => [qw(192.0.2.9 198.51.100.33 203.0.113.44)],
    multicast  => [qw(224.0.0.1)],
    anycast    => [qw(192.88.99.45)],
    linklocal  => [qw(169.254.0.1)],
    unroutable => [
        qw(
            0.0.0.1
            100.64.1.2
            192.0.0.4
            198.18.0.55
            240.0.0.4
            255.255.255.254
            255.255.255.255
        )
    ],
);

my %ipv6_types = (
    private => [
        qw(
            fc00::
            fc01::1234
            fdef::
            fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    public => [
        qw(
            ::abcd:1234
            1::
            2::
            1:1:1:1::
            2001:abcd::
            abcd::
        )
    ],
    loopback    => [qw(::1)],
    ipv4_mapped => [
        qw(
            ::ffff:0:0
            ::ffff:0:1234
            ::ffff:1.2.3.4
            ::ffff:ffff:ffff
        )
    ],
    discard => [
        qw(
            100::
            100::1234
            100:0000:0000:0000:ffff:ffff:ffff:ffff
        )
    ],
    multicast => [
        qw(
            ff00::
            ffff::
            ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    linklocal => [
        qw(
            fe80::
            fe89::
            febf::
            febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    special => [
        [ '2001::'     => [qw( teredo )] ],
        [ '2001::1234' => [qw( teredo )] ],
        qw(
            2001:1ff:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    teredo => [
        qw(
            2001::
            2001::1234
            2001:0:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    orchid => [
        qw(
            2001:10::
            2001:10::1234
            2001:001F:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
        )
    ],
    documentation => [
        qw(
            2001:db8::
            2001:db8::1234
            2001:0db8:ffff:ffff:ffff:ffff:ffff:ffff
        )
    ],
    unspecified => [
        qw(
            ::
            ::0
        )
    ],
);

my %ip_types = map { $_ => [ @{ $ipv4_types{$_} }, @{ $ipv6_types{$_} } ] }
    qw( linklocal loopback multicast private public );

sub run_tests {
    _ipv4_basic_tests();
    _type_tests( \%ipv4_types, 4 );
    _ipv4_innet_tests();

    _ipv6_basic_tests();
    _type_tests( \%ipv6_types, 6 );

    # Tests for subs without a family
    _ip_basic_tests();
    _type_tests( \%ip_types );
}

sub _ipv4_basic_tests {
    my @valid_ipv4 = qw(
        0.0.0.0
        1.2.3.4
        216.17.184.1
        255.255.255.255
    );

    for my $ip (@valid_ipv4) {
        is( is_ipv4($ip),          $ip, "is_ipv4($ip) returns $ip" );
        is( $object->is_ipv4($ip), $ip, "->is_ipv4($ip) returns $ip" );
    }

    my @invalid_ipv4 = (
        qw(
            2067:fa88::0
            www.neely.cx
            216.17.184.G
            216.17.184.1.
            216.17.184
            216.17.184.
            256.17.184.1
            216.017.184.1
            016.17.184.1
        ),
        "1.1.1.1\0 exploit goes here",
    );

    for my $ip (@invalid_ipv4) {
        is( is_ipv4($ip),          undef, "is_ipv4($ip) returns undef" );
        is( $object->is_ipv4($ip), undef, "->is_ipv4($ip) returns undef" );

        for my $type ( sort keys %ipv4_types ) {
            my ( $is_sub_name, $is_sub ) = _sub_for_type( $type, 4 );

            is( $is_sub->($ip), undef, "$is_sub_name($ip) returns undef" );
            is(
                $object->$is_sub_name($ip), undef,
                "->$is_sub_name($ip) returns undef"
            );
        }
    }
}

sub _ipv4_innet_tests {
    my @tests = (
        [ '216.17.184.1', '216.17.184.0/24', 1 ],
        [ '127.0.0.1',    '216.17.184.0/24', 0 ],
        [ 'invalid',      '216.17.184.0/24', 0 ],
        [ '0.0.0.0',      'default',         1 ],
        [ '1.2.3.4',      'default',         1 ],
        [ '216.240.32.1', '216.240.32.1',    1 ],
    );

    # These are accepted for backwards compatibility with the time when we
    # used Net::Netmask.
    my @deprecated = (
        [ '216.240.32.1', '216.240.32/24',              1, 1 ],
        [ '216.240.32.1', '216.240/16',                 1, 1 ],
        [ '216.240.32.1', '216.240.32.0:255.255.255.0', 1, 1 ],
        [ '216.240.32.1', '216.240.32.0-255.255.255.0', 1, 1 ],
        [ '216.240.32.1', '216.240.32',                 1, 1 ],
        [ '216.240.32.1', '216.240',                    1, 1 ],
        [ '216.240.32.1', '216',                        1, 1 ],
        [ '216.240.32.1', '216.240.32.0#0.0.31.255',    1, 1 ],
    );

    my @warnings;
    for my $triplet ( @tests, @deprecated ) {
        my ( $ip, $network, $is_member, $is_deprecated ) = @{$triplet};

        my $expect = $is_member ? $ip : undef;

        my $expect_string = $expect || 'undef';

        local $SIG{__WARN__} = sub { push @warnings, @_ }
            if $is_deprecated;

        is(
            is_innet_ipv4( $ip, $network ), $expect,
            "is_innet_ipv4($ip, $network) returns $expect_string"
        );
    }

    is(
        scalar @warnings,
        1,
        'got one warning from is_innet_ipv4'
    );

    like(
        $warnings[0],
        qr/\QUse of non-CIDR notation for networks with is_innet_ipv4() is deprecated/,
        'got expected deprecation warning'
    );

    like(
        $warnings[0],
        qr/at line \d+ of Test::Data::Validate::IP in sub Test::Data::Validate::IP::_ipv4_innet_tests/,
        'deprecation warning identifies caller'
    );
}

sub _ipv6_basic_tests {
    my @valid = qw(
        2067:fa88::0
        2067:FA88::1
        2067:FA88::
        2001:4888:205:7126:e0:120::
        2607:fa88::8a2e:370:7334
        2001:0db8:0000:0000:0000:0000:1428:57ab
        2001:0db8:0000:0000:0000::1428:57ab
        2001:0db8:0:0:0:0:1428:57ab
        2001:0db8:0:0::1428:57ab
        2001:0db8::1428:57ab
        2001:db8::1428:57ab
        ::
        ::0
        ::1
        ::ffff:12.34.56.78
        0:0:0:0:0:ffff:12.34.56.78
    );

    for my $ip (@valid) {
        is( is_ipv6($ip),          $ip, "is_ipv6($ip) returns $ip" );
        is( $object->is_ipv6($ip), $ip, "->is_ipv6($ip) returns $ip" );
    }

    my @invalid = (
        qw(
            0000:00:0000:0000:0000:0000:0000:00000
            2067:fa88
            2067:FA88
            2067:::
            2067:::1
            2067::1:
            216.17.184.1
            bbb.bbb.bbb
            :::
            g123::1234
            :abcd
        ),
        "::1\0 invalid",
    );

    for my $ip (@invalid) {
        is( is_ipv6($ip),          undef, "is_ipv6($ip) returns undef" );
        is( $object->is_ipv6($ip), undef, "->is_ipv6($ip) returns undef" );

        for my $type ( sort keys %ipv6_types ) {
            my ( $is_sub_name, $is_sub ) = _sub_for_type( $type, 6 );

            is( $is_sub->($ip), undef, "$is_sub_name($ip) returns undef" );
            is(
                $object->$is_sub_name($ip), undef,
                "->$is_sub_name($ip) returns undef"
            );
        }
    }
}

sub _ip_basic_tests {
    my @valid = qw( 1.1.1.1 2067:fa88::0 ::1.1.1.1 );

    for my $ip (@valid) {
        is( is_ip($ip),          $ip, "is_ip($ip) returns $ip" );
        is( $object->is_ip($ip), $ip, "->is_ip($ip) returns $ip" );

    }

    my @invalid = (
        qw(
            2067:fa88
            216.17.184
            :216.17.184
        ),
        "::1\0 invalid"
    );
    for my $ip (@invalid) {
        is( is_ip($ip),          undef, "is_ip($ip) returns undef" );
        is( $object->is_ip($ip), undef, "->is_ip($ip) returns undef" );
    }
}

sub _type_tests {
    my $types     = shift;
    my $ip_number = shift;

    my @types = sort keys %{$types};

    for my $type (@types) {
        for my $test ( @{ $types->{$type} } ) {
            my ( $ip, $is_also ) = ref $test ? @{$test} : ( $test, [] );

            my ( $is_sub_name, $is_sub ) = _sub_for_type( $type, $ip_number );

            is( $is_sub->($ip), $ip, "$is_sub_name($ip) returns $ip" );
            is(
                $object->$is_sub_name($ip), $ip,
                "->$is_sub_name($ip) returns $ip"
            );

            for my $other ( sort grep { $_ ne $type } @types ) {
                next if grep { $_ eq $other } @{$is_also};

                my ( $other_sub_name, $other_sub )
                    = _sub_for_type( $other, $ip_number );

                ## no critic (Subroutines::ProtectPrivateSubs)
                if (
                    Data::Validate::IP::_network_is_subnet_of(
                        $type, $other
                    )
                ) {
                    is(
                        $other_sub->($ip), $ip,
                        "$other_sub_name($ip) returns $ip"
                    );
                    is(
                        $object->$other_sub_name($ip), $ip,
                        "->$other_sub_name($ip) returns $ip"
                    );
                }
                else {
                    is(
                        $other_sub->($ip), undef,
                        "$other_sub_name($ip) returns undef"
                    );
                    is(
                        $object->$other_sub_name($ip), undef,
                        "->$other_sub_name($ip) returns undef"
                    );
                }
            }
        }
    }
}

sub _sub_for_type {
    my $type      = shift;
    my $ip_number = shift;

    my $sub_name = 'is_' . $type . '_ip';
    $sub_name .= 'v' . $ip_number if defined $ip_number;

    my $sub = do {
        ## no critic (TestingAndDebugging::ProhibitNoStrict)
        no strict 'refs';
        \&{$sub_name};
        }
        or die "No sub named $sub_name was imported";

    return ( $sub_name, $sub );
}

1;