[tor-commits] [stem/master] Unit test to include directory authorities in document
atagar at torproject.org
atagar at torproject.org
Sat Oct 13 18:35:45 UTC 2012
commit 72cb24dde90c928135a686707db6baa5a88d5147
Author: Damian Johnson <atagar at torproject.org>
Date: Sat Oct 6 14:09:34 2012 -0700
Unit test to include directory authorities in document
Adding a document test that includes authority entries. The especially
interesting bit of this is checking that validation propagates to the
KeyCertificate constructor (ie, it's checking that the 'validate' flag is being
passed from the document to authority and authority to cert classes).
---
stem/descriptor/networkstatus.py | 6 +++
test/mocking.py | 10 ++++-
test/unit/descriptor/networkstatus/document.py | 43 ++++++++++++++++++++++-
3 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 80eb17b..59e616a 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -835,6 +835,12 @@ class DirectoryAuthority(stem.descriptor.Descriptor):
"""
return self._unrecognized_lines
+
+ def __cmp__(self, other):
+ if not isinstance(other, DirectoryAuthority):
+ return 1
+
+ return str(self) > str(other)
class KeyCertificate(stem.descriptor.Descriptor):
"""
diff --git a/test/mocking.py b/test/mocking.py
index 89ea2f6..2d656dd 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -591,13 +591,14 @@ def get_key_certificate(attr = None, exclude = (), content = False):
else:
return stem.descriptor.networkstatus.KeyCertificate(desc_content, validate = True)
-def get_network_status_document(attr = None, exclude = (), routers = None, content = False):
+def get_network_status_document(attr = None, exclude = (), authorities = None, routers = None, content = False):
"""
Provides the descriptor content for...
stem.descriptor.networkstatus.NetworkStatusDocument
:param dict attr: keyword/value mappings to be included in the descriptor
:param list exclude: mandatory keywords to exclude from the descriptor
+ :param list authorities: directory authorities to include in the document
:param list routers: router status entries to include in the document
:param bool content: provides the str content of the descriptor rather than the class if True
@@ -625,8 +626,13 @@ def get_network_status_document(attr = None, exclude = (), routers = None, conte
desc_content = _get_descriptor_content(attr, exclude, NETWORK_STATUS_DOCUMENT_HEADER, NETWORK_STATUS_DOCUMENT_FOOTER)
+ # inject the authorities and/or routers between the header and footer
+ if authorities:
+ footer_div = desc_content.find("\ndirectory-footer") + 1
+ authority_content = "\n".join([str(a) for a in authorities]) + "\n"
+ desc_content = desc_content[:footer_div] + authority_content + desc_content[footer_div:]
+
if routers:
- # inject the routers between the header and footer
footer_div = desc_content.find("\ndirectory-footer") + 1
router_content = "\n".join([str(r) for r in routers]) + "\n"
desc_content = desc_content[:footer_div] + router_content + desc_content[footer_div:]
diff --git a/test/unit/descriptor/networkstatus/document.py b/test/unit/descriptor/networkstatus/document.py
index 0d9f237..d0bb06a 100644
--- a/test/unit/descriptor/networkstatus/document.py
+++ b/test/unit/descriptor/networkstatus/document.py
@@ -8,8 +8,8 @@ import StringIO
import stem.version
from stem.descriptor import Flag
-from stem.descriptor.networkstatus import HEADER_STATUS_DOCUMENT_FIELDS, FOOTER_STATUS_DOCUMENT_FIELDS, DEFAULT_PARAMS, BANDWIDTH_WEIGHT_ENTRIES, RouterStatusEntry, NetworkStatusDocument, parse_file
-from test.mocking import get_router_status_entry, get_network_status_document, CRYPTO_BLOB, DOC_SIG
+from stem.descriptor.networkstatus import HEADER_STATUS_DOCUMENT_FIELDS, FOOTER_STATUS_DOCUMENT_FIELDS, DEFAULT_PARAMS, BANDWIDTH_WEIGHT_ENTRIES, RouterStatusEntry, DirectoryAuthority, NetworkStatusDocument, parse_file
+from test.mocking import get_router_status_entry, get_directory_authority, get_network_status_document, CRYPTO_BLOB, DOC_SIG
class TestNetworkStatusDocument(unittest.TestCase):
def test_minimal_consensus(self):
@@ -634,4 +634,43 @@ class TestNetworkStatusDocument(unittest.TestCase):
self.assertRaises(ValueError, NetworkStatusDocument, content)
document = NetworkStatusDocument(content, False)
self.assertEquals((entry3,), document.routers)
+
+ def test_with_directory_authorities(self):
+ """
+ Includes a couple directory authorities in the document.
+ """
+
+ for is_document_vote in (False, True):
+ for is_authorities_vote in (False, True):
+ authority1 = get_directory_authority({'contact': 'doctor jekyll'}, is_vote = is_authorities_vote)
+ authority2 = get_directory_authority({'contact': 'mister hyde'}, is_vote = is_authorities_vote)
+
+ vote_status = "vote" if is_document_vote else "consensus"
+ content = get_network_status_document({"vote-status": vote_status}, authorities = (authority1, authority2), content = True)
+
+ if is_document_vote == is_authorities_vote:
+ document = NetworkStatusDocument(content)
+ self.assertEquals((authority1, authority2), document.directory_authorities)
+ else:
+ # authority votes in a consensus or consensus authorities in a vote
+ self.assertRaises(ValueError, NetworkStatusDocument, content)
+ document = NetworkStatusDocument(content, validate = False)
+ self.assertEquals((authority1, authority2), document.directory_authorities)
+
+ def test_authority_validation_flag_propagation(self):
+ """
+ Includes invalid certificate content in an authority entry. This is testing
+ that the 'validate' flag propagages from the document to authority, and
+ authority to certificate classes.
+ """
+
+ # make the dir-key-published field of the certiciate be malformed
+ authority_content = get_directory_authority(is_vote = True, content = True)
+ authority_content = authority_content.replace("dir-key-published 2011", "dir-key-published 2011a")
+
+ content = get_network_status_document({"vote-status": "vote"}, authorities = (authority_content,), content = True)
+ self.assertRaises(ValueError, NetworkStatusDocument, content)
+
+ document = NetworkStatusDocument(content, validate = False)
+ self.assertEquals((DirectoryAuthority(authority_content, False, True),), document.directory_authorities)
More information about the tor-commits
mailing list