[tor-commits] [flashproxy/master] document and add tests for the None/"" default behaviour of parse_addr_spec

infinity0 at torproject.org infinity0 at torproject.org
Thu Nov 21 17:14:36 UTC 2013


commit 8d5eeb8bd9f589451d95d6e6a2ba3f2697c8cab0
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Thu Nov 21 16:56:14 2013 +0000

    document and add tests for the None/"" default behaviour of parse_addr_spec
---
 flashproxy/test/test_util.py |    8 ++++++++
 flashproxy/util.py           |   14 ++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/flashproxy/test/test_util.py b/flashproxy/test/test_util.py
index af4c2e6..935dd1f 100644
--- a/flashproxy/test/test_util.py
+++ b/flashproxy/test/test_util.py
@@ -27,6 +27,14 @@ class ParseAddrSpecTest(unittest.TestCase):
         self.assertEqual(parse_addr_spec(":", defhost="1234::1", defport=9999), ("1234::1", 9999))
         self.assertEqual(parse_addr_spec("", defhost="1234::1", defport=9999), ("1234::1", 9999))
 
+    def test_empty_defaults(self):
+        self.assertEqual(parse_addr_spec("192.168.0.2:8888"), ("192.168.0.2", 8888))
+        self.assertEqual(parse_addr_spec("", defhost="", defport=0), ("", 0))
+        self.assertEqual(parse_addr_spec(":8888", defhost=""), ("", 8888))
+        self.assertRaises(ValueError, parse_addr_spec, ":8888")
+        self.assertEqual(parse_addr_spec("192.168.0.2", defport=0), ("192.168.0.2", 0))
+        self.assertRaises(ValueError, parse_addr_spec, "192.168.0.2")
+
     def test_canonical_ip_noresolve(self):
         """Test that canonical_ip does not do DNS resolution by default."""
         self.assertRaises(ValueError, canonical_ip, *parse_addr_spec("example.com:80"))
diff --git a/flashproxy/util.py b/flashproxy/util.py
index b069bf7..a53bdad 100644
--- a/flashproxy/util.py
+++ b/flashproxy/util.py
@@ -4,11 +4,15 @@ import socket
 def parse_addr_spec(spec, defhost = None, defport = None):
     """Parse a host:port specification and return a 2-tuple ("host", port) as
     understood by the Python socket functions.
+
     >>> parse_addr_spec("192.168.0.1:9999")
     ('192.168.0.1', 9999)
 
-    If defhost or defport are given, those parts of the specification may be
-    omitted; if so, they will be filled in with defaults.
+    If defhost or defport are given and not None, the respective parts of the
+    specification may be omitted, and will be filled in with the defaults.
+    If defhost or defport are omitted or None, the respective parts of the
+    specification must be given, or else a ValueError will be raised.
+
     >>> parse_addr_spec("192.168.0.2:8888", defhost="192.168.0.1", defport=9999)
     ('192.168.0.2', 8888)
     >>> parse_addr_spec(":8888", defhost="192.168.0.1", defport=9999)
@@ -21,6 +25,12 @@ def parse_addr_spec(spec, defhost = None, defport = None):
     ('192.168.0.1', 9999)
     >>> parse_addr_spec("", defhost="192.168.0.1", defport=9999)
     ('192.168.0.1', 9999)
+    >>> parse_addr_spec(":")
+    Traceback (most recent call last):
+    [..]
+    ValueError: Bad address specification ":"
+    >>> parse_addr_spec(":", "", 0)
+    ('', 0)
 
     IPv6 addresses must be enclosed in square brackets."""
     host = None



More information about the tor-commits mailing list