commit c7ea62037adef91fe11728caab75e19096c69d52 Author: Damian Johnson atagar@torproject.org Date: Sun May 13 16:24:37 2012 -0700
Parsing conn-bi-direct extrainfo lines
Another special snowflake extrainfo attribute. Parsing and added a test. --- stem/descriptor/extrainfo_descriptor.py | 34 +++++++++++++++++++++++ test/unit/descriptor/extrainfo_descriptor.py | 38 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py index 1363579..2b6997c 100644 --- a/stem/descriptor/extrainfo_descriptor.py +++ b/stem/descriptor/extrainfo_descriptor.py @@ -180,6 +180,13 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): geoip_db_digest (str) - sha1 of geoIP database file signature (str) - signature for this extrainfo descriptor (*)
+ conn_bi_direct_end (datetime) - end of the sampling interval + conn_bi_direct_interval (int) - seconds per interval + conn_bi_direct_below (int) - connections that read/wrote less than 20 KiB + conn_bi_direct_read (int) - connections that read at least 10x more than wrote + conn_bi_direct_write (int) - connections that wrote at least 10x more than read + conn_bi_direct_both (int) - remaining connections + Bytes read/written for relayed traffic: read_history_end (datetime) - end of the sampling interval read_history_interval (int) - seconds per interval @@ -274,6 +281,13 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): self.geoip_db_digest = None self.signature = None
+ self.conn_bi_direct_end = None + self.conn_bi_direct_interval = None + self.conn_bi_direct_below = None + self.conn_bi_direct_read = None + self.conn_bi_direct_write = None + self.conn_bi_direct_both = None + self.read_history_end = None self.read_history_interval = None self.read_history_values = None @@ -528,6 +542,26 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): self.dir_stats_interval = interval except ValueError, exc: if validate: raise exc + elif keyword == "conn-bi-direct": + # "conn-bi-direct" YYYY-MM-DD HH:MM:SS (NSEC s) BELOW,READ,WRITE,BOTH + + try: + timestamp, interval, remainder = _parse_timestamp_and_interval(keyword, value) + stats = remainder.split(",") + + if len(stats) != 4 or not \ + (stats[0].isdigit() and stats[1].isdigit() and \ + stats[2].isdigit() and stats[3].isdigit()): + raise ValueError("conn-bi-direct line should end with four numeric values: %s" % line) + + self.conn_bi_direct_end = timestamp + self.conn_bi_direct_interval = interval + self.conn_bi_direct_below = int(stats[0]) + self.conn_bi_direct_read = int(stats[1]) + self.conn_bi_direct_write = int(stats[2]) + self.conn_bi_direct_both = int(stats[3]) + except ValueError, exc: + if validate: raise exc elif keyword in ("read-history", "write-history", "dirreq-read-history", "dirreq-write-history"): # "<keyword>" YYYY-MM-DD HH:MM:SS (NSEC s) NUM,NUM,NUM,NUM,NUM... try: diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py index f856341..76a4344 100644 --- a/test/unit/descriptor/extrainfo_descriptor.py +++ b/test/unit/descriptor/extrainfo_descriptor.py @@ -231,6 +231,44 @@ class TestExtraInfoDescriptor(unittest.TestCase): self.assertEqual({}, getattr(desc, attr)) self.assertEqual({}, getattr(desc, unknown_attr))
+ def test_conn_bi_direct(self): + """ + Parses the conn-bi-direct line with valid and invalid data. + """ + + desc_text = _make_descriptor({"conn-bi-direct": "2012-05-03 12:07:50 (500 s) 277431,12089,0,2134"}) + desc = ExtraInfoDescriptor(desc_text) + self.assertEquals(datetime.datetime(2012, 5, 3, 12, 7, 50), desc.conn_bi_direct_end) + self.assertEquals(500, desc.conn_bi_direct_interval) + self.assertEquals(277431, desc.conn_bi_direct_below) + self.assertEquals(12089, desc.conn_bi_direct_read) + self.assertEquals(0, desc.conn_bi_direct_write) + self.assertEquals(2134, desc.conn_bi_direct_both) + + test_entries = ( + "", + "2012-05-03 ", + "2012-05-03", + "2012-05-03 12:07:60 (500 s)", + "2012-05-03 12:07:50 (500s)", + "2012-05-03 12:07:50 (500 s", + "2012-05-03 12:07:50 (500 )", + "2012-05-03 12:07:50 (500 s)11", + "2012-05-03 12:07:50 (500 s) 277431,12089,0", + "2012-05-03 12:07:50 (500 s) 277431,12089,0a,2134", + "2012-05-03 12:07:50 (500 s) -277431,12089,0,2134", + ) + + for entry in test_entries: + desc_text = _make_descriptor({"conn-bi-direct": entry}) + desc = self._expect_invalid_attr(desc_text) + self.assertEquals(None, desc.conn_bi_direct_end) + self.assertEquals(None, desc.conn_bi_direct_interval) + self.assertEquals(None, desc.conn_bi_direct_below) + self.assertEquals(None, desc.conn_bi_direct_read) + self.assertEquals(None, desc.conn_bi_direct_write) + self.assertEquals(None, desc.conn_bi_direct_both) + def test_percentage_lines(self): """ Uses valid and invalid data to tests lines of the form...