2015-10-22 14:51:01 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Configuration;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2020-10-10 06:08:58 -04:00
|
|
|
class ConfigurationTest extends TestCase
|
2015-10-22 14:51:01 -04:00
|
|
|
{
|
2019-12-25 01:58:14 -05:00
|
|
|
private $_minimalConfig;
|
|
|
|
|
2016-07-11 08:15:20 -04:00
|
|
|
private $_options;
|
2015-10-22 14:51:01 -04:00
|
|
|
|
2019-12-25 01:58:14 -05:00
|
|
|
private $_path;
|
2016-07-13 03:41:45 -04:00
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function setUp(): void
|
2015-10-22 14:51:01 -04:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::confBackup();
|
2019-12-25 01:58:14 -05:00
|
|
|
$this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
|
2017-10-04 14:31:37 -04:00
|
|
|
$this->_options = Configuration::getDefaults();
|
2016-07-11 08:15:20 -04:00
|
|
|
$this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
|
2016-10-29 04:24:08 -04:00
|
|
|
$this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
|
|
|
|
$this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
|
2019-12-25 01:58:14 -05:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_cfg';
|
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
2015-10-22 14:51:01 -04:00
|
|
|
}
|
|
|
|
|
2020-10-10 06:22:20 -04:00
|
|
|
public function tearDown(): void
|
2015-10-22 14:51:01 -04:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2019-12-25 01:58:14 -05:00
|
|
|
Helper::rmDir($this->_path);
|
2017-10-08 05:03:17 -04:00
|
|
|
if (is_file(CONF)) {
|
|
|
|
unlink(CONF);
|
|
|
|
}
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::confRestore();
|
2015-10-22 14:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultConfigFile()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleFreshConfigFile()
|
|
|
|
{
|
2016-08-22 10:20:14 -04:00
|
|
|
Helper::createIniFile(CONF, $this->_options);
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:13:15 -04:00
|
|
|
public function testHandleMissingConfigFile()
|
|
|
|
{
|
2017-10-08 05:03:17 -04:00
|
|
|
if (is_file(CONF)) {
|
|
|
|
unlink(CONF);
|
|
|
|
}
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 15:13:15 -04:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
|
|
|
|
}
|
|
|
|
|
2015-10-22 14:51:01 -04:00
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 2
|
|
|
|
*/
|
|
|
|
public function testHandleBlankConfigFile()
|
|
|
|
{
|
|
|
|
file_put_contents(CONF, '');
|
2016-08-09 05:54:42 -04:00
|
|
|
new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleMinimalConfigFile()
|
|
|
|
{
|
2016-07-13 03:41:45 -04:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testHandleInvalidSection()
|
|
|
|
{
|
2016-07-13 03:41:45 -04:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$conf->getKey('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 4
|
|
|
|
*/
|
|
|
|
public function testHandleInvalidKey()
|
|
|
|
{
|
2016-07-13 03:41:45 -04:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$conf->getKey('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleGetKey()
|
|
|
|
{
|
2016-07-13 03:41:45 -04:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-22 14:51:01 -04:00
|
|
|
$this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleWrongTypes()
|
|
|
|
{
|
2016-10-29 04:24:08 -04:00
|
|
|
$original_options = $this->_options;
|
2015-10-24 02:44:17 -04:00
|
|
|
$original_options['main']['syntaxhighlightingtheme'] = 'foo';
|
2016-10-29 04:24:08 -04:00
|
|
|
$options = $original_options;
|
|
|
|
$options['main']['discussion'] = 'true';
|
|
|
|
$options['main']['opendiscussion'] = 0;
|
|
|
|
$options['main']['password'] = -1; // evaluates to TRUE
|
|
|
|
$options['main']['fileupload'] = 'false';
|
|
|
|
$options['expire_options']['foo'] = 'bar';
|
|
|
|
$options['formatter_options'][] = 'foo';
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::createIniFile(CONF, $options);
|
2016-10-29 04:24:08 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-24 02:44:17 -04:00
|
|
|
$original_options['expire_options']['foo'] = intval('bar');
|
2016-10-29 04:24:08 -04:00
|
|
|
$original_options['formatter_options'][0] = 'foo';
|
2015-10-24 02:44:17 -04:00
|
|
|
$this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleMissingSubKeys()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['expire_options']['1week']);
|
|
|
|
unset($options['expire_options']['1year']);
|
|
|
|
unset($options['expire_options']['never']);
|
2016-08-22 10:20:14 -04:00
|
|
|
Helper::createIniFile(CONF, $options);
|
2016-10-29 04:24:08 -04:00
|
|
|
$conf = new Configuration;
|
2015-10-24 02:44:17 -04:00
|
|
|
$options['expire']['default'] = '5min';
|
|
|
|
$this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
|
2015-10-22 14:51:01 -04:00
|
|
|
}
|
2015-10-24 02:44:17 -04:00
|
|
|
|
2016-07-13 03:41:45 -04:00
|
|
|
public function testHandlePreRenameConfig()
|
|
|
|
{
|
2016-10-29 04:24:08 -04:00
|
|
|
$options = $this->_options;
|
2016-07-13 03:41:45 -04:00
|
|
|
$options['model']['class'] = 'zerobin_data';
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
|
2016-07-13 03:41:45 -04:00
|
|
|
|
|
|
|
$options['model']['class'] = 'zerobin_db';
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
2017-10-04 14:31:37 -04:00
|
|
|
|
|
|
|
public function testHandleConfigFileRename()
|
|
|
|
{
|
2017-11-13 16:05:29 -05:00
|
|
|
$options = $this->_options;
|
2017-10-04 14:31:37 -04:00
|
|
|
Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', $options);
|
|
|
|
|
|
|
|
$options['main']['opendiscussion'] = true;
|
|
|
|
$options['main']['fileupload'] = true;
|
|
|
|
$options['main']['template'] = 'darkstrap';
|
|
|
|
Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $options);
|
|
|
|
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertFileExists(CONF, 'old configuration file gets converted');
|
|
|
|
$this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
|
|
|
|
$this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', 'old configuration sample file gets removed');
|
|
|
|
$this->assertTrue(
|
|
|
|
$conf->getKey('opendiscussion') &&
|
|
|
|
$conf->getKey('fileupload') &&
|
|
|
|
$conf->getKey('template') === 'darkstrap',
|
|
|
|
'configuration values get converted'
|
|
|
|
);
|
|
|
|
}
|
2017-10-04 15:55:03 -04:00
|
|
|
|
|
|
|
public function testRenameIniSample()
|
|
|
|
{
|
|
|
|
$iniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
|
|
|
|
|
|
|
|
Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $this->_options);
|
|
|
|
if (is_file(CONF)) {
|
|
|
|
unlink(CONF);
|
|
|
|
}
|
2017-10-08 05:03:17 -04:00
|
|
|
rename(CONF_SAMPLE, $iniSample);
|
2017-10-04 15:55:03 -04:00
|
|
|
new Configuration;
|
|
|
|
$this->assertFileNotExists($iniSample, 'old sample file gets removed');
|
2017-10-08 05:03:17 -04:00
|
|
|
$this->assertFileExists(CONF_SAMPLE, 'new sample file gets created');
|
|
|
|
$this->assertFileExists(CONF, 'old configuration file gets converted');
|
|
|
|
$this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
|
2017-10-04 15:55:03 -04:00
|
|
|
}
|
2019-12-25 01:58:14 -05:00
|
|
|
|
|
|
|
public function testConfigPath()
|
|
|
|
{
|
|
|
|
// setup
|
|
|
|
$configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
|
|
|
|
$options = $this->_options;
|
|
|
|
$options['main']['name'] = 'OtherBin';
|
|
|
|
Helper::createIniFile($configFile, $options);
|
|
|
|
|
|
|
|
// test
|
|
|
|
putenv('CONFIG_PATH=' . $this->_path);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported');
|
|
|
|
|
|
|
|
// cleanup environment
|
|
|
|
if (is_file($configFile)) {
|
|
|
|
unlink($configFile);
|
|
|
|
}
|
|
|
|
putenv('CONFIG_PATH');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigPathIni()
|
|
|
|
{
|
|
|
|
// setup
|
|
|
|
$configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.ini';
|
|
|
|
$configMigrated = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
|
|
|
|
$options = $this->_options;
|
|
|
|
$options['main']['name'] = 'OtherBin';
|
|
|
|
Helper::createIniFile($configFile, $options);
|
|
|
|
$this->assertFileNotExists(CONF, 'configuration in the default location is non existing');
|
|
|
|
|
|
|
|
// test
|
|
|
|
putenv('CONFIG_PATH=' . $this->_path);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported for ini files as well');
|
|
|
|
$this->assertFileExists($configMigrated, 'old configuration file gets converted');
|
|
|
|
$this->assertFileNotExists($configFile, 'old configuration file gets removed');
|
|
|
|
$this->assertFileNotExists(CONF, 'configuration is not created in the default location');
|
|
|
|
|
|
|
|
// cleanup environment
|
|
|
|
if (is_file($configFile)) {
|
|
|
|
unlink($configFile);
|
|
|
|
}
|
|
|
|
putenv('CONFIG_PATH');
|
|
|
|
}
|
2015-10-22 14:51:01 -04:00
|
|
|
}
|