Skip to content

Commit 4233563

Browse files
author
Simon MacMullen
committed
Attempt at a test
1 parent c7dfce2 commit 4233563

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.rabbitmq.client.test.server;
2+
3+
import com.rabbitmq.client.Address;
4+
import com.rabbitmq.client.AuthenticationFailureException;
5+
import com.rabbitmq.client.Connection;
6+
import com.rabbitmq.client.ConnectionFactory;
7+
import com.rabbitmq.tools.Host;
8+
import junit.framework.TestCase;
9+
10+
import java.io.IOException;
11+
import java.net.Inet4Address;
12+
import java.net.InetAddress;
13+
import java.net.NetworkInterface;
14+
import java.net.SocketException;
15+
import java.util.Enumeration;
16+
17+
public class LoopbackUsers extends TestCase {
18+
@Override
19+
protected void setUp() throws IOException {
20+
Host.rabbitmqctl("add_user test test");
21+
Host.rabbitmqctl("set_permissions test '.*' '.*' '.*'");
22+
}
23+
24+
@Override
25+
protected void tearDown() throws IOException {
26+
Host.rabbitmqctl("delete_user test");
27+
}
28+
29+
public void testLoopback() throws IOException {
30+
String addr = findRealIPAddress().getHostAddress();
31+
assertGuestFail(addr);
32+
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, []).'");
33+
assertGuestSucceed(addr);
34+
Host.rabbitmqctl("eval 'application:set_env(rabbit, loopback_users, [<<\"guest\">>]).'");
35+
assertGuestFail(addr);
36+
}
37+
38+
private void assertGuestSucceed(String addr) throws IOException {
39+
succeedConnect("guest", addr);
40+
succeedConnect("guest", "localhost");
41+
succeedConnect("test", addr);
42+
succeedConnect("test", "localhost");
43+
}
44+
45+
private void assertGuestFail(String addr) throws IOException {
46+
failConnect("guest", addr);
47+
succeedConnect("guest", "localhost");
48+
succeedConnect("test", addr);
49+
succeedConnect("test", "localhost");
50+
}
51+
52+
private void succeedConnect(String name, String addr) throws IOException {
53+
getFactory(name, addr).newConnection().close();
54+
}
55+
56+
private void failConnect(String name, String addr) throws IOException {
57+
try {
58+
getFactory(name, addr).newConnection();
59+
fail();
60+
}
61+
catch (AuthenticationFailureException e) {
62+
// success
63+
}
64+
}
65+
66+
private ConnectionFactory getFactory(String name, String addr) {
67+
ConnectionFactory factory = new ConnectionFactory();
68+
factory.setUsername(name);
69+
factory.setPassword(name);
70+
factory.setHost(addr);
71+
return factory;
72+
}
73+
74+
// Find the first IP address of a network interface that is up, not loopback, not point to point (e.g. VPN thing)
75+
private static InetAddress findRealIPAddress() throws SocketException {
76+
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
77+
while (ifs.hasMoreElements()) {
78+
NetworkInterface nif = ifs.nextElement();
79+
if (nif.isUp() && !nif.isPointToPoint() && !nif.isLoopback() && !nif.isVirtual()) {
80+
Enumeration<InetAddress> addrs = nif.getInetAddresses();
81+
while (addrs.hasMoreElements()) {
82+
InetAddress addr = addrs.nextElement();
83+
if (addr instanceof Inet4Address) {
84+
return addr;
85+
}
86+
}
87+
}
88+
}
89+
throw new RuntimeException("Could not determine real network address");
90+
}
91+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ public static void add(TestSuite suite) {
4242
suite.addTestSuite(Shutdown.class);
4343
suite.addTestSuite(BlockedConnection.class);
4444
suite.addTestSuite(ChannelLimitNegotiation.class);
45+
suite.addTestSuite(LoopbackUsers.class);
4546
}
4647
}

0 commit comments

Comments
 (0)