From 422226143441c5a2c7a3a3be5a4eae2a96eb0aa5 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 13 May 2016 16:01:47 +0200 Subject: [PATCH] IcingaCommand: fix windows absolute path detection Also add a couple of new related tests fixes #11550 --- library/Director/Objects/IcingaCommand.php | 9 +++- .../Director/Objects/IcingaCommandTest.php | 51 +++++++++++++++++++ .../Director/Objects/rendered/command1.out | 4 ++ .../Director/Objects/rendered/command2.out | 4 ++ .../Director/Objects/rendered/command3.out | 4 ++ .../Director/Objects/rendered/command4.out | 4 ++ .../Director/Objects/rendered/command5.out | 4 ++ .../Director/Objects/rendered/command6.out | 4 ++ 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 test/php/library/Director/Objects/rendered/command1.out create mode 100644 test/php/library/Director/Objects/rendered/command2.out create mode 100644 test/php/library/Director/Objects/rendered/command3.out create mode 100644 test/php/library/Director/Objects/rendered/command4.out create mode 100644 test/php/library/Director/Objects/rendered/command5.out create mode 100644 test/php/library/Director/Objects/rendered/command6.out diff --git a/library/Director/Objects/IcingaCommand.php b/library/Director/Objects/IcingaCommand.php index 1e48a3ab..f2206264 100644 --- a/library/Director/Objects/IcingaCommand.php +++ b/library/Director/Objects/IcingaCommand.php @@ -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; diff --git a/test/php/library/Director/Objects/IcingaCommandTest.php b/test/php/library/Director/Objects/IcingaCommandTest.php index a20d3923..992b7672 100644 --- a/test/php/library/Director/Objects/IcingaCommandTest.php +++ b/test/php/library/Director/Objects/IcingaCommandTest.php @@ -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(); diff --git a/test/php/library/Director/Objects/rendered/command1.out b/test/php/library/Director/Objects/rendered/command1.out new file mode 100644 index 00000000..12e156f8 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command1.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ "C:\\test.exe" ] +} + diff --git a/test/php/library/Director/Objects/rendered/command2.out b/test/php/library/Director/Objects/rendered/command2.out new file mode 100644 index 00000000..e853285f --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command2.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ "/tmp/bla" ] +} + diff --git a/test/php/library/Director/Objects/rendered/command3.out b/test/php/library/Director/Objects/rendered/command3.out new file mode 100644 index 00000000..7e7eef93 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command3.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ PluginDir + "/tmp/bla" ] +} + diff --git a/test/php/library/Director/Objects/rendered/command4.out b/test/php/library/Director/Objects/rendered/command4.out new file mode 100644 index 00000000..3dc7ac5d --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command4.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ "\\\\network\\share\\bla.exe" ] +} + diff --git a/test/php/library/Director/Objects/rendered/command5.out b/test/php/library/Director/Objects/rendered/command5.out new file mode 100644 index 00000000..1e575771 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command5.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ BlahDir + "\\network\\share\\bla.exe" ] +} + diff --git a/test/php/library/Director/Objects/rendered/command6.out b/test/php/library/Director/Objects/rendered/command6.out new file mode 100644 index 00000000..3f123ce6 --- /dev/null +++ b/test/php/library/Director/Objects/rendered/command6.out @@ -0,0 +1,4 @@ +object CheckCommand "___TEST___command" { + command = [ PluginDir + "/network\\share\\bla.exe" ] +} +