Skip to content

Commit c20a65c

Browse files
author
Simon MacMullen
committed
Some tests of SASL mechanisms.
1 parent 61d9d02 commit c20a65c

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

test/src/com/rabbitmq/client/test/functional/FunctionalTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static TestSuite suite() {
7070
suite.addTestSuite(RecoverAfterCancel.class);
7171
suite.addTestSuite(UnexpectedFrames.class);
7272
suite.addTestSuite(PerQueueTTL.class);
73+
suite.addTestSuite(SaslMechanisms.class);
7374

7475
return suite;
7576
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.rabbitmq.client.test.functional;
2+
3+
import com.rabbitmq.client.Connection;
4+
import com.rabbitmq.client.ConnectionFactory;
5+
import com.rabbitmq.client.PossibleAuthenticationFailureException;
6+
import com.rabbitmq.client.SaslConfig;
7+
import com.rabbitmq.client.test.BrokerTestCase;
8+
9+
import javax.security.sasl.SaslClient;
10+
import javax.security.sasl.SaslException;
11+
import java.io.IOException;
12+
import java.util.Arrays;
13+
14+
public class SaslMechanisms extends BrokerTestCase {
15+
private String[] mechanisms;
16+
17+
public class Client implements SaslClient {
18+
public Client(String name, byte[][] responses) {
19+
this.name = name;
20+
this.responses = responses;
21+
}
22+
23+
private String name;
24+
private byte[][] responses;
25+
private int counter;
26+
27+
public String getMechanismName() {
28+
return name;
29+
}
30+
31+
public boolean hasInitialResponse() {
32+
return true;
33+
}
34+
35+
public byte[] evaluateChallenge(byte[] bytes) throws SaslException {
36+
counter ++;
37+
return responses[counter-1];
38+
}
39+
40+
public boolean isComplete() {
41+
return counter >= responses.length;
42+
}
43+
44+
public byte[] unwrap(byte[] bytes, int i, int i1) throws SaslException {
45+
throw new UnsupportedOperationException();
46+
}
47+
48+
public byte[] wrap(byte[] bytes, int i, int i1) throws SaslException {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
public Object getNegotiatedProperty(String s) {
53+
throw new UnsupportedOperationException();
54+
}
55+
56+
public void dispose() throws SaslException {
57+
}
58+
}
59+
60+
public class Config implements SaslConfig {
61+
private String name;
62+
private byte[][] responses;
63+
64+
public Config(String name, byte[][] responses) {
65+
this.name = name;
66+
this.responses = responses;
67+
}
68+
69+
public SaslClient getSaslClient(String[] mechanisms) throws SaslException {
70+
SaslMechanisms.this.mechanisms = mechanisms;
71+
return new Client(name, responses);
72+
}
73+
}
74+
75+
// TODO test gibberish examples. ATM the server is not very robust.
76+
77+
public void testPlainLogin() throws IOException {
78+
loginOk("PLAIN", new byte[][] {"\0guest\0guest".getBytes()} );
79+
loginBad("PLAIN", new byte[][] {"\0guest\0wrong".getBytes()} );
80+
}
81+
82+
public void testAMQPlainLogin() throws IOException {
83+
// guest / guest
84+
loginOk("AMQPLAIN", new byte[][] {{5,76,79,71,73,78,83,0,0,0,5,103,117,101,115,116,8,80,65,83,83,87,79,82,68,83,0,0,0,5,103,117,101,115,116}} );
85+
// guest / wrong
86+
loginBad("AMQPLAIN", new byte[][] {{5,76,79,71,73,78,83,0,0,0,5,103,117,101,115,116,8,80,65,83,83,87,79,82,68,83,0,0,0,5,119,114,111,110,103}} );
87+
}
88+
89+
public void testCRLogin() throws IOException {
90+
// Make sure mechanisms is populated
91+
loginOk("PLAIN", new byte[][] {"\0guest\0guest".getBytes()} );
92+
93+
// We might be running this standalone
94+
if (Arrays.asList(mechanisms).contains("RABBIT-CR-DEMO")) {
95+
loginOk("RABBIT-CR-DEMO", new byte[][] {"guest".getBytes(), "guest".getBytes()} );
96+
loginBad("RABBIT-CR-DEMO", new byte[][] {"guest".getBytes(), "wrong".getBytes()} );
97+
}
98+
}
99+
100+
private void loginOk(String name, byte[][] responses) throws IOException {
101+
ConnectionFactory factory = new ConnectionFactory();
102+
factory.setSaslConfig(new Config(name, responses));
103+
Connection connection = factory.newConnection();
104+
connection.close();
105+
}
106+
107+
private void loginBad(String name, byte[][] responses) throws IOException {
108+
try {
109+
loginOk(name, responses);
110+
fail("Login succeeded!");
111+
} catch (PossibleAuthenticationFailureException e) {
112+
// Ok
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)