Skip to content

Commit 312c34c

Browse files
Merge pull request #233 from rabbitmq/rabbitmq-server-505
Add integration test for topic authorisation
2 parents 4fc1bbb + 34eb055 commit 312c34c

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/test/java/com/rabbitmq/client/test/server/ServerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
LoopbackUsers.class,
4141
XDeathHeaderGrowth.class,
4242
PriorityQueues.class,
43-
43+
TopicPermissions.class
4444
})
4545
public class ServerTests {
4646

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.client.test.server;
17+
18+
import com.rabbitmq.client.AMQP;
19+
import com.rabbitmq.client.AlreadyClosedException;
20+
import com.rabbitmq.client.BuiltinExchangeType;
21+
import com.rabbitmq.client.test.BrokerTestCase;
22+
import com.rabbitmq.tools.Host;
23+
import org.junit.Test;
24+
25+
import java.io.IOException;
26+
import java.util.concurrent.Callable;
27+
import java.util.concurrent.TimeoutException;
28+
29+
import static org.junit.Assert.fail;
30+
31+
public class TopicPermissions extends BrokerTestCase {
32+
33+
String protectedTopic = "protected.topic";
34+
String notProtectedTopic = "not.protected.topic";
35+
String noneTopicExchange = "not.a.topic";
36+
37+
@Override
38+
protected void createResources() throws IOException, TimeoutException {
39+
channel.exchangeDeclare(protectedTopic, BuiltinExchangeType.TOPIC);
40+
channel.exchangeDeclare(notProtectedTopic, BuiltinExchangeType.TOPIC);
41+
channel.exchangeDeclare(noneTopicExchange, BuiltinExchangeType.DIRECT);
42+
43+
Host.rabbitmqctl("set_topic_permissions -p / guest " + protectedTopic + " \"^a\"");
44+
Host.rabbitmqctl("set_topic_permissions -p / guest " + noneTopicExchange + " \"^a\"");
45+
}
46+
47+
@Override
48+
protected void releaseResources() throws IOException {
49+
channel.exchangeDelete(protectedTopic);
50+
channel.exchangeDelete(notProtectedTopic);
51+
channel.exchangeDelete(noneTopicExchange);
52+
53+
Host.rabbitmqctl("clear_topic_permissions -p / guest");
54+
}
55+
56+
@Test
57+
public void topicPermissions() throws IOException {
58+
assertAccessOk("Routing key matches on protected topic, should pass", new Callable<Void>() {
59+
@Override
60+
public Void call() throws Exception {
61+
channel.basicPublish(protectedTopic, "a.b.c", null, "content".getBytes());
62+
channel.basicQos(0);
63+
return null;
64+
}
65+
});
66+
assertAccessRefused("Routing key does not match on protected topic, should not pass", new Callable<Void>() {
67+
@Override
68+
public Void call() throws Exception {
69+
channel.basicPublish(protectedTopic, "b.c", null, "content".getBytes());
70+
channel.basicQos(0);
71+
return null;
72+
}
73+
});
74+
assertAccessOk("Message sent on not-protected exchange, should pass", new Callable<Void>() {
75+
@Override
76+
public Void call() throws Exception {
77+
channel.basicPublish(notProtectedTopic, "a.b.c", null, "content".getBytes());
78+
channel.basicQos(0);
79+
return null;
80+
}
81+
});
82+
assertAccessOk("Routing key does not match on protected exchange, but not a topic, should pass", new Callable<Void>() {
83+
@Override
84+
public Void call() throws Exception {
85+
channel.basicPublish(noneTopicExchange, "b.c", null, "content".getBytes());
86+
channel.basicQos(0);
87+
return null;
88+
}
89+
});
90+
}
91+
92+
void assertAccessOk(String description, Callable<Void> action) {
93+
try {
94+
action.call();
95+
} catch(Exception e) {
96+
fail(description + " (" + e.getMessage()+")");
97+
}
98+
}
99+
100+
void assertAccessRefused(String description, Callable<Void> action) throws IOException {
101+
try {
102+
action.call();
103+
fail(description);
104+
} catch (IOException e) {
105+
checkShutdownSignal(AMQP.ACCESS_REFUSED, e);
106+
openChannel();
107+
} catch (AlreadyClosedException e) {
108+
checkShutdownSignal(AMQP.ACCESS_REFUSED, e);
109+
openChannel();
110+
} catch(Exception e) {
111+
fail("Unexpected exception: " + e.getMessage());
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)