[tor-commits] [stem/master] Add support for encoding link specifiers.

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


commit 86974319b9e98668af9d5711b5d5e0a729237646
Author: George Kadianakis <desnacked at riseup.net>
Date:   Thu Oct 10 20:48:43 2019 +0300

    Add support for encoding link specifiers.
    
    We need this to encode v3 introduction points.
---
 stem/client/datatype.py            | 15 +++++++++++++++
 test/unit/client/link_specifier.py | 12 ++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/stem/client/datatype.py b/stem/client/datatype.py
index 7e33e353..22212036 100644
--- a/stem/client/datatype.py
+++ b/stem/client/datatype.py
@@ -574,6 +574,21 @@ 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
 
 class LinkByIPv4(LinkSpecifier):
   """
diff --git a/test/unit/client/link_specifier.py b/test/unit/client/link_specifier.py
index 470ee276..9ae5c6f9 100644
--- a/test/unit/client/link_specifier.py
+++ b/test/unit/client/link_specifier.py
@@ -57,3 +57,15 @@ 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)
+





More information about the tor-commits mailing list