directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smckin...@apache.org
Subject [07/51] [partial] directory-fortress-core git commit: FC-109 - rename rbac package to impl
Date Tue, 02 Jun 2015 18:36:32 GMT
http://git-wip-us.apache.org/repos/asf/directory-fortress-core/blob/ba64d26a/src/test/java/org/apache/directory/fortress/core/impl/accelerator/TestAccelerator.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/directory/fortress/core/impl/accelerator/TestAccelerator.java
b/src/test/java/org/apache/directory/fortress/core/impl/accelerator/TestAccelerator.java
new file mode 100644
index 0000000..73826ba
--- /dev/null
+++ b/src/test/java/org/apache/directory/fortress/core/impl/accelerator/TestAccelerator.java
@@ -0,0 +1,277 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.fortress.core.impl.accelerator;
+
+import org.apache.directory.fortress.core.*;
+import org.apache.directory.fortress.core.SecurityException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.directory.fortress.core.AccelMgr;
+import org.apache.directory.fortress.core.model.Permission;
+import org.apache.directory.fortress.core.model.Session;
+import org.apache.directory.fortress.core.impl.TestUtils;
+import org.apache.directory.fortress.core.model.User;
+import org.apache.directory.fortress.core.model.UserRole;
+
+import static org.junit.Assert.*;
+
+public class TestAccelerator
+{
+    private static final Logger LOG = LoggerFactory.getLogger( TestAccelerator.class );
+
+    @BeforeClass
+    public static void testSetup()
+    {
+    }
+
+    @AfterClass
+    public static void testCleanup()
+    {
+        // Teardown for data used by the unit tests
+    }
+
+    @Test
+    public void testSessionRoles()
+    {
+        LOG.info( "testSessionRoles..." );
+        User user = new User();
+        user.setUserId( "rbacuser1" );
+        user.setPassword( "secret".toCharArray() );
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+            accelMgr.sessionRoles( session );
+        }
+        catch( org.apache.directory.fortress.core.SecurityException se)
+        {
+            se.printStackTrace();
+        }
+    }
+
+    @Test
+    public void testCreateSession()
+    {
+        LOG.info( "testCreateSession..." );
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            User user = new User();
+            // positive test case:
+            user.setUserId( "rbacuser1" );
+            user.setPassword( "secret".toCharArray() );
+            user.setRole( "rbacrole1" );
+            user.setRole( "rbacrole2" );
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+            assertTrue( session.isAuthenticated() );
+            try
+            {
+                // negative test case:
+                user.setUserId( "rbacuser1" );
+                user.setPassword( "secretx".toCharArray() );
+                session = null;
+                session = accelMgr.createSession( user, false );
+                fail("failed negative createSession for rbacuser1");
+            }
+            catch( SecurityException se)
+            {
+                // sucess
+            }
+
+            // negative case should leave the session null.
+            assertNull( session );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+    }
+
+    @Test
+    public void testCheckAccess()
+    {
+        AccelMgr accelMgr = null;
+        LOG.info( "testCheckAccess..." );
+        User user = new User();
+        user.setUserId( "rbacuser1" );
+        user.setPassword( "secret".toCharArray() );
+        //user.setRole( "rbacrole1" );
+        //user.setRole( "rbacrole2" );
+        Session session = null;
+        try
+        {
+            accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+        try
+        {
+            // positive test case:
+            Permission perm = new Permission();
+            perm.setObjName( "/impl/cal2.jsp" );
+            //perm.setObjId( "123456" );
+            perm.setOpName( "8am" );
+            boolean result = accelMgr.checkAccess( session, perm );
+            assertTrue( result );
+            // negative test case:
+            perm.setOpName( "9am" );
+            result = accelMgr.checkAccess( session, perm );
+            assertTrue( !result );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+    }
+
+    @Test
+    public void testDeleteSession()
+    {
+        LOG.info( "testDeleteSession..." );
+        User user = new User();
+        user.setUserId( "rbacuser1" );
+        user.setPassword( "secret".toCharArray() );
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+            accelMgr.deleteSession( session );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+        }
+    }
+
+    //@Test
+    public void testAddActiveRole()
+    {
+        LOG.info( "testAddActiveRole..." );
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            User user = new User();
+            // positive test case:
+            user.setUserId( "rbacuser1" );
+            user.setPassword( "secret".toCharArray() );
+            user.setRole( "rbacrole1" );
+            //user.setRole( "rbacrole2" );
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+            assertTrue( session.isAuthenticated() );
+            UserRole userRole = new UserRole( user.getUserId(), "rbacrole2" );
+            accelMgr.addActiveRole( session, userRole );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+    }
+
+    @Test
+    public void testDropActiveRole()
+    {
+        LOG.info( "testDropActiveRole..." );
+        //AcceleratorDAO aDao = new org.apache.directory.fortress.core.impl.AcceleratorDAO();
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            User user = new User();
+            // positive test case:
+            user.setUserId( "rbacuser1" );
+            user.setPassword( "secret".toCharArray() );
+            //user.setRole( "rbacrole1" );
+            //user.setRole( "rbacrole2" );
+            session = accelMgr.createSession( user, false );
+            assertNotNull( session );
+            assertTrue( session.isAuthenticated() );
+            UserRole userRole = new UserRole( user.getUserId(), "rbacrole2" );
+            accelMgr.dropActiveRole( session, userRole );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+    }
+
+
+    @Test
+    public void testCombinedCalls()
+    {
+        LOG.info( "testCombinedCalls..." );
+        try
+        {
+            AccelMgr accelMgr = AccelMgrFactory.createInstance( TestUtils.getContext() );
+            Session session;
+            User user = new User();
+            // positive test case:
+            user.setUserId( "rbacuser1" );
+            user.setPassword( "secret".toCharArray() );
+            session = accelMgr.createSession( user, false );
+            // positive test case:
+            Permission perm = new Permission();
+            perm.setObjName( "/impl/cal2.jsp" );
+            perm.setOpName( "8am" );
+            boolean result = accelMgr.checkAccess( session, perm );
+            assertTrue( result );
+
+            // drop role1:
+            UserRole userRole = new UserRole( user.getUserId(), "rbacrole1" );
+            accelMgr.dropActiveRole( session, userRole );
+
+            // this should return false:
+            result = accelMgr.checkAccess( session, perm );
+            assertTrue( !result );
+
+            // now add role1 back again:
+            userRole = new UserRole( user.getUserId(), "rbacrole1" );
+            accelMgr.addActiveRole( session, userRole );
+
+            // this should return true:
+            result = accelMgr.checkAccess( session, perm );
+            assertTrue( result );
+        }
+        catch( SecurityException se)
+        {
+            se.printStackTrace();
+            fail();
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-fortress-core/blob/ba64d26a/src/test/java/org/apache/directory/fortress/core/impl/apacheds/AdminManagerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/directory/fortress/core/impl/apacheds/AdminManagerTest.java
b/src/test/java/org/apache/directory/fortress/core/impl/apacheds/AdminManagerTest.java
new file mode 100644
index 0000000..96f5283
--- /dev/null
+++ b/src/test/java/org/apache/directory/fortress/core/impl/apacheds/AdminManagerTest.java
@@ -0,0 +1,180 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.fortress.core.impl.apacheds;
+
+
+import static org.junit.Assert.fail;
+
+import java.util.Set;
+
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifFiles;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreatePartition;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.directory.fortress.core.AdminMgr;
+import org.apache.directory.fortress.core.AdminMgrFactory;
+import org.apache.directory.fortress.core.SecurityException;
+import org.apache.directory.fortress.core.ldap.ApacheDsDataProvider;
+import org.apache.directory.fortress.core.ldap.LdapCounters;
+import org.apache.directory.fortress.core.impl.DelegatedMgrImplTest;
+import org.apache.directory.fortress.core.impl.FortressJUnitTest;
+import org.apache.directory.fortress.core.impl.OrgUnitTestData;
+import org.apache.directory.fortress.core.model.Session;
+import org.apache.directory.fortress.core.impl.TestUtils;
+import org.apache.directory.fortress.core.model.User;
+import org.apache.directory.fortress.core.model.UserRole;
+import org.apache.directory.fortress.core.impl.UserTestData;
+import org.apache.directory.fortress.core.util.LogUtil;
+import org.apache.directory.fortress.core.util.cache.CacheMgr;
+
+
+@RunWith(FrameworkRunner.class)
+@CreateDS(name = "classDS", partitions =
+    { @CreatePartition(name = "example", suffix = "dc=example,dc=com") })
+@CreateLdapServer(
+    transports =
+        {
+            @CreateTransport(protocol = "LDAP", port = 10389)
+    })
+@ApplyLdifFiles(
+    { "fortress-schema.ldif", "init-ldap.ldif"/*, "test-data.ldif"*/})
+public class AdminManagerTest extends AbstractLdapTestUnit
+{
+    private static final Logger LOG = LoggerFactory.getLogger( AdminManagerTest.class.getName()
);
+    private static Session adminSess = null;
+
+
+    @Before
+    public void init()
+    {
+        CacheMgr.getInstance().clearAll();
+    }
+
+
+    @After
+    public void displayCounters()
+    {
+        LdapCounters counters = ApacheDsDataProvider.getLdapCounters();
+        System.out.println( "NUMBER OF READS: " + counters.getRead() );
+        System.out.println( "NUMBER OF SEARCHES: " + counters.getSearch() );
+        System.out.println( "NUMBER OF COMPARES: " + counters.getCompare() );
+        System.out.println( "NUMBER OF BINDS: " + counters.getBind() );
+        System.out.println( "NUMBER OF ADDS: " + counters.getAdd() );
+        System.out.println( "NUMBER OF MODS: " + counters.getMod() );
+        System.out.println( "NUMBER OF DELETES: " + counters.getDelete() );
+    }
+
+
+    /**
+     *
+     * @return
+     * @throws org.apache.directory.fortress.core.SecurityException
+     */
+    private AdminMgr getManagedAdminMgr() throws SecurityException
+    {
+        if ( FortressJUnitTest.isAdminEnabled() && adminSess == null )
+        {
+            adminSess = DelegatedMgrImplTest.createAdminSession();
+        }
+
+        return AdminMgrFactory.createInstance( TestUtils.getContext(), adminSess );
+    }
+
+
+    /**
+     * @param uArray
+     */
+    private void addUsers( String msg, String[][] uArray, boolean isAdmin )
+    {
+        LogUtil.logIt( msg );
+        
+        try
+        {
+            AdminMgr adminMgr;
+            
+            if ( isAdmin )
+            {
+                adminMgr = getManagedAdminMgr();
+            }
+            else
+            {
+                adminMgr = AdminMgrFactory.createInstance( TestUtils.getContext() );
+            }
+            
+            for ( String[] usr : uArray )
+            {
+                User user = UserTestData.getUser( usr );
+                adminMgr.addUser( user );
+                LOG.debug( "addUsers user [" + user.getUserId() + "] successful" );
+                // Does User have Role assignments?
+                Set<String> asgnRoles = UserTestData.getAssignedRoles( usr );
+                if ( asgnRoles != null )
+                {
+                    for ( String name : asgnRoles )
+                    {
+                        adminMgr.assignUser( new UserRole( user.getUserId(), name ) );
+                    }
+                }
+            }
+        }
+        catch ( SecurityException ex )
+        {
+            ex.printStackTrace();
+            LOG.error(
+                "addUsers: caught SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(),
ex );
+            fail( ex.getMessage() );
+        }
+    }
+
+
+    @Test
+    public void testAddUser()
+    {
+        // Add the mandatory elements
+        DelegatedMgrImplTest.addOrgUnits( "ADD ORGS_DEV1", OrgUnitTestData.ORGS_DEV1 );
+
+        //     public User addUser(User user)
+        addUsers( "ADD-USRS TU1", UserTestData.USERS_TU1, true );
+        addUsers( "ADD-USRS TU2", UserTestData.USERS_TU2, true );
+        addUsers( "ADD-USRS TU3", UserTestData.USERS_TU3, true );
+        addUsers( "ADD-USRS TU4", UserTestData.USERS_TU4, true );
+        addUsers( "ADD-USRS TU5", UserTestData.USERS_TU5, true );
+        addUsers( "ADD-USRS TU6", UserTestData.USERS_TU6, true );
+        addUsers( "ADD-USRS TU7_HIER", UserTestData.USERS_TU7_HIER, true );
+        addUsers( "ADD-USRS TU8_SSD", UserTestData.USERS_TU8_SSD, true );
+        addUsers( "ADD-USRS TU9_SSD_HIER", UserTestData.USERS_TU9_SSD_HIER, true );
+        addUsers( "ADD-USRS TU10_SSD_HIER", UserTestData.USERS_TU10_SSD_HIER, true );
+        addUsers( "ADD-USRS TU11_SSD_HIER", UserTestData.USERS_TU11_SSD_HIER, true );
+        addUsers( "ADD-USRS TU12_DSD", UserTestData.USERS_TU12_DSD, true );
+        addUsers( "ADD-USRS TU13_DSD_HIER", UserTestData.USERS_TU13_DSD_HIER, true );
+        addUsers( "ADD-USRS TU14_DSD_HIER", UserTestData.USERS_TU14_DSD_HIER, true );
+        addUsers( "ADD-USRS TU15_DSD_HIER", UserTestData.USERS_TU15_DSD_HIER, true );
+    }
+}


Mime
View raw message