summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamian Johnson <atagar@torproject.org>2016-01-01 18:09:13 -0800
committerDamian Johnson <atagar@torproject.org>2016-01-01 18:09:13 -0800
commitf7c96a68d0eac8e171f835a75fbe24e989c0e301 (patch)
treecf92a3258f37b83de4576128bc0b2f57512b5ffd
parent9b2e4c136d2a76e227b19d01811d166abebb408d (diff)
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
-rw-r--r--stem/exit_policy.py8
-rw-r--r--test/unit/exit_policy/rule.py10
2 files changed, 12 insertions, 6 deletions
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 717d856d..7f238b1d 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 5458c106..ecd96a8b 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