[asterisk-commits] AMI: Escape string values test. (testsuite[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jun 8 13:16:26 CDT 2015


Matt Jordan has submitted this change and it was merged.

Change subject: AMI: Escape string values test.
......................................................................


AMI: Escape string values test.

Test to make sure 'C' sequences like '\r\n' are properly escaped for some
AMI values.

ASTERISK-24934 #close
Reported by: warren smith

Change-Id: I010deba4a0892792564ec49bdae1f3dd34c7decf
---
M lib/python/asterisk/ami.py
M sample-yaml/ami-config.yaml.sample
A tests/manager/escaped_values/test-config.yaml
M tests/manager/tests.yaml
4 files changed, 67 insertions(+), 8 deletions(-)

Approvals:
  Samuel Galarneau: Looks good to me, but someone else must approve
  Matt Jordan: Looks good to me, approved; Verified



diff --git a/lib/python/asterisk/ami.py b/lib/python/asterisk/ami.py
index 365d206..f3a8e00 100644
--- a/lib/python/asterisk/ami.py
+++ b/lib/python/asterisk/ami.py
@@ -46,6 +46,12 @@
         self._event_observers = []
         self.count = {}
 
+        if instance_config.get('escape-pattern', True):
+            self.match = (lambda pattern, s, flags=0:
+                          re.match(re.escape(pattern), s, flags))
+        else:
+            self.match = (re.match)
+
         if 'count' in instance_config:
             count = instance_config['count']
             if isinstance(count, int):
@@ -134,7 +140,7 @@
             if key.lower() not in event:
                 LOGGER.debug("Condition %s not in event, returning", key)
                 return
-            if not re.match(value, event.get(key.lower())):
+            if not self.match(value, event.get(key.lower())):
                 LOGGER.debug("Condition %s: %s does not match %s: %s in event",
                              key, value, key, event.get(key.lower()))
                 return
@@ -146,7 +152,7 @@
             if key.lower() not in event:
                 LOGGER.debug("Condition %s not in event, returning", key)
                 return
-            if re.match(value, event.get(key.lower())):
+            if self.match(value, event.get(key.lower())):
                 LOGGER.debug("Condition %s: %s matches %s: %s in event",
                              key, value, key, event.get(key.lower()))
                 return
@@ -222,7 +228,7 @@
                 LOGGER.warning("Requirement %s does not exist in event %s",
                                key, event['event'])
                 self.passed = False
-            elif not re.match(value, event.get(key.lower())):
+            elif not self.match(value, event.get(key.lower())):
                 LOGGER.warning("Requirement %s: %s does not match %s: %s in "
                                "event", key, value, key,
                                event.get(key.lower(), ''))
@@ -236,7 +242,7 @@
                 LOGGER.warning("Requirement %s does not exist in event %s",
                                key, event['event'])
                 self.passed = False
-            elif re.match(value, event.get(key.lower(), '')):
+            elif self.match(value, event.get(key.lower(), '')):
                 LOGGER.warning("Requirement %s: %s matches %s: %s in event",
                                key, value, key, event.get(key.lower(), ''))
                 self.passed = False
@@ -290,7 +296,7 @@
                 LOGGER.warning("Requirement %s does not exist in event %s",
                                key, event['event'])
                 self.passed = False
-            elif not re.match(value, event.get(key.lower())):
+            elif not self.match(value, event.get(key.lower())):
                 LOGGER.warning("Requirement %s: %s does not match %s: "
                                "%s in event", key, value, key,
                                event.get(key.lower()))
@@ -304,7 +310,7 @@
                 LOGGER.warning("Requirement %s does not exist in event %s",
                                key, event['event'])
                 self.passed = False
-            elif re.match(value, event.get(key.lower(), '')):
+            elif self.match(value, event.get(key.lower(), '')):
                 LOGGER.warning("Requirement %s: %s matches %s: %s in event",
                                key, value, key, event.get(key.lower(), ''))
                 self.passed = False
@@ -362,7 +368,7 @@
                     extra_item = item.get(extra_key.lower())
                     if extra_item is None:
                         continue
-                    extra_match = re.match(extra_item, str(extra_value))
+                    extra_match = self.match(extra_item, str(extra_value))
                     if extra_match is None \
                             or extra_match.end() != len(str(extra_value)):
                         LOGGER.debug('Skipping %s - %s does not equal %s for '
@@ -370,7 +376,7 @@
                                      extra_item, str(extra_value), extra_key)
                         return False
             else:
-                match = re.match(item, value)
+                match = self.match(item, value)
                 if match is None or match.end() != len(value):
                     LOGGER.debug('Skipping %s - %s does not equal %s '
                                  'for field %s', event['eventname'], item,
diff --git a/sample-yaml/ami-config.yaml.sample b/sample-yaml/ami-config.yaml.sample
index cf3cf44..9f8b190 100644
--- a/sample-yaml/ami-config.yaml.sample
+++ b/sample-yaml/ami-config.yaml.sample
@@ -2,6 +2,11 @@
 
 ami-config:
     -
+        # When set to "True" the pattern to match values against is escaped
+        # (all non-alphanumerics are backslashed) prior to matching. Defaults
+        # to "False".
+        escape-pattern: False
+
         # The type of the instance. 'headermatch' means that when
         # certain events with certain headers are encountered,
         # Then regexes should be applied to the event to ensure that
diff --git a/tests/manager/escaped_values/test-config.yaml b/tests/manager/escaped_values/test-config.yaml
new file mode 100644
index 0000000..33b4ab9
--- /dev/null
+++ b/tests/manager/escaped_values/test-config.yaml
@@ -0,0 +1,47 @@
+testinfo:
+    summary: 'Test that AMI values are escaped for C sequences.'
+    description: |
+        "AMI values may contain characters, for instance \r\n, within them
+         that need to be escaped or else they could cause problems. This test
+         makes sure those sequences are properly escaped."
+
+properties:
+    minversion: '13.5.0'
+    dependencies:
+        - python: 'twisted'
+        - python: 'starpy'
+        - asterisk: 'res_manager_presencestate'
+    tags:
+        - AMI
+
+test-modules:
+    test-object:
+        config-section: object-config
+        typename: 'test_case.TestCaseModule'
+    modules:
+        -
+            config-section: 'ami-config'
+            typename: 'pluggable_modules.EventActionModule'
+
+object-config:
+    reactor-timeout: 15
+    connect-ami: True
+
+ami-config:
+    -
+        ami-start:
+        ami-actions:
+            action:
+                Action: 'SetVar'
+                Variable: 'PRESENCE_STATE(CustomPresence:Stuff)'
+                Value: 'away,things,stuff\r\nthings'
+    -
+        ami-events:
+            escape-pattern: True
+            conditions:
+                match:
+                    Event: 'PresenceStateChange'
+            requirements:
+                match:
+                    Message: 'stuff\\r\\nthings'
+
diff --git a/tests/manager/tests.yaml b/tests/manager/tests.yaml
index e929fd7..eab0ef4 100644
--- a/tests/manager/tests.yaml
+++ b/tests/manager/tests.yaml
@@ -21,4 +21,5 @@
     - test: 'userevent'
     - test: 'connected_line'
     - test: 'response-time'
+    - test: 'escaped_values'
     - dir:  'redirect'

-- 
To view, visit https://gerrit.asterisk.org/561
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I010deba4a0892792564ec49bdae1f3dd34c7decf
Gerrit-PatchSet: 3
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Samuel Galarneau <sgalarneau at digium.com>



More information about the asterisk-commits mailing list