[stem/master] Test for the ExitPolicy's constructor

commit 5ccc2ca7b4d7ab1974871400e0b8fb3ff1478df4 Author: Damian Johnson <atagar@torproject.org> Date: Mon Jul 16 09:55:06 2012 -0700 Test for the ExitPolicy's constructor Test to make sure that we can handle both string and ExitPolicy lists. This also checks that we can easily handle the split() output when breaking up a csv. Added __eq__() methods for the ExitPolicy and ExitPolicyRule to make policy comparisons easier. --- stem/exit_policy.py | 18 +++++++++++++++++- test/unit/exit_policy/policy.py | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/stem/exit_policy.py b/stem/exit_policy.py index 71602f3..07886c3 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -218,6 +218,12 @@ class ExitPolicy(object): def __str__(self): return ', '.join([str(rule) for rule in self._rules]) + + def __eq__(self, other): + if isinstance(other, ExitPolicy): + return self._rules == list(other) + else: + return False class ExitPolicyRule(object): """ @@ -494,7 +500,17 @@ class ExitPolicyRule(object): raise ValueError("Malformed port range: %s" % self.rule) else: raise ValueError("Port value isn't a wildcard, integer, or range: %s" % self.rule) - + + def __eq__(self, other): + if isinstance(other, ExitPolicyRule): + # Our string representation encompasses our effective policy. Technically + # this isn't quite right since our rule attribute may differ (ie, "accept + # 0.0.0.0/0" == "accept 0.0.0.0/0.0.0.0" will be True), but these + # policies are effectively equivilant. + + return str(self) == str(other) + else: + return False class MicrodescriptorExitPolicy: """ diff --git a/test/unit/exit_policy/policy.py b/test/unit/exit_policy/policy.py index 4d544b3..ea5ab90 100644 --- a/test/unit/exit_policy/policy.py +++ b/test/unit/exit_policy/policy.py @@ -1,14 +1,31 @@ """ -Unit tests for the stem.exit_policy.ExitPolicy parsing and class. +Unit tests for the stem.exit_policy.ExitPolicy class. """ import unittest import stem.exit_policy import stem.util.system +from stem.exit_policy import ExitPolicy, ExitPolicyRule import test.mocking as mocking class TestExitPolicy(unittest.TestCase): + 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. + + expected_policy = ExitPolicy( + ExitPolicyRule('accept *:80'), + ExitPolicyRule('accept *:443'), + ExitPolicyRule('reject *:*'), + ) + + policy = ExitPolicy('accept *:80', 'accept *:443', 'reject *:*') + self.assertEquals(expected_policy, policy) + + policy = ExitPolicy(*"accept *:80, accept *:443, reject *:*".split(",")) + self.assertEquals(expected_policy, policy) + def test_parsing(self): """ Tests parsing by the ExitPolicy class constructor.
participants (1)
-
atagar@torproject.org