1 18 package org.apache.activemq.security; 19 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Properties ; 23 import java.util.Set ; 24 25 import javax.naming.Context ; 26 import javax.naming.NameClassPair ; 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 29 import javax.naming.directory.DirContext ; 30 import javax.naming.directory.InitialDirContext ; 31 32 import org.apache.activemq.command.ActiveMQDestination; 33 import org.apache.activemq.command.ActiveMQQueue; 34 import org.apache.activemq.command.ActiveMQTopic; 35 import org.apache.activemq.jaas.GroupPrincipal; 36 import org.apache.directory.server.core.configuration.StartupConfiguration; 37 import org.apache.directory.server.core.jndi.CoreContextFactory; 38 import org.springframework.context.ApplicationContext; 39 import org.springframework.context.support.ClassPathXmlApplicationContext; 40 41 import junit.framework.TestCase; 42 43 50 public class LDAPAuthorizationMapTest extends TestCase { 51 private HashMap options; 52 private LDAPAuthorizationMap authMap; 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 57 startLdapServer(); 58 59 authMap = new LDAPAuthorizationMap(); 60 } 61 62 protected void startLdapServer() throws Exception { 63 ApplicationContext factory = new ClassPathXmlApplicationContext("org/apache/activemq/security/ldap-spring.xml"); 64 StartupConfiguration cfg = (StartupConfiguration) factory.getBean("configuration"); 65 Properties env = (Properties ) factory.getBean("environment"); 66 67 env.setProperty(Context.PROVIDER_URL, ""); 68 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName()); 69 env.putAll(cfg.toJndiEnvironment()); 70 71 new InitialDirContext (env); 72 } 73 74 protected void tearDown() throws Exception { 75 super.tearDown(); 76 } 77 78 public void testOpen() throws Exception { 79 DirContext ctx = authMap.open(); 80 HashSet set = new HashSet (); 81 NamingEnumeration list = ctx.list("ou=destinations,o=ActiveMQ,dc=example,dc=com"); 82 while (list.hasMore()) { 83 NameClassPair ncp = (NameClassPair ) list.next(); 84 set.add(ncp.getName()); 85 } 86 assertTrue(set.contains("ou=topics")); 87 assertTrue(set.contains("ou=queues")); 88 } 89 90 94 public void testGetAdminACLs() { 95 ActiveMQDestination q1 = new ActiveMQQueue("queue1"); 96 Set aclsq1 = authMap.getAdminACLs(q1); 97 assertEquals(1, aclsq1.size()); 98 assertTrue(aclsq1.contains(new GroupPrincipal("role1"))); 99 100 ActiveMQDestination t1 = new ActiveMQTopic("topic1"); 101 Set aclst1 = authMap.getAdminACLs(t1); 102 assertEquals(1, aclst1.size()); 103 assertTrue(aclst1.contains(new GroupPrincipal("role1"))); 104 } 105 106 110 public void testGetReadACLs() { 111 ActiveMQDestination q1 = new ActiveMQQueue("queue1"); 112 Set aclsq1 = authMap.getReadACLs(q1); 113 assertEquals(1, aclsq1.size()); 114 assertTrue(aclsq1.contains(new GroupPrincipal("role1"))); 115 116 ActiveMQDestination t1 = new ActiveMQTopic("topic1"); 117 Set aclst1 = authMap.getReadACLs(t1); 118 assertEquals(1, aclst1.size()); 119 assertTrue(aclst1.contains(new GroupPrincipal("role2"))); 120 } 121 122 126 public void testGetWriteACLs() { 127 ActiveMQDestination q1 = new ActiveMQQueue("queue1"); 128 Set aclsq1 = authMap.getWriteACLs(q1); 129 assertEquals(2, aclsq1.size()); 130 assertTrue(aclsq1.contains(new GroupPrincipal("role1"))); 131 assertTrue(aclsq1.contains(new GroupPrincipal("role2"))); 132 133 ActiveMQDestination t1 = new ActiveMQTopic("topic1"); 134 Set aclst1 = authMap.getWriteACLs(t1); 135 assertEquals(1, aclst1.size()); 136 assertTrue(aclst1.contains(new GroupPrincipal("role3"))); 137 } 138 139 } 140 | Popular Tags |