2013-06-28 19:00:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Tests\Icinga\Authentication;
|
|
|
|
|
|
|
|
//use Icinga\Protocol\Ldap\Exception;
|
|
|
|
//use Zend_Config_Ini;
|
|
|
|
|
2013-07-26 15:01:52 +02:00
|
|
|
require_once('Zend/Config/Ini.php');
|
|
|
|
require_once('Zend/Db.php');
|
|
|
|
require_once('../../library/Icinga/Authentication/UserBackend.php');
|
|
|
|
require_once('../../library/Icinga/Protocol/Ldap/Exception.php');
|
|
|
|
require_once('../../library/Icinga/Application/Config.php');
|
|
|
|
require_once('../../library/Icinga/Authentication/Credentials.php');
|
|
|
|
require_once('../../library/Icinga/Authentication/Backend/DbUserBackend.php');
|
|
|
|
require_once('../../library/Icinga/Authentication/User.php');
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
use Icinga\Authentication\Backend\DbUserBackend;
|
|
|
|
use Icinga\Util\Crypto;
|
|
|
|
use Icinga\Authentication\Credentials;
|
|
|
|
use Icinga\Authentication\User;
|
|
|
|
use Icinga\Application\Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Class fpr DbUserBackend
|
|
|
|
* Created Wed, 17 Jul 2013 11:52:34 +0000
|
|
|
|
*
|
|
|
|
* @package Tests\Icinga\Authentication
|
|
|
|
*/
|
|
|
|
class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
private $dbUserBackend;
|
|
|
|
private $db;
|
2013-07-26 15:01:52 +02:00
|
|
|
private $testTable = 'account';
|
|
|
|
private $testDatabase = 'icinga_unittest';
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be identical with the column names defined in DbUserBackend
|
|
|
|
*/
|
2013-07-26 15:01:52 +02:00
|
|
|
private $USER_NAME_COLUMN = 'user_name',
|
|
|
|
$FIRST_NAME_COLUMN = 'first_name',
|
|
|
|
$LAST_NAME_COLUMN = 'last_name',
|
|
|
|
$LAST_LOGIN_COLUMN = 'last_login',
|
|
|
|
$SALT_COLUMN = 'salt',
|
|
|
|
$PASSWORD_COLUMN = 'password',
|
|
|
|
$ACTIVE_COLUMN = 'active',
|
|
|
|
$DOMAIN_COLUMN = 'domain',
|
|
|
|
$EMAIL_COLUMN = 'email';
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
private $users;
|
2013-07-25 10:05:47 +02:00
|
|
|
private $mysql;
|
|
|
|
private $pgsql;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
private $dbTypeMap = Array(
|
|
|
|
'mysql' => 'PDO_MYSQL',
|
|
|
|
'pgsql' => 'PDO_PGSQL'
|
|
|
|
);
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
|
|
|
* Create a preset-configuration that can be used to access the database
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* with the icinga_unittest account.
|
|
|
|
* @return \stdClass
|
|
|
|
*/
|
|
|
|
private function getBackendConfig()
|
|
|
|
{
|
|
|
|
$config = new \stdClass();
|
2013-07-26 15:01:52 +02:00
|
|
|
$config->host = '127.0.0.1';
|
|
|
|
$config->user = 'icinga_unittest';
|
|
|
|
$config->password= 'icinga_unittest';
|
2013-07-25 10:05:47 +02:00
|
|
|
$config->table = $this->testTable;
|
|
|
|
$config->db = $this->testDatabase;
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Create a backend with the given database type
|
|
|
|
*
|
2013-07-26 15:01:52 +02:00
|
|
|
* @param $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
2013-07-25 10:05:47 +02:00
|
|
|
* @return DbUserBackend|null
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function createBackend($dbType)
|
|
|
|
{
|
|
|
|
try {
|
2013-07-25 10:05:47 +02:00
|
|
|
$config = $this->getBackendConfig();
|
|
|
|
$config->dbtype = $dbType;
|
|
|
|
$db = $this->createDb($dbType,$config);
|
|
|
|
$this->setUpDb($db);
|
|
|
|
return new DbUserBackend($config);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) {
|
2013-07-26 15:01:52 +02:00
|
|
|
echo 'CREATE_BACKEND_ERROR:'.$e->getMessage();
|
2013-07-25 10:05:47 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the backends and fill it with sample-data.
|
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->users = Array(
|
|
|
|
0 => Array(
|
|
|
|
$this->USER_NAME_COLUMN => 'user1',
|
|
|
|
$this->PASSWORD_COLUMN => 'secret1',
|
|
|
|
$this->SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->ACTIVE_COLUMN => 1
|
2013-06-28 19:00:30 +02:00
|
|
|
),
|
|
|
|
1 => Array(
|
|
|
|
$this->USER_NAME_COLUMN => 'user2',
|
|
|
|
$this->PASSWORD_COLUMN => 'secret2',
|
|
|
|
$this->SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->ACTIVE_COLUMN => 1
|
2013-06-28 19:00:30 +02:00
|
|
|
),
|
|
|
|
2 => Array(
|
|
|
|
$this->USER_NAME_COLUMN => 'user3',
|
|
|
|
$this->PASSWORD_COLUMN => 'secret3',
|
|
|
|
$this->SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->ACTIVE_COLUMN => 0
|
2013-06-28 19:00:30 +02:00
|
|
|
)
|
|
|
|
);
|
2013-07-26 15:01:52 +02:00
|
|
|
$this->mysql = $this->createBackend('mysql');
|
|
|
|
$this->pgsql = $this->createBackend('pgsql');
|
2013-07-25 10:05:47 +02:00
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
|
|
|
* Test the PostgreSQL backend.
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
public function testPgsql()
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
if(!empty($this->pgsql)){
|
|
|
|
$this->runBackendAuthentication($this->pgsql);
|
|
|
|
$this->runBackendUsername($this->pgsql);
|
|
|
|
}
|
|
|
|
else{
|
2013-07-26 15:01:52 +02:00
|
|
|
echo '\nSKIPPING PGSQL TEST...\n';
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the MySQL-Backend.
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
public function testMySQL()
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
if(!empty($this->mysql)){
|
|
|
|
$this->runBackendAuthentication($this->mysql);
|
|
|
|
$this->runBackendUsername($this->mysql);
|
|
|
|
}
|
|
|
|
else{
|
2013-07-26 15:01:52 +02:00
|
|
|
echo '\nSKIPPING MYSQL TEST...\n';
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Create a database with the given config and type
|
|
|
|
*
|
2013-07-26 15:01:52 +02:00
|
|
|
* @param $dbtype The database type as a string, like 'mysql' or 'pgsql'.
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $config The configuration-object.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function createDb($dbtype,$config)
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
return \Zend_Db::factory($this->dbTypeMap[$dbtype],
|
2013-06-28 19:00:30 +02:00
|
|
|
array(
|
|
|
|
'host' => $config->host,
|
|
|
|
'username' => $config->user,
|
|
|
|
'password' => $config->password,
|
2013-07-26 15:01:52 +02:00
|
|
|
'dbname' => 'icinga_unittest'
|
2013-06-28 19:00:30 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Try to drop all databases that may eventually be present
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
public function tearDown()
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
try{
|
2013-07-26 15:01:52 +02:00
|
|
|
$db = $this->createDb('mysql',$this->getBackendConfig());
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->tearDownDb($db);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) { }
|
|
|
|
try {
|
2013-07-26 15:01:52 +02:00
|
|
|
$db = $this->createDb('pgsql',$this->getBackendConfig());
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->tearDownDb($db);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) { }
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Drop the test database in the given db
|
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $db
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function tearDownDb($db)
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
$db->exec('DROP TABLE '.$this->testTable);
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Fill the given database with the sample-data provided in users
|
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $db
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function setUpDb($db)
|
|
|
|
{
|
2013-07-26 15:01:52 +02:00
|
|
|
try {
|
|
|
|
$this->tearDownDb($db);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// if no database exists, an exception will be thrown
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
$db->exec('CREATE TABLE '.$this->testTable.' (
|
2013-07-25 10:05:47 +02:00
|
|
|
'.$this->USER_NAME_COLUMN.' varchar(255) NOT NULL,
|
|
|
|
'.$this->FIRST_NAME_COLUMN.' varchar(255),
|
|
|
|
'.$this->LAST_NAME_COLUMN.' varchar(255),
|
|
|
|
'.$this->LAST_LOGIN_COLUMN.' timestamp,
|
|
|
|
'.$this->SALT_COLUMN.' varchar(255),
|
|
|
|
'.$this->DOMAIN_COLUMN.' varchar(255),
|
|
|
|
'.$this->EMAIL_COLUMN.' varchar(255),
|
|
|
|
'.$this->PASSWORD_COLUMN.' varchar(255) NOT NULL,
|
|
|
|
'.$this->ACTIVE_COLUMN.' BOOL,
|
|
|
|
PRIMARY KEY ('.$this->USER_NAME_COLUMN.')
|
|
|
|
)');
|
2013-07-25 16:47:43 +02:00
|
|
|
for ($i = 0; $i < count($this->users); $i++) {
|
2013-06-28 19:00:30 +02:00
|
|
|
$usr = $this->users[$i];
|
|
|
|
$data = Array(
|
|
|
|
$this->USER_NAME_COLUMN => $usr[$this->USER_NAME_COLUMN],
|
2013-07-26 15:01:52 +02:00
|
|
|
$this->PASSWORD_COLUMN => hash_hmac('sha256',
|
2013-07-24 10:10:43 +02:00
|
|
|
$usr[$this->SALT_COLUMN],
|
|
|
|
$usr[$this->PASSWORD_COLUMN]
|
|
|
|
),
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->ACTIVE_COLUMN => $usr[$this->ACTIVE_COLUMN],
|
|
|
|
$this->SALT_COLUMN => $usr[$this->SALT_COLUMN]
|
|
|
|
);
|
|
|
|
$db->insert($this->testTable,$data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Run the hasUsername test against an instance of DbUserBackend
|
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $backend The backend that will be tested.
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function runBackendUsername($backend)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
// Known user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertTrue($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
|
|
|
$this->users[0][$this->USER_NAME_COLUMN],
|
|
|
|
$this->users[0][$this->PASSWORD_COLUMN])
|
|
|
|
));
|
|
|
|
|
|
|
|
// Unknown user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertFalse($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
|
|
|
'unkown user',
|
|
|
|
'secret')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Inactive user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertFalse($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
|
|
|
$this->users[2][$this->USER_NAME_COLUMN],
|
|
|
|
$this->users[2][$this->PASSWORD_COLUMN])
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Run the authentication test against an instance of DbUserBackend
|
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $backend The backend that will be tested.
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function runBackendAuthentication($backend)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
// Known user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertNotNull($backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
|
|
|
$this->users[0][$this->USER_NAME_COLUMN],
|
|
|
|
$this->users[0][$this->PASSWORD_COLUMN])
|
|
|
|
));
|
|
|
|
|
|
|
|
// Wrong password
|
|
|
|
$this->assertNull(
|
2013-07-25 10:05:47 +02:00
|
|
|
$backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
|
|
|
$this->users[1][$this->USER_NAME_COLUMN],
|
|
|
|
'wrongpassword')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
// Nonexisting user
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->assertNull(
|
2013-07-25 10:05:47 +02:00
|
|
|
$backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-07-25 10:05:47 +02:00
|
|
|
'nonexisting user',
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->users[1][$this->PASSWORD_COLUMN])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Inactive user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertNull(
|
|
|
|
$backend->authenticate(
|
|
|
|
new Credentials(
|
|
|
|
$this->users[2][$this->USER_NAME_COLUMN],
|
|
|
|
$this->users[2][$this->PASSWORD_COLUMN])
|
2013-06-28 19:00:30 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|