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

[tor-commits] [stem/master] VPADDING cells can now be constructed correctly with size 0



commit e70dcbda95d4e4cc5c87f1b71d2980d4c031330d
Author: Dave Rolek <dmr-x@xxxxxxxxxx>
Date:   Sun Jun 3 21:16:36 2018 +0000

    VPADDING cells can now be constructed correctly with size 0
    
    Unclear if the previous behavior was intentional, but the spec says
    nothing about requiring size > 0, so making this easier.
    
    It was already possible by specifying payload directly, so specifying
    size should have feature parity.
---
 stem/client/cell.py      | 2 +-
 test/unit/client/cell.py | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index ce9392b2..3c923b35 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -588,7 +588,7 @@ class VPaddingCell(Cell):
 
   def __init__(self, size = None, payload = None):
     if payload is None:
-      payload = os.urandom(size) if size else os.urandom(random.randint(128, 1024))
+      payload = os.urandom(size) if size is not None else os.urandom(random.randint(128, 1024))
     elif size 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)))
 
diff --git a/test/unit/client/cell.py b/test/unit/client/cell.py
index 76fe17ea..93452195 100644
--- a/test/unit/client/cell.py
+++ b/test/unit/client/cell.py
@@ -60,8 +60,10 @@ NETINFO_CELLS = {
   b'\x00\x00\x08ZZ\xb6\x90\x04\x04\x7f\x00\x00\x01\x01\x04\x04aq\x0f\x02' + ZERO * (FIXED_PAYLOAD_LEN - 17): (datetime.datetime(2018, 1, 14, 1, 46, 56), Address('127.0.0.1'), [Address('97.113.15.2')]),
 }
 
+VPADDING_CELL_EMPTY_PACKED = b'\x00\x00\x80\x00\x00'
+
 VPADDING_CELLS = {
-  b'\x00\x00\x80\x00\x00': b'',
+  VPADDING_CELL_EMPTY_PACKED: b'',
   b'\x00\x00\x80\x00\x01\x08': b'\x08',
   b'\x00\x00\x80\x00\x02\x08\x11': b'\x08\x11',
   b'\x00\x00\x80\x01\xfd' + RANDOM_PAYLOAD: RANDOM_PAYLOAD,
@@ -213,6 +215,10 @@ class TestCell(unittest.TestCase):
       self.assertEqual(cell_bytes, VPaddingCell(payload = payload).pack(2))
       self.assertEqual(payload, Cell.pop(cell_bytes, 2)[0].payload)
 
+    empty_constructed_cell = VPaddingCell(size = 0)
+    self.assertEqual(VPADDING_CELL_EMPTY_PACKED, empty_constructed_cell.pack(2))
+    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')
 
   def test_certs_cell(self):



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