|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License at |
| 4 | +// http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 8 | +// License for the specific language governing rights and limitations |
| 9 | +// under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developers of the Original Code are LShift Ltd, |
| 14 | +// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd. |
| 15 | +// |
| 16 | +// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, |
| 17 | +// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd |
| 18 | +// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial |
| 19 | +// Technologies LLC, and Rabbit Technologies Ltd. |
| 20 | +// |
| 21 | +// Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift |
| 22 | +// Ltd. Portions created by Cohesive Financial Technologies LLC are |
| 23 | +// Copyright (C) 2007-2009 Cohesive Financial Technologies |
| 24 | +// LLC. Portions created by Rabbit Technologies Ltd are Copyright |
| 25 | +// (C) 2007-2009 Rabbit Technologies Ltd. |
| 26 | +// |
| 27 | +// All Rights Reserved. |
| 28 | +// |
| 29 | +// Contributor(s): ______________________________________. |
| 30 | +// |
| 31 | + |
| 32 | +package com.rabbitmq.client.test.functional; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +import com.rabbitmq.client.AMQP; |
| 37 | +import com.rabbitmq.client.Channel; |
| 38 | +import com.rabbitmq.client.Command; |
| 39 | +import com.rabbitmq.client.Connection; |
| 40 | +import com.rabbitmq.client.ConnectionFactory; |
| 41 | +import com.rabbitmq.client.ConnectionParameters; |
| 42 | +import com.rabbitmq.client.Method; |
| 43 | +import com.rabbitmq.client.QueueingConsumer; |
| 44 | +import com.rabbitmq.client.ShutdownSignalException; |
| 45 | +import com.rabbitmq.client.impl.AMQChannel; |
| 46 | +import com.rabbitmq.client.impl.AMQImpl; |
| 47 | +import com.rabbitmq.tools.Host; |
| 48 | + |
| 49 | +public class Permissions extends BrokerTestCase |
| 50 | +{ |
| 51 | + |
| 52 | + protected Channel adminCh; |
| 53 | + |
| 54 | + public Permissions() |
| 55 | + { |
| 56 | + ConnectionParameters params = new ConnectionParameters(); |
| 57 | + params.setUsername("test"); |
| 58 | + params.setPassword("test"); |
| 59 | + params.setVirtualHost("/test"); |
| 60 | + connectionFactory = new ConnectionFactory(params); |
| 61 | + } |
| 62 | + |
| 63 | + protected void setUp() |
| 64 | + throws IOException |
| 65 | + { |
| 66 | + addRestrictedAccount(); |
| 67 | + super.setUp(); |
| 68 | + } |
| 69 | + |
| 70 | + protected void tearDown() |
| 71 | + throws IOException |
| 72 | + { |
| 73 | + super.tearDown(); |
| 74 | + deleteRestrictedAccount(); |
| 75 | + } |
| 76 | + |
| 77 | + protected void addRestrictedAccount() |
| 78 | + throws IOException |
| 79 | + { |
| 80 | + runCtl("add_user test test"); |
| 81 | + runCtl("add_user testadmin test"); |
| 82 | + runCtl("add_vhost /test"); |
| 83 | + runCtl("set_permissions -p /test test configure write read"); |
| 84 | + runCtl("set_permissions -p /test testadmin \".*\" \".*\" \".*\""); |
| 85 | + } |
| 86 | + |
| 87 | + protected void deleteRestrictedAccount() |
| 88 | + throws IOException |
| 89 | + { |
| 90 | + runCtl("clear_permissions -p /test testadmin"); |
| 91 | + runCtl("clear_permissions -p /test test"); |
| 92 | + runCtl("delete_vhost /test"); |
| 93 | + runCtl("delete_user testadmin"); |
| 94 | + runCtl("delete_user test"); |
| 95 | + } |
| 96 | + |
| 97 | + protected void runCtl(String command) |
| 98 | + throws IOException |
| 99 | + { |
| 100 | + Host.executeCommand("../rabbitmq-server/scripts/rabbitmqctl " + |
| 101 | + command); |
| 102 | + } |
| 103 | + |
| 104 | + protected void createResources() |
| 105 | + throws IOException |
| 106 | + { |
| 107 | + ConnectionParameters params = new ConnectionParameters(); |
| 108 | + params.setUsername("testadmin"); |
| 109 | + params.setPassword("test"); |
| 110 | + params.setVirtualHost("/test"); |
| 111 | + ConnectionFactory factory = new ConnectionFactory(params); |
| 112 | + Connection connection = factory.newConnection("localhost"); |
| 113 | + adminCh = connection.createChannel(); |
| 114 | + withNames(new WithName() { |
| 115 | + public void with(String name) throws IOException { |
| 116 | + adminCh.exchangeDeclare(name, "direct"); |
| 117 | + adminCh.queueDeclare(name); |
| 118 | + }}); |
| 119 | + } |
| 120 | + |
| 121 | + protected void releaseResources() |
| 122 | + throws IOException |
| 123 | + { |
| 124 | + withNames(new WithName() { |
| 125 | + public void with(String name) throws IOException { |
| 126 | + adminCh.queueDelete(name); |
| 127 | + adminCh.exchangeDelete(name); |
| 128 | + }}); |
| 129 | + adminCh.getConnection().abort(); |
| 130 | + } |
| 131 | + |
| 132 | + protected void withNames(WithName action) |
| 133 | + throws IOException |
| 134 | + { |
| 135 | + action.with("configure"); |
| 136 | + action.with("write"); |
| 137 | + action.with("read"); |
| 138 | + } |
| 139 | + |
| 140 | + public void testExchangeConfiguration() |
| 141 | + throws IOException |
| 142 | + { |
| 143 | + runConfigureTest(new WithName() { |
| 144 | + public void with(String name) throws IOException { |
| 145 | + channel.exchangeDeclare(name, "direct"); |
| 146 | + }}); |
| 147 | + runConfigureTest(new WithName() { |
| 148 | + public void with(String name) throws IOException { |
| 149 | + channel.exchangeDeclare(name, "direct", true, false, false, null); |
| 150 | + }}); |
| 151 | + runConfigureTest(new WithName() { |
| 152 | + public void with(String name) throws IOException { |
| 153 | + channel.exchangeDelete(name); |
| 154 | + }}); |
| 155 | + } |
| 156 | + |
| 157 | + public void testQueueConfiguration() |
| 158 | + throws IOException |
| 159 | + { |
| 160 | + runConfigureTest(new WithName() { |
| 161 | + public void with(String name) throws IOException { |
| 162 | + channel.queueDeclare(name); |
| 163 | + }}); |
| 164 | + runConfigureTest(new WithName() { |
| 165 | + public void with(String name) throws IOException { |
| 166 | + channel.queueDeclare(name, true, false, false, false, null); |
| 167 | + }}); |
| 168 | + runConfigureTest(new WithName() { |
| 169 | + public void with(String name) throws IOException { |
| 170 | + channel.queueDelete(name); |
| 171 | + }}); |
| 172 | + } |
| 173 | + |
| 174 | + public void testBinding() |
| 175 | + throws IOException |
| 176 | + { |
| 177 | + runTest(false, true, false, new WithName() { |
| 178 | + public void with(String name) throws IOException { |
| 179 | + channel.queueBind(name, "read", ""); |
| 180 | + }}); |
| 181 | + runTest(false, false, true, new WithName() { |
| 182 | + public void with(String name) throws IOException { |
| 183 | + channel.queueBind("write", name, ""); |
| 184 | + }}); |
| 185 | + } |
| 186 | + |
| 187 | + public void testPublish() |
| 188 | + throws IOException |
| 189 | + { |
| 190 | + runTest(false, true, false, new WithName() { |
| 191 | + public void with(String name) throws IOException { |
| 192 | + channel.basicPublish(name, "", null, "foo".getBytes()); |
| 193 | + //followed by a dummy synchronous command in order |
| 194 | + //to catch any errors |
| 195 | + ((AMQChannel)channel).exnWrappingRpc(new AMQImpl.Channel.Flow(true)); |
| 196 | + }}); |
| 197 | + } |
| 198 | + |
| 199 | + public void testGet() |
| 200 | + throws IOException |
| 201 | + { |
| 202 | + runTest(false, false, true, new WithName() { |
| 203 | + public void with(String name) throws IOException { |
| 204 | + channel.basicGet(name, true); |
| 205 | + }}); |
| 206 | + } |
| 207 | + |
| 208 | + public void testConsume() |
| 209 | + throws IOException |
| 210 | + { |
| 211 | + runTest(false, false, true, new WithName() { |
| 212 | + public void with(String name) throws IOException { |
| 213 | + channel.basicConsume(name, new QueueingConsumer(channel)); |
| 214 | + }}); |
| 215 | + } |
| 216 | + |
| 217 | + public void testPurge() |
| 218 | + throws IOException |
| 219 | + { |
| 220 | + runTest(false, false, true, new WithName() { |
| 221 | + public void with(String name) throws IOException { |
| 222 | + ((AMQChannel)channel).exnWrappingRpc(new AMQImpl.Queue.Purge(0, name, false)); |
| 223 | + }}); |
| 224 | + } |
| 225 | + |
| 226 | + protected void runConfigureTest(WithName test) |
| 227 | + throws IOException |
| 228 | + { |
| 229 | + runTest(true, "configure-me", test); |
| 230 | + runTest(false, "write-me", test); |
| 231 | + runTest(false, "read-me", test); |
| 232 | + } |
| 233 | + |
| 234 | + protected void runTest(boolean expC, boolean expW, boolean expR, |
| 235 | + WithName test) |
| 236 | + throws IOException |
| 237 | + { |
| 238 | + runTest(expC, "configure", test); |
| 239 | + runTest(expW, "write", test); |
| 240 | + runTest(expR, "read", test); |
| 241 | + } |
| 242 | + |
| 243 | + protected void runTest(boolean exp, String name, WithName test) |
| 244 | + throws IOException |
| 245 | + { |
| 246 | + try { |
| 247 | + test.with(name); |
| 248 | + assertTrue(exp); |
| 249 | + } catch (IOException e) { |
| 250 | + assertFalse(exp); |
| 251 | + Throwable t = e.getCause(); |
| 252 | + assertTrue(t instanceof ShutdownSignalException); |
| 253 | + Object r = ((ShutdownSignalException)t).getReason(); |
| 254 | + assertTrue(r instanceof Command); |
| 255 | + Method m = ((Command)r).getMethod(); |
| 256 | + assertTrue(m instanceof AMQP.Channel.Close); |
| 257 | + assertEquals(AMQP.ACCESS_REFUSED, |
| 258 | + ((AMQP.Channel.Close)m).getReplyCode()); |
| 259 | + //This fails due to bug 20296 |
| 260 | + //openChannel(); |
| 261 | + channel = connection.createChannel(channel.getChannelNumber() + 1); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + public interface WithName { |
| 266 | + public void with(String name) throws IOException; |
| 267 | + } |
| 268 | + |
| 269 | +} |
0 commit comments