[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Explicitly test negative VPADDING sizes



commit 9040cf46b24946d9cc3a96ab25e5a0af506cdad6
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Jun 16 11:03:08 2018 -0700

    Explicitly test negative VPADDING sizes
    
    Nitpickiest of nitpicks...
    
      * Explicitly checking for negative VPADDING sizes so we can an error message
        that includes the argument.
    
      * Testing for negative sizes in our unit tests.
    
      * Flatten our validation checks a bit.
---
 stem/client/cell.py      | 13 ++++++-------
 test/unit/client/cell.py |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index d5e71e8a..61503c40 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -586,15 +586,14 @@ class VPaddingCell(Cell):
   IS_FIXED_SIZE = False
 
   def __init__(self, size = None, payload = None):
-    if payload is None:
-      if size is not None:
-        payload = os.urandom(size)  # enforces size >= 0
-      else:
-        raise ValueError('VPaddingCell constructor must specify payload or size')
-    elif size is not None and size != len(payload):
+    if size is None and payload is None:
+      raise ValueError('VPaddingCell constructor must specify payload or size')
+    elif size is not None and size < 0:
+      raise ValueError('VPaddingCell size (%s) cannot be negative' % size)
+    elif size is not None and payload is not None and size != len(payload):
       raise ValueError('VPaddingCell constructor specified both a size of %i bytes and payload of %i bytes' % (size, len(payload)))
 
-    self.payload = payload
+    self.payload = payload if payload is not None else os.urandom(size)
 
   def pack(self, link_protocol):
     return VPaddingCell._pack(link_protocol, self.payload)
diff --git a/test/unit/client/cell.py b/test/unit/client/cell.py
index 785d48d2..2123cd6f 100644
--- a/test/unit/client/cell.py
+++ b/test/unit/client/cell.py
@@ -221,7 +221,7 @@ class TestCell(unittest.TestCase):
     self.assertEqual(b'', empty_constructed_cell.payload)
 
     self.assertRaisesRegexp(ValueError, 'VPaddingCell constructor specified both a size of 5 bytes and payload of 1 bytes', VPaddingCell, 5, '\x02')
-
+    self.assertRaisesRegexp(ValueError, re.escape('VPaddingCell size (-15) cannot be negative'), VPaddingCell, -15)
     self.assertRaisesRegexp(ValueError, '^%s$' % re.escape('VPaddingCell constructor must specify payload or size'), VPaddingCell)
 
   def test_certs_cell(self):



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits