commit f7c96a68d0eac8e171f835a75fbe24e989c0e301 Author: Damian Johnson atagar@torproject.org Date: Fri Jan 1 18:09:13 2016 -0800
Allow multiple spaces in exit policies
Stem's being a little too picky here. Tor allows extra spaces in exit policies so tolerating it here too...
https://trac.torproject.org/projects/tor/ticket/17942 --- stem/exit_policy.py | 8 ++++---- test/unit/exit_policy/rule.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py index 717d856..7f238b1 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -578,10 +578,10 @@ class MicroExitPolicy(ExitPolicy):
policy = policy[6:]
- if not policy.startswith(' ') or (len(policy) - 1 != len(policy.lstrip())): + if not policy.startswith(' '): raise ValueError('A microdescriptor exit policy should have a space separating accept/reject from its port list: %s' % self._policy)
- policy = policy[1:] + policy = policy.lstrip()
# convert our port list into MicroExitPolicyRule rules = [] @@ -657,10 +657,10 @@ class ExitPolicyRule(object):
exitpattern = rule[6:]
- if not exitpattern.startswith(' ') or (len(exitpattern) - 1 != len(exitpattern.lstrip())): + if not exitpattern.startswith(' '): raise ValueError('An exit policy should have a space separating its accept/reject from the exit pattern: %s' % rule)
- exitpattern = exitpattern[1:] + exitpattern = exitpattern.lstrip()
if ':' not in exitpattern: raise ValueError("An exitpattern must be of the form 'addrspec:portspec': %s" % rule) diff --git a/test/unit/exit_policy/rule.py b/test/unit/exit_policy/rule.py index 5458c10..ecd96a8 100644 --- a/test/unit/exit_policy/rule.py +++ b/test/unit/exit_policy/rule.py @@ -4,7 +4,7 @@ Unit tests for the stem.exit_policy.ExitPolicyRule class.
import unittest
-from stem.exit_policy import AddressType, ExitPolicyRule +from stem.exit_policy import AddressType, ExitPolicyRule, MicroExitPolicy
class TestExitPolicyRule(unittest.TestCase): @@ -15,7 +15,6 @@ class TestExitPolicyRule(unittest.TestCase): invalid_inputs = ( 'accept', 'reject', - 'accept *:*', 'accept\t*:*', 'accept\n*:*', 'acceptt *:*', @@ -29,6 +28,13 @@ class TestExitPolicyRule(unittest.TestCase): for rule_arg in invalid_inputs: self.assertRaises(ValueError, ExitPolicyRule, rule_arg)
+ def test_with_multiple_spaces(self): + rule = ExitPolicyRule('accept *:80') + self.assertEqual('accept *:80', str(rule)) + + policy = MicroExitPolicy('accept 80,443') + self.assertTrue(policy.can_exit_to('75.119.206.243', 80)) + def test_str_unchanged(self): # provides a series of test inputs where the str() representation should # match the input rule