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

[tor-commits] [stem/master] Parse lifetime field



commit eb36c093b44950a784e102ef18029aa3915ad9c7
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Aug 22 17:03:19 2019 -0700

    Parse lifetime field
    
    Trivial from a parsing perspective but I gotta admit, I'm kinda confused how
    this is useful since unlike v2 the descriptor doesn't note its publication
    time.
---
 stem/descriptor/hidden_service.py         | 14 +++++++++++++-
 test/unit/descriptor/hidden_service_v3.py | 15 +++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/stem/descriptor/hidden_service.py b/stem/descriptor/hidden_service.py
index 4baad548..44bf114b 100644
--- a/stem/descriptor/hidden_service.py
+++ b/stem/descriptor/hidden_service.py
@@ -157,7 +157,16 @@ def _parse_version_line(descriptor, entries):
   if value.isdigit():
     descriptor.version = int(value)
   else:
-    raise ValueError('version line must have a positive integer value: %s' % value)
+    raise ValueError('%s line must have a positive integer value: %s' % (keyword, value))
+
+
+def _parse_lifetime(descriptor, entries):
+  value = _value('descriptor-lifetime', entries)
+
+  if value.isdigit():
+    descriptor.lifetime = int(value)
+  else:
+    raise ValueError('descriptor-lifetime line must have a positive integer value: %s' % value)
 
 
 def _parse_protocol_versions_line(descriptor, entries):
@@ -472,6 +481,7 @@ class HiddenServiceDescriptorV3(BaseHiddenServiceDescriptor):
   Version 3 hidden service descriptor.
 
   :var int version: **\\*** hidden service descriptor version
+  :var int lifetime: **\\*** minutes after publication this descriptor is valid
 
   **\\*** attribute is either required when we're parsed with validation or has
   a default value, others are left as **None** if undefined
@@ -483,10 +493,12 @@ class HiddenServiceDescriptorV3(BaseHiddenServiceDescriptor):
 
   ATTRIBUTES = {
     'version': (None, _parse_version_line),
+    'lifetime': (None, _parse_lifetime),
   }
 
   PARSER_FOR_LINE = {
     'hs-descriptor': _parse_version_line,
+    'descriptor-lifetime': _parse_lifetime,
   }
 
   @classmethod
diff --git a/test/unit/descriptor/hidden_service_v3.py b/test/unit/descriptor/hidden_service_v3.py
index 04a95a6d..b7a23c86 100644
--- a/test/unit/descriptor/hidden_service_v3.py
+++ b/test/unit/descriptor/hidden_service_v3.py
@@ -29,6 +29,7 @@ class TestHiddenServiceDescriptorV3(unittest.TestCase):
       desc = next(stem.descriptor.parse_file(descriptor_file, 'hidden-service-descriptor-3 1.0', validate = True))
 
     self.assertEqual(3, desc.version)
+    self.assertEqual(180, desc.lifetime)
 
   def test_invalid_version(self):
     """
@@ -43,3 +44,17 @@ class TestHiddenServiceDescriptorV3(unittest.TestCase):
 
     for test_value in test_values:
       expect_invalid_attr(self, {'hs-descriptor': test_value}, 'version')
+
+  def test_invalid_lifetime(self):
+    """
+    Checks that our lifetime field expects a numeric value.
+    """
+
+    test_values = (
+      '',
+      '-10',
+      'hello',
+    )
+
+    for test_value in test_values:
+      expect_invalid_attr(self, {'descriptor-lifetime': test_value}, 'lifetime')



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