[tor-commits] [stem/master] Unit test to exercise all general auth use cases

atagar at torproject.org atagar at torproject.org
Tue Dec 27 00:56:42 UTC 2011


commit e642cb669ebc01055ac1873c709ef57585efe99f
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Dec 26 13:52:17 2011 -0800

    Unit test to exercise all general auth use cases
    
    I'm aiming for both expansive and deep unit testing for the general
    authenticate method and this does the former. It runs the authenticate()
    function under all combinations of valid input and having authenticate_*
    functions either succeed or raise one of its exceptions. At present this just
    checks that the authenticate() function always either succeeds or raises a
    AuthenticationFailure - an important test, but I'll next try to make it a
    deeper validity check.
---
 test/unit/connection/authentication.py |   66 +++++++++++++++++++++++++++++++-
 1 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/test/unit/connection/authentication.py b/test/unit/connection/authentication.py
index a570bfe..bae723f 100644
--- a/test/unit/connection/authentication.py
+++ b/test/unit/connection/authentication.py
@@ -20,7 +20,7 @@ def raise_exception(exception_type):
   if not exception_type: return no_op()
   
   def _raise(exc_type, *args):
-    raise exc_type
+    raise exc_type(None)
   
   return functools.partial(_raise, exception_type)
 
@@ -31,6 +31,25 @@ def get_protocolinfo(auth_methods):
   protocolinfo_response.auth_methods = auth_methods
   return lambda *args: protocolinfo_response
 
+def _get_all_auth_method_combinations():
+  """
+  Enumerates all types of authentication that a PROTOCOLINFO response may
+  provide, returning a tuple with the AuthMethod enums.
+  """
+  
+  for is_none in (False, True):
+    for is_password in (False, True):
+      for is_cookie in (False, True):
+        for is_unknown in (False, True):
+          auth_methods = []
+          
+          if is_none: auth_methods.append(stem.connection.AuthMethod.NONE)
+          if is_password: auth_methods.append(stem.connection.AuthMethod.PASSWORD)
+          if is_cookie: auth_methods.append(stem.connection.AuthMethod.COOKIE)
+          if is_unknown: auth_methods.append(stem.connection.AuthMethod.UNKNOWN)
+          
+          yield tuple(auth_methods)
+
 class TestAuthenticate(unittest.TestCase):
   """
   Under the covers the authentiate function really just translates a
@@ -78,4 +97,49 @@ class TestAuthenticate(unittest.TestCase):
     # tests where get_protocolinfo raises a SocketError
     stem.connection.get_protocolinfo = raise_exception(stem.socket.SocketError)
     self.assertRaises(stem.connection.AuthenticationFailure, stem.connection.authenticate, None)
+  
+  def test_all_use_cases(self):
+    """
+    Does basic validation that all valid use cases for the PROTOCOLINFO input
+    and dependent functions result in either success or a AuthenticationFailed
+    subclass being raised.
+    """
+    
+    # exceptions that the authentication functions are documented to raise
+    auth_none_exc_types = (None,
+      stem.connection.OpenAuthRejected)
+    
+    auth_password_exc_types = (None,
+      stem.connection.PasswordAuthRejected,
+      stem.connection.IncorrectPassword)
+    
+    auth_cookie_exc_types = (None,
+      stem.connection.IncorrectCookieSize,
+      stem.connection.UnreadableCookieFile,
+      stem.connection.CookieAuthRejected,
+      stem.connection.IncorrectCookieValue)
+    
+    # auth functions don't suppress controller exceptions
+    control_exc_types = (
+      stem.socket.ProtocolError,
+      stem.socket.SocketError,
+      stem.socket.SocketClosed)
+    
+    for auth_methods in _get_all_auth_method_combinations():
+      protocolinfo_input = get_protocolinfo(auth_methods)()
+      
+      for auth_none_exc in auth_none_exc_types + control_exc_types:
+        for auth_password_exc in auth_password_exc_types + control_exc_types:
+          for auth_cookie_exc in auth_cookie_exc_types + control_exc_types:
+            stem.connection.authenticate_none = raise_exception(auth_none_exc)
+            stem.connection.authenticate_password = raise_exception(auth_password_exc)
+            stem.connection.authenticate_cookie = raise_exception(auth_cookie_exc)
+            
+            # calling authenticate should either succeed or raise a
+            # AuthenticationFailure subclass
+            
+            try:
+              stem.connection.authenticate(None, protocolinfo_response = protocolinfo_input)
+            except stem.connection.AuthenticationFailure:
+              pass
 





More information about the tor-commits mailing list