commit 6f36f95711d54fee1bdf7391842f5e76dde8538b Author: Damian Johnson atagar@torproject.org Date: Tue Jul 17 08:10:17 2012 -0700
Adding a test for pydoc header examples
Little test for our pydoc example, and fixing a couple little bugs. There's a larger bug due to our naive summary() implementation though a better solution isn't immediately coming to mind. --- stem/exit_policy.py | 20 +++++++++++++++----- test/unit/exit_policy/policy.py | 9 +++++++++ 2 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py index fca6034..12cc11f 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -168,21 +168,31 @@ class ExitPolicy(object): is_whitelist = not rule.is_accept break
- # Iterates over the policys and adds the the ports we'll return (ie, allows - # if a whitelist and rejects if a blacklist). Regardless of a port's - # allow/reject policy, all further entries with that port are ignored since - # policies respect the first matching policy. + # Iterates over the policys and adds the the ports we'll return (ie, + # allows if a whitelist and rejects if a blacklist). Regardless of a + # port's allow/reject policy, all further entries with that port are + # ignored since policies respect the first matching policy. + + # TODO: The following will be prohibitively expensive if someome has + # policy entries that aren't a wildcard, but covers most ports. For + # instance... + # + # accept 1025-65535 # just accepts non-privilaged ports + # + # On one hand handling ranges is a pita, but on the other this + # implementation is naive. Patches welcome.
display_ports, skip_ports = [], []
for rule in self._rules: if not rule.is_address_wildcard(): continue + elif rule.is_port_wildcard(): break
for port in xrange(rule.min_port, rule.max_port + 1): if port in skip_ports: continue
# if accept + whitelist or reject + blacklist then add - if policy.is_accept == is_whitelist: + if rule.is_accept == is_whitelist: display_ports.append(port)
# all further entries with this port should be ignored diff --git a/test/unit/exit_policy/policy.py b/test/unit/exit_policy/policy.py index ea5ab90..a6eb00e 100644 --- a/test/unit/exit_policy/policy.py +++ b/test/unit/exit_policy/policy.py @@ -10,6 +10,15 @@ from stem.exit_policy import ExitPolicy, ExitPolicyRule import test.mocking as mocking
class TestExitPolicy(unittest.TestCase): + def test_example(self): + # tests the ExitPolicy and MicrodescriptorExitPolicy pydoc examples + policy = ExitPolicy("accept *:80", "accept *:443", "reject *:*") + self.assertEquals("accept *:80, accept *:443, reject *:*", str(policy)) + self.assertEquals("accept 80, 443", policy.summary()) + self.assertTrue(policy.can_exit_to("75.119.206.243", 80)) + + # TODO: add MicrodescriptorExitPolicy after it has been revised + def test_constructor(self): # The ExitPolicy constructor takes a series of string or ExitPolicyRule # entries. Extra whitespace is ignored to make csvs easier to handle.