3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 require_once('test_common.php');
26 * Basic Test cases to configuration manager module.
30 class ConfigurationManagerTest extends PHPUnit_Framework_TestCase {
32 public static $application = null;
34 public function setUp() {
35 if(self::$application === null) {
36 self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected');
37 self::$application->run();
41 public function testGetDbNameByTypeValid() {
43 'pgsql' => 'PostgreSQL',
47 foreach ($testData as $shortName => $longName) {
48 $result = self::$application->getModule('configuration')->getDbNameByType($shortName);
49 $this->assertEquals($longName, $result);
53 public function testGetDbNameByTypeInvalid() {
54 $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'Mysql', 'MYSQL', 'SQlite', 'MySqL', null, true, 0, -1);
55 for ($i = 0; $i < count($testData); $i++) {
56 $result = self::$application->getModule('configuration')->getDbNameByType($testData[$i]);
57 $this->assertNull($result);
61 public function testIsPostgreSQLTypeValid() {
63 $result = self::$application->getModule('configuration')->isPostgreSQLType($testData);
64 $this->assertTrue($result);
67 public function testIsPostgreSQLTypeInvalid() {
68 $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'mysql', 'MYSQL', 'sqlite', 'MySqL', null, true, 0, -1);
69 for ($i = 0; $i < count($testData); $i++) {
70 $result = self::$application->getModule('configuration')->isPostgreSQLType($testData[$i]);
71 $this->assertFalse($result);
75 public function testIsMySQLTypeValid() {
77 $result = self::$application->getModule('configuration')->isMySQLType($testData);
78 $this->assertTrue($result);
81 public function testIsMySQLTypeInvalid() {
82 $testData = array("mysql\n", ' mysql', ' mysql ', 'm ysql', 'MYSQ', 'sqlite', 'pgsql', 'mysq', null, true, 0, -1);
83 for ($i = 0; $i < count($testData); $i++) {
84 $result = self::$application->getModule('configuration')->isMySQLType($testData[$i]);
85 $this->assertFalse($result);
88 public function testIsSQLiteTypeValid() {
90 $result = self::$application->getModule('configuration')->isSQLiteType($testData);
91 $this->assertTrue($result);
94 public function testIsSQLiteTypeInvalid() {
95 $testData = array("sqlite\n", ' sqlite', ' sqlite ', 's qlite', 'sqlit', 'pgsql', 'mysqs', null, true, 0, -1);
96 for ($i = 0; $i < count($testData); $i++) {
97 $result = self::$application->getModule('configuration')->isSQLiteType($testData[$i]);
98 $this->assertFalse($result);
102 public function testGetDefaultLanguageValid() {
106 $mock = $this->getMockBuilder('ConfigurationManager')->setMethods(array('isApplicationConfig'))->getMock();
107 $mock->expects($this->once())->method('isApplicationConfig')->will($this->returnValue($mockData));
108 $result = $mock->getLanguage();
109 $this->assertEquals($testData, $result);
112 public function testGetCryptedPassword() {
114 $testData[] = array('password' => 'zzzzzzzzz', 'hash' => 'enEzofZKX2/wM');
115 $testData[] = array('password' => 'admin', 'hash' => 'YWG41BPzVAkN6');
116 $testData[] = array('password' => 'a dmin', 'hash' => 'YSz/JgtyVAHuc');
117 $testData[] = array('password' => 'a', 'hash' => 'YQebj.HAzzu1c');
118 $testData[] = array('password' => 'ADMIN', 'hash' => 'QUtX9W0NVx75o');
119 $testData[] = array('password' => ' ', 'hash' => 'IAeLKSsdm161I');
120 $testData[] = array('password' => 'a b c', 'hash' => 'YSMYQTGUwHPTE');
121 $testData[] = array('password' => 'ąśćłóżźćń', 'hash' => 'xIObay0jyQnD2');
122 $testData[] = array('password' => '$$$$', 'hash' => 'JCgedtNc0KHRw');
123 $testData[] = array('password' => '\$$\$$', 'hash' => 'XCFJMFspzHfN6');
124 $testData[] = array('password' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'hash' => 'MD.GrjSxikZ26');
125 $testData[] = array('password' => "\t\n\t\r", 'hash' => 'CQUexWT5q3vHc');
126 $testData[] = array('password' => '\t\n\t\r', 'hash' => 'XHACZ9CpS6KIw');
127 $testData[] = array('password' => '$a=1;print $a;', 'hash' => 'JGr9Nl2UPwz1Y');
129 for ($i = 0; $i < count($testData); $i++) {
130 $result = self::$application->getModule('configuration')->getCryptedPassword($testData[$i]['password']);
131 $this->assertEquals($testData[$i]['hash'], $result);
135 public function testGetRandomString() {
138 'pattern' => '/^[a-zA-Z0-9]{62}$/',
139 'characters' => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
141 for ($i = 0; $i < $testData['iterations']; $i++) {
142 $str = self::$application->getModule('configuration')->getRandomString();
143 $result = preg_match($testData['pattern'], $str);
144 $this->assertEquals(1, $result);
147 $charactersCopy = $testData['characters'];
148 $str = self::$application->getModule('configuration')->getRandomString();
149 for ($i = 0; $i < strlen($testData['characters']); $i++) {
150 $pos = strpos($str, $charactersCopy[$i]);
152 $str_l = substr($str, 0, $pos);
153 $str_r = ($len > 1) ? substr($str, ($pos+1), $len) : '';
154 $str = $str_l . $str_r;
156 $result = empty($str);
157 $this->assertTrue($result);