1616
1717package com .rabbitmq .client .test .functional ;
1818
19+ import com .rabbitmq .client .AMQP ;
20+ import com .rabbitmq .client .AuthenticationFailureException ;
1921import com .rabbitmq .client .Connection ;
2022import com .rabbitmq .client .ConnectionFactory ;
2123import com .rabbitmq .client .LongString ;
2224import com .rabbitmq .client .PossibleAuthenticationFailureException ;
2325import com .rabbitmq .client .SaslConfig ;
2426import com .rabbitmq .client .SaslMechanism ;
27+ import com .rabbitmq .client .impl .AMQConnection ;
2528import com .rabbitmq .client .impl .LongStringHelper ;
29+ import com .rabbitmq .client .impl .SocketFrameHandler ;
2630import com .rabbitmq .client .test .BrokerTestCase ;
2731
32+ import javax .net .SocketFactory ;
2833import java .io .IOException ;
2934import java .util .Arrays ;
35+ import java .util .HashMap ;
36+ import java .util .Map ;
37+ import java .util .concurrent .Executors ;
3038
3139public class SaslMechanisms extends BrokerTestCase {
3240 private String [] mechanisms ;
@@ -91,6 +99,47 @@ public void testCRLogin() throws IOException {
9199 }
92100 }
93101
102+ public void testConnectionCloseAuthFailureUsername () throws IOException {
103+ connectionCloseAuthFailure ("incorrect-username" , "incorrect-password" );
104+ }
105+
106+ public void testConnectionCloseAuthFailurePassword () throws IOException {
107+ connectionCloseAuthFailure (connectionFactory .getUsername (), "incorrect-password" );
108+ }
109+
110+ public void connectionCloseAuthFailure (String username , String password ) throws IOException {
111+ String failDetail = "for username " + username + " and password " + password ;
112+ try {
113+ Connection conn = connectionWithoutCapabilities (username , password );
114+ fail ("Expected PossibleAuthenticationFailureException " + failDetail );
115+ conn .abort ();
116+ } catch (PossibleAuthenticationFailureException paf ) {
117+ if (paf instanceof AuthenticationFailureException ) {
118+ fail ("Not expecting AuthenticationFailureException " + failDetail );
119+ }
120+ }
121+ }
122+
123+ // start a connection without capabilities, causing authentication failures
124+ // to be reported by the broker by closing the connection
125+ private Connection connectionWithoutCapabilities (String username , String password ) throws IOException {
126+ Map <String , Object > customProperties = connection .getClientProperties ();
127+ customProperties .remove ("capabilities" );
128+ AMQConnection conn =
129+ new AMQConnection (username ,
130+ password ,
131+ new SocketFrameHandler (SocketFactory .getDefault ().createSocket ("localhost" , AMQP .PROTOCOL .PORT )),
132+ Executors .newFixedThreadPool (1 ),
133+ connectionFactory .getVirtualHost (),
134+ customProperties ,
135+ connectionFactory .getRequestedFrameMax (),
136+ connectionFactory .getRequestedChannelMax (),
137+ connectionFactory .getRequestedHeartbeat (),
138+ connectionFactory .getSaslConfig ());
139+ conn .start ();
140+ return conn ;
141+ }
142+
94143 private void loginOk (String name , byte [][] responses ) throws IOException {
95144 ConnectionFactory factory = new ConnectionFactory ();
96145 factory .setSaslConfig (new Config (name , responses ));
@@ -102,7 +151,7 @@ private void loginBad(String name, byte[][] responses) throws IOException {
102151 try {
103152 loginOk (name , responses );
104153 fail ("Login succeeded!" );
105- } catch (PossibleAuthenticationFailureException e ) {
154+ } catch (AuthenticationFailureException e ) {
106155 // Ok
107156 }
108157 }
0 commit comments