[tor-commits] [stem/master] Size unit tests

atagar at torproject.org atagar at torproject.org
Sun Jan 21 02:04:04 UTC 2018


commit 6a0789d367283ed5e64ab7082e76c9b9d811838b
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Jan 16 11:49:04 2018 -0800

    Size unit tests
    
    Adding unit test coverage for our Size class. This has been exercised
    transitively by our other tests, but direct tests of our lower level types
    still help. These actually caught a surprising number of issues with regard
    to the exceptions we raise.
---
 stem/client/__init__.py   | 17 ++++++++++-------
 test/unit/client/types.py | 43 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index d15f0aa4..defa09fa 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -147,17 +147,20 @@ class Size(object):
     """
     Encodes bytes into a packed field.
 
-    :param bytes content: content to encode
+    :param int content: content to encode
 
-    :raises: **ValueError** if content isn't of the right size
+    :raises: **ValueError** if incorrect type or size
     """
 
-    unpacked = struct.pack(self.format, content)
+    if not isinstance(content, int):
+      raise ValueError('Size.pack encodes an integer, but was a %s' % type(content).__name__)
+
+    packed = struct.pack(self.format, content)
 
-    if self.size != len(unpacked):
-      raise ValueError("'%s' is the wrong size for a %s field" % (unpacked, self.name))
+    if self.size != len(packed):
+      raise ValueError('%s is the wrong size for a %s field' % (repr(packed), self.name))
 
-    return unpacked
+    return packed
 
   def unpack(self, content):
     """
@@ -169,7 +172,7 @@ class Size(object):
     """
 
     if self.size != len(content):
-      raise ValueError("'%s' is the wrong size for a %s field" % (content, self.name))
+      raise ValueError('%s is the wrong size for a %s field' % (repr(content), self.name))
 
     return struct.unpack(self.format, content)[0]
 
diff --git a/test/unit/client/types.py b/test/unit/client/types.py
index a85a0b8d..a4d69a19 100644
--- a/test/unit/client/types.py
+++ b/test/unit/client/types.py
@@ -2,9 +2,10 @@
 Unit tests for the types in stem.client.
 """
 
+import re
 import unittest
 
-from stem.client import Address
+from stem.client import Address, Size
 
 
 class TestClientTypes(unittest.TestCase):
@@ -16,3 +17,43 @@ class TestClientTypes(unittest.TestCase):
     self.assertEqual(4, addr.type_int)
     self.assertEqual('127.0.0.1', addr.value)
     self.assertEqual('\x7f\x00\x00\x01', addr.value_bin)
+
+  def test_size_attributes(self):
+    self.assertEqual('CHAR', Size.CHAR.name)
+    self.assertEqual('!B', Size.CHAR.format)
+
+    self.assertEqual(1, Size.CHAR.size)
+    self.assertEqual(2, Size.SHORT.size)
+    self.assertEqual(4, Size.LONG.size)
+    self.assertEqual(8, Size.LONG_LONG.size)
+
+  def test_size_pack(self):
+    self.assertEqual('\x12', Size.CHAR.pack(18))
+    self.assertEqual('\x00\x12', Size.SHORT.pack(18))
+    self.assertEqual('\x00\x00\x00\x12', Size.LONG.pack(18))
+    self.assertEqual('\x00\x00\x00\x00\x00\x00\x00\x12', Size.LONG_LONG.pack(18))
+
+    self.assertRaisesRegexp(ValueError, 'Size.pack encodes an integer, but was a str', Size.CHAR.pack, 'hi')
+
+    bad_size = Size('BAD_SIZE', 1, '!H')
+    self.assertRaisesRegexp(ValueError, re.escape("'\\x00\\x12' is the wrong size for a BAD_SIZE field"), bad_size.pack, 18)
+
+  def test_size_unpack(self):
+    self.assertEqual(18, Size.CHAR.unpack('\x12'))
+    self.assertEqual(18, Size.SHORT.unpack('\x00\x12'))
+    self.assertEqual(18, Size.LONG.unpack('\x00\x00\x00\x12'))
+    self.assertEqual(18, Size.LONG_LONG.unpack('\x00\x00\x00\x00\x00\x00\x00\x12'))
+
+    self.assertEqual(ord('a'), Size.CHAR.unpack('a'))
+    self.assertEqual(24930, Size.SHORT.unpack('ab'))
+
+    self.assertRaisesRegexp(ValueError, re.escape("'\\x00\\x12' is the wrong size for a CHAR field"), Size.CHAR.unpack, '\x00\x12')
+
+  def test_size_pop(self):
+    self.assertEqual((18, ''), Size.CHAR.pop('\x12'))
+
+    self.assertEqual((0, '\x12'), Size.CHAR.pop('\x00\x12'))
+    self.assertEqual((18, ''), Size.SHORT.pop('\x00\x12'))
+
+    self.assertRaisesRegexp(ValueError, "'' is the wrong size for a CHAR field", Size.CHAR.pop, '')
+    self.assertRaisesRegexp(ValueError, re.escape("'\\x12' is the wrong size for a SHORT field"), Size.SHORT.pop, '\x12')





More information about the tor-commits mailing list