[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/tests - testSecurityManager.py:1.1.2.3

Tres Seaver tseaver@zope.com
Fri, 30 Nov 2001 22:39:59 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv30342/tests

Modified Files:
      Tag: Zope-3x-branch
	testSecurityManager.py 
Log Message:


  - Clean up docstrings on 'IExecutableObject' and
    'IExecutableObjectWithCustomSecurityPolicy', including note that both
    are "pseudo" interfaces (no objects will actually assert them).

  - Rename '[I]SecurityManger.addContext' -> 'pushExecutable', and
    'removeContext' -> 'popExecutable', to avoid name clash with the
    SecurityContext objects used by security policies (also makes the
    stacked nature more explicit).

  - Added tests for 'SecurityManager.pushExecutable' and 'popExecutable'.

  - Ensured that all SecurityManger tests restore security policy, if
    munged.


=== Zope3/lib/python/Zope/App/Security/tests/testSecurityManager.py 1.1.2.2 => 1.1.2.3 ===
         return 1
 
+class DummyExecutable:
+
+    """__implements__ = (pseudo) IExecutableObject"""
+
+class DummyExecutableWithCustomPolicy:
+
+    """__implements__ = (pseudo) IExecutableObjectWithCustomSecurityPolicy"""
+
+    def _customSecurityPolicy( self ):
+        return PermissiveSecurityPolicy()
+
 class Test( unittest.TestCase ):
 
     _oldPolicy = None
 
     def setUp( self ):
+
         self._context = SecurityContext( 'xyzzy' )
 
     def tearDown( self ):
+
         if self._oldPolicy is not None:
             from Zope.App.Security.SecurityManager import setSecurityPolicy
             setSecurityPolicy( self._oldPolicy )
@@ -48,24 +61,26 @@
         return SecurityManager( self._context )
 
     def _setPermissive( self ):
+
         from Zope.App.Security.SecurityManager import setSecurityPolicy
         self._oldPolicy = setSecurityPolicy( PermissiveSecurityPolicy() )
             
     def test_import( self ):
+
         from Zope.App.Security.SecurityManager import SecurityManager
         from Zope.App.Security.ISecurityManager import ISecurityManager
 
         verify( ISecurityManager, SecurityManager )
 
     def test_empty( self ):
-        
+ 
         mgr = self._makeMgr()
 
         self.assertEqual( mgr.getUser(), self._context.user )
         self.failIf( mgr.calledByExecutable() )
 
     def test_w_default_policy( self ):
-        
+ 
         mgr = self._makeMgr()
 
         self.assertRaises( Unauthorized, mgr.validate, None, None )
@@ -73,10 +88,93 @@
         self.failIf( mgr.checkPermission( None, None ) )
 
     def test_w_permissive_policy( self ):
-        
+ 
         mgr = self._makeMgr()
         self._setPermissive()
 
         mgr.validate( None, None )
         mgr.validateValue( None )
         self.failUnless( mgr.checkPermission( None, None ) )
+
+    def test_exec_stack_overflow( self ):
+ 
+        from Zope.App.Security.SecurityManager import MAX_STACK_SIZE
+        mgr = self._makeMgr()
+
+        for i in range( MAX_STACK_SIZE ):
+            mgr.pushExecutable( None )
+
+        self.assertRaises( SystemError, mgr.pushExecutable, None )
+
+    def test_pushExecutable_simple( self ):
+
+        mgr = self._makeMgr()
+        self.failIf( mgr.calledByExecutable() )
+
+        mgr.pushExecutable( DummyExecutable() )
+        self.failUnless( mgr.calledByExecutable() )
+
+    def test_popExecutable_simple( self ):
+
+        mgr = self._makeMgr()
+        exe = DummyExecutable()
+        exe2 = DummyExecutable()
+
+        mgr.pushExecutable( exe )
+        mgr.pushExecutable( exe2 )
+        mgr.popExecutable( exe2 )
+        self.failUnless( mgr.calledByExecutable() )
+
+        mgr.popExecutable( exe )
+        self.failIf( mgr.calledByExecutable() )
+
+    def test_popExecutable_nomatch( self ):
+
+        mgr = self._makeMgr()
+        exe = DummyExecutable()
+        exe2 = DummyExecutable()
+        other = DummyExecutable()
+
+        mgr.pushExecutable( exe )
+        mgr.pushExecutable( exe2 )
+        mgr.popExecutable( other ) # not on stack => no change
+        self.failUnless( mgr.calledByExecutable() )
+
+        mgr.popExecutable( exe ) # bottom of stack => empty it
+        self.failIf( mgr.calledByExecutable() )
+
+    def test_pushExecutable_customPolicy( self ):
+
+        mgr = self._makeMgr()
+        exe = DummyExecutableWithCustomPolicy()
+        self.failIf( mgr.checkPermission( None, None ) )
+        mgr.pushExecutable( exe )
+        self.failUnless( mgr.checkPermission( None, None ) )
+        mgr.popExecutable( exe )
+        self.failIf( mgr.checkPermission( None, None ) )
+
+    def test_pushPop_complexPolicies( self ):
+
+        mgr = self._makeMgr()
+
+        exe1 = DummyExecutableWithCustomPolicy()
+        exe2 = DummyExecutable()
+        exe3 = DummyExecutableWithCustomPolicy()
+
+        mgr.pushExecutable( exe1 ) # now has custom permissive policy
+        self.failUnless( mgr.checkPermission( None, None ) )
+
+        mgr.pushExecutable( exe2 ) # now has default policy
+        self.failIf( mgr.checkPermission( None, None ) )
+
+        mgr.pushExecutable( exe3 ) # now has custom permissive policy
+        self.failUnless( mgr.checkPermission( None, None ) )
+
+        mgr.popExecutable( exe3 ) # back to default policy
+        self.failIf( mgr.checkPermission( None, None ) )
+
+        mgr.popExecutable( exe2 ) # back to has custom permissive policy
+        self.failUnless( mgr.checkPermission( None, None ) )
+
+        mgr.popExecutable( exe1 ) # back to default policy
+        self.failIf( mgr.checkPermission( None, None ) )