1 18 package org.apache.activemq.security; 19 20 import org.apache.activemq.CombinationTestSupport; 21 import org.apache.activemq.JmsTestSupport; 22 import org.apache.activemq.broker.BrokerService; 23 import org.apache.activemq.command.ActiveMQDestination; 24 import org.apache.activemq.command.ActiveMQMessage; 25 import org.apache.activemq.command.ActiveMQQueue; 26 import org.apache.activemq.command.ActiveMQTopic; 27 28 import javax.jms.Connection ; 29 import javax.jms.JMSException ; 30 import javax.jms.Message ; 31 import javax.jms.MessageConsumer ; 32 import javax.jms.Session ; 33 import javax.jms.TextMessage ; 34 35 39 public class SecurityTestSupport extends JmsTestSupport { 40 41 public ActiveMQDestination destination; 42 43 46 protected BrokerService createBroker() throws Exception { 47 BrokerService broker = super.createBroker(); 48 broker.setPopulateJMSXUserID(true); 49 return broker; 50 } 51 52 public void testUserReceiveFails() throws JMSException { 53 doReceive(true); 54 } 55 56 public void testInvalidAuthentication() throws JMSException { 57 try { 58 Connection c = factory.createConnection(); 60 connections.add(c); 61 c.start(); 62 fail("Expected exception."); 63 } 64 catch (JMSException e) { 65 } 66 67 try { 68 Connection c = factory.createConnection("user", "krap"); 70 connections.add(c); 71 c.start(); 72 fail("Expected exception."); 73 } 74 catch (JMSException e) { 75 } 76 77 try { 78 Connection c = factory.createConnection("userkrap", null); 80 connections.add(c); 81 c.start(); 82 fail("Expected exception."); 83 } 84 catch (JMSException e) { 85 } 86 } 87 88 public void testUserReceiveSucceeds() throws JMSException { 89 Message m = doReceive(false); 90 assertEquals("system", ((ActiveMQMessage)m).getUserID()); 91 assertEquals("system", m.getStringProperty("JMSXUserID")); 92 } 93 94 public void testGuestReceiveSucceeds() throws JMSException { 95 doReceive(false); 96 } 97 98 public void testGuestReceiveFails() throws JMSException { 99 doReceive(true); 100 } 101 102 public void testUserSendSucceeds() throws JMSException { 103 Message m = doSend(false); 104 assertEquals("user", ((ActiveMQMessage)m).getUserID()); 105 assertEquals("user", m.getStringProperty("JMSXUserID")); 106 } 107 108 public void testUserSendFails() throws JMSException { 109 doSend(true); 110 } 111 112 public void testGuestSendFails() throws JMSException { 113 doSend(true); 114 } 115 116 public void testGuestSendSucceeds() throws JMSException { 117 doSend(false); 118 } 119 120 123 public Message doSend(boolean fail) throws JMSException { 124 125 Connection adminConnection = factory.createConnection("system", "manager"); 126 connections.add(adminConnection); 127 128 adminConnection.start(); 129 Session adminSession = adminConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 130 MessageConsumer consumer = adminSession.createConsumer(destination); 131 132 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 133 try { 134 sendMessages(session, destination, 1); 135 } 136 catch (JMSException e) { 137 if (!fail || !(e.getCause() instanceof SecurityException )) { 141 throw e; 142 } 143 } 144 145 Message m = consumer.receive(1000); 146 if (fail) 147 assertNull(m); 148 else { 149 assertNotNull(m); 150 assertEquals("0", ((TextMessage ) m).getText()); 151 assertNull(consumer.receiveNoWait()); 152 } 153 return m; 154 } 155 156 159 public Message doReceive(boolean fail) throws JMSException { 160 161 connection.start(); 162 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 163 MessageConsumer consumer = null; 164 try { 165 consumer = session.createConsumer(destination); 166 if (fail) 167 fail("Expected failure due to security constraint."); 168 } 169 catch (JMSException e) { 170 if (fail && e.getCause() instanceof SecurityException ) 171 return null; 172 throw e; 173 } 174 175 Connection adminConnection = factory.createConnection("system", "manager"); 176 connections.add(adminConnection); 177 Session adminSession = adminConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 178 sendMessages(adminSession, destination, 1); 179 180 Message m = consumer.receive(1000); 181 assertNotNull(m); 182 assertEquals("0", ((TextMessage ) m).getText()); 183 assertNull(consumer.receiveNoWait()); 184 return m; 185 186 } 187 188 191 public void initCombosForTestUserReceiveFails() { 192 addCombinationValues("userName", new Object [] { "user" }); 193 addCombinationValues("password", new Object [] { "password" }); 194 addCombinationValues("destination", new Object [] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("GUEST.BAR"), 195 new ActiveMQTopic("GUEST.BAR"), }); 196 } 197 198 201 public void initCombosForTestInvalidAuthentication() { 202 addCombinationValues("userName", new Object [] { "user" }); 203 addCombinationValues("password", new Object [] { "password" }); 204 } 205 206 209 public void initCombosForTestUserReceiveSucceeds() { 210 addCombinationValues("userName", new Object [] { "user" }); 211 addCombinationValues("password", new Object [] { "password" }); 212 addCombinationValues("destination", new Object [] { new ActiveMQQueue("USERS.FOO"), new ActiveMQTopic("USERS.FOO"), }); 213 } 214 215 218 public void initCombosForTestGuestReceiveSucceeds() { 219 addCombinationValues("userName", new Object [] { "guest" }); 220 addCombinationValues("password", new Object [] { "password" }); 221 addCombinationValues("destination", new Object [] { new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR"), }); 222 } 223 224 227 public void initCombosForTestGuestReceiveFails() { 228 addCombinationValues("userName", new Object [] { "guest" }); 229 addCombinationValues("password", new Object [] { "password" }); 230 addCombinationValues("destination", new Object [] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"), 231 new ActiveMQTopic("USERS.FOO"), }); 232 } 233 234 237 public void initCombosForTestUserSendSucceeds() { 238 addCombinationValues("userName", new Object [] { "user" }); 239 addCombinationValues("password", new Object [] { "password" }); 240 addCombinationValues("destination", new Object [] { new ActiveMQQueue("USERS.FOO"), new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("USERS.FOO"), 241 new ActiveMQTopic("GUEST.BAR"), }); 242 } 243 244 247 public void initCombosForTestUserSendFails() { 248 addCombinationValues("userName", new Object [] { "user" }); 249 addCombinationValues("password", new Object [] { "password" }); 250 addCombinationValues("destination", new Object [] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), }); 251 } 252 253 256 public void initCombosForTestGuestSendFails() { 257 addCombinationValues("userName", new Object [] { "guest" }); 258 addCombinationValues("password", new Object [] { "password" }); 259 addCombinationValues("destination", new Object [] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"), 260 new ActiveMQTopic("USERS.FOO"), }); 261 } 262 263 266 public void initCombosForTestGuestSendSucceeds() { 267 addCombinationValues("userName", new Object [] { "guest" }); 268 addCombinationValues("password", new Object [] { "password" }); 269 addCombinationValues("destination", new Object [] { new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR"), }); 270 } 271 } 272 | Popular Tags |