[Asterisk-code-review] AMI: Escape string values test. (testsuite[master])

Kevin Harwell asteriskteam at digium.com
Mon Jun 1 12:03:04 CDT 2015


Kevin Harwell has uploaded a new change for review.

  https://gerrit.asterisk.org/561

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(-)


  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/61/561/1

diff --git a/lib/python/asterisk/ami.py b/lib/python/asterisk/ami.py
index 3cbeeaf..53e46b3 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..c234bf3
--- /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.4.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 7ab7265..5e6e0d4 100644
--- a/tests/manager/tests.yaml
+++ b/tests/manager/tests.yaml
@@ -20,6 +20,7 @@
     - test: 'status_all_vars'
     - test: 'userevent'
     - test: 'connected_line'
+    - test: 'escaped_values'
     # Temporarily disabled while failures are debugged
     #- test: 'response-time'
     - dir:  'redirect'

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I010deba4a0892792564ec49bdae1f3dd34c7decf
Gerrit-PatchSet: 1
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>



More information about the asterisk-code-review mailing list