1#!/usr/bin/perl -w
2
3use strict;
4
5use Test2::V0;
6
7use Net::Netmask;
8
9my $debug = 0;
10
11ok( Net::Netmask->debug($debug) == $debug, "unable to set debug" );
12
13# test a variety of ip's with bytes greater than 255.
14# all these tests should return undef
15
16my @tests = (
17    {
18        input => ['209.256.68.22:255.255.224.0'],
19        error => qr/^could not parse /,
20        type  => 'bad net byte',
21    },
22    {
23        input => ['209.180.68.22:256.255.224.0'],
24        error => qr/^illegal netmask: /,
25        type  => 'bad mask byte',
26    },
27    {
28        input => [ '209.157.300.22', '255.255.224.0' ],
29        error => qr/^could not parse /,
30        type  => 'bad net byte',
31    },
32    {
33        input => [ '300.157.70.33', '0xffffe000' ],
34        error => qr/^could not parse /,
35        type  => 'bad net byte',
36    },
37    {
38        input => ['209.500.70.33/19'],
39        error => qr/^could not parse /,
40        type  => 'bad net byte',
41    },
42    {
43        input => ['140.999.82'],
44        error => qr/^could not parse /,
45        type  => 'bad net byte',
46    },
47    {
48        input => ['899.174'],
49        error => qr/^could not parse /,
50        type  => 'bad net byte',
51    },
52    {
53        input => ['900'],
54        error => qr/^could not parse /,
55        type  => 'bad net byte',
56    },
57    {
58        input => ['209.157.300/19'],
59        error => qr/^could not parse /,
60        type  => 'bad net byte',
61    },
62    {
63        input => ['209.300.64.0-209.157.95.255'],
64        error => qr/^illegal dotted quad/,
65        type  => 'bad net byte',
66    },
67    # test ranges that are a power-of-two big, but are not legal blocks
68    {
69        input => ['218.0.0.0 - 211.255.255.255'],
70        error => qr/^could not find exact fit/,
71        type  => 'inexact fit',
72    },
73    # test some more bad nets/masks
74    {
75        input => ['218.0.0.4 - 218.0.0.11'],
76        error => qr/^could not find exact fit/,
77        type  => 'inexact fit',
78    },
79    {
80        input => ['10.10.10.10#256.0.0.0'],
81        error => qr/^illegal hostmask:/,
82        type  => 'bad mask byte',
83    },
84    {
85        input => [ '209.157.200.22', '256.255.224.0' ],
86        error => qr/^illegal netmask:/,
87        type  => 'bad mask byte',
88    },
89    {
90        input => [ '10.10.10.10', '0xF' ],
91        error => qr/^illegal netmask:/,
92        type  => 'bad mask',
93    },
94    {
95        input => ['209.200.70.33/33'],
96        error => qr/^illegal number of bits/,
97        type  => 'bad mask',
98    },
99    {
100        input => ['209.200.64.0-309.157.95.255'],
101        error => qr/^illegal dotted quad/,
102        type  => 'bad mask byte',
103    },
104    # completely invalid args
105    {
106        input => ['foo'],
107        error => qr/^could not parse /,
108        type  => 'bad net',
109    },
110    {
111        input => [ '10.10.10.10', 'foo' ],
112        error => qr/^could not parse /,
113        type  => 'bad mask',
114    },
115    {
116        input => [ '10.10.10', 'foo' ],
117        error => qr/^could not parse /,
118        type  => 'bad mask',
119    },
120    {
121        input => [ '10.10', 'foo' ],
122        error => qr/^could not parse /,
123        type  => 'bad mask',
124    },
125    {
126        input => [ '10', 'foo' ],
127        error => qr/^could not parse /,
128        type  => 'bad mask',
129    },
130    {
131        input => [ '10.10.10.10', '0xYYY' ],
132        error => qr/^could not parse /,
133        type  => 'bad mask',
134    },
135    # These do weird things that users almost certainly don't expect,
136    # creating a potential security issue.  I.E. all of the below IP
137    # addresses would be valid to inet_aton().
138    {
139        input => [ '1.131844', '32' ],
140        error => qr/^could not parse /,
141        type  => 'ambiguous',
142    },
143    {
144        input => [ '0192.0.1.2', '32' ],
145        error => qr/^could not parse /,
146        type  => 'ambiguous',
147    },
148    {
149        input => [ '192.00.1.2', '32' ],
150        error => qr/^could not parse /,
151        type  => 'ambiguous',
152    },
153    {
154        input => [ '192.0.01.2', '32' ],
155        error => qr/^could not parse /,
156        type  => 'ambiguous',
157    },
158    {
159        input => [ '192.0.1.02', '32' ],
160        error => qr/^could not parse /,
161        type  => 'ambiguous',
162    },
163    {
164        input => [ '0xC0.0x1.0x3.0x4', '32' ],
165        error => qr/^could not parse /,
166        type  => 'ambiguous',
167    },
168    {
169        input => [ '1.131844/32' ],
170        error => qr/^could not parse /,
171        type  => 'ambiguous',
172    },
173    {
174        input => [ '0192.0.1.2/32' ],
175        error => qr/^could not parse /,
176        type  => 'ambiguous',
177    },
178    {
179        input => [ '192.00.1.2/32' ],
180        error => qr/^could not parse /,
181        type  => 'ambiguous',
182    },
183    {
184        input => [ '192.0.01.2/32' ],
185        error => qr/^could not parse /,
186        type  => 'ambiguous',
187    },
188    {
189        input => [ '192.0.1.02/32' ],
190        error => qr/^could not parse /,
191        type  => 'ambiguous',
192    },
193    {
194        input => [ '0xC0.0x1.0x3.0x4/32' ],
195        error => qr/^could not parse /,
196        type  => 'ambiguous',
197    },
198    {
199        input => [ '1.131844' ],
200        error => qr/^could not parse /,
201        type  => 'ambiguous',
202    },
203    {
204        input => [ '0192.0.1.2' ],
205        error => qr/^could not parse /,
206        type  => 'ambiguous',
207    },
208    {
209        input => [ '192.00.1.2' ],
210        error => qr/^could not parse /,
211        type  => 'ambiguous',
212    },
213    {
214        input => [ '0192.0.01.2' ],
215        error => qr/^could not parse /,
216        type  => 'ambiguous',
217    },
218    {
219        input => [ '192.0.1.02' ],
220        error => qr/^could not parse /,
221        type  => 'ambiguous',
222    },
223    {
224        input => [ '0xC0.0x1.0x3.0x4' ],
225        error => qr/^could not parse /,
226        type  => 'ambiguous',
227    },
228    {
229        input => [ '10/8' ],
230        error => qr/^could not parse /,
231        type  => 'ambiguous',
232    },
233    {
234        input => [ '10.0/8' ],
235        error => qr/^could not parse /,
236        type  => 'ambiguous',
237    },
238    {
239        input => [ '10.0.0/8' ],
240        error => qr/^could not parse /,
241        type  => 'ambiguous',
242    },
243    {
244        input => [ '10', '8' ],
245        error => qr/^could not parse /,
246        type  => 'ambiguous',
247    },
248    {
249        input => [ '10.0', '8' ],
250        error => qr/^could not parse /,
251        type  => 'ambiguous',
252    },
253    {
254        input => [ '10.0.0', '8' ],
255        error => qr/^could not parse /,
256        type  => 'ambiguous',
257    },
258    {
259        input => [ '10', '8' ],
260        error => qr/^could not parse /,
261        type  => 'ambiguous',
262    },
263    {
264        input => [ '10.0' ],
265        error => qr/^could not parse /,
266        type  => 'ambiguous',
267    },
268    {
269        input => [ '10.0.0' ],
270        error => qr/^could not parse /,
271        type  => 'ambiguous',
272    },
273    {
274        input => [ '2001::/129' ],
275        error => qr/^illegal number of bits/,
276        type  => 'bad mask',
277    },
278);
279
280foreach my $test (@tests) {
281    my $input = $test->{input};
282    my $err   = $test->{error};
283    my $name  = ( join ', ', @{ $test->{input} } );
284    my $type  = $test->{type};
285
286    my $result = Net::Netmask->safe_new(@$input);
287    is( $result, undef, "$name $type" );
288    like( Net::Netmask->errstr, $err, "$name errstr mismatch" );
289
290    warns { $result = Net::Netmask->new(@$input) };
291    if ($result->{PROTOCOL} eq 'IPv4') {
292        is( "$result", "0.0.0.0/0", "result is 0.0.0.0/0" );
293    } else {
294        is( "$result", "::/0", "result is 0.0.0.0/0" );
295    }
296}
297
298# test whois numbers with space between dash (valid!)
299ok( Net::Netmask->safe_new('209.157.64.0 - 209.157.95.255'), "whois with single space around dash" );
300ok( Net::Netmask->safe_new('209.157.64.0   -   209.157.95.255'),
301    "whois with mulitple spaces around dash" );
302
303done_testing;
304
305