]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php
baculum: Assign Baculum copyright to Kern Sibbald
[bacula/bacula] / gui / baculum / tests / units / Class / ConfigurationManagerUsersConfigTest.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  * Test cases to read/write/check Baculum users file (baculum.users).
27  *
28  * @author Marcin Haba
29  */
30 class ConfigurationManagerUsersConfigTest 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                 if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) {
40                         copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH);
41                         remove_path(BACULUM_ROOT_DIR . 'protected/Data', true);
42                 }
43         }
44
45         public function tearDown() {
46                 if (file_exists(BACKUP_FILES_PATH)) {
47                         remove_path(BACKUP_FILES_PATH . 'protected/Data', true);
48                         copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data');
49                         remove_path(BACKUP_FILES_PATH);
50                 }
51         }
52
53         public function testSetUsersConfig() {
54                 $testData = array();
55                 $testData[] = array( // create new user admin432
56                         'data' => array(
57                                 'user' => 'admin432',
58                                 'password' => 'myStrOnGPa$$!',
59                                 'firstusage' => true,
60                                 'olduser' => null
61                         ),
62                         'result' => 'admin432:bXOrb7VkxRBYY'
63                 );
64                 $testData[] = array( // add user willyWonka to config
65                         'data' => array(
66                                 'user' => 'willyWonka',
67                                 'password' => '#=ChOcOlAtEE=#',
68                                 'firstusage' => false,
69                                 'olduser' => null
70                         ),
71                         'result' => "admin432:bXOrb7VkxRBYY\nwillyWonka:IzGy5zT.vOaeI"
72                 );
73                 $testData[] = array( // clear users config and create user willyWonka again
74                         'data' => array(
75                                 'user' => 'willyWonka',
76                                 'password' => '#=ChOcOlAtEE=#',
77                                 'firstusage' => true,
78                                 'olduser' => null
79                         ),
80                         'result' => "willyWonka:IzGy5zT.vOaeI"
81                 );
82                 $testData[] = array( // add user donkamillo to config
83                         'data' => array(
84                                 'user' => 'donkamillo',
85                                 'password' => '!@#$%^&*()_+}{|"\'<>?/.,',
86                                 'firstusage' => false,
87                                 'olduser' => null
88                         ),
89                         'result' => "willyWonka:IzGy5zT.vOaeI\ndonkamillo:IU029IHEUYWhY"
90                 );
91                 $testData[] = array( // rename user willyWonka to user johny11
92                         'data' => array(
93                                 'user' => 'johny11',
94                                 'password' => '#=ChOcOlAtEE=#',
95                                 'firstusage' => false,
96                                 'olduser' => 'willyWonka'
97                         ),
98                         'result' => "donkamillo:IU029IHEUYWhY\njohny11:IzGy5zT.vOaeI"
99                 );
100                 $testData[] = array( // change password for user johny11
101                         'data' => array(
102                                 'user' => 'johny11',
103                                 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,',
104                                 'firstusage' => false,
105                                 'olduser' => null
106                         ),
107                         'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52"
108                 );
109                 $testData[] = array( // input the same password again for user johny11
110                         'data' => array(
111                                 'user' => 'johny11',
112                                 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,',
113                                 'firstusage' => false,
114                                 'olduser' => null
115                         ),
116                         'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52"
117                 );
118                 for ($i = 0; $i < count($testData); $i++) {
119                         $result = self::$application->getModule('configuration')->setUsersConfig(
120                                 $testData[$i]['data']['user'],
121                                 $testData[$i]['data']['password'],
122                                 $testData[$i]['data']['firstusage'],
123                                 $testData[$i]['data']['olduser']
124                         );
125                         $this->assertTrue($result);
126                         $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users');
127                         $this->assertEquals($testData[$i]['result'], $result);
128                 }
129         }
130
131         public function testCheckAndClearUsersConfig() {
132                 $testData[] = array( // input the same password again for user johny11
133                         'data' => array(
134                                 'user' => 'johny11',
135                                 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,',
136                                 'firstusage' => false,
137                                 'olduser' => null
138                         ),
139                         'result' => "johny11:Q2XfKovUGQe52"
140                 );
141                 $result = self::$application->getModule('configuration')->isUsersConfig();
142                 $this->assertFalse($result);
143                 for ($i = 0; $i < count($testData); $i++) {
144                         $result = self::$application->getModule('configuration')->setUsersConfig(
145                                 $testData[$i]['data']['user'],
146                                 $testData[$i]['data']['password'],
147                                 $testData[$i]['data']['firstusage'],
148                                 $testData[$i]['data']['olduser']
149                         );
150                         $this->assertTrue($result);
151                         $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users');
152                         $this->assertEquals($testData[$i]['result'], $result);
153                 }
154                 $result = self::$application->getModule('configuration')->isUsersConfig();
155                 $this->assertTrue($result);
156                 $result = self::$application->getModule('configuration')->clearUsersConfig();
157                 $this->assertTrue($result);
158         }
159
160         public function testGetAllUsers() {
161                 $testData = array();
162                 $testData[] = array(
163                         'user' => 'hmS2Tbx4VispuvjgRcDPIqadG3ylkn9AELeo1FYQCHWKOZw8N6J5MfBtX7rUz',
164                         'password' => 'enEzofZKX2/wM'
165                 );
166                 $testData[] = array(
167                         'user' => 'hmS2Tbx4VispuvjgRcDPIqadG3ylkn9AELeo1FYQCHWKOZw8N6J5MfBtX7rUz$',
168                         'password' => 'xbe9m5cgdFzc2'
169                 );
170                 $testData[] = array(
171                         'user' => 'ąśźćłóżźćń',
172                         'password' => 'ąśźćłóżźćń'
173                 );
174                 $testData[] = array(
175                         'user' => '0',
176                         'password' => 'enEzofZK!X2/wM'
177                 );
178                 $testData[] = array(
179                         'user' => '-1-',
180                         'password' => '!@#$%^&*()_)O(*&^%$#@@!}{PO|":L?><M~><][\';/.,<'
181                 );
182                 $testData[] = array(
183                         'user' => '(myUsEr$^)',
184                         'password' => ':::::'
185                 );
186                 $testData[] = array(
187                         'user' => 'abcd#$@!',
188                         'password' => 'MzfDL89YPyQeM'
189                 );
190                 $testData[] = array(
191                         'user' => '^$$#@@!!!:-)',
192                         'password' => 'PTwuUMPFz/0PE'
193                 );
194
195                 // check if config does not exist
196                 $result = self::$application->getModule('configuration')->isUsersConfig();
197                 $this->assertFalse($result);
198
199                 // add users to config
200                 for ($i = 0; $i < count($testData); $i++) {
201                         $result = self::$application->getModule('configuration')->setUsersConfig(
202                                 $testData[$i]['user'],
203                                 $testData[$i]['password']
204                         );
205                         $this->assertTrue($result);
206                 }
207
208                 // read users from config
209                 $result = self::$application->getModule('configuration')->getAllUsers();
210
211                 $this->assertEquals(count($testData), count($result));
212
213                 /*
214                  * Check if users (and passwords) added to config and users (and passwords) read from
215                  * config are the same
216                  */
217                 for ($i = 0; $i < count($result); $i++) {
218                         $user = $testData[$i]['user'];
219                         $plain_pwd = $testData[$i]['password'];
220                         $this->assertTrue(array_key_exists($user, $result));
221                         $enc_pwd = self::$application->getModule('configuration')->getCryptedPassword($plain_pwd);
222                         $this->assertEquals($enc_pwd, $result[$testData[$i]['user']]);
223                 }
224         }
225 }
226 ?>