| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.qpid.proton.systemtests; |
| 20 |
|
| 21 |
import static org.apache.qpid.proton.systemtests.TestLoggingHelper.bold; |
| 22 |
import static org.junit.Assert.assertArrayEquals; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertFalse; |
| 25 |
import static org.junit.Assert.assertNull; |
| 26 |
import static org.junit.Assert.assertTrue; |
| 27 |
import static org.junit.Assert.fail; |
| 28 |
|
| 29 |
import java.nio.charset.StandardCharsets; |
| 30 |
import java.util.Arrays; |
| 31 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 32 |
import java.util.logging.Logger; |
| 33 |
|
| 34 |
import org.junit.Test; |
| 35 |
|
| 36 |
import org.apache.qpid.proton.Proton; |
| 37 |
import org.apache.qpid.proton.engine.Sasl; |
| 38 |
import org.apache.qpid.proton.engine.SaslListener; |
| 39 |
import org.apache.qpid.proton.engine.Transport; |
| 40 |
import org.apache.qpid.proton.engine.Sasl.SaslOutcome; |
| 41 |
|
| 42 |
public class SaslTest extends EngineTestBase |
| 43 |
{ |
| 44 |
private static final Logger LOGGER = Logger.getLogger(SaslTest.class.getName()); |
| 45 |
private static final String TESTMECH1 = "TESTMECH1"; |
| 46 |
private static final String TESTMECH2 = "TESTMECH2"; |
| 47 |
private static final byte[] INITIAL_RESPONSE_BYTES = "initial-response-bytes".getBytes(StandardCharsets.UTF_8); |
| 48 |
private static final byte[] CHALLENGE_BYTES = "challenge-bytes".getBytes(StandardCharsets.UTF_8); |
| 49 |
private static final byte[] RESPONSE_BYTES = "response-bytes".getBytes(StandardCharsets.UTF_8); |
| 50 |
private static final byte[] ADDITIONAL_DATA_BYTES = "additional-data-bytes".getBytes(StandardCharsets.UTF_8); |
| 51 |
|
| 52 |
@Test |
| 53 |
public void testSaslHostnamePropagationAndRetrieval() throws Exception |
| 54 |
{ |
| 55 |
LOGGER.fine(bold("======== About to create transports")); |
| 56 |
|
| 57 |
getClient().transport = Proton.transport(); |
| 58 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 59 |
|
| 60 |
Sasl clientSasl = getClient().transport.sasl(); |
| 61 |
clientSasl.client(); |
| 62 |
|
| 63 |
// Set the server hostname we are connecting to from the client |
| 64 |
String hostname = "my-remote-host-123"; |
| 65 |
clientSasl.setRemoteHostname(hostname); |
| 66 |
|
| 67 |
// Verify we can't get the hostname on the client |
| 68 |
try |
| 69 |
{ |
| 70 |
clientSasl.getHostname(); |
| 71 |
fail("should have throw IllegalStateException"); |
| 72 |
} |
| 73 |
catch (IllegalStateException ise) |
| 74 |
{ |
| 75 |
// expected |
| 76 |
} |
| 77 |
|
| 78 |
getServer().transport = Proton.transport(); |
| 79 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 80 |
|
| 81 |
// Configure the server to do ANONYMOUS |
| 82 |
Sasl serverSasl = getServer().transport.sasl(); |
| 83 |
serverSasl.server(); |
| 84 |
serverSasl.setMechanisms("ANONYMOUS"); |
| 85 |
|
| 86 |
// Verify we can't set the hostname on the server |
| 87 |
try |
| 88 |
{ |
| 89 |
serverSasl.setRemoteHostname("some-other-host"); |
| 90 |
fail("should have throw IllegalStateException"); |
| 91 |
} |
| 92 |
catch (IllegalStateException ise) |
| 93 |
{ |
| 94 |
// expected |
| 95 |
} |
| 96 |
|
| 97 |
assertNull(serverSasl.getHostname()); |
| 98 |
assertArrayEquals(new String[0], clientSasl.getRemoteMechanisms()); |
| 99 |
|
| 100 |
pumpClientToServer(); |
| 101 |
pumpServerToClient(); |
| 102 |
|
| 103 |
// Verify we got the mechs, set the chosen mech, and verify the |
| 104 |
// server still doesnt know the hostname set/requested by the client |
| 105 |
assertArrayEquals(new String[] {"ANONYMOUS"} , clientSasl.getRemoteMechanisms()); |
| 106 |
clientSasl.setMechanisms("ANONYMOUS"); |
| 107 |
assertNull(serverSasl.getHostname()); |
| 108 |
|
| 109 |
pumpClientToServer(); |
| 110 |
|
| 111 |
// Verify the server now knows that the client set the hostname field |
| 112 |
assertEquals(hostname, serverSasl.getHostname()); |
| 113 |
} |
| 114 |
|
| 115 |
/** 5.3.2 SASL Negotiation. */ |
| 116 |
@Test |
| 117 |
public void testSaslNegotiation() throws Exception |
| 118 |
{ |
| 119 |
getClient().transport = Proton.transport(); |
| 120 |
getServer().transport = Proton.transport(); |
| 121 |
|
| 122 |
Sasl clientSasl = getClient().transport.sasl(); |
| 123 |
clientSasl.client(); |
| 124 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 125 |
|
| 126 |
Sasl serverSasl = getServer().transport.sasl(); |
| 127 |
serverSasl.server(); |
| 128 |
serverSasl.setMechanisms(TESTMECH1, TESTMECH2); |
| 129 |
assertEquals("Server should not yet know the remote's chosen mechanism.", |
| 130 |
0, |
| 131 |
serverSasl.getRemoteMechanisms().length); |
| 132 |
|
| 133 |
pumpClientToServer(); |
| 134 |
pumpServerToClient(); |
| 135 |
|
| 136 |
assertArrayEquals("Client should now know the server's mechanisms.", |
| 137 |
new String[]{TESTMECH1, TESTMECH2}, |
| 138 |
clientSasl.getRemoteMechanisms()); |
| 139 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 140 |
clientSasl.setMechanisms(TESTMECH1); |
| 141 |
|
| 142 |
pumpClientToServer(); |
| 143 |
|
| 144 |
assertArrayEquals("Server should now know the client's chosen mechanism.", |
| 145 |
new String[]{TESTMECH1}, |
| 146 |
serverSasl.getRemoteMechanisms()); |
| 147 |
|
| 148 |
serverSasl.send(CHALLENGE_BYTES, 0, CHALLENGE_BYTES.length); |
| 149 |
|
| 150 |
pumpServerToClient(); |
| 151 |
|
| 152 |
byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()]; |
| 153 |
clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length); |
| 154 |
|
| 155 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 156 |
assertArrayEquals("Client should now know the server's challenge", |
| 157 |
CHALLENGE_BYTES, |
| 158 |
clientReceivedChallengeBytes); |
| 159 |
|
| 160 |
clientSasl.send(RESPONSE_BYTES, 0, RESPONSE_BYTES.length); |
| 161 |
|
| 162 |
pumpClientToServer(); |
| 163 |
|
| 164 |
byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()]; |
| 165 |
serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length); |
| 166 |
|
| 167 |
assertArrayEquals("Server should now know the client's response", |
| 168 |
RESPONSE_BYTES, |
| 169 |
serverReceivedResponseBytes); |
| 170 |
|
| 171 |
serverSasl.done(SaslOutcome.PN_SASL_OK); |
| 172 |
pumpServerToClient(); |
| 173 |
|
| 174 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome()); |
| 175 |
} |
| 176 |
|
| 177 |
/** 5.3.2 SASL Negotiation. ...challenge/response step can occur zero or more times*/ |
| 178 |
@Test |
| 179 |
public void testOptionalChallengeResponseStepOmitted() throws Exception |
| 180 |
{ |
| 181 |
getClient().transport = Proton.transport(); |
| 182 |
getServer().transport = Proton.transport(); |
| 183 |
|
| 184 |
Sasl clientSasl = getClient().transport.sasl(); |
| 185 |
clientSasl.client(); |
| 186 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 187 |
|
| 188 |
Sasl serverSasl = getServer().transport.sasl(); |
| 189 |
serverSasl.server(); |
| 190 |
serverSasl.setMechanisms(TESTMECH1); |
| 191 |
assertEquals("Server should not yet know the remote's chosen mechanism.", |
| 192 |
0, |
| 193 |
serverSasl.getRemoteMechanisms().length); |
| 194 |
|
| 195 |
pumpClientToServer(); |
| 196 |
pumpServerToClient(); |
| 197 |
|
| 198 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 199 |
clientSasl.setMechanisms(TESTMECH1); |
| 200 |
|
| 201 |
pumpClientToServer(); |
| 202 |
|
| 203 |
serverSasl.done(SaslOutcome.PN_SASL_OK); |
| 204 |
pumpServerToClient(); |
| 205 |
|
| 206 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome()); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* 5.3.3.5 The additional-data field carries additional data on successful authentication outcome as specified |
| 211 |
* by the SASL specification [RFC4422]. |
| 212 |
*/ |
| 213 |
@Test |
| 214 |
public void testOutcomeAdditionalData() throws Exception |
| 215 |
{ |
| 216 |
getClient().transport = Proton.transport(); |
| 217 |
getServer().transport = Proton.transport(); |
| 218 |
|
| 219 |
Sasl clientSasl = getClient().transport.sasl(); |
| 220 |
clientSasl.client(); |
| 221 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 222 |
|
| 223 |
Sasl serverSasl = getServer().transport.sasl(); |
| 224 |
serverSasl.server(); |
| 225 |
serverSasl.setMechanisms(TESTMECH1); |
| 226 |
|
| 227 |
pumpClientToServer(); |
| 228 |
pumpServerToClient(); |
| 229 |
|
| 230 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 231 |
clientSasl.setMechanisms(TESTMECH1); |
| 232 |
|
| 233 |
pumpClientToServer(); |
| 234 |
|
| 235 |
serverSasl.send(CHALLENGE_BYTES, 0, CHALLENGE_BYTES.length); |
| 236 |
|
| 237 |
pumpServerToClient(); |
| 238 |
|
| 239 |
byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()]; |
| 240 |
clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length); |
| 241 |
|
| 242 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 243 |
clientSasl.send(RESPONSE_BYTES, 0, RESPONSE_BYTES.length); |
| 244 |
|
| 245 |
pumpClientToServer(); |
| 246 |
|
| 247 |
byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()]; |
| 248 |
serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length); |
| 249 |
|
| 250 |
serverSasl.send(ADDITIONAL_DATA_BYTES, 0, ADDITIONAL_DATA_BYTES.length); |
| 251 |
serverSasl.done(SaslOutcome.PN_SASL_OK); |
| 252 |
pumpServerToClient(); |
| 253 |
|
| 254 |
byte[] clientReceivedAdditionalDataBytes = new byte[clientSasl.pending()]; |
| 255 |
clientSasl.recv(clientReceivedAdditionalDataBytes, 0, clientReceivedAdditionalDataBytes.length); |
| 256 |
|
| 257 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome()); |
| 258 |
assertArrayEquals("Client should now know the serrver's additional-data", |
| 259 |
ADDITIONAL_DATA_BYTES, |
| 260 |
clientReceivedAdditionalDataBytes); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* 5.3.3.6 Connection authentication failed due to an unspecified problem with the supplied credentials. |
| 265 |
*/ |
| 266 |
@Test |
| 267 |
public void testAuthenticationFails() throws Exception |
| 268 |
{ |
| 269 |
getClient().transport = Proton.transport(); |
| 270 |
getServer().transport = Proton.transport(); |
| 271 |
|
| 272 |
Sasl clientSasl = getClient().transport.sasl(); |
| 273 |
clientSasl.client(); |
| 274 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 275 |
|
| 276 |
Sasl serverSasl = getServer().transport.sasl(); |
| 277 |
serverSasl.server(); |
| 278 |
serverSasl.setMechanisms(TESTMECH1); |
| 279 |
|
| 280 |
pumpClientToServer(); |
| 281 |
pumpServerToClient(); |
| 282 |
|
| 283 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 284 |
clientSasl.setMechanisms(TESTMECH1); |
| 285 |
|
| 286 |
pumpClientToServer(); |
| 287 |
|
| 288 |
serverSasl.done(SaslOutcome.PN_SASL_AUTH); |
| 289 |
pumpServerToClient(); |
| 290 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_AUTH, clientSasl.getOutcome()); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* Test that transports configured to do so are able to perform SASL process where frames are |
| 296 |
* exchanged larger than the 512byte min-max-frame-size. |
| 297 |
*/ |
| 298 |
@Test |
| 299 |
public void testSaslNegotiationWithConfiguredLargerFrameSize() throws Exception |
| 300 |
{ |
| 301 |
final byte[] largeInitialResponseBytesOrig = fillBytes("initialResponse", 1431); |
| 302 |
final byte[] largeChallengeBytesOrig = fillBytes("challenge", 1375); |
| 303 |
final byte[] largeResponseBytesOrig = fillBytes("response", 1282); |
| 304 |
final byte[] largeAdditionalBytesOrig = fillBytes("additionalData", 1529); |
| 305 |
|
| 306 |
getClient().transport = Proton.transport(); |
| 307 |
getServer().transport = Proton.transport(); |
| 308 |
|
| 309 |
// Configure transports to allow for larger initial frame sizes |
| 310 |
getClient().transport.setInitialRemoteMaxFrameSize(2048); |
| 311 |
getServer().transport.setInitialRemoteMaxFrameSize(2048); |
| 312 |
|
| 313 |
Sasl clientSasl = getClient().transport.sasl(); |
| 314 |
clientSasl.client(); |
| 315 |
|
| 316 |
Sasl serverSasl = getServer().transport.sasl(); |
| 317 |
serverSasl.server(); |
| 318 |
|
| 319 |
// Negotiate the mech |
| 320 |
serverSasl.setMechanisms(TESTMECH1, TESTMECH2); |
| 321 |
|
| 322 |
pumpClientToServer(); |
| 323 |
pumpServerToClient(); |
| 324 |
|
| 325 |
assertArrayEquals("Client should now know the server's mechanisms.", new String[] { TESTMECH1, TESTMECH2 }, |
| 326 |
clientSasl.getRemoteMechanisms()); |
| 327 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 328 |
|
| 329 |
// Select a mech, send large initial response along with it in sasl-init, verify server receives it |
| 330 |
clientSasl.setMechanisms(TESTMECH1); |
| 331 |
byte[] initialResponseBytes = Arrays.copyOf(largeInitialResponseBytesOrig, largeInitialResponseBytesOrig.length); |
| 332 |
clientSasl.send(initialResponseBytes, 0, initialResponseBytes.length); |
| 333 |
|
| 334 |
pumpClientToServer(); |
| 335 |
|
| 336 |
assertArrayEquals("Server should now know the client's chosen mechanism.", new String[] { TESTMECH1 }, |
| 337 |
serverSasl.getRemoteMechanisms()); |
| 338 |
|
| 339 |
byte[] serverReceivedInitialResponseBytes = new byte[serverSasl.pending()]; |
| 340 |
serverSasl.recv(serverReceivedInitialResponseBytes, 0, serverReceivedInitialResponseBytes.length); |
| 341 |
|
| 342 |
assertArrayEquals("Server should now know the clients initial response", largeInitialResponseBytesOrig, |
| 343 |
serverReceivedInitialResponseBytes); |
| 344 |
|
| 345 |
// Send a large challenge in a sasl-challenge, verify client receives it |
| 346 |
byte[] challengeBytes = Arrays.copyOf(largeChallengeBytesOrig, largeChallengeBytesOrig.length); |
| 347 |
serverSasl.send(challengeBytes, 0, challengeBytes.length); |
| 348 |
|
| 349 |
pumpServerToClient(); |
| 350 |
|
| 351 |
byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()]; |
| 352 |
clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length); |
| 353 |
|
| 354 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome()); |
| 355 |
assertArrayEquals("Client should now know the server's challenge", largeChallengeBytesOrig, |
| 356 |
clientReceivedChallengeBytes); |
| 357 |
|
| 358 |
// Send a large response in a sasl-response, verify server receives it |
| 359 |
byte[] responseBytes = Arrays.copyOf(largeResponseBytesOrig, largeResponseBytesOrig.length); |
| 360 |
clientSasl.send(responseBytes, 0, responseBytes.length); |
| 361 |
|
| 362 |
pumpClientToServer(); |
| 363 |
|
| 364 |
byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()]; |
| 365 |
serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length); |
| 366 |
|
| 367 |
assertArrayEquals("Server should now know the client's response", largeResponseBytesOrig, serverReceivedResponseBytes); |
| 368 |
|
| 369 |
// Send an outcome with large additional data in a sasl-outcome, verify client receives it |
| 370 |
byte[] additionalBytes = Arrays.copyOf(largeAdditionalBytesOrig, largeAdditionalBytesOrig.length); |
| 371 |
serverSasl.send(additionalBytes, 0, additionalBytes.length); |
| 372 |
serverSasl.done(SaslOutcome.PN_SASL_OK); |
| 373 |
pumpServerToClient(); |
| 374 |
|
| 375 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome()); |
| 376 |
|
| 377 |
byte[] clientReceivedAdditionalBytes = new byte[clientSasl.pending()]; |
| 378 |
clientSasl.recv(clientReceivedAdditionalBytes, 0, clientReceivedAdditionalBytes.length); |
| 379 |
|
| 380 |
assertArrayEquals("Client should now know the server's outcome additional data", largeAdditionalBytesOrig, |
| 381 |
clientReceivedAdditionalBytes); |
| 382 |
} |
| 383 |
|
| 384 |
private byte[] fillBytes(String seedString, int length) |
| 385 |
{ |
| 386 |
byte[] seed = seedString.getBytes(StandardCharsets.UTF_8); |
| 387 |
byte[] bytes = new byte[length]; |
| 388 |
for (int i = 0; i < length; i++) |
| 389 |
{ |
| 390 |
bytes[i] = seed[i % seed.length]; |
| 391 |
} |
| 392 |
|
| 393 |
return bytes; |
| 394 |
} |
| 395 |
|
| 396 |
@Test |
| 397 |
public void testSaslNegotiationUsingListener() throws Exception |
| 398 |
{ |
| 399 |
getClient().transport = Proton.transport(); |
| 400 |
getServer().transport = Proton.transport(); |
| 401 |
|
| 402 |
AtomicBoolean mechanismsReceived = new AtomicBoolean(); |
| 403 |
AtomicBoolean challengeReceived = new AtomicBoolean(); |
| 404 |
AtomicBoolean outcomeReceived = new AtomicBoolean(); |
| 405 |
|
| 406 |
Sasl clientSasl = getClient().transport.sasl(); |
| 407 |
clientSasl.client(); |
| 408 |
clientSasl.setListener(new ClientSaslHandling(mechanismsReceived, challengeReceived, outcomeReceived)); |
| 409 |
|
| 410 |
AtomicBoolean initReceived = new AtomicBoolean(); |
| 411 |
AtomicBoolean responseReceived = new AtomicBoolean(); |
| 412 |
|
| 413 |
Sasl serverSasl = getServer().transport.sasl(); |
| 414 |
serverSasl.server(); |
| 415 |
serverSasl.setMechanisms(TESTMECH1, TESTMECH2); |
| 416 |
serverSasl.setListener(new ServerSaslHandling(initReceived, responseReceived)); |
| 417 |
|
| 418 |
pumpClientToServer(); |
| 419 |
pumpServerToClient(); |
| 420 |
|
| 421 |
assertTrue("mechanisms were not received by client", mechanismsReceived.get()); |
| 422 |
assertFalse("init was received by server", initReceived.get()); |
| 423 |
|
| 424 |
pumpClientToServer(); |
| 425 |
|
| 426 |
assertTrue("init was not received by server", initReceived.get()); |
| 427 |
assertFalse("challenge was received by client", challengeReceived.get()); |
| 428 |
|
| 429 |
pumpServerToClient(); |
| 430 |
|
| 431 |
assertTrue("challenge was not received by client", challengeReceived.get()); |
| 432 |
assertFalse("response was received by server", responseReceived.get()); |
| 433 |
|
| 434 |
pumpClientToServer(); |
| 435 |
|
| 436 |
assertTrue("response was received by server", responseReceived.get()); |
| 437 |
assertFalse("outcome was received by client", outcomeReceived.get()); |
| 438 |
|
| 439 |
pumpServerToClient(); |
| 440 |
|
| 441 |
assertTrue("outcome was received by client", outcomeReceived.get()); |
| 442 |
|
| 443 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome()); |
| 444 |
} |
| 445 |
|
| 446 |
private static class ServerSaslHandling implements SaslListener |
| 447 |
{ |
| 448 |
AtomicBoolean initReceived = new AtomicBoolean(); |
| 449 |
AtomicBoolean responseReceived = new AtomicBoolean(); |
| 450 |
|
| 451 |
public ServerSaslHandling(AtomicBoolean initReceived, AtomicBoolean responseReceived) |
| 452 |
{ |
| 453 |
this.initReceived = initReceived; |
| 454 |
this.responseReceived = responseReceived; |
| 455 |
} |
| 456 |
|
| 457 |
@Override |
| 458 |
public void onSaslInit(Sasl s, Transport t) |
| 459 |
{ |
| 460 |
assertArrayEquals("Server should now know the client's chosen mechanism.", |
| 461 |
new String[]{TESTMECH1}, s.getRemoteMechanisms()); |
| 462 |
|
| 463 |
byte[] serverReceivedInitialBytes = new byte[s.pending()]; |
| 464 |
s.recv(serverReceivedInitialBytes, 0, serverReceivedInitialBytes.length); |
| 465 |
|
| 466 |
assertArrayEquals("Server should now know the client's initial response.", |
| 467 |
INITIAL_RESPONSE_BYTES, serverReceivedInitialBytes); |
| 468 |
|
| 469 |
s.send(CHALLENGE_BYTES, 0, CHALLENGE_BYTES.length); |
| 470 |
|
| 471 |
assertFalse("Should not have already received init", initReceived.getAndSet(true)); |
| 472 |
} |
| 473 |
|
| 474 |
@Override |
| 475 |
public void onSaslResponse(Sasl s, Transport t) |
| 476 |
{ |
| 477 |
byte[] serverReceivedResponseBytes = new byte[s.pending()]; |
| 478 |
s.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length); |
| 479 |
|
| 480 |
assertArrayEquals("Server should now know the client's response", RESPONSE_BYTES, serverReceivedResponseBytes); |
| 481 |
|
| 482 |
s.send(ADDITIONAL_DATA_BYTES, 0, ADDITIONAL_DATA_BYTES.length); |
| 483 |
s.done(SaslOutcome.PN_SASL_OK); |
| 484 |
|
| 485 |
assertFalse("Should not have already received response", responseReceived.getAndSet(true)); |
| 486 |
} |
| 487 |
|
| 488 |
@Override |
| 489 |
public void onSaslMechanisms(Sasl s, Transport t) { } |
| 490 |
|
| 491 |
@Override |
| 492 |
public void onSaslChallenge(Sasl s, Transport t) { } |
| 493 |
|
| 494 |
@Override |
| 495 |
public void onSaslOutcome(Sasl s, Transport t) { } |
| 496 |
} |
| 497 |
|
| 498 |
private static class ClientSaslHandling implements SaslListener |
| 499 |
{ |
| 500 |
AtomicBoolean mechanismsReceived = new AtomicBoolean(); |
| 501 |
AtomicBoolean challengeReceived = new AtomicBoolean(); |
| 502 |
AtomicBoolean outcomeReceived = new AtomicBoolean(); |
| 503 |
|
| 504 |
public ClientSaslHandling(AtomicBoolean mechanismsReceived, AtomicBoolean challengeReceived, AtomicBoolean outcomeReceived) |
| 505 |
{ |
| 506 |
this.mechanismsReceived = mechanismsReceived; |
| 507 |
this.challengeReceived = challengeReceived; |
| 508 |
this.outcomeReceived = outcomeReceived; |
| 509 |
} |
| 510 |
|
| 511 |
@Override |
| 512 |
public void onSaslMechanisms(Sasl s, Transport t) |
| 513 |
{ |
| 514 |
assertArrayEquals("Client should now know the server's mechanisms.", |
| 515 |
new String[]{TESTMECH1, TESTMECH2}, s.getRemoteMechanisms()); |
| 516 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, s.getOutcome()); |
| 517 |
|
| 518 |
s.setMechanisms(TESTMECH1); |
| 519 |
s.send(INITIAL_RESPONSE_BYTES, 0, INITIAL_RESPONSE_BYTES.length); |
| 520 |
|
| 521 |
assertFalse("Should not have already received mechanisms", mechanismsReceived.getAndSet(true)); |
| 522 |
} |
| 523 |
|
| 524 |
@Override |
| 525 |
public void onSaslChallenge(Sasl s, Transport t) |
| 526 |
{ |
| 527 |
byte[] clientReceivedChallengeBytes = new byte[s.pending()]; |
| 528 |
s.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length); |
| 529 |
|
| 530 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, s.getOutcome()); |
| 531 |
assertArrayEquals("Client should now know the server's challenge", |
| 532 |
CHALLENGE_BYTES, clientReceivedChallengeBytes); |
| 533 |
|
| 534 |
s.send(RESPONSE_BYTES, 0, RESPONSE_BYTES.length); |
| 535 |
|
| 536 |
assertFalse("Should not have already received challenge", challengeReceived.getAndSet(true)); |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public void onSaslOutcome(Sasl s, Transport t) |
| 541 |
{ |
| 542 |
assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, s.getOutcome()); |
| 543 |
|
| 544 |
byte[] clientReceivedAdditionalBytes = new byte[s.pending()]; |
| 545 |
s.recv(clientReceivedAdditionalBytes, 0, clientReceivedAdditionalBytes.length); |
| 546 |
|
| 547 |
assertArrayEquals("Client should now know the server's outcome additional data", clientReceivedAdditionalBytes, |
| 548 |
clientReceivedAdditionalBytes); |
| 549 |
|
| 550 |
assertFalse("Should not have already received outcome", outcomeReceived.getAndSet(true)); |
| 551 |
} |
| 552 |
|
| 553 |
@Override |
| 554 |
public void onSaslInit(Sasl s, Transport t) { } |
| 555 |
|
| 556 |
@Override |
| 557 |
public void onSaslResponse(Sasl s, Transport t) { } |
| 558 |
} |
| 559 |
} |