From e6482b1731d35753608e6bb2c8f9241272d671d3 Mon Sep 17 00:00:00 2001 From: Marcel Naeve Date: Sat, 14 May 2016 15:51:11 +0200 Subject: [PATCH] added some phpunit tests and fixed address/domain detection in email validation. --- .idea/php.xml | 2 +- Validate.php | 9 ++-- ValidateTest.php | 135 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 ValidateTest.php diff --git a/.idea/php.xml b/.idea/php.xml index 6563aa8..1a63a62 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/Validate.php b/Validate.php index 9ed5a4b..ccae15e 100644 --- a/Validate.php +++ b/Validate.php @@ -19,16 +19,17 @@ class Validate $results = true; $email = trim($email); // sanitize before validation $split = explode('@', $email); + $address = $split[count($split)-1]; // Whitelisted? if(isset($lists['whitelist'])) { - if(in_array($email, $lists['whitelist']) || in_array($split[1], $lists['whitelist'])) + if(in_array($email, $lists['whitelist']) || in_array($address, $lists['whitelist'])) return true; } // Blacklisted? if(isset($lists['blacklist'])) { - if(in_array($email, $lists['blacklist']) || in_array($split[1], $lists['blacklist'])) + if(in_array($email, $lists['blacklist']) || in_array($address, $lists['blacklist'])) return false; } @@ -43,8 +44,8 @@ class Validate } // check for dns MX entry - if($results && $level >= 2) { - $results = checkdnsrr($split[1], "MX"); + if($results && $level >= 2 && !Validate::ip($address)) { + $results = checkdnsrr($address, "MX"); } return $results; diff --git a/ValidateTest.php b/ValidateTest.php new file mode 100644 index 0000000..d4b42b7 --- /dev/null +++ b/ValidateTest.php @@ -0,0 +1,135 @@ + + * Date: 14.05.2016 + * Time: 11:25 + */ +class ValidateTest extends PHPUnit_Framework_TestCase +{ + /* + * Validate::email(); Tests + */ + public function testEmail_myEmail_level1() + { + $result = Validate::email("confu5ed@serious-pro.de"); + $this->assertTrue($result); + } + public function testEmail_myEmail_level2() + { + $result = Validate::email("confu5ed@serious-pro.de", 2); + $this->assertTrue($result); + } + public function testEmail_myEmail_level1_whitelisted() + { + $result = Validate::email("confu5ed@serious-pro.de", 1,["whitelist"=>["confu5ed@serious-pro.de"]]); + $this->assertTrue($result); + } + public function testEmail_myEmail_level1_not_in_whitelist() + { + $result = Validate::email("confu5ed@serious-pro.de", 1,["whitelist"=>["confu5ed@serious-pro.eu"]]); + $this->assertTrue($result); + } + public function testEmail_myEmail_level1_blacklisted() + { + $result = Validate::email("confu5ed@serious-pro.de",1,["blacklist"=>["confu5ed@serious-pro.de"]]); + $this->assertFalse($result); + } + public function testEmail_myEmail_level1_not_in_blacklist() + { + $result = Validate::email("confu5ed@serious-pro.de", 1,["blacklist"=>["confu5ed@serious-pro.eu"]]); + $this->assertTrue($result); + } + public function testEmail_myEmail_level2_whitelisted() + { + $result = Validate::email("confu5ed@serious-pro.de", 2,["whitelist"=>["confu5ed@serious-pro.de"]]); + $this->assertTrue($result); + } + public function testEmail_myEmail_level2_not_in_whitelist() + { + $result = Validate::email("confu5ed@serious-pro.de", 2,["whitelist"=>["confu5ed@serious-pro.eu"]]); + $this->assertTrue($result); + } + public function testEmail_myEmail_level2_blacklisted() + { + $result = Validate::email("confu5ed@serious-pro.de", 2,["blacklist"=>["confu5ed@serious-pro.de"]]); + $this->assertFalse($result); + } + public function testEmail_myEmail_level2_not_in_blacklist() + { + $result = Validate::email("confu5ed@serious-pro.de", 2,["blacklist"=>["confu5ed@serious-pro.eu"]]); + $this->assertTrue($result); + } + + public function testEmail_random1_level1() + { + $result = Validate::email("tempus.lorem.fringilla@vitae.co.uk"); + $this->assertTrue($result); + } + public function testEmail_random2_level1() + { + $result = Validate::email("Etiam.ligula@et.com"); + $this->assertTrue($result); + } + public function testEmail_random3_level1() + { + $result = Validate::email("orci.tincidunt.adipiscing@famesacturpis.edu"); + $this->assertTrue($result); + } + public function testEmail_random4_level1() + { + $result = Validate::email("_______@example.com"); + $this->assertTrue($result); + } + public function testEmail_random5_level1() + { + $result = Validate::email("nonummy.Fusce.fermentum@ut.org"); + $this->assertTrue($result); + } + public function testEmail_random6_level1() + { + $result = Validate::email('"email"@example.com'); + $this->assertTrue($result); + } + public function testEmail_random7_level1() + { + $result = Validate::email('much."more\ unusual"@example.com'); + $this->assertTrue($result); + } + public function testEmail_random8_level1() + { + $result = Validate::email('very.unusual."@".unusual.com@example.com'); + $this->assertTrue($result); + } + public function testEmail_random9_level1() + { + $result = Validate::email('very."(),:;<>[]".VERY."very@\\"very".unusual@strange.example.com'); + $this->assertTrue($result); + } + public function testEmail_random10_level2() + { + $result = Validate::email('very."(),:;<>[]".VERY."very@\\"very".unusual@serious-pro.de', 2); + $this->assertTrue($result); + } + + public function testEmail_invalid1_level1() + { + $result = Validate::email("me@me@serious-pro.de"); + $this->assertFalse($result); + } + public function testEmail_invalid2_level1() + { + $result = Validate::email("me me@serious-pro.de"); + $this->assertFalse($result); + } + public function testEmail_invalid3_level1() + { + $result = Validate::email("me@serious pro.de"); + $this->assertFalse($result); + } + + + +}