From 4917b9c057928716b534030547f85b64b051128a Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Wed, 5 Jun 2019 09:07:21 +0200 Subject: [PATCH] added first validation tests Signed-off-by: Michael Kaufmann --- lib/Froxlor/Validate/Validate.php | 7 +-- tests/Froxlor/ValidateTest.php | 100 ++++++++++++++++++++++++++++++ tests/bootstrap.php | 2 + 3 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 tests/Froxlor/ValidateTest.php diff --git a/lib/Froxlor/Validate/Validate.php b/lib/Froxlor/Validate/Validate.php index c6989cd7..2a767fe6 100644 --- a/lib/Froxlor/Validate/Validate.php +++ b/lib/Froxlor/Validate/Validate.php @@ -35,8 +35,8 @@ class Validate } // Check if the $str is one of the values which represent the default for an 'empty' value - if (is_array($emptydefault) && ! empty($emptydefault) && in_array($str, $emptydefault) && isset($emptydefault[0])) { - return $emptydefault[0]; + if (is_array($emptydefault) && ! empty($emptydefault) && in_array($str, $emptydefault)) { + return $str; } if ($pattern == '') { @@ -61,7 +61,6 @@ class Validate } \Froxlor\UI\Response::standard_error($lng, $fieldname, $throw_exception); - exit(); } /** @@ -99,7 +98,6 @@ class Validate return false; } else { \Froxlor\UI\Response::standard_error($lng, $ip, $throw_exception); - exit(); } } @@ -118,7 +116,6 @@ class Validate return false; } else { \Froxlor\UI\Response::standard_error($lng, $ip, $throw_exception); - exit(); } } diff --git a/tests/Froxlor/ValidateTest.php b/tests/Froxlor/ValidateTest.php new file mode 100644 index 00000000..0c7edb15 --- /dev/null +++ b/tests/Froxlor/ValidateTest.php @@ -0,0 +1,100 @@ +assertEquals("user input", $teststr); + } + + public function testValidateStrInEmptyDefault() + { + $teststr = Validate::validate("user input", "test-field", '', '', [ + "user test", + "user input", + "user bla" + ], true); + $this->assertEquals("user input", $teststr); + } + + public function testValidateEmptyDefaultNoArray() + { + $teststr = Validate::validate("user input", "test-field", '', '', "user input", true); + $this->assertEquals("user input", $teststr); + } + + public function testValidateRemoveNotAllowedChar() + { + $teststr = Validate::validate("user " . PHP_EOL . "input", "test-field", '', '', [], true); + $this->assertEquals("user input", $teststr); + } + + public function testValidateStringFormatError() + { + $this->expectException("Exception"); + $this->expectExceptionCode(400); + Validate::validate("user input", "test-field", '/^[A-Z]+$/i', '', [], true); + } + + public function testValidateIp() + { + $result = Validate::validate_ip2("12.34.56.78", false, 'invalidip', false, false, false, true); + $this->assertEquals("12.34.56.78", $result); + } + + public function testValidateIpPrivNotAllowed() + { + $this->expectException("Exception"); + $this->expectExceptionCode(400); + Validate::validate_ip2("10.0.0.1", false, 'invalidip', false, false, false, true); + } + + public function testValidateIpPrivNotAllowedBool() + { + $result = Validate::validate_ip2("10.0.0.1", true, 'invalidip', false, false, false, true); + $this->assertFalse($result); + } + + public function testValidateIpCidrNotAllowed() + { + $this->expectException("Exception"); + $this->expectExceptionCode(400); + Validate::validate_ip2("12.34.56.78/24", false, 'invalidip', false, false, false, true); + } + + public function testValidateIpCidrNotAllowedBool() + { + $result = Validate::validate_ip2("12.34.56.78/24", true, 'invalidip', false, false, false, true); + $this->assertFalse($result); + } + + public function testValidateIpCidr() + { + $result = Validate::validate_ip2("12.34.56.78/24", false, 'invalidip', false, false, true, true); + $this->assertEquals("12.34.56.78/24", $result); + } + + public function testValidateIpLocalhostAllowed() + { + $result = Validate::validate_ip2("127.0.0.1/32", false, 'invalidip', true, false, true, true); + $this->assertEquals("127.0.0.1/32", $result); + } + + public function testValidateIpLocalhostAllowedWrongIp() + { + $this->expectException("Exception"); + $this->expectExceptionCode(400); + Validate::validate_ip2("127.0.0.2", false, 'invalidip', true, false, false, true); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 87344530..5b2f1210 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -148,6 +148,8 @@ $sel_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_ADMINS . "` WHERE $admin_userdata = Database::pexecute_first($sel_stmt); $admin_userdata['adminsession'] = 1; +$log = \Froxlor\FroxlorLogger::getInstanceOf($admin_userdata); + Settings::Set('panel.standardlanguage', 'English', true); Settings::Set('panel.adminmail', 'admin@dev.froxlor.org', true); Settings::Set('panel.allow_domain_change_admin', '1', true);