[tor-commits] [stem/master] Boilerplate descriptor tests for DirectoryAuthority
atagar at torproject.org
atagar at torproject.org
Sat Oct 13 18:35:45 UTC 2012
commit 347d4c03a63b8a3f81bee83ee990e5ea4ada9125
Author: Damian Johnson <atagar at torproject.org>
Date: Fri Oct 5 08:53:17 2012 -0700
Boilerplate descriptor tests for DirectoryAuthority
Copying a few general tests from the other descriptors. I should move these
into a descriptor test superclass so I don't need to keep copying them. That
said, there are some minor tweaking that might prevent that...
---
stem/descriptor/networkstatus.py | 2 +-
.../networkstatus/directory_authority.py | 61 +++++++++++++++++++-
2 files changed, 61 insertions(+), 2 deletions(-)
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index fa02e44..039c90c 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -684,7 +684,7 @@ class DirectoryAuthority(stem.descriptor.Descriptor):
**\*** mandatory attribute
"""
- def __init__(self, raw_content, validate, is_vote = True):
+ def __init__(self, raw_content, validate = True, is_vote = False):
"""
Parse a directory authority entry in a v3 network status document.
diff --git a/test/unit/descriptor/networkstatus/directory_authority.py b/test/unit/descriptor/networkstatus/directory_authority.py
index dd4c5fa..cf16506 100644
--- a/test/unit/descriptor/networkstatus/directory_authority.py
+++ b/test/unit/descriptor/networkstatus/directory_authority.py
@@ -4,7 +4,8 @@ Unit tests for the DirectoryAuthority of stem.descriptor.networkstatus.
import unittest
-from test.mocking import get_directory_authority, get_key_certificate
+from stem.descriptor.networkstatus import DirectoryAuthority
+from test.mocking import get_directory_authority, get_key_certificate, AUTHORITY_HEADER
class TestDirectoryAuthority(unittest.TestCase):
def test_minimal_consensus_authority(self):
@@ -44,4 +45,62 @@ class TestDirectoryAuthority(unittest.TestCase):
self.assertEqual(None, authority.legacy_dir_key)
self.assertEqual(get_key_certificate(), authority.key_certificate)
self.assertEqual([], authority.get_unrecognized_lines())
+
+ def test_unrecognized_line(self):
+ """
+ Includes unrecognized content in the descriptor.
+ """
+
+ authority = get_directory_authority({"pepperjack": "is oh so tasty!"})
+ self.assertEquals(["pepperjack is oh so tasty!"], authority.get_unrecognized_lines())
+
+ def test_first_line(self):
+ """
+ Includes a non-mandatory field before the 'dir-source' line.
+ """
+
+ content = "ho-hum 567\n" + get_directory_authority(content = True)
+ self.assertRaises(ValueError, DirectoryAuthority, content)
+
+ authority = DirectoryAuthority(content, False)
+ self.assertEqual("turtles", authority.nickname)
+ self.assertEqual(["ho-hum 567"], authority.get_unrecognized_lines())
+
+ def test_missing_fields(self):
+ """
+ Parse an authority where a mandatory field is missing.
+ """
+
+ for excluded_field in ("dir-source", "contact"):
+ content = get_directory_authority(exclude = (excluded_field,), content = True)
+ self.assertRaises(ValueError, DirectoryAuthority, content)
+
+ authority = DirectoryAuthority(content, False)
+
+ if excluded_field == "dir-source":
+ self.assertEqual("Mike Perry <email>", authority.contact)
+ else:
+ self.assertEqual("turtles", authority.nickname)
+
+ def test_blank_lines(self):
+ """
+ Includes blank lines, which should be ignored.
+ """
+
+ authority = get_directory_authority({"dir-source": AUTHORITY_HEADER[0][1] + "\n\n\n"})
+ self.assertEqual("Mike Perry <email>", authority.contact)
+
+ def test_duplicate_lines(self):
+ """
+ Duplicates linesin the entry.
+ """
+
+ lines = get_directory_authority(content = True).split("\n")
+
+ for i in xrange(len(lines)):
+ content = "\n".join(lines[:i] + [lines[i]] + lines[i:])
+ self.assertRaises(ValueError, DirectoryAuthority, content)
+
+ authority = DirectoryAuthority(content, False)
+ self.assertEqual("turtles", authority.nickname)
More information about the tor-commits
mailing list