commit 8aedf0b81874d828c85470a5ef36dbce8324d291 Author: Isis Lovecruft isis@torproject.org Date: Fri Dec 22 03:18:06 2017 +0000
Add more unittests for legacy code in Bridges.py. --- bridgedb/test/test_Bridges.py | 95 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-)
diff --git a/bridgedb/test/test_Bridges.py b/bridgedb/test/test_Bridges.py index 092b033..5991952 100644 --- a/bridgedb/test/test_Bridges.py +++ b/bridgedb/test/test_Bridges.py @@ -14,6 +14,7 @@ from __future__ import print_function
import copy +import io import ipaddr import logging
@@ -23,9 +24,9 @@ from bridgedb import Bridges from bridgedb.test import util
# For additional logger output for debugging, comment out the following: -#logging.disable(50) +logging.disable(50) # and then uncomment the following line: -Bridges.logging.getLogger().setLevel(10) +#Bridges.logging.getLogger().setLevel(10)
class BridgeRingTests(unittest.TestCase): @@ -34,6 +35,11 @@ class BridgeRingTests(unittest.TestCase): def setUp(self): self.ring = Bridges.BridgeRing('fake-hmac-key')
+ def addRandomBridges(self): + bridges = copy.deepcopy(util.generateFakeBridges()) + + [self.ring.insert(bridge) for bridge in bridges] + def addBridgesFromSameSubnet(self): bridges = copy.deepcopy(util.generateFakeBridges()) subnet = "5.5.%d.%d" @@ -62,3 +68,88 @@ class BridgeRingTests(unittest.TestCase): # Since they're all in the same /16, we should only get one # bridge back: self.assertEqual(len(bridges), 1) + + def test_filterDistinctSubnets_random_bridges(self): + """Even after filtering, in a normal case we should get the amount of + bridges we asked for. However, we should always get at least one. + """ + self.addRandomBridges() + + chosen = self.ring.bridges.keys()[:3] + bridges = self.ring.filterDistinctSubnets(chosen) + + self.assertGreaterEqual(len(bridges), 1) + + def test_clear(self): + """Clear should get rid of all the inserted bridges.""" + self.addRandomBridges() + self.assertGreater(len(self.ring), 0) + self.ring.clear() + self.assertEqual(len(self.ring), 0) + + def test_getBridges_filterBySubnet(self): + """We should still get the number of bridges we asked for, even when + filtering by distinct subnets. + """ + self.addRandomBridges() + bridges = self.ring.getBridges('a' * Bridges.DIGEST_LEN, N=3, filterBySubnet=True) + self.assertEqual(len(bridges), 3) + + def test_dumpAssignments(self): + """This should dump the bridges to the file.""" + self.addRandomBridges() + + f = io.StringIO() + + self.ring.dumpAssignments(f) + + f.flush() + f.seek(0) + + data = f.read() + first = self.ring.bridges.values()[0].fingerprint + + # The first bridge's fingerprint should be within the data somewhere + self.assertIn(first, data) + + +class FixedBridgeSplitterTests(unittest.TestCase): + """Unittests for :class:`bridgedb.Bridges.FixedBridgeSplitter`.""" + + def setUp(self): + self.rings = [Bridges.BridgeRing('fake-hmac-key-1'), + Bridges.BridgeRing('fake-hmac-key-2')] + self.splitter = Bridges.FixedBridgeSplitter('fake-hmac-key', self.rings) + + def addRandomBridges(self): + bridges = copy.deepcopy(util.generateFakeBridges()) + + [self.splitter.insert(bridge) for bridge in bridges] + + def test_insert(self): + self.addRandomBridges() + self.assertGreater(len(self.splitter), 0) + + def test_clear(self): + """Clear should get rid of all the inserted bridges.""" + self.addRandomBridges() + self.assertGreater(len(self.splitter), 0) + self.splitter.clear() + self.assertEqual(len(self.splitter), 0) + + def test_dumpAssignments(self): + """This should dump the bridges to the file.""" + self.addRandomBridges() + + f = io.StringIO() + + self.splitter.dumpAssignments(f) + + f.flush() + f.seek(0) + + data = f.read() + first = self.splitter.rings[0].bridges.values()[0].fingerprint + + # The first bridge's fingerprint should be within the data somewhere + self.assertIn(first, data)
tor-commits@lists.torproject.org