Merge pull request #2410 from jrfnl/feature/tests-reorganize-20

Tests: move host validation tests to own file
This commit is contained in:
Marcus Bointon 2021-07-06 20:16:47 +02:00 committed by GitHub
commit 336b6976b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 35 deletions

View File

@ -4014,7 +4014,7 @@ class PHPMailer
empty($host)
|| !is_string($host)
|| strlen($host) > 256
|| !preg_match('/^([a-zA-Z\d.-]*|\[[a-fA-F\d:]+])$/', $host)
|| !preg_match('/^([a-zA-Z\d.-]*|\[[a-fA-F\d:]+\])$/', $host)
) {
return false;
}

View File

@ -0,0 +1,108 @@
<?php
/**
* PHPMailer - PHP email transport unit tests.
* PHP version 5.5.
*
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
* @author Andy Prevost
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\PHPMailer\PHPMailer;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Test host validation functionality.
*
* @covers \PHPMailer\PHPMailer\PHPMailer::isValidHost
*/
final class IsValidHostTest extends TestCase
{
/**
* Test host validation when a valid host is passed.
*
* @dataProvider dataValidHost
*
* @param string $input Input text string.
*/
public function testValidHost($input)
{
self::assertTrue(PHPMailer::isValidHost($input), 'Good hostname denied');
}
/**
* Data provider.
*
* @return array
*/
public function dataValidHost()
{
return [
'localhost' => ['localhost'],
'Domain alias' => ['a'],
'Domain: lowercase' => ['example.com'],
'Domain: uppercase' => ['EXAMPLE.COM'],
'Domain: mixed case' => ['Example.Com'],
'Domain: with dash' => ['my-example.com'],
'Domain: with subdomain' => ['smtp.gmail.com'],
'IPv4 address: 127.0.0.1' => ['127.0.0.1'],
'IPv4 address: external' => ['27.145.58.181'],
'Long hex address' => [trim(str_repeat('a0123456789.', 21), '.')],
'Bracketed IPv6: localhost' => ['[::1]'],
'Bracketed IPv6' => ['[0:1234:dc0:41:216:3eff:fe67:3e01]'],
'Bracketed IPv6 uppercase' => ['[0:1234:DC0:41:216:3EFF:FE67:3E01]'],
];
}
/**
* Test host validation when an invalid host is passed.
*
* @dataProvider dataInvalidHost
*
* @param string $input Input text string.
*/
public function testInvalidHost($input)
{
self::assertFalse(PHPMailer::isValidHost($input), 'Bad hostname accepted');
}
/**
* Data provider.
*
* @return array
*/
public function dataInvalidHost()
{
return [
'Invalid type: null' => [null],
'Invalid type: int' => [123],
'Invalid type: float' => [1.5],
'Invalid type: object' => [new \stdClass()],
'Invalid type: array' => [[]],
'Invalid input: empty string' => [''],
'Invalid input: domain - empty subdomain' => ['.example.com'],
'Invalid input: domain - with underscore' => ['my_example.com'],
'Invalid input: IPv4, no dots' => ['127001'],
'Invalid input: IPv4, too few groups' => ['168.0.001'],
'Invalid input: IPv4, too many groups' => ['22.55.14.73.9'],
'Invalid input: IPv4 address outside range' => ['999.0.0.0'],
'Invalid input: hex address, wrong size' => [trim(str_repeat('a0123456789.', 22), '.')],
'Invalid input: bracketed, not IPv6 (num)' => ['[1234]'],
'Invalid input: bracketed, not IPv6 (hex)' => ['[abCDef]'],
'Invalid input: bracketed, triple :' => ['[1234:::1]'],
'Invalid input: IPv6 empty brackets' => ['[]'],
'Invalid input: IPv6 without brackets' => ['0:1234:dc0:41:216:3eff:fe67:3e01'],
'Invalid input: IPv6 too few groups' => ['[0:1234:dc0:41]'],
'Invalid input: IPv6 too many groups' => ['[012q:1234:dc0:41:216:3eff:fe67:3e01:6fa5]'],
'Invalid input: IPv6 non-hex char' => ['[012q:1234:dc0:41:216:3eff:fe67:3e01]'],
'Invalid input: IPv6 disallowed chars' => ['[0:12_4:dc$:41:216:3e+f:fe67:3e01]'],
'Invalid input: IPv6 double bracketed' => ['[[::1]]'],
];
}
}

View File

@ -1358,40 +1358,6 @@ EOT;
self::assertFalse($smtp->mail("somewhere\nbad"), 'Bad SMTP command containing breaks accepted');
}
public function testHostValidation()
{
$good = [
'localhost',
'example.com',
'smtp.gmail.com',
'127.0.0.1',
trim(str_repeat('a0123456789.', 21), '.'),
'[::1]',
'[0:1234:dc0:41:216:3eff:fe67:3e01]',
];
$bad = [
null,
123,
1.5,
new \stdClass(),
[],
'',
'999.0.0.0',
'[1234]',
'[1234:::1]',
trim(str_repeat('a0123456789.', 22), '.'),
'0:1234:dc0:41:216:3eff:fe67:3e01',
'[012q:1234:dc0:41:216:3eff:fe67:3e01]',
'[[::1]]',
];
foreach ($good as $h) {
self::assertTrue(PHPMailer::isValidHost($h), 'Good hostname denied: ' . $h);
}
foreach ($bad as $h) {
self::assertFalse(PHPMailer::isValidHost($h), 'Bad hostname accepted: ' . var_export($h, true));
}
}
/**
* Tests the Custom header getter.
*/