add validation tests

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2019-06-05 20:52:37 +02:00
parent 4917b9c057
commit 0afbe3d13b
3 changed files with 157 additions and 62 deletions

View File

@@ -8,6 +8,7 @@ use Froxlor\Validate\Validate;
* @covers \Froxlor\Validate\Validate
* @covers \Froxlor\UI\Response
* @covers \Froxlor\FroxlorLogger
* @covers \Froxlor\Idna\IdnaWrapper
*/
class ValidateTest extends TestCase
{
@@ -97,4 +98,102 @@ class ValidateTest extends TestCase
$this->expectExceptionCode(400);
Validate::validate_ip2("127.0.0.2", false, 'invalidip', true, false, false, true);
}
public function testValidateUrl()
{
$result = Validate::validateUrl("https://froxlor.org/");
$this->assertTrue($result);
$result = Validate::validateUrl("http://forum.froxlor.org/");
$this->assertTrue($result);
$result = Validate::validateUrl("https://api.froxlor.org/doc/0.10.0/index.php");
$this->assertTrue($result);
$result = Validate::validateUrl("#froxlor");
$this->assertFalse($result);
$result = Validate::validateUrl("https://82.149.225.211/");
$this->assertTrue($result);
$result = Validate::validateUrl("https://82.149.225.300");
$this->assertFalse($result);
$result = Validate::validateUrl("82.149.225.211:443");
$this->assertTrue($result);
}
public function testValidateDomain()
{
$result = Validate::validateDomain('froxlor.org');
$this->assertEquals('froxlor.org', $result);
$result = Validate::validateDomain('_dmarc.froxlor.org');
$this->assertFalse($result);
$result = Validate::validateDomain('_dmarc.froxlor.org', true);
$this->assertEquals('_dmarc.froxlor.org', $result);
$result = Validate::validateDomain('test._dmarc.froxlor.org', true);
$this->assertEquals('test._dmarc.froxlor.org', $result);
$result = Validate::validateDomain('0815');
$this->assertFalse($result);
$result = Validate::validateDomain('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz');
$this->assertFalse($result);
}
public function testValidateHostname()
{
$result = Validate::validateLocalHostname('localhost');
$this->assertEquals('localhost', $result);
$result = Validate::validateLocalHostname('froxlor-srv02');
$this->assertEquals('froxlor-srv02', $result);
$result = Validate::validateLocalHostname('froxlor_org');
$this->assertFalse($result);
$result = Validate::validateLocalHostname('froxlor.org');
$this->assertFalse($result);
$result = Validate::validateLocalHostname('a--------------------------------------------------------------');
$this->assertEquals('a--------------------------------------------------------------', $result);
$result = Validate::validateLocalHostname('-hostname');
$this->assertFalse($result);
$result = Validate::validateLocalHostname('a-----------------------------------------------------------------');
$this->assertFalse($result);
}
public function testValidateEmail()
{
$result = Validate::validateEmail('team@froxlor.org');
$this->assertEquals('team@froxlor.org', $result);
$result = Validate::validateEmail('team.froxlor.org');
$this->assertFalse($result);
}
public function testValidateUsername()
{
$result = Validate::validateUsername('web123sql2');
$this->assertTrue($result);
$mysql_max = \Froxlor\Database\Database::getSqlUsernameLength() - strlen(\Froxlor\Settings::Get('customer.mysqlprefix'));
$result = Validate::validateUsername('web123sql2', true, $mysql_max);
$this->assertTrue($result);
// too long
$result = Validate::validateUsername('myperfectsuperduperwebuser123sql2', true, $mysql_max);
$this->assertFalse($result);
// not unix-conform
$result = Validate::validateUsername('web123-sql2', true, $mysql_max);
$this->assertFalse($result);
// non-unix-conform
$result = Validate::validateUsername('web123-sql2', false, $mysql_max);
$this->assertTrue($result);
$result = Validate::validateUsername('web123--sql2', false, $mysql_max);
$this->assertFalse($result);
$result = Validate::validateUsername('-web123sql2', false, $mysql_max);
$this->assertFalse($result);
$result = Validate::validateUsername('web123sql2-', false, $mysql_max);
$this->assertFalse($result);
}
public function testValidateSqlInterval()
{
$result = Validate::validateSqlInterval('60 HOUR');
$this->assertTrue($result);
$result = Validate::validateSqlInterval('2 MONTH');
$this->assertTrue($result);
$result = Validate::validateSqlInterval();
$this->assertFalse($result);
$result = Validate::validateSqlInterval('2 QUARTER');
$this->assertFalse($result);
$result = Validate::validateSqlInterval('1DAY');
$this->assertFalse($result);
}
}