2015-08-07 12:55:19 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
|
2015-08-07 12:55:19 +02:00
|
|
|
|
|
|
|
namespace Tests\Icinga\Config;
|
|
|
|
|
|
|
|
use Icinga\File\Ini\IniWriter;
|
|
|
|
use Icinga\Test\BaseTestCase;
|
|
|
|
use Icinga\Application\Config;
|
|
|
|
use Icinga\File\Ini\IniParser;
|
|
|
|
|
|
|
|
class IniParserTest extends BaseTestCase
|
|
|
|
{
|
|
|
|
protected $tempFile;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->tempFile = tempnam(sys_get_temp_dir(), 'icinga-ini-parser-test');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
unlink($this->tempFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSectionNameEscaping()
|
|
|
|
{
|
|
|
|
$config = <<<'EOD'
|
|
|
|
[title with \"quote]
|
2019-02-01 10:42:58 +01:00
|
|
|
|
|
|
|
[title with \;semicolon]
|
|
|
|
|
|
|
|
[title with \\backslash]
|
2015-08-07 12:55:19 +02:00
|
|
|
EOD;
|
2019-02-01 10:42:58 +01:00
|
|
|
|
2015-08-07 12:55:19 +02:00
|
|
|
$doc = IniParser::parseIni($config);
|
|
|
|
$this->assertTrue(
|
2019-02-01 10:42:58 +01:00
|
|
|
$doc->hasSection('title with "quote'),
|
|
|
|
'IniParser::parseIni does not recognize escaped quotes in section names'
|
2015-08-07 12:55:19 +02:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2019-02-01 10:42:58 +01:00
|
|
|
$doc->hasSection('title with ;semicolon'),
|
|
|
|
'IniParser::parseIni does not recognize escaped semicolons in section names'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$doc->hasSection('title with \\backslash'),
|
|
|
|
'IniParser::parseIni does not recognize escaped backslashes in section names'
|
|
|
|
);
|
|
|
|
|
|
|
|
(new IniWriter(Config::fromArray([
|
|
|
|
'title with "quote' => [],
|
|
|
|
'title with ;semicolon' => [],
|
|
|
|
'title with \\backslash' => []
|
|
|
|
]), $this->tempFile))->write();
|
|
|
|
|
|
|
|
$configObject = IniParser::parseIniFile($this->tempFile);
|
|
|
|
$this->assertTrue(
|
|
|
|
$configObject->hasSection('title with "quote'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped quotes in section names'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$configObject->hasSection('title with ;semicolon'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped semicolons in section names'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$configObject->hasSection('title with \\backslash'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped backslashes in section names'
|
2015-08-07 12:55:19 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDirectiveValueEscaping()
|
|
|
|
{
|
|
|
|
$config = <<<'EOD'
|
|
|
|
[section]
|
|
|
|
key1 = "key with escaped \"quote"
|
2019-02-01 10:42:58 +01:00
|
|
|
key2 = "key with escaped backslash \\"
|
|
|
|
key3 = "key with escaped backslash followed by quote \\\""
|
2015-08-07 12:55:19 +02:00
|
|
|
EOD;
|
2019-02-01 10:42:58 +01:00
|
|
|
|
2015-08-07 12:55:19 +02:00
|
|
|
$doc = IniParser::parseIni($config);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped "quote',
|
|
|
|
$doc->getSection('section')->getDirective('key1')->getValue(),
|
2019-02-01 10:42:58 +01:00
|
|
|
'IniParser::parseIni does not recognize escaped quotes in values'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped backslash \\',
|
|
|
|
$doc->getSection('section')->getDirective('key2')->getValue(),
|
|
|
|
'IniParser::parseIni does not recognize escaped backslashes in values'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped backslash followed by quote \\"',
|
|
|
|
$doc->getSection('section')->getDirective('key3')->getValue(),
|
|
|
|
'IniParser::parseIni does not recognize escaped backslashes followed by quotes in values'
|
|
|
|
);
|
|
|
|
|
|
|
|
(new IniWriter(Config::fromArray([
|
|
|
|
'section' => [
|
|
|
|
'key1' => 'key with escaped "quote',
|
|
|
|
'key2' => 'key with escaped backslash \\',
|
|
|
|
'key3' => 'key with escaped backslash followed by quote \\"'
|
|
|
|
]
|
|
|
|
]), $this->tempFile))->write();
|
|
|
|
|
|
|
|
$configObject = IniParser::parseIniFile($this->tempFile);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped "quote',
|
|
|
|
$configObject->getSection('section')->get('key1'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped quotes in values'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped backslash \\',
|
|
|
|
$configObject->getSection('section')->get('key2'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped backslashes in values'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'key with escaped backslash followed by quote \\"',
|
|
|
|
$configObject->getSection('section')->get('key3'),
|
|
|
|
'IniParser::parseIniFile does not recognize escaped backslashes followed by quotes in values'
|
2015-08-07 12:55:19 +02:00
|
|
|
);
|
|
|
|
}
|
2015-08-10 14:49:27 +02:00
|
|
|
|
|
|
|
public function testMultilineValues()
|
|
|
|
{
|
|
|
|
$config = <<<'EOD'
|
|
|
|
[section]
|
|
|
|
key1 = "with some
|
|
|
|
newline in the value"
|
|
|
|
EOD;
|
|
|
|
$doc = IniParser::parseIni($config);
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
|
|
|
count(explode("\n", $doc->getSection('section')->getDirective('key1')->getValue())),
|
2019-03-04 12:16:55 +01:00
|
|
|
'IniParser::parseIni does not recognize multi-line values'
|
|
|
|
);
|
|
|
|
|
|
|
|
(new IniWriter(Config::fromArray([
|
|
|
|
'section' => [
|
|
|
|
'key1' => "with some\nnewline in the value"
|
|
|
|
]
|
|
|
|
]), $this->tempFile))->write();
|
|
|
|
|
|
|
|
$doc = IniParser::parseIniFile($this->tempFile);
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
|
|
|
count(explode("\n", $doc->getSection('section')->get('key1'))),
|
|
|
|
'IniParser::parseIniFile does not recognize multi-line values'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvironmentVariableResolutionDoesNotWork()
|
|
|
|
{
|
|
|
|
(new IniWriter(Config::fromArray([
|
|
|
|
'section' => [
|
|
|
|
'key1' => '${PATH}_${APACHE_RUN_DIR}',
|
|
|
|
'key2' => '${APACHE_RUN_USER}'
|
|
|
|
]
|
|
|
|
]), $this->tempFile))->write();
|
|
|
|
|
|
|
|
$configObject = IniParser::parseIniFile($this->tempFile);
|
|
|
|
$this->assertEquals(
|
|
|
|
'${PATH}_${APACHE_RUN_DIR}',
|
|
|
|
$configObject->getSection('section')->get('key1'),
|
|
|
|
'IniParser::parseIniFile does resolve environment variables'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'${APACHE_RUN_USER}',
|
|
|
|
$configObject->getSection('section')->get('key2'),
|
|
|
|
'IniParser::parseIniFile does resolve environment variables'
|
2015-08-10 14:49:27 +02:00
|
|
|
);
|
|
|
|
}
|
2015-08-07 12:55:19 +02:00
|
|
|
}
|