From: Marcin Haba Date: Fri, 1 Jan 2016 08:53:16 +0000 (+0100) Subject: baculum: Tweak add rest unit tests to configuration manager X-Git-Tag: Release-7.4.0~51 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=b627f8ba6e163aadb1b6380bf1e42f4887350aff;p=bacula%2Fbacula baculum: Tweak add rest unit tests to configuration manager --- diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php index 05cece2110..43b1d1fd41 100644 --- a/gui/baculum/tests/units/Class/ConfigurationManagerTest.php +++ b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php @@ -108,6 +108,54 @@ class ConfigurationManagerTest extends PHPUnit_Framework_TestCase { $result = $mock->getLanguage(); $this->assertEquals($testData, $result); } + + public function testGetCryptedPassword() { + $testData = array(); + $testData[] = array('password' => 'zzzzzzzzz', 'hash' => 'enEzofZKX2/wM'); + $testData[] = array('password' => 'admin', 'hash' => 'YWG41BPzVAkN6'); + $testData[] = array('password' => 'a dmin', 'hash' => 'YSz/JgtyVAHuc'); + $testData[] = array('password' => 'a', 'hash' => 'YQebj.HAzzu1c'); + $testData[] = array('password' => 'ADMIN', 'hash' => 'QUtX9W0NVx75o'); + $testData[] = array('password' => ' ', 'hash' => 'IAeLKSsdm161I'); + $testData[] = array('password' => 'a b c', 'hash' => 'YSMYQTGUwHPTE'); + $testData[] = array('password' => 'ąśćłóżźćń', 'hash' => 'xIObay0jyQnD2'); + $testData[] = array('password' => '$$$$', 'hash' => 'JCgedtNc0KHRw'); + $testData[] = array('password' => '\$$\$$', 'hash' => 'XCFJMFspzHfN6'); + $testData[] = array('password' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'hash' => 'MD.GrjSxikZ26'); + $testData[] = array('password' => "\t\n\t\r", 'hash' => 'CQUexWT5q3vHc'); + $testData[] = array('password' => '\t\n\t\r', 'hash' => 'XHACZ9CpS6KIw'); + $testData[] = array('password' => '$a=1;print $a;', 'hash' => 'JGr9Nl2UPwz1Y'); + + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->getCryptedPassword($testData[$i]['password']); + $this->assertEquals($testData[$i]['hash'], $result); + } + } + + public function testGetRandomString() { + $testData = array( + 'iterations' => 50, + 'pattern' => '/^[a-zA-Z0-9]{62}$/', + 'characters' => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + ); + for ($i = 0; $i < $testData['iterations']; $i++) { + $str = self::$application->getModule('configuration')->getRandomString(); + $result = preg_match($testData['pattern'], $str); + $this->assertEquals(1, $result); + } + + $charactersCopy = $testData['characters']; + $str = self::$application->getModule('configuration')->getRandomString(); + for ($i = 0; $i < strlen($testData['characters']); $i++) { + $pos = strpos($str, $charactersCopy[$i]); + $len = strlen($str); + $str_l = substr($str, 0, $pos); + $str_r = ($len > 1) ? substr($str, ($pos+1), $len) : ''; + $str = $str_l . $str_r; + } + $result = empty($str); + $this->assertTrue($result); + } } ?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php index 69ac8f8f58..d94519f8b2 100644 --- a/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php +++ b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php @@ -156,5 +156,71 @@ class ConfigurationManagerUsersConfigTest extends PHPUnit_Framework_TestCase { $result = self::$application->getModule('configuration')->clearUsersConfig(); $this->assertTrue($result); } + + public function testGetAllUsers() { + $testData = array(); + $testData[] = array( + 'user' => 'hmS2Tbx4VispuvjgRcDPIqadG3ylkn9AELeo1FYQCHWKOZw8N6J5MfBtX7rUz', + 'password' => 'enEzofZKX2/wM' + ); + $testData[] = array( + 'user' => 'hmS2Tbx4VispuvjgRcDPIqadG3ylkn9AELeo1FYQCHWKOZw8N6J5MfBtX7rUz$', + 'password' => 'xbe9m5cgdFzc2' + ); + $testData[] = array( + 'user' => 'ąśźćłóżźćń', + 'password' => 'ąśźćłóżźćń' + ); + $testData[] = array( + 'user' => '0', + 'password' => 'enEzofZK!X2/wM' + ); + $testData[] = array( + 'user' => '-1-', + 'password' => '!@#$%^&*()_)O(*&^%$#@@!}{PO|":L?><][\';/.,<' + ); + $testData[] = array( + 'user' => '(myUsEr$^)', + 'password' => ':::::' + ); + $testData[] = array( + 'user' => 'abcd#$@!', + 'password' => 'MzfDL89YPyQeM' + ); + $testData[] = array( + 'user' => '^$$#@@!!!:-)', + 'password' => 'PTwuUMPFz/0PE' + ); + + // check if config does not exist + $result = self::$application->getModule('configuration')->isUsersConfig(); + $this->assertFalse($result); + + // add users to config + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->setUsersConfig( + $testData[$i]['user'], + $testData[$i]['password'] + ); + $this->assertTrue($result); + } + + // read users from config + $result = self::$application->getModule('configuration')->getAllUsers(); + + $this->assertEquals(count($testData), count($result)); + + /* + * Check if users (and passwords) added to config and users (and passwords) read from + * config are the same + */ + for ($i = 0; $i < count($result); $i++) { + $user = $testData[$i]['user']; + $plain_pwd = $testData[$i]['password']; + $this->assertTrue(array_key_exists($user, $result)); + $enc_pwd = self::$application->getModule('configuration')->getCryptedPassword($plain_pwd); + $this->assertEquals($enc_pwd, $result[$testData[$i]['user']]); + } + } } ?>