[tor-commits] [stem/master] Renaming stem.util.connection.get_mask()

atagar at torproject.org atagar at torproject.org
Sun Mar 10 07:16:29 UTC 2013


commit 26c854e7c032d2f7df822316f56b0d62075d29d2
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Mar 9 22:18:22 2013 -0800

    Renaming stem.util.connection.get_mask()
    
    Renaming the function to get_mask_ipv4() to match the module conventions now
    that we have ipv6 support.
---
 stem/exit_policy.py          |    2 +-
 stem/util/connection.py      |   56 +++++++++++++++++++++---------------------
 test/unit/util/connection.py |   40 +++++++++++++++---------------
 3 files changed, 49 insertions(+), 49 deletions(-)

diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index bae7af1..6b31f73 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -610,7 +610,7 @@ class ExitPolicyRule(object):
       if address_type == AddressType.WILDCARD:
         mask = None
       elif address_type == AddressType.IPv4:
-        mask = stem.util.connection.get_mask(self._masked_bits)
+        mask = stem.util.connection.get_mask_ipv4(self._masked_bits)
       elif address_type == AddressType.IPv6:
         mask = stem.util.connection.get_mask_ipv6(self._masked_bits)
 
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 52a3954..92776f5 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -13,9 +13,9 @@ but for now just moving the parts we need.
   is_valid_ipv6_address - checks if a string is a valid IPv6 address
   is_valid_port - checks if something is a valid representation for a port
   expand_ipv6_address - provides an IPv6 address with its collapsed portions expanded
-  get_mask - provides the mask representation for a given number of bits
-  get_masked_bits - provides the number of bits represented by a mask
+  get_mask_ipv4 - provides the mask representation for a given number of bits
   get_mask_ipv6 - provides the IPv6 mask representation for a given number of bits
+  get_masked_bits - provides the number of bits represented by a mask
   get_binary - provides the binary representation for an integer with padding
   get_address_binary - provides the binary representation for an address
 
@@ -164,7 +164,7 @@ def expand_ipv6_address(address):
   return address
 
 
-def get_mask(bits):
+def get_mask_ipv4(bits):
   """
   Provides the IPv4 mask for a given number of bits, in the dotted-quad format.
 
@@ -190,31 +190,6 @@ def get_mask(bits):
   return ".".join([str(int(octet, 2)) for octet in octets])
 
 
-def get_masked_bits(mask):
-  """
-  Provides the number of bits that an IPv4 subnet mask represents. Note that
-  not all masks can be represented by a bit count.
-
-  :param str mask: mask to be converted
-
-  :returns: **int** with the number of bits represented by the mask
-
-  :raises: **ValueError** if the mask is invalid or can't be converted
-  """
-
-  if not is_valid_ipv4_address(mask):
-    raise ValueError("'%s' is an invalid subnet mask" % mask)
-
-  # converts octets to binary representation
-  mask_bin = get_address_binary(mask)
-  mask_match = re.match("^(1*)(0*)$", mask_bin)
-
-  if mask_match:
-    return 32 - len(mask_match.groups()[1])
-  else:
-    raise ValueError("Unable to convert mask to a bit count: %s" % mask)
-
-
 def get_mask_ipv6(bits):
   """
   Provides the IPv6 mask for a given number of bits, in the hex colon-delimited
@@ -242,6 +217,31 @@ def get_mask_ipv6(bits):
   return ":".join(["%04x" % int(group, 2) for group in groupings]).upper()
 
 
+def get_masked_bits(mask):
+  """
+  Provides the number of bits that an IPv4 subnet mask represents. Note that
+  not all masks can be represented by a bit count.
+
+  :param str mask: mask to be converted
+
+  :returns: **int** with the number of bits represented by the mask
+
+  :raises: **ValueError** if the mask is invalid or can't be converted
+  """
+
+  if not is_valid_ipv4_address(mask):
+    raise ValueError("'%s' is an invalid subnet mask" % mask)
+
+  # converts octets to binary representation
+  mask_bin = get_address_binary(mask)
+  mask_match = re.match("^(1*)(0*)$", mask_bin)
+
+  if mask_match:
+    return 32 - len(mask_match.groups()[1])
+  else:
+    raise ValueError("Unable to convert mask to a bit count: %s" % mask)
+
+
 def get_binary(value, bits):
   """
   Provides the given value as a binary string, padded with zeros to the given
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index c7db6d3..ecbe04a 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -96,18 +96,30 @@ class TestConnection(unittest.TestCase):
 
     self.assertRaises(ValueError, stem.util.connection.expand_ipv6_address, "127.0.0.1")
 
-  def test_get_mask(self):
+  def test_get_mask_ipv4(self):
     """
-    Checks the get_mask function.
+    Checks the get_mask_ipv4 function.
     """
 
-    self.assertEquals("255.255.255.255", stem.util.connection.get_mask(32))
-    self.assertEquals("255.255.255.248", stem.util.connection.get_mask(29))
-    self.assertEquals("255.255.254.0", stem.util.connection.get_mask(23))
-    self.assertEquals("0.0.0.0", stem.util.connection.get_mask(0))
+    self.assertEquals("255.255.255.255", stem.util.connection.get_mask_ipv4(32))
+    self.assertEquals("255.255.255.248", stem.util.connection.get_mask_ipv4(29))
+    self.assertEquals("255.255.254.0", stem.util.connection.get_mask_ipv4(23))
+    self.assertEquals("0.0.0.0", stem.util.connection.get_mask_ipv4(0))
 
-    self.assertRaises(ValueError, stem.util.connection.get_mask, -1)
-    self.assertRaises(ValueError, stem.util.connection.get_mask, 33)
+    self.assertRaises(ValueError, stem.util.connection.get_mask_ipv4, -1)
+    self.assertRaises(ValueError, stem.util.connection.get_mask_ipv4, 33)
+
+  def test_get_mask_ipv6(self):
+    """
+    Checks the get_mask_ipv6 function.
+    """
+
+    self.assertEquals("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", stem.util.connection.get_mask_ipv6(128))
+    self.assertEquals("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE:0000", stem.util.connection.get_mask_ipv6(111))
+    self.assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", stem.util.connection.get_mask_ipv6(0))
+
+    self.assertRaises(ValueError, stem.util.connection.get_mask_ipv6, -1)
+    self.assertRaises(ValueError, stem.util.connection.get_mask_ipv6, 129)
 
   def test_get_masked_bits(self):
     """
@@ -122,18 +134,6 @@ class TestConnection(unittest.TestCase):
     self.assertRaises(ValueError, stem.util.connection.get_masked_bits, "blarg")
     self.assertRaises(ValueError, stem.util.connection.get_masked_bits, "255.255.0.255")
 
-  def test_get_mask_ipv6(self):
-    """
-    Checks the get_mask_ipv6 function.
-    """
-
-    self.assertEquals("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", stem.util.connection.get_mask_ipv6(128))
-    self.assertEquals("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE:0000", stem.util.connection.get_mask_ipv6(111))
-    self.assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", stem.util.connection.get_mask_ipv6(0))
-
-    self.assertRaises(ValueError, stem.util.connection.get_mask_ipv6, -1)
-    self.assertRaises(ValueError, stem.util.connection.get_mask, 129)
-
   def test_get_address_binary(self):
     """
     Checks the get_address_binary function.





More information about the tor-commits mailing list