| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.engine.impl; |
| 22 |
|
| 23 |
import static junit.framework.TestCase.assertTrue; |
| 24 |
import static org.junit.Assert.assertArrayEquals; |
| 25 |
import static org.junit.Assert.assertEquals; |
| 26 |
import static org.mockito.ArgumentMatchers.eq; |
| 27 |
import static org.mockito.Mockito.spy; |
| 28 |
import static org.mockito.Mockito.verify; |
| 29 |
|
| 30 |
import java.nio.charset.StandardCharsets; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
import org.apache.qpid.proton.amqp.Binary; |
| 35 |
import org.apache.qpid.proton.amqp.Symbol; |
| 36 |
import org.apache.qpid.proton.amqp.security.SaslFrameBody; |
| 37 |
import org.apache.qpid.proton.amqp.security.SaslMechanisms; |
| 38 |
import org.apache.qpid.proton.framing.TransportFrame; |
| 39 |
import org.junit.Test; |
| 40 |
import org.mockito.ArgumentCaptor; |
| 41 |
|
| 42 |
public class SaslImplTest { |
| 43 |
|
| 44 |
@Test |
| 45 |
public void testPlainHelperEncodesExpectedResponse() { |
| 46 |
TransportImpl transport = new TransportImpl(); |
| 47 |
SaslImpl sasl = new SaslImpl(transport, 512); |
| 48 |
|
| 49 |
// Use a username + password with a unicode char that encodes |
| 50 |
// differently under changing charsets |
| 51 |
String username = "username-with-unicode" + "\u1F570"; |
| 52 |
String password = "password-with-unicode" + "\u1F570"; |
| 53 |
|
| 54 |
byte[] usernameBytes = username.getBytes(StandardCharsets.UTF_8); |
| 55 |
byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8); |
| 56 |
|
| 57 |
byte[] expectedResponseBytes = new byte[usernameBytes.length + passwordBytes.length + 2]; |
| 58 |
System.arraycopy(usernameBytes, 0, expectedResponseBytes, 1, usernameBytes.length); |
| 59 |
System.arraycopy(passwordBytes, 0, expectedResponseBytes, 2 + usernameBytes.length, passwordBytes.length); |
| 60 |
|
| 61 |
sasl.plain(username, password); |
| 62 |
|
| 63 |
assertEquals("Unexpected response data", new Binary(expectedResponseBytes), sasl.getChallengeResponse()); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void testProtocolTracingLogsToTracer() { |
| 68 |
TransportImpl transport = new TransportImpl(); |
| 69 |
List<SaslFrameBody> bodies = new ArrayList<>(); |
| 70 |
transport.setProtocolTracer(new ProtocolTracer() |
| 71 |
{ |
| 72 |
@Override |
| 73 |
public void receivedSaslBody(final SaslFrameBody saslFrameBody) |
| 74 |
{ |
| 75 |
bodies.add(saslFrameBody); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void receivedFrame(TransportFrame transportFrame) { } |
| 80 |
|
| 81 |
@Override |
| 82 |
public void sentFrame(TransportFrame transportFrame) { } |
| 83 |
}); |
| 84 |
|
| 85 |
SaslImpl sasl = new SaslImpl(transport, 512); |
| 86 |
|
| 87 |
SaslMechanisms mechs = new SaslMechanisms(); |
| 88 |
mechs.setSaslServerMechanisms(Symbol.valueOf("TESTMECH")); |
| 89 |
|
| 90 |
assertEquals(0, bodies.size()); |
| 91 |
sasl.handle(mechs, null); |
| 92 |
assertEquals(1, bodies.size()); |
| 93 |
assertTrue(bodies.get(0) instanceof SaslMechanisms); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void testProtocolTracingLogsToSystem() { |
| 98 |
TransportImpl transport = new TransportImpl(); |
| 99 |
transport.trace(2); |
| 100 |
|
| 101 |
TransportImpl spy = spy(transport); |
| 102 |
|
| 103 |
final String testMechName = "TESTMECH"; |
| 104 |
SaslMechanisms mechs = new SaslMechanisms(); |
| 105 |
mechs.setSaslServerMechanisms(Symbol.valueOf(testMechName)); |
| 106 |
|
| 107 |
SaslImpl sasl = new SaslImpl(spy, 512); |
| 108 |
|
| 109 |
sasl.handle(mechs, null); |
| 110 |
|
| 111 |
ArgumentCaptor<SaslMechanisms> frameBodyCatcher = ArgumentCaptor.forClass(SaslMechanisms.class); |
| 112 |
verify(spy).log(eq(TransportImpl.INCOMING), frameBodyCatcher.capture()); |
| 113 |
|
| 114 |
Symbol[] expectedMechs = new Symbol[] { Symbol.valueOf(testMechName)}; |
| 115 |
assertArrayEquals(expectedMechs, frameBodyCatcher.getValue().getSaslServerMechanisms()); |
| 116 |
} |
| 117 |
} |