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

[tor-commits] [stem/master] Boilerplate KeyCertificate unit tests



commit 92b691c5e9ace6803a5ed24d42e7ec41665595c8
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Sep 29 10:19:16 2012 -0700

    Boilerplate KeyCertificate unit tests
    
    General unit tests that I've been including with most descriptor types. I
    should probably include these in a more systematic fasion...
---
 stem/descriptor/networkstatus.py                   |    4 +-
 .../descriptor/networkstatus/key_certificate.py    |   52 +++++++++++++++++++-
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 01fb2f3..ad74442 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -877,9 +877,11 @@ class KeyCertificate(stem.descriptor.Descriptor):
             raise ValueError("Key certificate's address isn't a valid IPv4 address: %s" % line)
           elif not stem.util.connection.is_valid_port(dirport):
             raise ValueError("Key certificate's dirport is invalid: %s" % line)
+        elif not dirport.isdigit():
+          continue
         
         self.address = address
-        self.dir_port = dirport
+        self.dir_port = int(dirport)
       elif keyword == 'fingerprint':
         # "fingerprint" fingerprint
         
diff --git a/test/unit/descriptor/networkstatus/key_certificate.py b/test/unit/descriptor/networkstatus/key_certificate.py
index 703b61d..e3f62e0 100644
--- a/test/unit/descriptor/networkstatus/key_certificate.py
+++ b/test/unit/descriptor/networkstatus/key_certificate.py
@@ -6,7 +6,7 @@ import datetime
 import unittest
 
 from stem.descriptor.networkstatus import KeyCertificate
-from test.mocking import get_key_certificate, CRYPTO_BLOB
+from test.mocking import get_key_certificate, CRYPTO_BLOB, KEY_CERTIFICATE_HEADER, KEY_CERTIFICATE_FOOTER
 
 class TestKeyCertificate(unittest.TestCase):
   def test_minimal(self):
@@ -27,4 +27,54 @@ class TestKeyCertificate(unittest.TestCase):
     self.assertEqual(None, certificate.crosscert)
     self.assertTrue(CRYPTO_BLOB in certificate.certification)
     self.assertEqual([], certificate.get_unrecognized_lines())
+  
+  def test_unrecognized_line(self):
+    """
+    Includes unrecognized content in the descriptor.
+    """
+    
+    certificate = get_key_certificate({"pepperjack": "is oh so tasty!"})
+    self.assertEquals(["pepperjack is oh so tasty!"], certificate.get_unrecognized_lines())
+  
+  def test_first_and_last_lines(self):
+    """
+    Includes a non-mandatory field before the 'dir-key-certificate-version'
+    line or after the 'dir-key-certification' line.
+    """
+    
+    content = get_key_certificate(content = True)
+    
+    for cert_text in ("dir-address 127.0.0.1:80\n" + content,
+                      content + "\ndir-address 127.0.0.1:80"):
+      self.assertRaises(ValueError, KeyCertificate, cert_text)
+      
+      certificate = KeyCertificate(cert_text, False)
+      self.assertEqual("127.0.0.1", certificate.address)
+      self.assertEqual(80, certificate.dir_port)
+  
+  def test_missing_fields(self):
+    """
+    Parse a key certificate where a mandatory field is missing.
+    """
+    
+    mandatory_fields = [entry[0] for entry in KEY_CERTIFICATE_HEADER + KEY_CERTIFICATE_FOOTER]
+    
+    for excluded_field in mandatory_fields:
+      content = get_key_certificate(exclude = (excluded_field,), content = True)
+      self.assertRaises(ValueError, KeyCertificate, content)
+      
+      certificate = KeyCertificate(content, False)
+      
+      if excluded_field == "fingerprint":
+        self.assertEqual(3, certificate.version)
+      else:
+        self.assertEqual("27B6B5996C426270A5C95488AA5BCEB6BCC86956", certificate.fingerprint)
+  
+  def test_blank_lines(self):
+    """
+    Includes blank lines, which should be ignored.
+    """
+    
+    certificate = get_key_certificate({"dir-key-published": "2011-11-28 21:51:04\n\n\n"})
+    self.assertEqual(datetime.datetime(2011, 11, 28, 21, 51, 4), certificate.published)
 



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