[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaConfigHelper: fix numeric dictionary key...

git at icinga.org git at icinga.org
Tue Aug 30 14:16:30 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 6427b226219a2d76e31087ad00cc6123979ffb6a
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=6427b226219a2d76e31087ad00cc6123979ffb6a

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Aug 30 12:15:48 2016 +0000

IcingaConfigHelper: fix numeric dictionary key...

...rendering and add related tests

fixes #12591

---

 .../Director/IcingaConfig/IcingaConfigHelper.php   |    4 +--
 .../IcingaConfig/IcingaConfigHelperTest.php        |   28 ++++++++++++++++++++
 .../Director/IcingaConfig/rendered/dict1.out       |    6 +++++
 3 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php
index ac33cb9..6d5610b 100644
--- a/library/Director/IcingaConfig/IcingaConfigHelper.php
+++ b/library/Director/IcingaConfig/IcingaConfigHelper.php
@@ -105,7 +105,7 @@ class IcingaConfigHelper
 
     public static function renderDictionaryKey($key)
     {
-        if (preg_match('/^[a-z0-9_]+\d*$/i', $key)) {
+        if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $key)) {
             return static::escapeIfReserved($key);
         } else {
             return static::renderString($key);
@@ -168,7 +168,7 @@ class IcingaConfigHelper
 
     public static function isReserved($string)
     {
-        return in_array($string, self::$reservedWords);
+        return in_array($string, self::$reservedWords, true);
     }
 
     public static function escapeIfReserved($string)
diff --git a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
index 7bde0bd..c73b5bb 100644
--- a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
+++ b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
@@ -41,4 +41,32 @@ class IcingaConfigHelperTest extends BaseTestCase
         $this->assertEquals(c::renderInterval(86400), '1d');
         $this->assertEquals(c::renderInterval(86459), '86459s');
     }
+
+    public function testCorrectlyIdentifiesReservedWords()
+    {
+        $this->assertTrue(c::isReserved('include'), 'include is a reserved word');
+        $this->assertFalse(c::isReserved(0), '(int) 0 is not a reserved word');
+        $this->assertFalse(c::isReserved(1), '(int) 1 is not a reserved word');
+        $this->assertFalse(c::isReserved(true), '(boolean) true is not a reserved word');
+        $this->assertTrue(c::isReserved('true'), '(string) true is a reserved word');
+    }
+
+    public function testWhetherDictionaryRendersCorrectly()
+    {
+        $dict = (object) array(
+            'key1'     => 'bla',
+            'include'  => 'reserved',
+            'spe cial' => 'value',
+            '0'        => 'numeric',
+        );
+        $this->assertEquals(
+            c::renderDictionary($dict),
+            rtrim($this->loadRendered('dict1'))
+        );
+    }
+
+    protected function loadRendered($name)
+    {
+        return file_get_contents(__DIR__ . '/rendered/' . $name . '.out');
+    }
 }
diff --git a/test/php/library/Director/IcingaConfig/rendered/dict1.out b/test/php/library/Director/IcingaConfig/rendered/dict1.out
new file mode 100644
index 0000000..ff6f022
--- /dev/null
+++ b/test/php/library/Director/IcingaConfig/rendered/dict1.out
@@ -0,0 +1,6 @@
+{
+    @include = reserved
+    key1 = bla
+    "spe cial" = value
+    "0" = numeric
+}



More information about the icinga-checkins mailing list