commit 7620d5e511a9e48f1423d4b3bda335529ba9562b
Author: Isis Lovecruft <isis(a)torproject.org>
Date: Wed Apr 1 00:48:21 2015 +0000
Move unittest utility functions for generating random IP addresses.
* MOVE several functions for generating random IP addresses from
bridgedb.test.legacy_Tests to bridgedb.test.util.
---
lib/bridgedb/test/legacy_Tests.py | 57 +++++++++++++------------------------
lib/bridgedb/test/util.py | 26 +++++++++++++++++
2 files changed, 45 insertions(+), 38 deletions(-)
diff --git a/lib/bridgedb/test/legacy_Tests.py b/lib/bridgedb/test/legacy_Tests.py
index d416d22..cd0c6de 100644
--- a/lib/bridgedb/test/legacy_Tests.py
+++ b/lib/bridgedb/test/legacy_Tests.py
@@ -36,38 +36,20 @@ from bridgedb.Stability import BridgeHistory
from bridgedb.parse import addr
from bridgedb.test import deprecated_networkstatus as networkstatus
+from bridgedb.test.util import bracketIPv6
+from bridgedb.test.util import randomIP
+from bridgedb.test.util import randomIPv4
+from bridgedb.test.util import randomIPv6
+from bridgedb.test.util import randomIPString
+from bridgedb.test.util import randomIPv4String
+from bridgedb.test.util import randomIPv6String
from math import log
+
def suppressWarnings():
warnings.filterwarnings('ignore', '.*tmpnam.*')
-def randomIP():
- if random.choice(xrange(2)):
- return randomIP4()
- return randomIP6()
-
-def randomIP4():
- return ipaddr.IPv4Address(random.getrandbits(32))
-
-def randomIP4String():
- return randomIP4().compressed
-
-def randomIP6():
- return ipaddr.IPv6Address(random.getrandbits(128))
-
-def randomIP6String():
- return bracketIP6(randomIP6().compressed)
-
-def randomIPString():
- if random.choice(xrange(2)):
- return randomIP4String()
- return randomIP6String()
-
-def bracketIP6(ip):
- """Put brackets around an IPv6 address, just as tor does."""
- return "[%s]" % ip
-
def random16IP():
upper = "123.123." # same 16
lower = ".".join([str(random.randrange(1,256)) for _ in xrange(2)])
@@ -111,7 +93,7 @@ def randomCountrySpec():
def fakeBridge(orport=8080, running=True, stable=True, or_addresses=False,
transports=False):
nn = "bridge-%s"%random.randrange(0,1000000)
- ip = ipaddr.IPAddress(randomIP4())
+ ip = ipaddr.IPAddress(randomIPv4())
fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
b = bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)
b.setStatus(running, stable)
@@ -121,7 +103,7 @@ def fakeBridge(orport=8080, running=True, stable=True, or_addresses=False,
for i in xrange(8):
# Only add or_addresses if they are valid. Otherwise, the test
# will randomly fail if an invalid address is chosen:
- address = randomIP4String()
+ address = randomIPv4String()
portlist = addr.PortList(randomPortSpec())
if addr.isValidIP(address):
oraddrs.append((address, portlist,))
@@ -145,7 +127,7 @@ def fakeBridge(orport=8080, running=True, stable=True, or_addresses=False,
def fakeBridge6(orport=8080, running=True, stable=True, or_addresses=False,
transports=False):
nn = "bridge-%s"%random.randrange(0,1000000)
- ip = ipaddr.IPAddress(randomIP6())
+ ip = ipaddr.IPAddress(randomIPv6())
fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
b = bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)
b.setStatus(running, stable)
@@ -155,10 +137,10 @@ def fakeBridge6(orport=8080, running=True, stable=True, or_addresses=False,
for i in xrange(8):
# Only add or_addresses if they are valid. Otherwise, the test
# will randomly fail if an invalid address is chosen:
- address = randomIP6()
+ address = randomIPv6()
portlist = addr.PortList(randomPortSpec())
if addr.isValidIP(address):
- address = bracketIP6(address)
+ address = bracketIPv6(address)
oraddrs.append((address, portlist,))
for address, portlist in oraddrs:
@@ -317,7 +299,7 @@ class IPBridgeDistTests(unittest.TestCase):
d.insert(fakeBridge(or_addresses=True))
for i in xrange(500):
- bridges = d.getBridgesForIP(randomIP4String(),
+ bridges = d.getBridgesForIP(randomIPv4String(),
"faketimestamp",
bridgeFilterRules=[filterBridgesByIP6])
bridge = random.choice(bridges)
@@ -333,7 +315,7 @@ class IPBridgeDistTests(unittest.TestCase):
d.insert(fakeBridge(or_addresses=True))
for i in xrange(500):
- bridges = d.getBridgesForIP(randomIP4String(),
+ bridges = d.getBridgesForIP(randomIPv4String(),
"faketimestamp",
bridgeFilterRules=[filterBridgesByIP4])
bridge = random.choice(bridges)
@@ -349,7 +331,7 @@ class IPBridgeDistTests(unittest.TestCase):
d.insert(fakeBridge(or_addresses=True))
for i in xrange(50):
- bridges = d.getBridgesForIP(randomIP4String(),
+ bridges = d.getBridgesForIP(randomIPv4String(),
"faketimestamp", 1,
bridgeFilterRules=[
filterBridgesByIP4,
@@ -373,7 +355,7 @@ class IPBridgeDistTests(unittest.TestCase):
d.insert(fakeBridge(or_addresses=True))
for i in xrange(5):
- b = d.getBridgesForIP(randomIP4String(), "x", 1, bridgeFilterRules=[
+ b = d.getBridgesForIP(randomIPv4String(), "x", 1, bridgeFilterRules=[
filterBridgesByIP4, filterBridgesByIP6])
assert len(b) == 0
@@ -396,10 +378,10 @@ class IPBridgeDistTests(unittest.TestCase):
b.blockingCountries[key] = set(['cn'])
for i in xrange(5):
- b = d.getBridgesForIP(randomIP4String(), "x", 1, bridgeFilterRules=[
+ b = d.getBridgesForIP(randomIPv4String(), "x", 1, bridgeFilterRules=[
filterBridgesByNotBlockedIn("cn")])
assert len(b) == 0
- b = d.getBridgesForIP(randomIP4String(), "x", 1, bridgeFilterRules=[
+ b = d.getBridgesForIP(randomIPv4String(), "x", 1, bridgeFilterRules=[
filterBridgesByNotBlockedIn("us")])
assert len(b) > 0
@@ -722,4 +704,3 @@ def main():
suppressWarnings()
unittest.TextTestRunner(verbosity=1).run(testSuite())
-
diff --git a/lib/bridgedb/test/util.py b/lib/bridgedb/test/util.py
index bec590d..2949eca 100644
--- a/lib/bridgedb/test/util.py
+++ b/lib/bridgedb/test/util.py
@@ -111,6 +111,32 @@ def getBridgeDBPID(pidfile="bridgedb.pid"):
return pid
+def bracketIPv6(ip):
+ """Put brackets around an IPv6 address, just as tor does."""
+ return "[%s]" % ip
+
+def randomIPv4():
+ return ipaddr.IPv4Address(random.getrandbits(32))
+
+def randomIPv6():
+ return ipaddr.IPv6Address(random.getrandbits(128))
+
+def randomIP():
+ if random.choice(xrange(2)):
+ return randomIPv4()
+ return randomIPv6()
+
+def randomIPv4String():
+ return randomIPv4().compressed
+
+def randomIPv6String():
+ return bracketIPv6(randomIPv6().compressed)
+
+def randomIPString():
+ if random.choice(xrange(2)):
+ return randomIPv4String()
+ return randomIPv6String()
+
#: Mixin class for use with :api:`~twisted.trial.unittest.TestCase`. A
#: ``TestCaseMixin`` can be used to add additional methods, which should be