Skip to content

Commit

Permalink
Move unittest utility functions for generating random IP addresses.
Browse files Browse the repository at this point in the history
 * MOVE several functions for generating random IP addresses from
   bridgedb.test.legacy_Tests to bridgedb.test.util.
  • Loading branch information
isislovecruft committed Apr 1, 2015
1 parent 0341b72 commit 7620d5e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
57 changes: 19 additions & 38 deletions lib/bridgedb/test/legacy_Tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,20 @@

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)])
Expand Down Expand Up @@ -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)
Expand All @@ -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,))
Expand All @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -317,7 +299,7 @@ def testDistWithFilterIP6(self):
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)
Expand All @@ -333,7 +315,7 @@ def testDistWithFilterIP4(self):
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)
Expand All @@ -349,7 +331,7 @@ def testDistWithFilterBoth(self):
d.insert(fakeBridge(or_addresses=True))

for i in xrange(50):
bridges = d.getBridgesForIP(randomIP4String(),
bridges = d.getBridgesForIP(randomIPv4String(),
"faketimestamp", 1,
bridgeFilterRules=[
filterBridgesByIP4,
Expand All @@ -373,7 +355,7 @@ def testDistWithFilterAll(self):
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

Expand All @@ -396,10 +378,10 @@ def testDistWithFilterBlockedCountries(self):
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

Expand Down Expand Up @@ -722,4 +704,3 @@ def main():
suppressWarnings()

unittest.TextTestRunner(verbosity=1).run(testSuite())

26 changes: 26 additions & 0 deletions lib/bridgedb/test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7620d5e

Please sign in to comment.