IcingaCommand: fix windows absolute path detection

Also add a couple of new related tests

fixes #11550
This commit is contained in:
Thomas Gelf 2016-05-13 16:01:47 +02:00
parent 3b1145284b
commit 4222261434
8 changed files with 83 additions and 1 deletions

View File

@ -87,7 +87,7 @@ class IcingaCommand extends IcingaObject
if (preg_match('~^([A-Z][A-Za-z0-9_]+\s\+\s)(.+?)$~', $command, $m)) {
$prefix = $m[1];
$command = $m[2];
} elseif ($command[0] !== '/') {
} elseif (! $this->isAbsolutePath($command)) {
$prefix = 'PluginDir + ';
$command = '/' . $command;
}
@ -97,6 +97,13 @@ class IcingaCommand extends IcingaObject
return c::renderKeyValue('command', c::renderArray($parts));
}
protected function isAbsolutePath($path)
{
return $path[0] === '/'
|| $path[0] === '\\'
|| preg_match('/^[A-Z]:\\\/', substr($path, 0, 3));
}
public static function setPluginDir($pluginDir)
{
self::$pluginDir = $pluginDir;

View File

@ -58,6 +58,52 @@ class IcingaCommandTest extends BaseTestCase
);
}
public function testAbsolutePathsAreDetected()
{
$command = $this->command();
$command->command = 'C:\\test.exe';
$this->assertEquals(
$this->loadRendered('command1'),
(string) $command
);
$command->command = '/tmp/bla';
$this->assertEquals(
$this->loadRendered('command2'),
(string) $command
);
$command->command = 'tmp/bla';
$this->assertEquals(
$this->loadRendered('command3'),
(string) $command
);
$command->command = '\\\\network\\share\\bla.exe';
$this->assertEquals(
$this->loadRendered('command4'),
(string) $command
);
$command->command = 'BlahDir + \\network\\share\\bla.exe';
$this->assertEquals(
$this->loadRendered('command5'),
(string) $command
);
$command->command = 'network\\share\\bla.exe';
$this->assertEquals(
$this->loadRendered('command6'),
(string) $command
);
}
protected function command()
{
return IcingaCommand::create(
@ -68,6 +114,11 @@ class IcingaCommandTest extends BaseTestCase
);
}
protected function loadRendered($name)
{
return file_get_contents(__DIR__ . '/rendered/' . $name . '.out');
}
public function tearDown()
{
$db = $this->getDb();

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ "C:\\test.exe" ]
}

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ "/tmp/bla" ]
}

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ PluginDir + "/tmp/bla" ]
}

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ "\\\\network\\share\\bla.exe" ]
}

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ BlahDir + "\\network\\share\\bla.exe" ]
}

View File

@ -0,0 +1,4 @@
object CheckCommand "___TEST___command" {
command = [ PluginDir + "/network\\share\\bla.exe" ]
}