From 84f021965b3f0aceec816ff2c842beca4d55326f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lesimple?= Date: Mon, 13 Nov 2023 11:03:21 +0000 Subject: [PATCH] enh: 35% faster is_valid_ip() when fast=1 --- lib/perl/OVH/Bastion.pm | 51 +-- tests/unit/run.pl | 857 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 874 insertions(+), 34 deletions(-) diff --git a/lib/perl/OVH/Bastion.pm b/lib/perl/OVH/Bastion.pm index f5b12fc83..4d9a61e11 100644 --- a/lib/perl/OVH/Bastion.pm +++ b/lib/perl/OVH/Bastion.pm @@ -644,45 +644,28 @@ sub is_valid_ip { my $allowPrefixes = $params{'allowPrefixes'}; # if not, a /24 or /32 notation is rejected my $fast = $params{'fast'}; # fast mode: avoid instantiating Net::IP... except if ipv6 - if ($fast and $ip !~ m{:}) { - # fast asked and it's not an IPv6, regex ftw - ## no critic (ProhibitUnusedCapture) - if ( - $ip =~ m{^(? - (?[0-9]{1,3}) - \. - (?[0-9]{1,3}) - \. - (?[0-9]{1,3}) - \. - (?[0-9]{1,3}) - ) - ( - (?/) - (?\d+) - )? - $}x - ) - { - if (defined $+{'prefix'} and not $allowPrefixes) { + if ($fast and index($ip, ':') == -1) { + # We're being asked to be fast, and it's not an IPv6, just use a regex + # and don't instanciate a Net::IP. Also don't use named captures, as they're slower + if ($ip =~ m{^(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}))(/([0-9]{1,2}))?$}) { + if (defined $7 and not $allowPrefixes) { return R('KO_INVALID_IP', msg => "Invalid IP address ($ip), as prefixes are not allowed"); } - foreach my $key (qw{ x1 x2 x3 x4 }) { - return R('KO_INVALID_IP', msg => "Invalid IP address ($ip)") - if (not defined $+{$key} or $+{$key} > 255); - } - if (defined $+{'prefix'} and $+{'prefix'} > 32) { - return R('KO_INVALID_IP', msg => "Invalid IP address ($ip)"); - } - if (defined $+{'slash'} and not defined $+{'prefix'}) { - # got a / in $ip but it's not followed by \d+ + if ($2 > 255 || $3 > 255 || $4 > 255 || $5 > 255) { return R('KO_INVALID_IP', msg => "Invalid IP address ($ip)"); } - - if (defined $+{'prefix'} && $+{'prefix'} != 32) { - return R('OK', value => {ip => $ip, prefix => $+{'prefix'}}); + if (defined $7) { + if ($7 > 32) { + return R('KO_INVALID_IP', msg => "Invalid IP address ($ip)"); + } + # valid prefix? + if ((2**24 * $2 + 2**16 * $3 + 2**8 * $4 + $5) & (2**(32 - $7) - 1) != 0) { + return R('KO_INVALID_IP', msg => "Invalid prefix ($7) for this IP address ($ip)"); + } + return R('OK', value => {ip => $ip, prefix => $7}); } - return R('OK', value => {ip => $+{'shortip'}}); + # int() to remove useless zeroes such as 01.002.003.04 => 1.2.3.4 + return R('OK', value => {ip => int($2) . '.' . int($3) . '.' . int($4) . '.' . int($5)}); } return R('KO_INVALID_IP', msg => "Invalid IP address ($ip)"); } diff --git a/tests/unit/run.pl b/tests/unit/run.pl index 54b847381..b5b92820d 100755 --- a/tests/unit/run.pl +++ b/tests/unit/run.pl @@ -420,4 +420,861 @@ "build_re_from_wildcards() 2" ); +# is_valid_ip + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/32", "version" => "4", "prefixlen" => "32", "ip" => "1.2.0.0", "type" => "single",} + ), + "is_valid_ip(1.2.0.0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/16", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/16,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/24", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/24,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/32", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/32", "version" => "4", "prefixlen" => "32", "ip" => "1.2.0.0", "type" => "single",} + ), + "is_valid_ip(1.2.0.0/32,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/33", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/33,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "255.255.255.255/32", + "version" => "4", + "prefixlen" => "32", + "ip" => "255.255.255.255", + "type" => "single", + } + ), + "is_valid_ip(255.255.255.255,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/16", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/16,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/24", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/24,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/32", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "255.255.255.255/32", + "version" => "4", + "prefixlen" => "32", + "ip" => "255.255.255.255", + "type" => "single", + } + ), + "is_valid_ip(255.255.255.255/32,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/33", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/33,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/16", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/16,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/24", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/24,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/32", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/32,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/33", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/33,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "4.5.6.0/32", "version" => "4", "prefixlen" => "32", "ip" => "4.5.6.0", "type" => "single",} + ), + "is_valid_ip(4.5.6.0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/16", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/16,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/24", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/24,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/32", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "4.5.6.0/32", "version" => "4", "prefixlen" => "32", "ip" => "4.5.6.0", "type" => "single",} + ), + "is_valid_ip(4.5.6.0/32,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/33", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/33,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0", allowPrefixes => 0, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "0000:0000:0000:0000:0000:feef:0000:0000/128", + "version" => "6", + "prefixlen" => "128", + "ip" => "0000:0000:0000:0000:0000:feef:0000:0000", + "type" => "single", + } + ), + "is_valid_ip(::feef:0:0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/0", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/0,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/16", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/16,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/24", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/24,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/32", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/32,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/33", allowPrefixes => 0, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/33,fast=0,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/32", "version" => "4", "prefixlen" => "32", "ip" => "1.2.0.0", "type" => "single",} + ), + "is_valid_ip(1.2.0.0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/16", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/16", "version" => "4", "prefixlen" => "16", "ip" => "1.2.0.0/16", "type" => "prefix",} + ), + "is_valid_ip(1.2.0.0/16,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/24", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/24", "version" => "4", "prefixlen" => "24", "ip" => "1.2.0.0/24", "type" => "prefix",} + ), + "is_valid_ip(1.2.0.0/24,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/32", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "1.2.0.0/32", "version" => "4", "prefixlen" => "32", "ip" => "1.2.0.0", "type" => "single",} + ), + "is_valid_ip(1.2.0.0/32,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/33", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/33,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "255.255.255.255/32", + "version" => "4", + "prefixlen" => "32", + "ip" => "255.255.255.255", + "type" => "single", + } + ), + "is_valid_ip(255.255.255.255,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/16", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/16,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/24", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/24,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/32", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "255.255.255.255/32", + "version" => "4", + "prefixlen" => "32", + "ip" => "255.255.255.255", + "type" => "single", + } + ), + "is_valid_ip(255.255.255.255/32,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/33", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/33,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/16", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/16,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/24", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/24,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/32", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/32,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/33", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/33,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "4.5.6.0/32", "version" => "4", "prefixlen" => "32", "ip" => "4.5.6.0", "type" => "single",} + ), + "is_valid_ip(4.5.6.0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/16", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/16,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/24", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "4.5.6.0/24", "version" => "4", "prefixlen" => "24", "ip" => "4.5.6.0/24", "type" => "prefix",} + ), + "is_valid_ip(4.5.6.0/24,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/32", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => + {"prefix" => "4.5.6.0/32", "version" => "4", "prefixlen" => "32", "ip" => "4.5.6.0", "type" => "single",} + ), + "is_valid_ip(4.5.6.0/32,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/33", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/33,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0", allowPrefixes => 1, fast => 0), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "0000:0000:0000:0000:0000:feef:0000:0000/128", + "version" => "6", + "prefixlen" => "128", + "ip" => "0000:0000:0000:0000:0000:feef:0000:0000", + "type" => "single", + } + ), + "is_valid_ip(::feef:0:0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/0", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/0,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/16", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/16,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/24", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/24,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/32", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/32,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/33", allowPrefixes => 1, fast => 0), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/33,fast=0,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0", allowPrefixes => 0, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "1.2.0.0",}), + "is_valid_ip(1.2.0.0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/16", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/16,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/24", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/24,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/32", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/32,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/33", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/33,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255", allowPrefixes => 0, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "255.255.255.255",}), + "is_valid_ip(255.255.255.255,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/16", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/16,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/24", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/24,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/32", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/32,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/33", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/33,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/16", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/16,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/24", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/24,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/32", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/32,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/33", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/33,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0", allowPrefixes => 0, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "4.5.6.0",}), + "is_valid_ip(4.5.6.0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/16", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/16,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/24", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/24,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/32", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/32,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/33", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/33,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0", allowPrefixes => 0, fast => 1), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "0000:0000:0000:0000:0000:feef:0000:0000/128", + "version" => "6", + "prefixlen" => "128", + "ip" => "0000:0000:0000:0000:0000:feef:0000:0000", + "type" => "single", + } + ), + "is_valid_ip(::feef:0:0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/0", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/0,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/16", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/16,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/24", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/24,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/32", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/32,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/33", allowPrefixes => 0, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/33,fast=1,pfx=0)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "1.2.0.0",}), + "is_valid_ip(1.2.0.0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/0", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "0", "ip" => "1.2.0.0/0",}), + "is_valid_ip(1.2.0.0/0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/16", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "16", "ip" => "1.2.0.0/16",}), + "is_valid_ip(1.2.0.0/16,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/24", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "24", "ip" => "1.2.0.0/24",}), + "is_valid_ip(1.2.0.0/24,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/32", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "32", "ip" => "1.2.0.0/32",}), + "is_valid_ip(1.2.0.0/32,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "1.2.0.0/33", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(1.2.0.0/33,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "255.255.255.255",}), + "is_valid_ip(255.255.255.255,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/0", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/16", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/16,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/24", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/24,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/32", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "32", "ip" => "255.255.255.255/32",}), + "is_valid_ip(255.255.255.255/32,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "255.255.255.255/33", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(255.255.255.255/33,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/0", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/16", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/16,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/24", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/24,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/32", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/32,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "256.0.0.0/33", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(256.0.0.0/33,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"ip" => "4.5.6.0",}), + "is_valid_ip(4.5.6.0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/0", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "0", "ip" => "4.5.6.0/0",}), + "is_valid_ip(4.5.6.0/0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/16", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "16", "ip" => "4.5.6.0/16",}), + "is_valid_ip(4.5.6.0/16,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/24", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "24", "ip" => "4.5.6.0/24",}), + "is_valid_ip(4.5.6.0/24,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/32", allowPrefixes => 1, fast => 1), + R("OK", msg => ignore(), value => {"prefix" => "32", "ip" => "4.5.6.0/32",}), + "is_valid_ip(4.5.6.0/32,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "4.5.6.0/33", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(4.5.6.0/33,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0", allowPrefixes => 1, fast => 1), + R( + "OK", + msg => ignore(), + value => { + "prefix" => "0000:0000:0000:0000:0000:feef:0000:0000/128", + "version" => "6", + "prefixlen" => "128", + "ip" => "0000:0000:0000:0000:0000:feef:0000:0000", + "type" => "single", + } + ), + "is_valid_ip(::feef:0:0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/0", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/0,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/16", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/16,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/24", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/24,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/32", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/32,fast=1,pfx=1)" +); + +cmp_deeply( + OVH::Bastion::is_valid_ip(ip => "::feef:0:0/33", allowPrefixes => 1, fast => 1), + R("KO_INVALID_IP", msg => ignore(), value => undef), + "is_valid_ip(::feef:0:0/33,fast=1,pfx=1)" +); + done_testing();