[stem/master] Test and fix for the ExitPolicy's summary() method

commit c1ced39c89092a81dad3bd3d8ad3545296a64190 Author: Damian Johnson <atagar@torproject.org> Date: Tue Jul 17 09:47:21 2012 -0700 Test and fix for the ExitPolicy's summary() method Ok, I was being stupid. The performance issue for the summary() method was due to an O(n) lookup when checking if we had seen a port (which made, for instance, to full-port-range policies choke on a 65535^2 operation). Swapped to using a set instead to make it constant time. --- stem/exit_policy.py | 13 ++----------- test/unit/exit_policy/policy.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/stem/exit_policy.py b/stem/exit_policy.py index 2d00553..8a5f528 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -179,16 +179,7 @@ class ExitPolicy(object): # 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 = [], [] + display_ports, skip_ports = [], set() for rule in self._rules: if not rule.is_address_wildcard(): continue @@ -202,7 +193,7 @@ class ExitPolicy(object): display_ports.append(port) # all further entries with this port should be ignored - skip_ports.append(port) + skip_ports.add(port) # convert port list to a list of ranges (ie, ['1-3'] rather than [1, 2, 3]) if display_ports: diff --git a/test/unit/exit_policy/policy.py b/test/unit/exit_policy/policy.py index 97cad1a..5b7324e 100644 --- a/test/unit/exit_policy/policy.py +++ b/test/unit/exit_policy/policy.py @@ -88,6 +88,22 @@ class TestExitPolicy(unittest.TestCase): policy = ExitPolicy(*rules) self.assertEquals(expected_result, policy.is_exiting_allowed()) + def test_summary_examples(self): + # checks the summary() method's pydoc examples + + policy = ExitPolicy('accept *:80', 'accept *:443', 'reject *:*') + self.assertEquals("accept 80, 443", policy.summary()) + + policy = ExitPolicy('accept *:443', 'reject *:1-1024', 'accept *:*') + self.assertEquals("reject 1-442, 444-1024", policy.summary()) + + def test_summary_large_ranges(self): + # checks the summary() method when the policy includes very large port ranges + + policy = ExitPolicy('reject *:80-65535', 'accept *:1-65533', 'reject *:*') + self.assertEquals("accept 1-79", policy.summary()) + + def test_microdesc_exit_parsing(self): microdesc_exit_policy = stem.exit_policy.MicrodescriptorExitPolicy("accept 80,443")
participants (1)
-
atagar@torproject.org