Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 4675b5a

Browse files
committed
Refactor tests to avoid duplicate path selection
1 parent b042d04 commit 4675b5a

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

bwscanner/circuit.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,39 @@
77
from bwscanner.logger import log
88

99

10+
def is_valid_exit(relay):
11+
"""
12+
Check that has the correct flags and exit policy for exiting
13+
14+
TODO: Check the exit policy
15+
"""
16+
is_exit = ('exit' in relay.flags and 'badexit' not in relay.flags)
17+
return is_exit and 'authority' not in relay.flags
18+
19+
20+
def random_path_to_exit(exit_relay, relays):
21+
"""
22+
Choose a random path to the specified exit relay
23+
"""
24+
# Sample an extra relay in case one of the choices is the exit.
25+
candidate_relays = random.sample(relays, 3)
26+
if exit_relay in candidate_relays:
27+
candidate_relays.remove(exit_relay)
28+
return candidate_relays[0:2] + [exit_relay]
29+
30+
1031
class CircuitGenerator(object):
1132
def __init__(self, state):
1233
self.state = state
1334
self.relays = list(set(state.routers.values()))
14-
self.exits = [relay for relay in self.relays if self.is_valid_exit(relay)]
35+
self.exits = [relay for relay in self.relays if is_valid_exit(relay)]
1536

1637
def __iter__(self):
1738
return self
1839

1940
def next(self):
2041
raise NotImplementedError
2142

22-
@staticmethod
23-
def is_valid_exit(relay):
24-
"""
25-
Check that has the correct flags and exit policy for exiting
26-
27-
TODO: Check the exit policy
28-
"""
29-
is_exit = ('exit' in relay.flags and 'badexit' not in relay.flags)
30-
return is_exit and 'authority' not in relay.flags
31-
3243

3344
class ExitScan(CircuitGenerator):
3445
"""
@@ -48,11 +59,7 @@ def circuit_generator():
4859
to use the exit relay more than once.
4960
"""
5061
for exit_relay in self.exits:
51-
# Sample an extra relay in case one of the choices is the exit.
52-
candidate_relays = random.sample(self.relays, 3)
53-
if exit_relay in candidate_relays:
54-
candidate_relays.remove(exit_relay)
55-
yield candidate_relays[0:2] + [exit_relay]
62+
yield random_path_to_exit(exit_relay, self.relays)
5663

5764
self._circgen = circuit_generator()
5865

test/template.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
from twisted.trial import unittest
77
from txtorcon.torstate import build_tor_connection
88

9+
from bwscanner import circuit
910
from bwscanner.attacher import SOCKSClientStreamAttacher
1011

1112

1213
class TorTestCase(unittest.TestCase):
13-
"""
14-
XXX: Use code from circuit.py for the path selection rather than
15-
repeating path selection here.
16-
"""
1714

1815
@defer.inlineCallbacks
1916
def setUp(self):
@@ -30,14 +27,11 @@ def routers(self):
3027

3128
@property
3229
def exits(self):
33-
return [r for r in self.routers if 'exit' in r.flags]
30+
return [r for r in self.routers if circuit.is_valid_exit(r)]
3431

3532
def random_path(self):
3633
exit_relay = random.choice(self.exits)
37-
selected_relays = random.sample(self.routers, 3)
38-
if exit_relay in selected_relays:
39-
selected_relays.remove(exit_relay)
40-
return selected_relays[0:2] + [exit_relay]
34+
return circuit.random_path_to_exit(exit_relay, self.routers)
4135

4236
@defer.inlineCallbacks
4337
def tearDown(self):

0 commit comments

Comments
 (0)