File: 102_ipv4_strict.t

package info (click to toggle)
libregexp-common-perl 2024080801-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,328 kB
  • sloc: perl: 17,842; makefile: 2
file content (54 lines) | stat: -rwxr-xr-x 1,662 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
#!/usr/bin/perl

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

use Regexp::Common;
use Test::More;

my $r = eval "require Test::Regexp; 1";

unless ($r) {
    print "1..0 # SKIP Test::Regexp not found\n";
    exit;
}

my $Test = Test::Regexp:: -> new -> init (
    pattern       =>  $RE {net} {IPv4} {strict},
    keep_pattern  =>  $RE {net} {IPv4} {strict} {-keep},
    name          => "Strict IPv4 addresses",
);
 

foreach my $number (0 .. 999) {
    my $address = "$number.$number.$number.$number";
    if ($number < 256) {
        $Test -> match ($address,
                       [$address, $number, $number, $number, $number],
                       test => "Accept number $number");
        if ($number < 10) {
            my $address = sprintf "%d.%d.%02d.%d" => ($number) x 4;
            $Test -> no_match ($address, reason => "Leading 0 not allowed");
        }
        if ($number < 100) {
            my $address = sprintf "%d.%03d.%d.%d" => ($number) x 4;
            $Test -> no_match ($address, reason => "Leading 0 not allowed");
        }
    }
    else {
        $Test -> no_match ($address, reason => "Number exceeds 256");
    }
}


$Test -> no_match ("1.2.3.4.5",    reason => "To many octets");
$Test -> no_match ("1.2.3",        reason => "No enough octets");
$Test -> no_match ("12.34.ab.56",  reason => "Non numbers in octets");
$Test -> no_match ("1.1234.2.3",   reason => "Too many digits in octet");
$Test -> no_match ("12:34:45:67",  reason => "Incorrect separator");
$Test -> no_match ("+12.34.56.78", reason => "Garbage before address");
$Test -> no_match ("12.34.56.78 ", reason => "Garbage after address");


done_testing;