[tor-commits] [stem/master] Unpack cell header

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


commit 4e07952399317df4f3250db162165b55fd94ada4
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jan 11 12:07:49 2018 -0800

    Unpack cell header
    
    Just unpacking enough to know which class unpack function for us to defer to.
---
 stem/client/cell.py      | 11 ++++++++---
 test/unit/client/cell.py |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index 42fe87e1..7a1644b3 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -93,11 +93,12 @@ class Cell(collections.namedtuple('Cell', ['name', 'value', 'fixed_size', 'for_c
     raise ValueError("'%s' isn't a valid cell value" % value)
 
   @staticmethod
-  def unpack(content):
+  def unpack(content, link_version):
     """
     Unpacks encoded bytes into a Cell subclass.
 
     :param bytes content: payload to decode
+    :param int link_version: link protocol version
 
     :returns: :class:`~stem.client.cell.Cell` subclass
 
@@ -106,7 +107,9 @@ class Cell(collections.namedtuple('Cell', ['name', 'value', 'fixed_size', 'for_c
       * NotImplementedError if unable to unpack this cell type
     """
 
-    return AuthorizeCell._unpack(content)
+    circ_id, content = Size.LONG.pop(content) if link_version > 3 else Size.SHORT.pop(content)
+    command, content = Size.CHAR.pop(content)
+    return Cell.by_value(command)._unpack(content, link_version, circ_id)
 
   @classmethod
   def _pack(cls, link_version, payload, circ_id = 0):
@@ -141,11 +144,13 @@ class Cell(collections.namedtuple('Cell', ['name', 'value', 'fixed_size', 'for_c
     return cell
 
   @classmethod
-  def _unpack(cls, content):
+  def _unpack(cls, content, circ_id, link_version):
     """
     Subclass implementation for unpacking cell content.
 
     :param bytes content: payload to decode
+    :param int link_version: link protocol version
+    :param int circ_id: circuit id cell is for
 
     :returns: instance of this cell type
 
diff --git a/test/unit/client/cell.py b/test/unit/client/cell.py
index 1335a77c..857cab84 100644
--- a/test/unit/client/cell.py
+++ b/test/unit/client/cell.py
@@ -29,7 +29,7 @@ class TestCell(unittest.TestCase):
     self.assertRaises(ValueError, Cell.by_value, None)
 
   def test_unpack_not_implemented(self):
-    self.assertRaisesRegexp(NotImplementedError, 'Unpacking not yet implemented for AUTHORIZE cells', Cell.unpack, 'boom')
+    self.assertRaisesRegexp(NotImplementedError, 'Unpacking not yet implemented for AUTHORIZE cells', Cell.unpack, '\x00\x00\x84\x00\x06\x00\x01\x00\x02\x00\x03', 2)
 
   def test_versions_pack(self):
     self.assertEqual('\x00\x00\x07\x00\x00', VersionsCell.pack([]))





More information about the tor-commits mailing list