From: <gi...@ba...> - 2015-12-31 11:55:39
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Bacula Community source". The branch, Branch-7.2 has been updated via b62f8adaac7147152225bac88032585c34e6f897 (commit) via 0dea7003e289c9049a7ee6df9190575b504ff78e (commit) via 2111894da0ca8e8aba85439de9a059638343e433 (commit) via f866471b6a9d95d7a9229e75b260e08d9ac9829a (commit) via 1100446451db866850ce1ec185db74ac9e41a4cc (commit) from b12a02b1b9c3c3e3ca2f64f66dbac1cd530546ad (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit b62f8adaac7147152225bac88032585c34e6f897 Author: Kern Sibbald <ke...@si...> Date: Thu Dec 31 11:40:02 2015 +0100 Fix seg fault in btape fixes bug #2180 commit 0dea7003e289c9049a7ee6df9190575b504ff78e Author: Kern Sibbald <ke...@si...> Date: Thu Dec 31 11:39:15 2015 +0100 Fix slight error in autoprune -- should fix bug #2151 commit 2111894da0ca8e8aba85439de9a059638343e433 Author: Kern Sibbald <ke...@si...> Date: Thu Dec 31 09:57:30 2015 +0100 Change copyright as per agreement with FSFE commit f866471b6a9d95d7a9229e75b260e08d9ac9829a Author: Marcin Haba <mar...@ba...> Date: Thu Dec 31 09:16:42 2015 +0100 baculum: Add first unit tests commit 1100446451db866850ce1ec185db74ac9e41a4cc Author: Marcin Haba <mar...@ba...> Date: Thu Dec 31 09:15:58 2015 +0100 baculum: Tweak add comments ----------------------------------------------------------------------- Summary of changes: diff --git a/bacula/src/cats/bvfs.c b/bacula/src/cats/bvfs.c index 09dee98..ff236cb 100644 --- a/bacula/src/cats/bvfs.c +++ b/bacula/src/cats/bvfs.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2009-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/cats.c b/bacula/src/cats/cats.c index ccdedd4..5e19cd5 100644 --- a/bacula/src/cats/cats.c +++ b/bacula/src/cats/cats.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/cats_null.c b/bacula/src/cats/cats_null.c index 7b83ea7..f3da1c2 100644 --- a/bacula/src/cats/cats_null.c +++ b/bacula/src/cats/cats_null.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/mysql.c b/bacula/src/cats/mysql.c index ac75618..3ac8af7 100644 --- a/bacula/src/cats/mysql.c +++ b/bacula/src/cats/mysql.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/postgresql.c b/bacula/src/cats/postgresql.c index 70d8e6b..f8fa4a9 100644 --- a/bacula/src/cats/postgresql.c +++ b/bacula/src/cats/postgresql.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2003-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -41,7 +40,7 @@ #include "postgres_ext.h" /* needed for NAMEDATALEN */ #include "pg_config_manual.h" /* get NAMEDATALEN on version 8.3 or later */ #define __BDB_POSTGRESQL_H_ 1 -#include "bdb_postgresql.h" +#include "bdb_postgresql.h" #define dbglvl_dbg DT_SQL|100 #define dbglvl_info DT_SQL|50 @@ -129,9 +128,7 @@ BDB *db_init_database(JCR *jcr, const char *db_driver, const char *db_name, cons mdb = New(BDB_POSTGRESQL()); if (!mdb) goto get_out; - /* - * Initialize the parent class members. - */ + /* Initialize the parent class members. */ mdb->m_db_name = bstrdup(db_name); mdb->m_db_user = bstrdup(db_user); if (db_password) { @@ -165,7 +162,7 @@ BDB *db_init_database(JCR *jcr, const char *db_driver, const char *db_name, cons #endif /* USE_BATCH_FILE_INSERT */ } mdb->m_allow_transactions = mult_db_connections; - + /* At this time, when mult_db_connections == true, this is for * specific console command such as bvfs or batch mode, and we don't * want to share a batch mode or bvfs. In the future, we can change @@ -315,7 +312,7 @@ void BDB_POSTGRESQL::bdb_close_database(JCR *jcr) db_list->remove(mdb); if (mdb->m_connected && mdb->m_db_handle) { PQfinish(mdb->m_db_handle); - } + } if (is_rwl_valid(&mdb->m_lock)) { rwl_destroy(&mdb->m_lock); } @@ -678,7 +675,7 @@ get_out: ok_out: return retval; } - + void BDB_POSTGRESQL::sql_free_result(void) { BDB_POSTGRESQL *mdb = this; @@ -743,10 +740,10 @@ SQL_ROW BDB_POSTGRESQL::sql_fetch_row(void) Dmsg1(dbglvl_info, "sql_fetch_row finishes returning %p\n", row); return row; -} +} const char *BDB_POSTGRESQL::sql_strerror(void) -{ +{ BDB_POSTGRESQL *mdb = this; return PQerrorMessage(mdb->m_db_handle); } diff --git a/bacula/src/cats/sql_delete.c b/bacula/src/cats/sql_delete.c index 7a6faa8..97bff2b 100644 --- a/bacula/src/cats/sql_delete.c +++ b/bacula/src/cats/sql_delete.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/sql_find.c b/bacula/src/cats/sql_find.c index 0a0878c..c9b2d5b 100644 --- a/bacula/src/cats/sql_find.c +++ b/bacula/src/cats/sql_find.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/sql_get.c b/bacula/src/cats/sql_get.c index 2414a13..4f60b7f 100644 --- a/bacula/src/cats/sql_get.c +++ b/bacula/src/cats/sql_get.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -177,7 +176,6 @@ int BDB::bdb_get_filename_record(JCR *jcr) { SQL_ROW row; int FilenameId = 0; - int num_rows; esc_name = check_pool_memory_size(esc_name, 2*fnl+2); bdb_escape_string(jcr, esc_name, fname, fnl); @@ -185,13 +183,12 @@ int BDB::bdb_get_filename_record(JCR *jcr) Mmsg(cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", esc_name); if (QueryDB(jcr, cmd)) { char ed1[30]; - num_rows = sql_num_rows(); - if (num_rows > 1) { + if (sql_num_rows() > 1) { Mmsg2(errmsg, _("More than one Filename!: %s for file: %s\n"), - edit_uint64(num_rows, ed1), fname); + edit_uint64(sql_num_rows(), ed1), fname); Jmsg(jcr, M_WARNING, 0, "%s", errmsg); } - if (num_rows >= 1) { + if (sql_num_rows() >= 1) { if ((row = sql_fetch_row()) == NULL) { Mmsg1(errmsg, _("error fetching row: %s\n"), sql_strerror()); } else { diff --git a/bacula/src/cats/sql_list.c b/bacula/src/cats/sql_list.c index 9f237e5..3e89c41 100644 --- a/bacula/src/cats/sql_list.c +++ b/bacula/src/cats/sql_list.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/sql_update.c b/bacula/src/cats/sql_update.c index 3c051aa..f4da1e3 100644 --- a/bacula/src/cats/sql_update.c +++ b/bacula/src/cats/sql_update.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/cats/sqlite.c b/bacula/src/cats/sqlite.c index 89f810e..bed7e62 100644 --- a/bacula/src/cats/sqlite.c +++ b/bacula/src/cats/sqlite.c @@ -2,7 +2,6 @@ Bacula(R) - The Network Backup Solution Copyright (C) 2000-2015 Kern Sibbald - Copyright (C) 2000-2014 Free Software Foundation Europe e.V. The original author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. diff --git a/bacula/src/dird/autoprune.c b/bacula/src/dird/autoprune.c index 018a5bf..46416f5 100644 --- a/bacula/src/dird/autoprune.c +++ b/bacula/src/dird/autoprune.c @@ -200,11 +200,6 @@ void prune_volumes(JCR *jcr, bool InChanger, MEDIA_DBR *mr, continue; } - if (has_volume_expired(jcr, &lmr)) { - Dmsg1(100, "Vol=%s has expired\n", lmr.VolumeName); - continue; /* Volume not usable */ - } - /* * If purged and not moved to another Pool, * then we stop pruning and take this volume. diff --git a/bacula/src/stored/block_util.c b/bacula/src/stored/block_util.c index 38b9958..11f3cf2 100644 --- a/bacula/src/stored/block_util.c +++ b/bacula/src/stored/block_util.c @@ -186,7 +186,7 @@ void DCR::free_blocks() */ void free_block(DEV_BLOCK *block) { - if (block) { + if (block && block->buf) { Dmsg1(999, "free_block buffer %x\n", block->buf); free_memory(block->buf); Dmsg1(999, "free_block block %x\n", block); diff --git a/gui/baculum/index.php b/gui/baculum/index.php index 683acb7..d70df07 100644 --- a/gui/baculum/index.php +++ b/gui/baculum/index.php @@ -19,11 +19,17 @@ * * Bacula(R) is a registered trademark of Kern Sibbald. */ + +/* + * Constant is used to localize always valid document root directory + * Using for placing Baculum files in document root subdirectory + */ define('APPLICATION_DIRECTORY', __DIR__); require_once('./protected/Init.php'); require_once('./framework/prado.php'); +// Start application $application=new TApplication; $application->run(); ?> diff --git a/gui/baculum/protected/Class/API.php b/gui/baculum/protected/Class/API.php index b04df45..9f631fd 100644 --- a/gui/baculum/protected/Class/API.php +++ b/gui/baculum/protected/Class/API.php @@ -22,29 +22,58 @@ Prado::using('Application.Class.Errors'); +/** + * Internal API client module. + * + * @author Marcin Haba + */ class API extends TModule { + /** + * API version (used in HTTP header) + */ const API_VERSION = '0.1'; + /** + * Store configuration data from settings file + * @access protected + */ protected $appCfg; + /** + * These errors are allowed in API response and they do not cause + * disturb application working (no direction to error page) + * @access private + */ private $allowedErrors = array( GenericError::ERROR_NO_ERRORS, BconsoleError::ERROR_INVALID_COMMAND, PoolError::ERROR_NO_VOLUMES_IN_POOL_TO_UPDATE ); - private function getConnection() { + /** + * Get connection request handler. + * For data requests is used cURL interface. + * @access public + * @return resource connection handler on success, false on errors + */ + public function getConnection() { $ch = curl_init(); + $userpwd = sprintf('%s:%s', $this->appCfg['baculum']['login'], $this->appCfg['baculum']['password']); + curl_setopt($ch, CURLOPT_USERPWD, $userpwd); + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . md5(session_id())); - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_setopt($ch, CURLOPT_USERPWD, $this->appCfg['baculum']['login'] . ':' . $this->appCfg['baculum']['password']); return $ch; } + /** + * Get API specific headers used in HTTP requests. + * @access private + * @return API specific headers + */ private function getAPIHeaders() { $headers = array( 'X-Baculum-API: ' . self::API_VERSION, @@ -55,29 +84,61 @@ class API extends TModule { return $headers; } + /** + * Initializes API module (framework module constructor) + * @access public + * @param TXmlElement $config API module configuration + */ public function init($config) { $this->initSessionCache(); $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig(); } + /** + * Get URL to use by internal API client's request. + * @access private + * @return string URL to internal API server + */ private function getURL() { $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http'; $host = $_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; + + // support for document root subdirectory $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix(); + $url = sprintf('%s://%s:%d%s/', $protocol, $host, $port, $urlPrefix); return $url; } - private function setParamsToUrl(&$url) { - $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?' ) . 'director=' . ((array_key_exists('director', $_SESSION)) ? $_SESSION['director'] : ''); - $this->Application->getModule('logging')->log(__FUNCTION__, PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); + /** + * Set URL parameters and prepare URL to request send. + * @access private + * @param string &$url reference to URL string variable + */ + private function setUrlParams(&$url) { + $url .= (preg_match('/\?/', $url) === 1 ? '&' : '?'); + $url .= 'director='; + if (array_key_exists('director', $_SESSION)) { + $url .= $_SESSION['director']; + } + + $this->Application->getModule('logging')->log( + __FUNCTION__, + PHP_EOL . PHP_EOL . 'EXECUTE URL ==> ' . $url . ' <==' . PHP_EOL . PHP_EOL, + Logging::CATEGORY_APPLICATION, + __FILE__, + __LINE__ + ); } /** - * API REQUESTS METHODS (get, set, create, delete) + * Internal API GET request. + * @access public + * @param array $params GET params to send in request + * @param bool $use_cache if true then try to use session cache, if false then always use fresh data + * @return object stdClass with request result as two properties: 'output' and 'error' */ - public function get(array $params, $use_cache = false) { $cached = null; $ret = null; @@ -88,7 +149,7 @@ class API extends TModule { $ret = $cached; } else { $url = $this->getURL() . implode('/', $params); - $this->setParamsToUrl($url); + $this->setUrlParams($url); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getAPIHeaders()); @@ -102,14 +163,24 @@ class API extends TModule { return $ret; } + /** + * Internal API SET request. + * @access public + * @param array $params GET params to send in request + * @param array $options POST params to send in request + * @return object stdClass with request result as two properties: 'output' and 'error' + */ public function set(array $params, array $options) { $url = $this->getURL() . implode('/', $params); - $this->setParamsToUrl($url); + $this->setUrlParams($url); $data = http_build_query(array('update' => $options)); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); - curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($this->getAPIHeaders(), array('X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data), 'Expect:'))); + curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge( + $this->getAPIHeaders(), + array('X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($data), 'Expect:') + )); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch); @@ -117,9 +188,16 @@ class API extends TModule { return $this->preParseOutput($result); } + /** + * Internal API CREATE request. + * @access public + * @param array $params GET params to send in request + * @param array $options POST params to send in request + * @return object stdClass with request result as two properties: 'output' and 'error' + */ public function create(array $params, array $options) { $url = $this->getURL() . implode('/', $params); - $this->setParamsToUrl($url); + $this->setUrlParams($url); $data = http_build_query(array('create' => $options)); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); @@ -131,9 +209,15 @@ class API extends TModule { return $this->preParseOutput($result); } + /** + * Internal API REMOVE request. + * @access public + * @param array $params GET params to send in request + * @return object stdClass with request result as two properties: 'output' and 'error' + */ public function remove(array $params) { $url = $this->getURL() . implode('/', $params); - $this->setParamsToUrl($url); + $this->setUrlParams($url); $ch = $this->getConnection(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); @@ -143,10 +227,28 @@ class API extends TModule { return $this->preParseOutput($result); } + /** + * Initially parse and prepare every Internal API response. + * If a error occurs then redirect to appropriate error page. + * @access private + * @param string $result response output as JSON string (not object yet) + * @return object stdClass parsed response with two top level properties 'output' and 'error' + */ private function preParseOutput($result) { - $this->Application->getModule('logging')->log(__FUNCTION__, $result, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); + // first write log with that what comes + $this->Application->getModule('logging')->log( + __FUNCTION__, + $result, + Logging::CATEGORY_APPLICATION, + __FILE__, + __LINE__ + ); + + // decode JSON to object $resource = json_decode($result); + $error = null; + if(is_object($resource) && property_exists($resource, 'error')) { if(!in_array($resource->error, $this->allowedErrors)) { $error = $resource->error; @@ -155,10 +257,24 @@ class API extends TModule { $error = AuthorizationError::ERROR_AUTHORIZATION_TO_WEBGUI_PROBLEM; } - $this->Application->getModule('logging')->log(__FUNCTION__, $resource, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__); + $this->Application->getModule('logging')->log( + __FUNCTION__, + $resource, + Logging::CATEGORY_APPLICATION, + __FILE__, + __LINE__ + ); + + // if other than allowed errors exist then show error page (redirect) if(!is_null($error)) { // Note! Redirection to error page takes place here. - $this->Response->redirect($this->Service->constructUrl('BaculumError',array('error' => $error), false)); + $this->Response->redirect( + $this->Service->constructUrl( + 'BaculumError', + array('error' => $error), + false + ) + ); } return $resource; diff --git a/gui/baculum/protected/Class/ConfigurationManager.php b/gui/baculum/protected/Class/ConfigurationManager.php index caa1bdd..b569281 100644 --- a/gui/baculum/protected/Class/ConfigurationManager.php +++ b/gui/baculum/protected/Class/ConfigurationManager.php @@ -22,11 +22,19 @@ Prado::using('Application.Class.Miscellaneous'); +/** + * Manage application configuration. + * Module is responsible for get/set application config data like: + * read/write application config and usersfiles, get application language + * and others. + * + * @author Marcin Haba + */ class ConfigurationManager extends TModule { /** - * Location o application configuration file. + * Application config file path */ const CONFIG_FILE = 'Application.Data.settings'; @@ -36,32 +44,39 @@ class ConfigurationManager extends TModule const USERS_FILE = 'Application.Data.baculum'; /** - * PostgreSQL default params. + * PostgreSQL default params */ const PGSQL = 'pgsql'; const PGSQL_NAME = 'PostgreSQL'; const PGSQL_PORT = 5432; /** - * MySQL default params. + * MySQL default params */ const MYSQL = 'mysql'; const MYSQL_NAME = 'MySQL'; const MYSQL_PORT = 3306; /** - * SQLite default params. + * SQLite default params */ const SQLITE = 'sqlite'; const SQLITE_NAME = 'SQLite'; const SQLITE_PORT = null; /** - * Default language for application. + * Default application language */ const DEFAULT_LANGUAGE = 'en'; + /** + * Get database name by database type (short name). + * @access public + * @param string $type database type ('pgsql', 'mysql' ...) + * @return mixed database name or null if database name not found + */ public function getDbNameByType($type) { + $type = (string) $type; switch($type) { case self::PGSQL: $dbName = self::PGSQL_NAME; break; case self::MYSQL: $dbName = self::MYSQL_NAME; break; @@ -71,34 +86,46 @@ class ConfigurationManager extends TModule return $dbName; } - public function getPostgreSQLType() { - return self::PGSQL; - } - - public function getMySQLType() { - return self::MYSQL; - } - - public function getSQLiteType() { - return self::SQLITE; - } - + /** + * Check if given database type is PostgreSQL type. + * @access public + * @param string $type database type ('pgsql', 'mysql' ...) + * @return boolean true if database type is PostgreSQL, otherwise false + */ public function isPostgreSQLType($type) { return ($type === self::PGSQL); } + /** + * Check if given database type is MySQL type. + * @access public + * @param string $type database type ('pgsql', 'mysql' ...) + * @return boolean true if database type is MySQL, otherwise false + */ public function isMySQLType($type) { return ($type === self::MYSQL); } + /** + * Check if given database type is SQLite type. + * @access public + * @param string $type database type ('sqlite', 'mysql' ...) + * @return boolean true if database type is SQLite, otherwise false + */ public function isSQLiteType($type) { return ($type === self::SQLITE); } + /** + * Get currently set application language short name. + * If no language set then default language is taken. + * @access public + * @return string lanuage short name + */ public function getLanguage() { $language = self::DEFAULT_LANGUAGE; if ($this->isApplicationConfig() === true) { - $config = $this->getApplicationConfig(); + $config = self::getApplicationConfig(); if (array_key_exists('lang', $config['baculum'])) { $language = $config['baculum']['lang']; } @@ -106,13 +133,8 @@ class ConfigurationManager extends TModule return $language; } - public function setLanguage($language) { - - } - /** - * Saving application configuration. - * + * Save application configuration. * @access public * @param array $config structure of config file params * @return boolean true if config save is successfully, false if config save is failure @@ -123,8 +145,7 @@ class ConfigurationManager extends TModule } /** - * Getting application configuration. - * + * Get application configuration. * @access public * @return array application configuration */ @@ -134,8 +155,7 @@ class ConfigurationManager extends TModule } /** - * Checking if application configuration file exists. - * + * Check if application configuration file exists. * @access public * @return boolean true if file exists, otherwise false */ @@ -143,20 +163,20 @@ class ConfigurationManager extends TModule return file_exists(Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf')); } + /** + * Get encrypted password to use in HTTP Basic auth. + * + * @access public + * @param string $password plain text password + * @return string encrypted password + */ public function getCryptedPassword($password) { $enc_pwd = crypt($password, base64_encode($password)); return $enc_pwd; } /** - * Saving user to users configuration file. - * - * NOTE! - * So far by webGUI is possible to set one user. - * For more users and restricted consoles, there is need to modify - * users and passwords file. - * - * TODO: Support for more than one user setting on webGUI. + * Save user to users configuration file. * * @access public * @param string $user username @@ -166,13 +186,13 @@ class ConfigurationManager extends TModule * @return boolean true if user saved successfully, otherwise false */ public function setUsersConfig($user, $password, $firstUsage = false, $oldUser = null) { - $allUsers = $this->getAllUsers(); - $password = $this->getCryptedPassword($password); - if($firstUsage === true) { $this->clearUsersConfig(); } + $allUsers = $this->getAllUsers(); + $password = $this->getCryptedPassword($password); + $userExists = array_key_exists($user, $allUsers); @@ -197,6 +217,14 @@ class ConfigurationManager extends TModule return $result; } + /** + * Read all users from HTTP Basic users file. + * Returned value is associative array with usernames as keys + * and encrypted passwords as values. + * + * @access public + * @return array users/passwords list + */ public function getAllUsers() { $allUsers = array(); if ($this->isUsersConfig() === true) { @@ -212,6 +240,15 @@ class ConfigurationManager extends TModule return $allUsers; } + /** + * Save HTTP Basic users file. + * Given parameter is associative array with usernames as keys + * and encrypted passwords as values. + * + * @access public + * @param array $allUsers users/passwords list + * @return boolean true if users file saved successfully, otherwise false + */ public function saveUserConfig($allUsers) { $users = array(); foreach ($allUsers as $user => $pwd) { @@ -226,6 +263,15 @@ class ConfigurationManager extends TModule return $result; } + /** + * Remove single user from HTTP Basic users file. + * Note, this method saves config file if username was existed + * before removing. + * + * @access public + * @param string $username user name to remove + * @return boolean true if users file saved successfully, otherwise false + */ public function removeUser($username) { $result = false; $allUsers = $this->getAllUsers(); @@ -237,8 +283,7 @@ class ConfigurationManager extends TModule } /** - * Checking if users configuration file exists. - * + * Check if users configuration file exists. * @access public * @return boolean true if file exists, otherwise false */ @@ -248,22 +293,44 @@ class ConfigurationManager extends TModule /** * Clear all content of users file. - * - * @access private + * @access public * @return boolean true if file cleared successfully, otherwise false */ - private function clearUsersConfig() { + public function clearUsersConfig() { $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users'); $result = file_put_contents($usersFile, '') !== false; return $result; } + /** + * Log in as specific user. + * + * Note, usually after this method call there required is using exit() just + * after method execution. Otherwise the HTTP redirection may be canceled on some + * web servers. + * + * @access public + * @param string $http_protocol 'http' or 'https' value + * @param string $host hostname without port, for example: my.own.host or localhost + * @param integer $port port number on which listens web server + * @param string $user user name to log in + * @param string $string plain text user's password + * @return none + */ public function switchToUser($http_protocol, $host, $port, $user, $password) { $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix(); $location = sprintf("%s://%s:%s@%s:%d%s", $http_protocol, $user, $password, $host, $port, $urlPrefix); header("Location: $location"); } + /** + * Get (pseudo)random string. + * + * Useful for log out user from HTTP Basic auth by providing random password. + * + * @access public + * @return string random 62 characters string from range [a-zA-Z0-9] + */ public function getRandomString() { $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $rand_string = str_shuffle($characters); diff --git a/gui/baculum/protected/Init.php b/gui/baculum/protected/Init.php index d362703..f7a5f1c 100644 --- a/gui/baculum/protected/Init.php +++ b/gui/baculum/protected/Init.php @@ -31,11 +31,23 @@ if (!ini_get('date.timezone')) { date_default_timezone_set($timezone); } -// Support for web servers which do not provide direct info about HTTP Basic auth to PHP superglobal $_SERVER array. -if(!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['PHP_AUTH_PW']) && isset($_SERVER['HTTP_AUTHORIZATION'])) { - list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); +/* + * Support for web servers (for example Lighttpd) which do not provide direct + * info about HTTP Basic auth to PHP superglobal $_SERVER array. + */ +if (!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['PHP_AUTH_PW']) && isset($_SERVER['HTTP_AUTHORIZATION'])) { + /* + * Substring 'Basic ' from HTTP authorization header + * Example 'Basic YWRtaW46YWRtaW4=' becomes 'YWRtaW46YWRtaW4=' + */ + $encoded_credentials = substr($_SERVER['HTTP_AUTHORIZATION'], 6); + $decoded_credentials = base64_decode($encoded_credentials); + + // initialize required auth superglobal $_SERVER array + list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $decoded_credentials); } +// Check requirements and if are some needed then show requirements page require_once('./protected/Pages/Requirements.php'); new Requirements(dirname(__DIR__)); diff --git a/gui/baculum/protected/application.xml b/gui/baculum/protected/application.xml index c1aea2b..7f4c774 100644 --- a/gui/baculum/protected/application.xml +++ b/gui/baculum/protected/application.xml @@ -1,9 +1,9 @@ <?xml version="1.0" encoding="utf-8"?> <application id="baculum" mode="Normal"> <paths> - <using namespace="Application.Class.*" /> <using namespace="System.Data.ActiveRecord.*" /> <using namespace="System.I18N.*" /> + <using namespace="Application.Class.*" /> <using namespace="Application.Portlets.BButton" /> <using namespace="Application.Portlets.BActiveButton" /> </paths> diff --git a/gui/baculum/tests/test_common.php b/gui/baculum/tests/test_common.php new file mode 100644 index 0000000..1e813f4 --- /dev/null +++ b/gui/baculum/tests/test_common.php @@ -0,0 +1,74 @@ +<?php +/* + * Bacula(R) - The Network Backup Solution + * Baculum - Bacula web interface + * + * Copyright (C) 2013-2015 Marcin Haba + * + * The main author of Baculum is Marcin Haba. + * The original author of Bacula is Kern Sibbald, with contributions + * from many others, a complete list can be found in the file AUTHORS. + * + * You may use this file and others of this release according to the + * license defined in the LICENSE file, which includes the Affero General + * Public License, v3.0 ("AGPLv3") and some additional permissions and + * terms pursuant to its AGPLv3 Section 7. + * + * This notice must be preserved when any source code is + * conveyed and/or propagated. + * + * Bacula(R) is a registered trademark of Kern Sibbald. + */ + +// Set framework in test run mode +define('PRADO_TEST_RUN', true); + +define('BACULUM_ROOT_DIR', dirname(__FILE__) . '/../'); +define('FRAMEWORK_ROOT_DIR', BACULUM_ROOT_DIR . 'framework'); +define('BACKUP_FILES_PATH', '/tmp/baculum-unittest'); + +set_include_path(dirname(__FILE__) . PATH_SEPARATOR . FRAMEWORK_ROOT_DIR . PATH_SEPARATOR . get_include_path()); + +require_once(FRAMEWORK_ROOT_DIR . '/prado.php'); + +class TApplicationTest extends TApplication { + + public function run() { + $this->initApplication(); + } +} + +function copy_path($source, $destination) { + if (is_dir($source)) { + if (!is_dir($destination)) { + mkdir($destination); + } + $path = dir($source); + while (($file = $path->read()) != false) { + if ($file == '.' || $file == '..') { + continue; + } + $pathDir = $source . '/' . $file; + copy($pathDir, $destination . '/' . $file); + } + $path->close(); + } +} + +function remove_path($path, $only_content = false) { + if(is_dir($path)) { + $dir = dir($path); + while (($file = $dir->read()) != false) { + if ($file == '.' || $file == '..') { + continue; + } + $pathDir = $path . '/' . $file; + unlink($pathDir); + } + if ($only_content === false) { + rmdir(dirname($pathDir)); + } + } +} + +?> diff --git a/gui/baculum/tests/units/Class/APITest.php b/gui/baculum/tests/units/Class/APITest.php new file mode 100644 index 0000000..d8e54bd --- /dev/null +++ b/gui/baculum/tests/units/Class/APITest.php @@ -0,0 +1,87 @@ +<?php +/* + * Bacula(R) - The Network Backup Solution + * Baculum - Bacula web interface + * + * Copyright (C) 2013-2015 Marcin Haba + * + * The main author of Baculum is Marcin Haba. + * The original author of Bacula is Kern Sibbald, with contributions + * from many others, a complete list can be found in the file AUTHORS. + * + * You may use this file and others of this release according to the + * license defined in the LICENSE file, which includes the Affero General + * Public License, v3.0 ("AGPLv3") and some additional permissions and + * terms pursuant to its AGPLv3 Section 7. + * + * This notice must be preserved when any source code is + * conveyed and/or propagated. + * + * Bacula(R) is a registered trademark of Kern Sibbald. + */ + +require_once('test_common.php'); + +/** + * Test cases to internal API client module. + * + * @author Marcin Haba + */ +class APITest extends PHPUnit_Framework_TestCase { + + public static $application = null; + + public function setUp() { + if (self::$application === null) { + self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected'); + self::$application->run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testApiVersion() { + $testData = array('result' => '0.1'); + $apiModule = self::$application->getModule('api'); + $this->assertEquals($testData['result'], $apiModule::API_VERSION); + } + + public function testGetConnection() { + // prepare example config + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'pgsql'; + $testData['db']['name'] = 'my database 123'; + $testData['db']['login'] = 'admin321'; + $testData['db']['password'] = 'Str0NgPa$$w0Rd'; + $testData['db']['ip_addr'] = '127.0.0.1'; + $testData['db']['port'] = '5433'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 1; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'AnOTHe3Str0n6 Pass W OR d'; + $testData['baculum']['debug'] = 0; + $testData['baculum']['lang'] = 'pl'; + + $testResult = array('type' => 'resource'); + + // save example config + self::$application->getModule('configuration')->setApplicationConfig($testData); + + $result = self::$application->getModule('api')->getConnection(); + $this->assertEquals($testResult['type'], gettype($result)); + } +} +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php new file mode 100644 index 0000000..4cea1d4 --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php @@ -0,0 +1,231 @@ +<?php +/* + * Bacula(R) - The Network Backup Solution + * Baculum - Bacula web interface + * + * Copyright (C) 2013-2015 Marcin Haba + * + * The main author of Baculum is Marcin Haba. + * The original author of Bacula is Kern Sibbald, with contributions + * from many others, a complete list can be found in the file AUTHORS. + * + * You may use this file and others of this release according to the + * license defined in the LICENSE file, which includes the Affero General + * Public License, v3.0 ("AGPLv3") and some additional permissions and + * terms pursuant to its AGPLv3 Section 7. + * + * This notice must be preserved when any source code is + * conveyed and/or propagated. + * + * Bacula(R) is a registered trademark of Kern Sibbald. + */ + +require_once('test_common.php'); + +/** + * Test cases to read/write/check Baculum configuration file (settings.conf). + * + * @author Marcin Haba + */ +class ConfigurationManagerApplicationSettingsTest extends PHPUnit_Framework_TestCase { + + public static $application = null; + + public function setUp() { + if (self::$application === null) { + self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected'); + self::$application->run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testSetApplicationConfigPostgreSQL() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'pgsql'; + $testData['db']['name'] = 'my database 123'; + $testData['db']['login'] = 'admin321'; + $testData['db']['password'] = 'Str0NgPa$$w0Rd'; + $testData['db']['ip_addr'] = '127.0.0.1'; + $testData['db']['port'] = '5433'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 1; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'AnOTHe3Str0n6 Pass W OR d'; + $testData['baculum']['debug'] = 0; + $testData['baculum']['lang'] = 'pl'; + $resultContent = '[db] +type = "pgsql" +name = "my database 123" +login = "admin321" +password = "Str0NgPa$$w0Rd" +ip_addr = "127.0.0.1" +port = "5433" +path = "" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/bconsole-{user}.conf" +use_sudo = "1" + +[baculum] +login = "ganiuszka" +password = "AnOTHe3Str0n6 Pass W OR d" +debug = "0" +lang = "pl" + +'; + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } + + public function testSetApplicationConfigMySQL() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'mysql'; + $testData['db']['name'] = 'mydatabase538'; + $testData['db']['login'] = 'administrator'; + $testData['db']['password'] = '#Str0NgPa$##$w0Rd^$#!&#*$@'; + $testData['db']['ip_addr'] = '192.168.0.4'; + $testData['db']['port'] = '3306'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 0; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'Str0n6 Pass4W2OR d'; + $testData['baculum']['debug'] = 1; + $testData['baculum']['lang'] = 'en'; + $resultContent = '[db] +type = "mysql" +name = "mydatabase538" +login = "administrator" +password = "#Str0NgPa$##$w0Rd^$#!&#*$@" +ip_addr = "192.168.0.4" +port = "3306" +path = "" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/bconsole-{user}.conf" +use_sudo = "0" + +[baculum] +login = "ganiuszka" +password = "Str0n6 Pass4W2OR d" +debug = "1" +lang = "en" + +'; + + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } + + public function testSetApplicationConfigSQLite() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'sqlite'; + $testData['db']['name'] = 'bacula'; + $testData['db']['login'] = 'bacula'; + $testData['db']['password'] = ''; + $testData['db']['ip_addr'] = ''; + $testData['db']['port'] = ''; + $testData['db']['path'] = '/home/gani/mydatabase.db'; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/users/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 0; + $testData['baculum']['login'] = 'kekeke'; + $testData['baculum']['password'] = 'kekeke'; + $testData['baculum']['debug'] = 1; + $testData['baculum']['lang'] = 'en'; + $resultContent = '[db] +type = "sqlite" +name = "bacula" +login = "bacula" +password = "" +ip_addr = "" +port = "" +path = "/home/gani/mydatabase.db" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/users/bconsole-{user}.conf" +use_sudo = "0" + +[baculum] +login = "kekeke" +password = "kekeke" +debug = "1" +lang = "en" + +'; + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } +} +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php new file mode 100644 index 0000000..05cece2 --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php @@ -0,0 +1,113 @@ +<?php +/* + * Bacula(R) - The Network Backup Solution + * Baculum - Bacula web interface + * + * Copyright (C) 2013-2015 Marcin Haba + * + * The main author of Baculum is Marcin Haba. + * The original author of Bacula is Kern Sibbald, with contributions + * from many others, a complete list can be found in the file AUTHORS. + * + * You may use this file and others of this release according to the + * license defined in the LICENSE file, which includes the Affero General + * Public License, v3.0 ("AGPLv3") and some additional permissions and + * terms pursuant to its AGPLv3 Section 7. + * + * This notice must be preserved when any source code is + * conveyed and/or propagated. + * + * Bacula(R) is a registered trademark of Kern Sibbald. + */ + +require_once('test_common.php'); + +/** + * Basic Test cases to configuration manager module. + * + * @author Marcin Haba + */ +class ConfigurationManagerTest extends PHPUnit_Framework_TestCase { + + public static $application = null; + + public function setUp() { + if(self::$application === null) { + self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected'); + self::$application->run(); + } + } + + public function testGetDbNameByTypeValid() { + $testData = array( + 'pgsql' => 'PostgreSQL', + 'mysql' => 'MySQL', + 'sqlite' => 'SQLite' + ); + foreach ($testData as $shortName => $longName) { + $result = self::$application->getModule('configuration')->getDbNameByType($shortName); + $this->assertEquals($longName, $result); + } + } + + public function testGetDbNameByTypeInvalid() { + $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'Mysql', 'MYSQL', 'SQlite', 'MySqL', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->getDbNameByType($testData[$i]); + $this->assertNull($result); + } + } + + public function testIsPostgreSQLTypeValid() { + $testData = 'pgsql'; + $result = self::$application->getModule('configuration')->isPostgreSQLType($testData); + $this->assertTrue($result); + } + + public function testIsPostgreSQLTypeInvalid() { + $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'mysql', 'MYSQL', 'sqlite', 'MySqL', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isPostgreSQLType($testData[$i]); + $this->assertFalse($result); + } + } + + public function testIsMySQLTypeValid() { + $testData = 'mysql'; + $result = self::$application->getModule('configuration')->isMySQLType($testData); + $this->assertTrue($result); + } + + public function testIsMySQLTypeInvalid() { + $testData = array("mysql\n", ' mysql', ' mysql ', 'm ysql', 'MYSQ', 'sqlite', 'pgsql', 'mysq', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isMySQLType($testData[$i]); + $this->assertFalse($result); + } + } + public function testIsSQLiteTypeValid() { + $testData = 'sqlite'; + $result = self::$application->getModule('configuration')->isSQLiteType($testData); + $this->assertTrue($result); + } + + public function testIsSQLiteTypeInvalid() { + $testData = array("sqlite\n", ' sqlite', ' sqlite ', 's qlite', 'sqlit', 'pgsql', 'mysqs', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isSQLiteType($testData[$i]); + $this->assertFalse($result); + } + } + + public function testGetDefaultLanguageValid() { + $mockData = false; + $testData = 'en'; + + $mock = $this->getMockBuilder('ConfigurationManager')->setMethods(array('isApplicationConfig'))->getMock(); + $mock->expects($this->once())->method('isApplicationConfig')->will($this->returnValue($mockData)); + $result = $mock->getLanguage(); + $this->assertEquals($testData, $result); + } +} + +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php new file mode 100644 index 0000000..69ac8f8 --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php @@ -0,0 +1,160 @@ +<?php +/* + * Bacula(R) - The Network Backup Solution + * Baculum - Bacula web interface + * + * Copyright (C) 2013-2015 Marcin Haba + * + * The main author of Baculum is Marcin Haba. + * The original author of Bacula is Kern Sibbald, with contributions + * from many others, a complete list can be found in the file AUTHORS. + * + * You may use this file and others of this release according to the + * license defined in the LICENSE file, which includes the Affero General + * Public License, v3.0 ("AGPLv3") and some additional permissions and + * terms pursuant to its AGPLv3 Section 7. + * + * This notice must be preserved when any source code is + * conveyed and/or propagated. + * + * Bacula(R) is a registered trademark of Kern Sibbald. + */ + +require_once('test_common.php'); + +/** + * Test cases to read/write/check Baculum users file (baculum.users). + * + * @author Marcin Haba + */ +class ConfigurationManagerUsersConfigTest extends PHPUnit_Framework_TestCase { + + public static $application = null; + + public function setUp() { + if (self::$application === null) { + self::$application = new TApplicationTest(BACULUM_ROOT_DIR . 'protected'); + self::$application->run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testSetUsersConfig() { + $testData = array(); + $testData[] = array( // create new user admin432 + 'data' => array( + 'user' => 'admin432', + 'password' => 'myStrOnGPa$$!', + 'firstusage' => true, + 'olduser' => null + ), + 'result' => 'admin432:bXOrb7VkxRBYY' + ); + $testData[] = array( // add user willyWonka to config + 'data' => array( + 'user' => 'willyWonka', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "admin432:bXOrb7VkxRBYY\nwillyWonka:IzGy5zT.vOaeI" + ); + $testData[] = array( // clear users config and create user willyWonka again + 'data' => array( + 'user' => 'willyWonka', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => true, + 'olduser' => null + ), + 'result' => "willyWonka:IzGy5zT.vOaeI" + ); + $testData[] = array( // add user donkamillo to config + 'data' => array( + 'user' => 'donkamillo', + 'password' => '!@#$%^&*()_+}{|"\'<>?/.,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "willyWonka:IzGy5zT.vOaeI\ndonkamillo:IU029IHEUYWhY" + ); + $testData[] = array( // rename user willyWonka to user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => false, + 'olduser' => 'willyWonka' + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:IzGy5zT.vOaeI" + ); + $testData[] = array( // change password for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52" + ); + $testData[] = array( // input the same password again for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52" + ); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->setUsersConfig( + $testData[$i]['data']['user'], + $testData[$i]['data']['password'], + $testData[$i]['data']['firstusage'], + $testData[$i]['data']['olduser'] + ); + $this->assertTrue($result); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users'); + $this->assertEquals($testData[$i]['result'], $result); + } + } + + public function testCheckAndClearUsersConfig() { + $testData[] = array( // input the same password again for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "johny11:Q2XfKovUGQe52" + ); + $result = self::$application->getModule('configuration')->isUsersConfig(); + $this->assertFalse($result); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->setUsersConfig( + $testData[$i]['data']['user'], + $testData[$i]['data']['password'], + $testData[$i]['data']['firstusage'], + $testData[$i]['data']['olduser'] + ); + $this->assertTrue($result); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users'); + $this->assertEquals($testData[$i]['result'], $result); + } + $result = self::$application->getModule('configuration')->isUsersConfig(); + $this->assertTrue($result); + $result = self::$application->getModule('configuration')->clearUsersConfig(); + $this->assertTrue($result); + } +} +?> hooks/post-receive -- Bacula Community source |