[tor-commits] [bridgedb/master] Add additional test utilities for generating random valid IPs.

isis at torproject.org isis at torproject.org
Sat Jul 25 19:26:22 UTC 2015


commit 372206bb8a73f5229b606a60f40de1c71e4804fe
Author: Isis Lovecruft <isis at torproject.org>
Date:   Sat Apr 18 03:15:19 2015 +0000

    Add additional test utilities for generating random valid IPs.
---
 lib/bridgedb/test/util.py |   25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lib/bridgedb/test/util.py b/lib/bridgedb/test/util.py
index 4aa2709..2d0c020 100644
--- a/lib/bridgedb/test/util.py
+++ b/lib/bridgedb/test/util.py
@@ -25,6 +25,7 @@ from functools import wraps
 from twisted.trial import unittest
 
 from bridgedb import util as bdbutil
+from bridgedb.parse.addr import isIPAddress
 
 
 def fileCheckDecorator(func):
@@ -121,6 +122,9 @@ def bracketIPv6(ip):
 def randomPort():
     return random.randint(1, 65535)
 
+def randomHighPort():
+    return random.randint(1024, 65535)
+
 def randomIPv4():
     return ipaddr.IPv4Address(random.getrandbits(32))
 
@@ -143,6 +147,27 @@ def randomIPString():
         return randomIPv4String()
     return randomIPv6String()
 
+def valid(func):
+    """Wrapper for the above ``randomIPv*`` functions to ensure they only
+    return addresses which BridgeDB considers "valid".
+
+    .. seealso:: :func:`bridgedb.parse.addr.isIPAddress`
+    """
+    @wraps(func)
+    def wrapper():
+        ip = None
+        while not isIPAddress(ip):
+            ip = func()
+        return ip
+    return wrapper
+
+randomValidIPv4       = valid(randomIPv4)
+randomValidIPv6       = valid(randomIPv6)
+randomValidIP         = valid(randomIP)
+randomValidIPv4String = valid(randomIPv4String)
+randomValidIPv6String = valid(randomIPv6String)
+randomValidIPString   = valid(randomIPString)
+
 
 #: Mixin class for use with :api:`~twisted.trial.unittest.TestCase`. A
 #: ``TestCaseMixin`` can be used to add additional methods, which should be





More information about the tor-commits mailing list