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

[tor-commits] [stem/master] Support CERTS cell packing



commit 9c5e228ef67f7f6dc71a927296c973fd5bcb8960
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jan 14 11:08:48 2018 -0800

    Support CERTS cell packing
---
 stem/client/cell.py      | 24 +++++++++++++++++++++++-
 test/unit/client/cell.py | 28 ++++++++++++++--------------
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index e3793491..73c1c8f2 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -37,6 +37,7 @@ Messages communicated over a Tor relay's ORPort.
 """
 
 import inspect
+import io
 import sys
 
 from stem import UNDEFINED
@@ -278,7 +279,7 @@ class VersionsCell(Cell):
     # since VERSION cells avoid most version dependent attributes.
 
     payload = b''.join([Size.SHORT.pack(v) for v in versions])
-    return cls._pack(3, payload)
+    return cls._pack(2, payload)
 
   @classmethod
   def _unpack(cls, content, circ_id, link_version):
@@ -342,6 +343,27 @@ class CertsCell(Cell):
     self.certificates = certs
 
   @classmethod
+  def pack(cls, certs, link_version):
+    """
+    Provides the payload for a series of certificates.
+
+    :param list certs: series of :class:`~stem.client.Certificate` for the cell
+    :param int link_version: link protocol version
+
+    :returns: **bytes** with a payload for these versions
+    """
+
+    payload = io.BytesIO()
+    payload.write(Size.CHAR.pack(len(certs)))
+
+    for cert in certs:
+      payload.write(Size.CHAR.pack(cert.type))
+      payload.write(Size.SHORT.pack(len(cert.value)))
+      payload.write(cert.value)
+
+    return cls._pack(link_version, payload.getvalue())
+
+  @classmethod
   def _unpack(cls, content, circ_id, link_version):
     cert_count, content = Size.CHAR.pop(content)
     certs = []
diff --git a/test/unit/client/cell.py b/test/unit/client/cell.py
index 40b09d95..b26549e2 100644
--- a/test/unit/client/cell.py
+++ b/test/unit/client/cell.py
@@ -5,7 +5,7 @@ Unit tests for the stem.client.cell.
 import unittest
 
 from stem.client import Certificate
-from stem.client.cell import Cell, VersionsCell
+from stem.client.cell import Cell, VersionsCell, CertsCell
 from test.unit.client import test_data
 
 VERSIONS_CELLS = {
@@ -14,6 +14,12 @@ VERSIONS_CELLS = {
   '\x00\x00\x07\x00\x06\x00\x01\x00\x02\x00\x03': [1, 2, 3],
 }
 
+CERTS_CELLS = {
+  '\x00\x00\x81\x00\x01\x00': [],
+  '\x00\x00\x81\x00\x04\x01\x01\x00\x00': [Certificate(type = 1, value = '')],
+  '\x00\x00\x81\x00\x05\x01\x01\x00\x01\x08': [Certificate(type = 1, value = '\x08')],
+}
+
 
 class TestCell(unittest.TestCase):
   def test_by_name(self):
@@ -45,19 +51,13 @@ class TestCell(unittest.TestCase):
 
   def test_versions_packing(self):
     for cell_bytes, versions in VERSIONS_CELLS.items():
-      packed = VersionsCell.pack(versions)
-      self.assertEqual(cell_bytes, packed)
-      self.assertEqual(versions, Cell.unpack(packed, 2)[0].versions)
-
-  def test_certs_unpack(self):
-    test_data = {
-      '\x00\x00\x81\x00\x01\x00': [],
-      '\x00\x00\x81\x00\x04\x01\x01\x00\x00': [Certificate(type = 1, value = '')],
-      '\x00\x00\x81\x00\x05\x01\x01\x00\x01\x08': [Certificate(type = 1, value = '\x08')],
-    }
-
-    for cell_bytes, expected in test_data.items():
-      self.assertEqual(expected, Cell.unpack(cell_bytes, 2)[0].certificates)
+      self.assertEqual(cell_bytes, VersionsCell.pack(versions))
+      self.assertEqual(versions, Cell.unpack(cell_bytes, 2)[0].versions)
+
+  def test_certs_packing(self):
+    for cell_bytes, certs in CERTS_CELLS.items():
+      self.assertEqual(cell_bytes, CertsCell.pack(certs, 2))
+      self.assertEqual(certs, Cell.unpack(cell_bytes, 2)[0].certificates)
 
     # extra bytes after the last certificate should be ignored
 



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