File: private.t

package info (click to toggle)
libnet-cidr-set-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 292 kB
  • sloc: perl: 1,116; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,911 bytes parent folder | download | duplicates (5)
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
#!perl

use strict;
use warnings;
use Test::More tests => 150;
use Net::CIDR::Set;
use Net::CIDR::Set::IPv4;
use Net::CIDR::Set::IPv6;

{
  # _inc
  for my $b ( 0 .. 31 ) {
    my $n = 1 << $b;
    my $p = $n - 1;
    my $q = $n + 1;
    is unpack( 'N', Net::CIDR::Set::_inc( pack 'N', $p ) ), $n,
     "_inc($p) == $n";
    is unpack( 'N', Net::CIDR::Set::_inc( pack 'N', $n ) ), $q,
     "_inc($n) == $q";
    is unpack( 'N', Net::CIDR::Set::_dec( pack 'N', $n ) ), $p,
     "_dec($n) == $p";
    is unpack( 'N', Net::CIDR::Set::_dec( pack 'N', $q ) ), $n,
     "_dec($q) == $n";
  }
  my @big = (
    {
      name   => '0 to 1',
      before => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
      after  => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ]
    },
    {
      name => 'wrap some',
      before =>
       [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255 ],
      after => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ]
    },
    {
      name   => 'wrap all',
      before => [
        255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255
      ],
      after => [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
    },
  );
  for my $b ( @big ) {
    my $name = $b->{name};
    my @inc  = unpack 'C*',
     Net::CIDR::Set::_inc( pack 'C*', @{ $b->{before} } );
    is_deeply [@inc], $b->{after}, "$name: _inc";
    my @dec = unpack 'C*', Net::CIDR::Set::_dec( pack 'C*', @inc );
    is_deeply [@dec], $b->{before}, "$name: _dec";
  }
}

{
  my @case = (
    {
      ip     => '127.0.0.1',
      expect => [ [ 0, 127, 0, 0, 1 ], [ 0, 127, 0, 0, 2 ] ]
    },
    {
      ip     => '192.168.0.0/16',
      expect => [ [ 0, 192, 168, 0, 0 ], [ 0, 192, 169, 0, 0 ] ]
    },
    {
      ip     => '192.168.0.0/255.255.0.0',
      expect => [ [ 0, 192, 168, 0, 0 ], [ 0, 192, 169, 0, 0 ] ]
    },
    {
      ip    => '192.168.0.0/0.0.255.255',
      error => qr{Can't decode}
    },
    {
      ip     => '0.0.0.0/0',
      expect => [ [ 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0 ] ]
    },
    {
      ip     => '192.168.0.12-192.168.1.13',
      expect => [ [ 0, 192, 168, 0, 12 ], [ 0, 192, 168, 1, 14 ] ]
    },
    {
      ip     => '0.0.0.0-255.255.255.255',
      expect => [ [ 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0 ] ]
    },
  );
  for my $case ( @case ) {
    my @enc = eval { Net::CIDR::Set::IPv4->encode( $case->{ip} ) };
    if ( my $error = $case->{error} ) {
      like $@, $error, 'error';
    }
    else {
      my @got = map { [ unpack 'C*', $_ ] } @enc;
      is_deeply [@got], $case->{expect}, "encode $case->{ip}";
    }
  }
}

{
  my @case = (
    {
      range => [ [ 0, 127, 0, 0, 1 ], [ 0, 127, 0, 0, 2 ] ],
      generic => 0,
      expect  => '127.0.0.1',
    },
    {
      range => [ [ 0, 127, 0, 0, 1 ], [ 0, 127, 0, 0, 2 ] ],
      generic => 1,
      expect  => '127.0.0.1/32',
    },
    {
      range => [ [ 0, 127, 0, 0, 1 ], [ 0, 127, 0, 0, 2 ] ],
      generic => 2,
      expect  => '127.0.0.1-127.0.0.1',
    },
    {
      range => [ [ 0, 192, 168, 0, 12 ], [ 0, 192, 168, 1, 14 ] ],
      generic => 0,
      expect  => '192.168.0.12-192.168.1.13',
    },
    {
      range => [ [ 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0 ] ],
      generic => 0,
      expect  => '0.0.0.0/0',
    },
    {
      range => [ [ 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0 ] ],
      generic => 1,
      expect  => '0.0.0.0/0',
    },
    {
      range => [ [ 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0 ] ],
      generic => 2,
      expect  => '0.0.0.0-255.255.255.255',
    },
  );
  for my $case ( @case ) {
    my $got
     = Net::CIDR::Set::IPv4->decode(
      ( map { pack 'C*', @$_ } @{ $case->{range} } ),
      $case->{generic} );
    is $got, $case->{expect}, "$got";
  }
}

{
  is Net::CIDR::Set::_conjunction( or => 1, 2, 3 ), '1, 2 or 3',
   '_conjunction';
  is Net::CIDR::Set::_and( 1, 2, 3 ), '1, 2 and 3', '_and';
}

# vim:ts=2:sw=2:et:ft=perl