]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/tests/units/Class/ConfigurationManagerTest.php
baculum: Assign Baculum copyright to Kern Sibbald
[bacula/bacula] / gui / baculum / tests / units / Class / ConfigurationManagerTest.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
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.
11  *
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.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 require_once('test_common.php');
24
25 /**
26  * Basic Test cases to configuration manager module.
27  *
28  * @author Marcin Haba
29  */
30 class ConfigurationManagerTest extends PHPUnit_Framework_TestCase {
31
32         public static $application = null;
33
34         public function setUp() {
35                 if(self::$application === null) {
36                         self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected');
37                         self::$application->run();
38                 }
39         }
40
41         public function testGetDbNameByTypeValid() {
42                 $testData = array(
43                         'pgsql' => 'PostgreSQL',
44                         'mysql' => 'MySQL',
45                         'sqlite' => 'SQLite'
46                 );
47                 foreach ($testData as $shortName => $longName) {
48                         $result = self::$application->getModule('configuration')->getDbNameByType($shortName);
49                         $this->assertEquals($longName, $result);
50                 }
51         }
52
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);
58                 }
59         }
60
61         public function testIsPostgreSQLTypeValid() {
62                 $testData = 'pgsql';
63                 $result = self::$application->getModule('configuration')->isPostgreSQLType($testData);
64                 $this->assertTrue($result);
65         }
66
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);
72                 }
73         }
74
75         public function testIsMySQLTypeValid() {
76                 $testData = 'mysql';
77                 $result = self::$application->getModule('configuration')->isMySQLType($testData);
78                 $this->assertTrue($result);
79         }
80
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);
86                 }
87         }
88         public function testIsSQLiteTypeValid() {
89                 $testData = 'sqlite';
90                 $result = self::$application->getModule('configuration')->isSQLiteType($testData);
91                 $this->assertTrue($result);
92         }
93
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);
99                 }
100         }
101
102         public function testGetDefaultLanguageValid() {
103                 $mockData = false;
104                 $testData = 'en';
105
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);
110         }
111
112         public function testGetCryptedPassword() {
113                 $testData = array();
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');
128
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);
132                 }
133         }
134
135         public function testGetRandomString() {
136                 $testData = array(
137                         'iterations' => 50,
138                         'pattern' => '/^[a-zA-Z0-9]{62}$/',
139                         'characters' => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
140                 );
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);
145                 }
146
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]);
151                         $len = strlen($str);
152                         $str_l = substr($str, 0, $pos);
153                         $str_r = ($len > 1) ? substr($str, ($pos+1), $len) : '';
154                         $str = $str_l . $str_r;
155                 }
156                 $result = empty($str);
157                 $this->assertTrue($result);
158         }
159 }
160
161 ?>