From a65ce5d3b07deb578cc4c4aba5695bcea8c07a87 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Mon, 27 Aug 2012 12:23:30 +0200 Subject: Rename ip_check to check_ip, add IP checking tests --- program/include/rcube_utils.php | 4 ++-- tests/Framework/Utils.php | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/program/include/rcube_utils.php b/program/include/rcube_utils.php index defb2aed1..aa748dc7f 100644 --- a/program/include/rcube_utils.php +++ b/program/include/rcube_utils.php @@ -94,7 +94,7 @@ class rcube_utils // Validate domain part if (preg_match('/^\[((IPv6:[0-9a-f:.]+)|([0-9.]+))\]$/i', $domain_part, $matches)) { - return self::ip_check(preg_replace('/^IPv6:/i', '', $matches[1])); // valid IPv4 or IPv6 address + return self::check_ip(preg_replace('/^IPv6:/i', '', $matches[1])); // valid IPv4 or IPv6 address } else { // If not an IP address @@ -154,7 +154,7 @@ class rcube_utils * * @return bool True if the address is valid */ - public static function ip_check($ip) + public static function check_ip($ip) { // IPv6, but there's no build-in IPv6 support if (strpos($ip, ':') !== false && !defined('AF_INET6')) { diff --git a/tests/Framework/Utils.php b/tests/Framework/Utils.php index b6cc5d577..503b69a4a 100644 --- a/tests/Framework/Utils.php +++ b/tests/Framework/Utils.php @@ -82,6 +82,53 @@ class Framework_Utils extends PHPUnit_Framework_TestCase $this->assertFalse(rcube_utils::check_email($email, false), $title); } + /** + * Valid IP addresses for test_valid_ip() + */ + function data_valid_ip() + { + return array( + array('0.0.0.0'), + array('123.123.123.123'), + array('::'), + array('::1'), + array('::1.2.3.4'), + array('2001:2d12:c4fe:5afe::1'), + ); + } + + /** + * Valid IP addresses for test_invalid_ip() + */ + function data_invalid_ip() + { + return array( + array(''), + array(0), + array('123.123.123.1234'), + array('1.1.1.1.1'), + array('::1.2.3.260'), + array('::1.0'), + array('2001::c4fe:5afe::1'), + ); + } + + /** + * @dataProvider data_valid_ip + */ + function test_valid_ip($ip) + { + $this->assertTrue(rcube_utils::check_ip($ip)); + } + + /** + * @dataProvider data_invalid_ip + */ + function test_invalid_ip($ip) + { + $this->assertFalse(rcube_utils::check_ip($ip)); + } + /** * rcube_utils::mod_css_styles() */ -- cgit v1.2.3