[tor-commits] [stem/master] Replace encode() with conventional pack()

atagar at torproject.org atagar at torproject.org
Sun Nov 17 23:40:39 UTC 2019


commit d27f15e3627ac45568df436ae4ef7ed9c4fc479f
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Oct 12 13:43:30 2019 -0700

    Replace encode() with conventional pack()
    
    Other classes in datatype.py support encoding via a pack() method. Changing
    both for consistency, readability, as well as python 2.x support...
    
      ======================================================================
      FAIL: test_encode_decode
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/client/link_specifier.py", line 70, in test_encode_decode
          self.assertEqual(test_bytes, test_case)
      AssertionError: '[3][32]CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC' != '\x03 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'
---
 stem/client/datatype.py            | 21 ++++++---------------
 test/unit/client/link_specifier.py | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/stem/client/datatype.py b/stem/client/datatype.py
index 22212036..b1f65955 100644
--- a/stem/client/datatype.py
+++ b/stem/client/datatype.py
@@ -574,21 +574,12 @@ class LinkSpecifier(object):
     else:
       return LinkSpecifier(link_type, value), content  # unrecognized type
 
-  def encode(self):
-    """
-    Encode this link specifier to bytes
-
-      LSTYPE (Link specifier type)           [1 byte]
-      LSLEN  (Link specifier length)         [1 byte]
-      LSPEC  (Link specifier)                [LSLEN bytes]
-    """
-    ls = b""
-
-    ls += bytes([self.type])
-    ls += bytes([len(self.value)])
-    ls += self.value
-
-    return ls
+  def pack(self):
+    cell = bytearray()
+    cell += Size.CHAR.pack(self.type)
+    cell += Size.CHAR.pack(len(self.value))
+    cell += self.value
+    return bytes(cell)
 
 class LinkByIPv4(LinkSpecifier):
   """
diff --git a/test/unit/client/link_specifier.py b/test/unit/client/link_specifier.py
index 9ae5c6f9..b42ea57c 100644
--- a/test/unit/client/link_specifier.py
+++ b/test/unit/client/link_specifier.py
@@ -58,14 +58,14 @@ class TestLinkSpecifier(unittest.TestCase):
   def test_wrong_size(self):
     self.assertRaisesWith(ValueError, 'Link specifier should have 32 bytes, but only had 7 remaining', LinkSpecifier.pop, b'\x04\x20CCCCCCC')
 
-  def test_encode_decode(self):
-    test_cases = [b'\x03\x20CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
-                  b'\x04\x20CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
-                  b'\x01\x12&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01#)',
-                  b'\x00\x06\x01\x02\x03\x04#)' ]
-
-    for test_case in test_cases:
-      destination, _ = LinkSpecifier.pop(test_case)
-      test_bytes = destination.encode()
-      self.assertEqual(test_bytes, test_case)
-
+  def test_pack(self):
+    test_inputs = (
+      b'\x03\x20CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
+      b'\x04\x20CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC',
+      b'\x01\x12&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01#)',
+      b'\x00\x06\x01\x02\x03\x04#)',
+    )
+
+    for val in test_inputs:
+      destination, _ = LinkSpecifier.pop(val)
+      self.assertEqual(val, destination.pack())





More information about the tor-commits mailing list