[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r12784: Stubs for unit tests. (in bridgedb/trunk: . lib/bridgedb)
Author: nickm
Date: 2007-12-12 13:48:46 -0500 (Wed, 12 Dec 2007)
New Revision: 12784
Added:
bridgedb/trunk/lib/bridgedb/Tests.py
Modified:
bridgedb/trunk/
bridgedb/trunk/lib/bridgedb/Bridges.py
bridgedb/trunk/lib/bridgedb/Dist.py
bridgedb/trunk/lib/bridgedb/Main.py
bridgedb/trunk/lib/bridgedb/Time.py
bridgedb/trunk/setup.py
Log:
r17117@catbus: nickm | 2007-12-12 13:48:42 -0500
Stubs for unit tests.
Property changes on: bridgedb/trunk
___________________________________________________________________
svk:merge ticket from /bridgedb/trunk [r17117] on 8246c3cf-6607-4228-993b-4d95d33730f1
Modified: bridgedb/trunk/lib/bridgedb/Bridges.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Bridges.py 2007-12-12 17:55:52 UTC (rev 12783)
+++ bridgedb/trunk/lib/bridgedb/Bridges.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -5,6 +5,7 @@
import binascii
import bisect
import hmac
+import re
import sha
import socket
import struct
@@ -18,9 +19,29 @@
DIGEST_LEN = 20
def is_valid_ip(ip):
+ """Return True if ip is the string encoding of a valid IPv4 address,
+ and False otherwise.
+
+ >>> is_valid_ip('1.2.3.4')
+ True
+ >>> is_valid_ip('1.2.3.255')
+ True
+ >>> is_valid_ip('1.2.3.256')
+ False
+ >>> is_valid_ip('1')
+ False
+ >>> is_valid_ip('1.2.3')
+ False
+ >>> is_valid_ip('xyzzy')
+ False
+ """
+
+ if not re.match(r'(\d+)\.(\d+)\.(\d+)\.(\d+)', ip):
+ # inet_aton likes "1.2" as a synonym for "0.0.1.2". We don't.
+ return False
try:
socket.inet_aton(ip)
- except socekt.error:
+ except socket.error:
return False
else:
return True
Modified: bridgedb/trunk/lib/bridgedb/Dist.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Dist.py 2007-12-12 17:55:52 UTC (rev 12783)
+++ bridgedb/trunk/lib/bridgedb/Dist.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -8,12 +8,13 @@
import socket
def uniformMap(ip):
- "Map an IP to an arbitrary 'area' string"
- # convert the IP for 4 bytes.
- s = socket.inet_aton(ip)
- # return the first 3.
- return s[:3]
+ """Map an IP to an arbitrary 'area' string, such that any two /24 addresses
+ get the same string.
+ >>> uniformMap('1.2.3.4')
+ '1.2.3'
+ """
+ return ".".join( ip.split(".")[:3] )
class IPBasedDistributor(bridgedb.Bridges.BridgeHolder):
def __init__(self, areaMapper, nClusters, key):
Modified: bridgedb/trunk/lib/bridgedb/Main.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Main.py 2007-12-12 17:55:52 UTC (rev 12783)
+++ bridgedb/trunk/lib/bridgedb/Main.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -32,11 +32,26 @@
)
def getKey(fname):
+ """Load the key stored in fname, or create a new 32-byte key and store
+ it in fname.
+
+ >>> name = os.tmpnam()
+ >>> os.path.exists(name)
+ False
+ >>> k1 = getKey(name)
+ >>> os.path.exists(name)
+ True
+ >>> open(name).read() == k1
+ True
+ >>> k2 = getKey(name)
+ >>> k1 == k2
+ True
+ """
try:
f = open(fname, 'r')
except IOError:
k = os.urandom(32)
- flags = os.O_WRONLY|os.O_TRUNC|getattr(os, "O_BIN", 0)
+ flags = os.O_WRONLY|os.O_TRUNC|os.O_CREAT|getattr(os, "O_BIN", 0)
fd = os.open(fname, flags, 0400)
os.write(fd, k)
os.close(fd)
Added: bridgedb/trunk/lib/bridgedb/Tests.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Tests.py (rev 0)
+++ bridgedb/trunk/lib/bridgedb/Tests.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -0,0 +1,42 @@
+# BridgeDB by Nick Mathewson.
+# Copyright (c) 2007, The Tor Project, Inc.
+# See LICENSE for licensing informatino
+
+import doctest
+import unittest
+import warnings
+
+import bridgedb.Bridges
+import bridgedb.Main
+import bridgedb.Dist
+import bridgedb.Time
+
+def suppressWarnings():
+ warnings.filterwarnings('ignore', '.*tmpnam.*')
+
+class TestCase0(unittest.TestCase):
+ def testFooIsFooish(self):
+ self.assert_(True)
+
+def testSuite():
+ suite = unittest.TestSuite()
+ loader = unittest.TestLoader()
+
+ for klass in [ TestCase0 ]:
+ suite.addTest(loader.loadTestsFromTestCase(klass))
+
+ for module in [ bridgedb.Bridges,
+ bridgedb.Main,
+ bridgedb.Dist,
+ bridgedb.Time ]:
+ suite.addTest(doctest.DocTestSuite(module))
+
+ return suite
+
+def main():
+ suppressWarnings()
+
+ unittest.TextTestRunner(verbosity=1).run(testSuite())
+
+
+
Modified: bridgedb/trunk/lib/bridgedb/Time.py
===================================================================
--- bridgedb/trunk/lib/bridgedb/Time.py 2007-12-12 17:55:52 UTC (rev 12783)
+++ bridgedb/trunk/lib/bridgedb/Time.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -41,6 +41,13 @@
assert False
def getInterval(self, when):
+ """
+
+ >>> t = calendar.timegm((2007, 12, 12, 0, 0, 0))
+ >>> I = IntervalSchedule('month', 1)
+ >>> I.getInterval(t)
+ '2007-12'
+ """
if self.itype == 'month':
tm = time.gmtime(when)
n = tm.tm_year * 12 + tm.tm_mon - 1
Modified: bridgedb/trunk/setup.py
===================================================================
--- bridgedb/trunk/setup.py 2007-12-12 17:55:52 UTC (rev 12783)
+++ bridgedb/trunk/setup.py 2007-12-12 18:48:46 UTC (rev 12784)
@@ -3,8 +3,35 @@
# See LICENSE for licensing informatino
import distutils
-from distutils.core import setup
+import sys
+from distutils.core import setup, Command
+
+class runTests(Command):
+ # Based on setup.py from mixminion, which is based on setup.py
+ # from Zooko's pyutil package, which is in turn based on
+ # http://mail.python.org/pipermail/distutils-sig/2002-January/002714.html
+ description = "Run unit tests"
+ user_options = []
+
+ def initialize_options(self):
+ pass
+
+ def finalize_options(self):
+ build = self.get_finalized_command('build')
+ self.build_purelib = build.build_purelib
+ self.build_platlib = build.build_platlib
+
+ def run(self):
+ self.run_command('build')
+ old_path = sys.path[:]
+ sys.path[0:0] = [ self.build_purelib, self.build_platlib ]
+ try:
+ testmod = __import__("bridgedb.Tests", globals(), "", [])
+ testmod.Tests.main()
+ finally:
+ sys.path = old_path
+
setup(name='BridgeDB',
version='0.1',
description='Bridge disbursal tool for use with Tor anonymity network',
@@ -12,6 +39,8 @@
author_email='nickm at torproject dot org',
url='https://www.torproject.org',
package_dir= {'' : 'lib'},
- packages=['bridgedb'])
+ packages=['bridgedb'],
+ cmdclass={'test' : runTests}
+ )