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

[tor-commits] [stem/master] Support for the ADDRMAP event's CACHED flag



commit 46d39df76cc51132c8e38c4fcbaabd45844e0c1c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Apr 6 16:52:09 2013 -0700

    Support for the ADDRMAP event's CACHED flag
    
    Adding support for the new CACHED flag added by Desoxy...
    
    https://trac.torproject.org/8596
    https://gitweb.torproject.org/torspec.git/commitdiff/25b0d43a9ebe89541625e9f1a090e4a701748c76
---
 stem/response/events.py      |   11 +++++++++++
 test/unit/response/events.py |   29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/stem/response/events.py b/stem/response/events.py
index 7ae4854..d9e3493 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -166,12 +166,15 @@ class AddrMapEvent(Event):
   :var datetime expiry: expiration time of the resolution in local time
   :var str error: error code if the resolution failed
   :var datetime utc_expiry: expiration time of the resolution in UTC
+  :var bool cached: **True** if the resolution will be kept until it expires,
+    **False** otherwise or **None** if undefined
   """
 
   _POSITIONAL_ARGS = ("hostname", "destination", "expiry")
   _KEYWORD_ARGS = {
     "error": "error",
     "EXPIRES": "utc_expiry",
+    "CACHED": "cached",
   }
   _OPTIONALLY_QUOTED = ("expiry")
 
@@ -191,6 +194,14 @@ class AddrMapEvent(Event):
     if self.utc_expiry is not None:
       self.utc_expiry = datetime.datetime.strptime(self.utc_expiry, "%Y-%m-%d %H:%M:%S")
 
+    if self.cached is not None:
+      if self.cached == "YES":
+        self.cached = True
+      elif self.cached == "NO":
+        self.cached = False
+      else:
+        raise stem.ProtocolError("An ADDRMAP event's CACHED mapping can only be 'YES' or 'NO': %s" % self)
+
 
 class AuthDirNewDescEvent(Event):
   """
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 98e7f95..e41c80c 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -29,6 +29,17 @@ EXPIRES="2012-11-19 08:50:13"'
 ADDRMAP_BAD_2 = '650 ADDRMAP www.atagar.com 75.119.206.243 "2012-11-19 00:50:13 \
 EXPIRES="2012-11-19 08:50:13"'
 
+ADDRMAP_CACHED = '650 ADDRMAP example.com 192.0.43.10 "2013-04-03 22:31:22" \
+EXPIRES="2013-04-03 20:31:22" \
+CACHED="YES"'
+
+ADDRMAP_NOT_CACHED = '650 ADDRMAP example.com 192.0.43.10 "2013-04-03 22:29:11" \
+EXPIRES="2013-04-03 20:29:11" \
+CACHED="NO"'
+
+ADDRMAP_CACHED_MALFORMED = '650 ADDRMAP example.com 192.0.43.10 "2013-04-03 22:29:11" \
+CACHED="KINDA"'
+
 # BUILDTIMEOUT_SET event from tor 0.2.3.16.
 
 BUILD_TIMEOUT_EVENT = "650 BUILDTIMEOUT_SET COMPUTED \
@@ -401,11 +412,29 @@ class TestEvents(unittest.TestCase):
     self.assertEqual(datetime.datetime(2012, 11, 19, 0, 50, 13), event.expiry)
     self.assertEqual("yes", event.error)
     self.assertEqual(datetime.datetime(2012, 11, 19, 8, 50, 13), event.utc_expiry)
+    self.assertEqual(None, event.cached)
 
     # malformed content where quotes are missing
     self.assertRaises(ProtocolError, _get_event, ADDRMAP_BAD_1)
     self.assertRaises(ProtocolError, _get_event, ADDRMAP_BAD_2)
 
+    # check the CACHED flag
+
+    event = _get_event(ADDRMAP_CACHED)
+
+    self.assertTrue(isinstance(event, stem.response.events.AddrMapEvent))
+    self.assertEqual("example.com", event.hostname)
+    self.assertEqual(True, event.cached)
+
+    event = _get_event(ADDRMAP_NOT_CACHED)
+
+    self.assertTrue(isinstance(event, stem.response.events.AddrMapEvent))
+    self.assertEqual("example.com", event.hostname)
+    self.assertEqual(False, event.cached)
+
+    # the CACHED argument should only allow YES or NO
+    self.assertRaises(ProtocolError, _get_event, ADDRMAP_CACHED_MALFORMED)
+
   def test_authdir_newdesc_event(self):
     # TODO: awaiting test data - https://trac.torproject.org/7534
 

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