From 222173adae59af660c18ce89c606b3ee212d9a4a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 9 Apr 2010 00:39:12 +0200 Subject: [PATCH 1/6] [ticket/9626] Adding tests for regular expressions. Adding tests for the IPv6 regular expression returned by get_preg_expression() PHPBB3-9626 --- tests/all_tests.php | 2 + tests/regex/all_tests.php | 40 ++++++++++++++++++ tests/regex/ipv6.php | 85 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 tests/regex/all_tests.php create mode 100644 tests/regex/ipv6.php diff --git a/tests/all_tests.php b/tests/all_tests.php index e693427809..7894d688ee 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -23,6 +23,7 @@ require_once 'security/all_tests.php'; require_once 'template/all_tests.php'; require_once 'text_processing/all_tests.php'; require_once 'dbal/all_tests.php'; +require_once 'regex/all_tests.php'; // exclude the test directory from code coverage reports PHPUnit_Util_Filter::addDirectoryToFilter('./'); @@ -44,6 +45,7 @@ class phpbb_all_tests $suite->addTest(phpbb_template_all_tests::suite()); $suite->addTest(phpbb_text_processing_all_tests::suite()); $suite->addTest(phpbb_dbal_all_tests::suite()); + $suite->addTest(phpbb_regex_all_tests::suite()); return $suite; } diff --git a/tests/regex/all_tests.php b/tests/regex/all_tests.php new file mode 100644 index 0000000000..aeb242b78b --- /dev/null +++ b/tests/regex/all_tests.php @@ -0,0 +1,40 @@ +addTestSuite('phpbb_ipv6_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_regex_all_tests::main') +{ + phpbb_regex_all_tests::main(); +} diff --git a/tests/regex/ipv6.php b/tests/regex/ipv6.php new file mode 100644 index 0000000000..7b0a48833f --- /dev/null +++ b/tests/regex/ipv6.php @@ -0,0 +1,85 @@ +regex = get_preg_expression('ipv6'); + } + + public function positive_match_data() + { + return array( + // Full length IPv6 address + array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), + + // No leading zeroes in the group + array('2001:db8:85a3:0:0:8a2e:370:1337'), + array('2001:db8:85a3:c:d:8a2e:370:1337'), + + // Consecutive all-zero groups + array('2001:db8:85a3::8a2e:370:1337'), + + // Last 32bit in dotted quad notation + array('2001:db8:0:1::192.168.0.2'), + + // Mapped IPv4 + array('::ffff:c000:280'), + array('::ffff:192.0.2.128'), + + // More tests + array('::'), + array('0:0:0:0:0:0:0:0'), + + array('::1'), + array('0:0::0:0:1'), + array('0:0:0:0:0:0:0:1'), + ); + } + + public function negative_match_data() + { + return array( + // IPv4 address + array('192.168.0.2'), + + // Out of hex scope + array('abcd:efgh:0000::0'), + + // Double :: + array('2001::23de::2002'), + + // Too many blocks + array('2001:0db8:85a3:08d3:1319:8a2e:0370:1337:4430'), + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($address) + { + $this->assertEquals(1, preg_match($this->regex, $address)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($address) + { + $this->assertEquals(0, preg_match($this->regex, $address)); + } +} + From 1c59f3dbd0e27be2057dc32344421e82122276ca Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 9 Apr 2010 00:54:19 +0200 Subject: [PATCH 2/6] [ticket/9626] Adding tests for the IPv4 regular expression. PHPBB3-9626 --- tests/regex/all_tests.php | 2 ++ tests/regex/ipv4.php | 72 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/regex/ipv4.php diff --git a/tests/regex/all_tests.php b/tests/regex/all_tests.php index aeb242b78b..32cd9a3a4e 100644 --- a/tests/regex/all_tests.php +++ b/tests/regex/all_tests.php @@ -15,6 +15,7 @@ if (!defined('PHPUnit_MAIN_METHOD')) require_once 'test_framework/framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; +require_once 'regex/ipv4.php'; require_once 'regex/ipv6.php'; class phpbb_regex_all_tests @@ -28,6 +29,7 @@ class phpbb_regex_all_tests { $suite = new PHPUnit_Framework_TestSuite('phpBB Regular Expressions'); + $suite->addTestSuite('phpbb_ipv4_test'); $suite->addTestSuite('phpbb_ipv6_test'); return $suite; diff --git a/tests/regex/ipv4.php b/tests/regex/ipv4.php new file mode 100644 index 0000000000..5ffc700831 --- /dev/null +++ b/tests/regex/ipv4.php @@ -0,0 +1,72 @@ +regex = get_preg_expression('ipv4'); + } + + public function positive_match_data() + { + return array( + array('0.0.0.0'), + array('127.0.0.1'), + array('192.168.0.1'), + array('255.255.255.255'), + ); + } + + public function negative_match_data() + { + return array( + // IPv6 addresses + array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), + array('2001:db8:85a3:c:d:8a2e:370:1337'), + array('2001:db8:85a3::8a2e:370:1337'), + array('2001:db8:0:1::192.168.0.2'), + array('0:0:0:0:0:0:0:1'), + array('0:0::0:0:1'), + array('::1'), + + // Out of scope + array('255.255.255.256'), + + // Other tests + array('a.b.c.d'), + array('11.22.33.'), + array('11.22.33'), + array('11.22'), + array('11'), + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($address) + { + $this->assertEquals(1, preg_match($this->regex, $address)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($address) + { + $this->assertEquals(0, preg_match($this->regex, $address)); + } +} + From 12e92fc45e7c023122d0c52d94fc49710ca6d4dd Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 9 Apr 2010 14:51:16 +0200 Subject: [PATCH 3/6] [ticket/9626] More IPv6 regex tests. PHPBB3-9626 --- tests/regex/ipv6.php | 70 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/tests/regex/ipv6.php b/tests/regex/ipv6.php index 7b0a48833f..3f62f65f49 100644 --- a/tests/regex/ipv6.php +++ b/tests/regex/ipv6.php @@ -24,6 +24,10 @@ class phpbb_ipv6_test extends phpbb_test_case return array( // Full length IPv6 address array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), + array('0000:0000:0000:0000:0000:0000:0000:0001'), + array('3FFE:0b00:0000:0000:0001:0000:0000:000a'), + array('3ffe:0b00:0000:0000:0001:0000:0000:000a'), + array('2002:0db8:0000:0000:0000:dead:1337:d00d'), // No leading zeroes in the group array('2001:db8:85a3:0:0:8a2e:370:1337'), @@ -31,38 +35,92 @@ class phpbb_ipv6_test extends phpbb_test_case // Consecutive all-zero groups array('2001:db8:85a3::8a2e:370:1337'), + array('1::2:3:4:5:6:7'), + array('1::2:3:4:5:6'), + array('1::2:3:4:5'), + array('1::2:3:4'), + array('1::2:3'), + array('1::2'), // Last 32bit in dotted quad notation array('2001:db8:0:1::192.168.0.2'), - // Mapped IPv4 - array('::ffff:c000:280'), - array('::ffff:192.0.2.128'), + // IPv4-compatible IPv6 address + array('::13.1.68.3'), + array('0:0:0:0:0:0:13.1.68.3'), - // More tests + // IPv4-mapped IPv6 address + array('::ffff:c000:280'), + array('::ffff:c000:0280'), + array('::ffff:192.0.2.128'), + array('0:0:0:0:0:ffff:c000:280'), + array('0:0:0:0:0:ffff:c000:0280'), + array('0:0:0:0:0:ffff:192.0.2.128'), + array('0000:0000:0000:0000:0000:ffff:c000:280'), + array('0000:0000:0000:0000:0000:ffff:c000:0280'), + array('0000:0000:0000:0000:0000:ffff:192.0.2.128'), + + // No trailing zeroes + array('fe80::'), + array('2002::'), + array('2001:db8::'), + array('2001:0db8:1234::'), + array('1:2:3:4:5:6::'), + array('1:2:3:4:5::'), + array('1:2:3:4::'), + array('1:2:3::'), + array('1:2::'), + + // No leading zeroes + array('::2:3:4:5:6:7:8'), + array('::2:3:4:5:6:7'), + array('::2:3:4:5:6'), + array('::2:3:4:5'), + array('::2:3:4'), + array('::2:3'), + array('::1'), + array('::8'), + array('::c'), + array('::abcd'), + + // All zeroes array('::'), array('0:0:0:0:0:0:0:0'), + array('0000:0000:0000:0000:0000:0000:0000:0000'), - array('::1'), + // More tests + array('2::10'), array('0:0::0:0:1'), array('0:0:0:0:0:0:0:1'), + array('::ffff:0:0'), ); } public function negative_match_data() { return array( + // Empty address + array(''), + // IPv4 address array('192.168.0.2'), - // Out of hex scope + // Out of scope array('abcd:efgh:0000::0'), + array('::ffff:192.168.255.256'), // Double :: array('2001::23de::2002'), + array('3ffe:b00::1::b'), + array('::1111:2222:3333:4444:5555:6666::'), // Too many blocks array('2001:0db8:85a3:08d3:1319:8a2e:0370:1337:4430'), + + // More tests + array('02001:0000:1234:0000:0000:C1C0:ABCD:9876'), + array('2001:0000:1234: 0000:0000:C1C0:ABCD:9876'), + array('::ffff:192x168.255.255'), ); } From 6a5ad05d8a15c0d4f6be2413190c5a2fdc270f5d Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 14 Apr 2010 02:27:07 +0200 Subject: [PATCH 4/6] [ticket/9626] A few tests for the email regular expression. PHPBB3-9626 --- tests/regex/all_tests.php | 2 + tests/regex/email.php | 78 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/regex/email.php diff --git a/tests/regex/all_tests.php b/tests/regex/all_tests.php index 32cd9a3a4e..0e193620e1 100644 --- a/tests/regex/all_tests.php +++ b/tests/regex/all_tests.php @@ -15,6 +15,7 @@ if (!defined('PHPUnit_MAIN_METHOD')) require_once 'test_framework/framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; +require_once 'regex/email.php'; require_once 'regex/ipv4.php'; require_once 'regex/ipv6.php'; @@ -29,6 +30,7 @@ class phpbb_regex_all_tests { $suite = new PHPUnit_Framework_TestSuite('phpBB Regular Expressions'); + $suite->addTestSuite('phpbb_email_test'); $suite->addTestSuite('phpbb_ipv4_test'); $suite->addTestSuite('phpbb_ipv6_test'); diff --git a/tests/regex/email.php b/tests/regex/email.php new file mode 100644 index 0000000000..37fb9d81d0 --- /dev/null +++ b/tests/regex/email.php @@ -0,0 +1,78 @@ +regex = '#^' . get_preg_expression('email') . '$#i'; + } + + public function positive_match_data() + { + return array( + array('nobody@phpbb.com'), + array('Nobody@sub.phpbb.com'), + array('alice.bob@foo.phpbb.com'), + array('alice-foo@bar.phpbb.com'), + array('alice_foo@bar.phpbb.com'), + array('alice+tag@foo.phpbb.com'), + array('alice&tag@foo.phpbb.com'), + + //array('"John Doe"@example.com'), + //array('Alice@[192.168.2.1]'), // IPv4 + //array('Bob@[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]'), // IPv6 + ); + } + + public function negative_match_data() + { + return array( + array('foo.example.com'), // @ is missing + array('.foo.example.com'), // . as first character + array('Foo.@example.com'), // . is last in local part + array('foo..123@example.com'), // . doubled + array('a@b@c@example.com'), // @ doubled + + array('()[]\;:,<>@example.com'), // invalid characters + array('abc(def@example.com'), // invalid character ( + array('abc)def@example.com'), // invalid character ) + array('abc[def@example.com'), // invalid character [ + array('abc]def@example.com'), // invalid character ] + array('abc\def@example.com'), // invalid character \ + array('abc;def@example.com'), // invalid character ; + array('abc:def@example.com'), // invalid character : + array('abc,def@example.com'), // invalid character , + array('abcdef@example.com'), // invalid character > + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($email) + { + $this->assertEquals(1, preg_match($this->regex, $email)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($address) + { + $this->assertEquals(0, preg_match($this->regex, $email)); + } +} + From 646b16f5db0ce68c69b520670b366dd61bfa03c1 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 17 May 2010 10:28:44 +0200 Subject: [PATCH 5/6] [ticket/9626] Add missing class prefix. PHPBB3-9626 --- tests/regex/all_tests.php | 6 +++--- tests/regex/email.php | 2 +- tests/regex/ipv4.php | 2 +- tests/regex/ipv6.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/regex/all_tests.php b/tests/regex/all_tests.php index 0e193620e1..f2a23a0f98 100644 --- a/tests/regex/all_tests.php +++ b/tests/regex/all_tests.php @@ -30,9 +30,9 @@ class phpbb_regex_all_tests { $suite = new PHPUnit_Framework_TestSuite('phpBB Regular Expressions'); - $suite->addTestSuite('phpbb_email_test'); - $suite->addTestSuite('phpbb_ipv4_test'); - $suite->addTestSuite('phpbb_ipv6_test'); + $suite->addTestSuite('phpbb_regex_email_test'); + $suite->addTestSuite('phpbb_regex_ipv4_test'); + $suite->addTestSuite('phpbb_regex_ipv6_test'); return $suite; } diff --git a/tests/regex/email.php b/tests/regex/email.php index 37fb9d81d0..b1519dfa5f 100644 --- a/tests/regex/email.php +++ b/tests/regex/email.php @@ -10,7 +10,7 @@ require_once 'test_framework/framework.php'; require_once '../phpBB/includes/functions.php'; -class phpbb_email_test extends phpbb_test_case +class phpbb_regex_email_test extends phpbb_test_case { protected $regex; diff --git a/tests/regex/ipv4.php b/tests/regex/ipv4.php index 5ffc700831..9d131ad0ca 100644 --- a/tests/regex/ipv4.php +++ b/tests/regex/ipv4.php @@ -10,7 +10,7 @@ require_once 'test_framework/framework.php'; require_once '../phpBB/includes/functions.php'; -class phpbb_ipv4_test extends phpbb_test_case +class phpbb_regex_ipv4_test extends phpbb_test_case { protected $regex; diff --git a/tests/regex/ipv6.php b/tests/regex/ipv6.php index 3f62f65f49..3d7a72e492 100644 --- a/tests/regex/ipv6.php +++ b/tests/regex/ipv6.php @@ -10,7 +10,7 @@ require_once 'test_framework/framework.php'; require_once '../phpBB/includes/functions.php'; -class phpbb_ipv6_test extends phpbb_test_case +class phpbb_regex_ipv6_test extends phpbb_test_case { protected $regex; From aaaa4f9abe8665946b79cc55bf3a4d7e0133b6c9 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 19 May 2010 16:56:45 +0200 Subject: [PATCH 6/6] [ticket/9626] Adding tests for the URL regular expression. PHPBB3-9626 --- tests/regex/all_tests.php | 2 ++ tests/regex/url.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/regex/url.php diff --git a/tests/regex/all_tests.php b/tests/regex/all_tests.php index f2a23a0f98..316a9d4a58 100644 --- a/tests/regex/all_tests.php +++ b/tests/regex/all_tests.php @@ -18,6 +18,7 @@ require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'regex/email.php'; require_once 'regex/ipv4.php'; require_once 'regex/ipv6.php'; +require_once 'regex/url.php'; class phpbb_regex_all_tests { @@ -33,6 +34,7 @@ class phpbb_regex_all_tests $suite->addTestSuite('phpbb_regex_email_test'); $suite->addTestSuite('phpbb_regex_ipv4_test'); $suite->addTestSuite('phpbb_regex_ipv6_test'); + $suite->addTestSuite('phpbb_regex_url_test'); return $suite; } diff --git a/tests/regex/url.php b/tests/regex/url.php new file mode 100644 index 0000000000..678b7d108f --- /dev/null +++ b/tests/regex/url.php @@ -0,0 +1,34 @@ +assertEquals($expected, preg_match('#^' . get_preg_expression('url') . '$#i', $url)); + } +}