| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.apache.qpid.proton.engine.impl; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertArrayEquals; |
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
import static org.junit.Assert.assertFalse; |
| 22 |
import static org.junit.Assert.assertNotEquals; |
| 23 |
import static org.junit.Assert.assertNotNull; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
import static org.junit.Assert.fail; |
| 26 |
import static org.mockito.ArgumentMatchers.isA; |
| 27 |
import static org.mockito.Mockito.eq; |
| 28 |
import static org.mockito.Mockito.spy; |
| 29 |
import static org.mockito.Mockito.verify; |
| 30 |
|
| 31 |
import java.nio.ByteBuffer; |
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Map; |
| 36 |
import java.util.Random; |
| 37 |
import java.util.concurrent.atomic.AtomicInteger; |
| 38 |
import java.util.function.Function; |
| 39 |
|
| 40 |
import org.apache.qpid.proton.amqp.Binary; |
| 41 |
import org.apache.qpid.proton.amqp.Symbol; |
| 42 |
import org.apache.qpid.proton.amqp.UnsignedInteger; |
| 43 |
import org.apache.qpid.proton.amqp.security.SaslFrameBody; |
| 44 |
import org.apache.qpid.proton.amqp.security.SaslInit; |
| 45 |
import org.apache.qpid.proton.amqp.transport.Open; |
| 46 |
import org.apache.qpid.proton.amqp.transport.ReceiverSettleMode; |
| 47 |
import org.apache.qpid.proton.amqp.transport.Transfer; |
| 48 |
import org.apache.qpid.proton.codec.AMQPDefinedTypes; |
| 49 |
import org.apache.qpid.proton.codec.DecoderImpl; |
| 50 |
import org.apache.qpid.proton.codec.EncoderImpl; |
| 51 |
import org.apache.qpid.proton.codec.ReadableBuffer; |
| 52 |
import org.apache.qpid.proton.framing.TransportFrame; |
| 53 |
|
| 54 |
import org.junit.Before; |
| 55 |
import org.junit.Test; |
| 56 |
import org.mockito.ArgumentCaptor; |
| 57 |
import org.mockito.Mockito; |
| 58 |
|
| 59 |
/** |
| 60 |
* Tests for the FrameWriter implementation |
| 61 |
*/ |
| 62 |
public class FrameWriterTest { |
| 63 |
|
| 64 |
private final TransportImpl transport = new TransportImpl(); |
| 65 |
|
| 66 |
private final DecoderImpl decoder = new DecoderImpl(); |
| 67 |
private final EncoderImpl encoder = new EncoderImpl(decoder); |
| 68 |
|
| 69 |
private ReadableBuffer bigPayload; |
| 70 |
private ReadableBuffer littlePayload; |
| 71 |
private ByteBuffer buffer; |
| 72 |
|
| 73 |
@Before |
| 74 |
public void setUp() { |
| 75 |
AMQPDefinedTypes.registerAllTypes(decoder, encoder); |
| 76 |
|
| 77 |
buffer = ByteBuffer.allocate(16384); |
| 78 |
|
| 79 |
encoder.setByteBuffer(buffer); |
| 80 |
decoder.setByteBuffer(buffer); |
| 81 |
|
| 82 |
Random random = new Random(System.currentTimeMillis()); |
| 83 |
bigPayload = ReadableBuffer.ByteBufferReader.allocate(4096); |
| 84 |
for (int i = 0; i < bigPayload.remaining(); ++i) { |
| 85 |
bigPayload.array()[i] = (byte) random.nextInt(127); |
| 86 |
} |
| 87 |
|
| 88 |
littlePayload = ReadableBuffer.ByteBufferReader.allocate(16); |
| 89 |
for (int i = 0; i < littlePayload.remaining(); ++i) { |
| 90 |
littlePayload.array()[i] = (byte) random.nextInt(127); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void testFrameWrittenToBuffer() { |
| 96 |
Transfer transfer = createTransfer(); |
| 97 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, transport); |
| 98 |
|
| 99 |
framer.writeFrame(transfer); |
| 100 |
assertNotEquals(0, framer.readBytes(buffer)); |
| 101 |
|
| 102 |
buffer.flip(); |
| 103 |
buffer.position(8); // Remove the Frame Header |
| 104 |
|
| 105 |
Object decoded = decoder.readObject(); |
| 106 |
assertNotNull(decoded); |
| 107 |
assertTrue(decoded instanceof Transfer); |
| 108 |
Transfer result = (Transfer) decoded; |
| 109 |
|
| 110 |
assertEquals(UnsignedInteger.ONE, result.getHandle()); |
| 111 |
assertEquals(UnsignedInteger.ZERO, result.getMessageFormat()); |
| 112 |
assertEquals(UnsignedInteger.valueOf(127), result.getDeliveryId()); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void testFailToWriteFrame() { |
| 117 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 1, transport); |
| 118 |
|
| 119 |
final class FailOnUnknownType implements Function<Boolean, Boolean> { |
| 120 |
@Override |
| 121 |
public Boolean apply(Boolean t) { |
| 122 |
throw new IllegalStateException(); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
Open open = new Open(); |
| 127 |
Map<Symbol, Object> invalidProperties = new HashMap<>(); |
| 128 |
invalidProperties.put(Symbol.valueOf("invalid-unknown-type"), new FailOnUnknownType()); |
| 129 |
open.setProperties(invalidProperties); |
| 130 |
|
| 131 |
try { |
| 132 |
framer.writeFrame(0, open, null, null); |
| 133 |
fail("should have thrown exception"); |
| 134 |
} catch (IllegalArgumentException e) { |
| 135 |
// Expected |
| 136 |
assertNotNull(e.getMessage()); |
| 137 |
assertTrue(e.getMessage().contains(FailOnUnknownType.class.getName())); |
| 138 |
} |
| 139 |
|
| 140 |
ByteBuffer destBuffer = ByteBuffer.allocate(16); |
| 141 |
int read = framer.readBytes(destBuffer); |
| 142 |
|
| 143 |
assertEquals("should not have been any output read", 0, read); |
| 144 |
assertEquals("should not have been any output in buffer", 0, destBuffer.position()); |
| 145 |
} |
| 146 |
|
| 147 |
@Test |
| 148 |
public void testFrameWrittenToBufferWithLargePayloadAndMaxFrameSizeInvokesHandlerOnce() { |
| 149 |
Transfer transfer = createTransfer(); |
| 150 |
FrameWriter framer = new FrameWriter(encoder, 2048, (byte) 0, transport); |
| 151 |
|
| 152 |
final AtomicInteger toLargeCallbackCount = new AtomicInteger(); |
| 153 |
|
| 154 |
framer.writeFrame(0, transfer, bigPayload, new Runnable() { |
| 155 |
|
| 156 |
@Override |
| 157 |
public void run() { |
| 158 |
toLargeCallbackCount.incrementAndGet(); |
| 159 |
} |
| 160 |
}); |
| 161 |
|
| 162 |
// Should have read some data during this encode. |
| 163 |
assertNotEquals(0, framer.readBytes(buffer)); |
| 164 |
|
| 165 |
buffer.flip(); |
| 166 |
byte[] header = new byte[FrameWriter.FRAME_HEADER_SIZE]; |
| 167 |
buffer.get(header); |
| 168 |
|
| 169 |
ReadableBuffer headerReader = ReadableBuffer.ByteBufferReader.wrap(header); |
| 170 |
int size = headerReader.getInt(); |
| 171 |
assertEquals(2048, size); |
| 172 |
|
| 173 |
// Should have only asked once for the handler to respond to the to large paylod. |
| 174 |
assertEquals(1, toLargeCallbackCount.get()); |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void testFrameWrittenToBufferWithLargePayloadAndNoMaxFrameSize() { |
| 179 |
Transfer transfer = createTransfer(); |
| 180 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, transport); |
| 181 |
|
| 182 |
framer.writeFrame(0, transfer, bigPayload, new PartialTransferHandler(transfer)); |
| 183 |
assertNotEquals(0, framer.readBytes(buffer)); |
| 184 |
|
| 185 |
buffer.flip(); |
| 186 |
|
| 187 |
byte[] header = new byte[FrameWriter.FRAME_HEADER_SIZE]; |
| 188 |
buffer.get(header); |
| 189 |
|
| 190 |
ReadableBuffer headerReader = ReadableBuffer.ByteBufferReader.wrap(header); |
| 191 |
int size = headerReader.getInt(); |
| 192 |
assertTrue(size > 4096); |
| 193 |
|
| 194 |
Object decoded = decoder.readObject(); |
| 195 |
assertNotNull(decoded); |
| 196 |
assertTrue(decoded instanceof Transfer); |
| 197 |
|
| 198 |
// Check for our payload |
| 199 |
assertTrue(buffer.hasRemaining()); |
| 200 |
assertEquals(4096, buffer.remaining()); |
| 201 |
|
| 202 |
byte[] payload = new byte[4096]; |
| 203 |
buffer.get(payload); |
| 204 |
|
| 205 |
assertArrayEquals(bigPayload.array(), payload); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void testFrameWrittenToBufferWithLargePayloadAndMaxFrameSize() { |
| 210 |
Transfer transfer = createTransfer(); |
| 211 |
FrameWriter framer = new FrameWriter(encoder, 2048, (byte) 0, transport); |
| 212 |
|
| 213 |
int payloadSize = 0; |
| 214 |
|
| 215 |
// Should take three write and three reads to get it all and the Transfer should |
| 216 |
// indicate that there is more data to come after the first two writes |
| 217 |
for (int i = 1; i <= 3; ++i) { |
| 218 |
transfer.setMore(false); |
| 219 |
framer.writeFrame(0, transfer, bigPayload, new PartialTransferHandler(transfer)); |
| 220 |
|
| 221 |
ByteBuffer intermediate = ByteBuffer.allocate(4096); |
| 222 |
int bytesRead = framer.readBytes(intermediate); |
| 223 |
intermediate.flip(); |
| 224 |
|
| 225 |
// Read the Frame Header |
| 226 |
byte[] header = new byte[FrameWriter.FRAME_HEADER_SIZE]; |
| 227 |
intermediate.get(header); |
| 228 |
ReadableBuffer headerReader = ReadableBuffer.ByteBufferReader.wrap(header); |
| 229 |
int frameSize = headerReader.getInt(); |
| 230 |
|
| 231 |
if (i < 3) { |
| 232 |
assertTrue(transfer.getMore()); |
| 233 |
assertEquals(2048, bytesRead); |
| 234 |
assertEquals(2048, frameSize); |
| 235 |
} else { |
| 236 |
assertFalse(transfer.getMore()); |
| 237 |
assertTrue(bytesRead < 2048); |
| 238 |
assertTrue(frameSize < 2048); |
| 239 |
} |
| 240 |
|
| 241 |
decoder.setBuffer(ReadableBuffer.ByteBufferReader.wrap(intermediate)); |
| 242 |
Object decoded = decoder.readObject(); |
| 243 |
assertNotNull(decoded); |
| 244 |
assertTrue(decoded instanceof Transfer); |
| 245 |
|
| 246 |
// Trim the Frame header and Transfer encoding size and store off actual payload |
| 247 |
payloadSize += bytesRead - intermediate.position(); |
| 248 |
|
| 249 |
// Accumulate the data minus the frame headers |
| 250 |
buffer.put(intermediate); |
| 251 |
} |
| 252 |
|
| 253 |
assertEquals(3, framer.getFramesOutput()); |
| 254 |
|
| 255 |
buffer.rewind(); |
| 256 |
buffer.limit(payloadSize); |
| 257 |
|
| 258 |
// Check for our payload |
| 259 |
assertTrue(buffer.hasRemaining()); |
| 260 |
assertEquals(4096, buffer.remaining()); |
| 261 |
|
| 262 |
byte[] payload = new byte[4096]; |
| 263 |
buffer.get(payload); |
| 264 |
|
| 265 |
assertArrayEquals(bigPayload.array(), payload); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void testWriteEmptyFrame() { |
| 270 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 1, transport); |
| 271 |
|
| 272 |
framer.writeFrame(16, null, null, null); |
| 273 |
|
| 274 |
ByteBuffer headerBuffer = ByteBuffer.allocate(16); |
| 275 |
framer.readBytes(headerBuffer); |
| 276 |
|
| 277 |
assertEquals(FrameWriter.FRAME_HEADER_SIZE, headerBuffer.position()); |
| 278 |
|
| 279 |
headerBuffer.flip(); |
| 280 |
|
| 281 |
// Size, offset, Frame type, channel |
| 282 |
assertEquals(FrameWriter.FRAME_HEADER_SIZE, headerBuffer.getInt()); |
| 283 |
assertEquals(2, headerBuffer.get()); |
| 284 |
assertEquals(1, headerBuffer.get()); |
| 285 |
assertEquals(16, headerBuffer.getShort()); |
| 286 |
} |
| 287 |
|
| 288 |
@Test |
| 289 |
public void testFrameWriterReportsFullBasedOnConfiguration() { |
| 290 |
Transfer transfer = createTransfer(); |
| 291 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, transport); |
| 292 |
|
| 293 |
framer.writeFrame(0, transfer, bigPayload, new PartialTransferHandler(transfer)); |
| 294 |
|
| 295 |
assertEquals(FrameWriter.DEFAULT_FRAME_BUFFER_FULL_MARK ,framer.getFrameWriterMaxBytes()); |
| 296 |
assertFalse(framer.isFull()); |
| 297 |
framer.setFrameWriterMaxBytes(2048); |
| 298 |
assertTrue(framer.isFull()); |
| 299 |
assertEquals(2048 ,framer.getFrameWriterMaxBytes()); |
| 300 |
framer.setFrameWriterMaxBytes(16384); |
| 301 |
assertFalse(framer.isFull()); |
| 302 |
assertEquals(16384, framer.getFrameWriterMaxBytes()); |
| 303 |
} |
| 304 |
|
| 305 |
@Test |
| 306 |
public void testFrameWriterLogsFramesToTracer() { |
| 307 |
List<TransportFrame> frames = new ArrayList<>(); |
| 308 |
transport.setProtocolTracer(new ProtocolTracer() |
| 309 |
{ |
| 310 |
@Override |
| 311 |
public void sentFrame(final TransportFrame transportFrame) |
| 312 |
{ |
| 313 |
frames.add(transportFrame); |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void receivedFrame(TransportFrame transportFrame) { } |
| 318 |
}); |
| 319 |
|
| 320 |
Transfer transfer = createTransfer(); |
| 321 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, transport); |
| 322 |
|
| 323 |
framer.writeFrame(16, transfer, bigPayload, new PartialTransferHandler(transfer)); |
| 324 |
|
| 325 |
assertEquals(1, frames.size()); |
| 326 |
TransportFrame sentFrame = frames.get(0); |
| 327 |
|
| 328 |
assertEquals(16, sentFrame.getChannel()); |
| 329 |
assertTrue(sentFrame.getBody() instanceof Transfer); |
| 330 |
|
| 331 |
Binary payload = sentFrame.getPayload(); |
| 332 |
|
| 333 |
assertEquals(bigPayload.capacity(), payload.getLength()); |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void testFrameWriterLogsFramesToSystem() { |
| 338 |
transport.trace(2); |
| 339 |
TransportImpl spy = Mockito.spy(transport); |
| 340 |
|
| 341 |
Transfer transfer = createTransfer(); |
| 342 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 0, spy); |
| 343 |
|
| 344 |
int channel = 16; |
| 345 |
int payloadLength = littlePayload.capacity(); |
| 346 |
|
| 347 |
framer.writeFrame(channel, transfer, littlePayload, new PartialTransferHandler(transfer)); |
| 348 |
|
| 349 |
ArgumentCaptor<TransportFrame> frameCatcher = ArgumentCaptor.forClass(TransportFrame.class); |
| 350 |
Mockito.verify(spy).log(eq(TransportImpl.OUTGOING), frameCatcher.capture()); |
| 351 |
|
| 352 |
assertEquals(channel, frameCatcher.getValue().getChannel()); |
| 353 |
assertTrue(frameCatcher.getValue().getBody() instanceof Transfer); |
| 354 |
|
| 355 |
Binary payload = frameCatcher.getValue().getPayload(); |
| 356 |
|
| 357 |
assertEquals(payloadLength, payload.getLength()); |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void testFrameWriterLogsSaslFramesToTracer() { |
| 362 |
List<SaslFrameBody> bodies = new ArrayList<>(); |
| 363 |
transport.setProtocolTracer(new ProtocolTracer() |
| 364 |
{ |
| 365 |
@Override |
| 366 |
public void sentSaslBody(final SaslFrameBody saslFrameBody) |
| 367 |
{ |
| 368 |
bodies.add(saslFrameBody); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public void receivedFrame(TransportFrame transportFrame) { } |
| 373 |
|
| 374 |
@Override |
| 375 |
public void sentFrame(TransportFrame transportFrame) { } |
| 376 |
}); |
| 377 |
|
| 378 |
SaslInit init = new SaslInit(); |
| 379 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, FrameWriter.SASL_FRAME_TYPE, transport); |
| 380 |
|
| 381 |
framer.writeFrame(0, init, null, null); |
| 382 |
|
| 383 |
assertEquals(1, bodies.size()); |
| 384 |
assertTrue(bodies.get(0) instanceof SaslInit); |
| 385 |
} |
| 386 |
|
| 387 |
@Test |
| 388 |
public void testFrameWriterLogsSaslFramesToSystem() { |
| 389 |
transport.trace(2); |
| 390 |
TransportImpl spy = spy(transport); |
| 391 |
|
| 392 |
SaslInit init = new SaslInit(); |
| 393 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, FrameWriter.SASL_FRAME_TYPE, spy); |
| 394 |
|
| 395 |
framer.writeFrame(0, init, null, null); |
| 396 |
|
| 397 |
verify(spy).log(eq(TransportImpl.OUTGOING), |
| 398 |
isA(SaslInit.class)); |
| 399 |
} |
| 400 |
|
| 401 |
@Test |
| 402 |
public void testWriteHeader() { |
| 403 |
FrameWriter framer = new FrameWriter(encoder, Integer.MAX_VALUE, (byte) 1, transport); |
| 404 |
byte[] header = new byte[] {0, 0, 0, 9, 3, 4, 0, 12}; |
| 405 |
framer.writeHeader(header); |
| 406 |
|
| 407 |
ByteBuffer headerBuffer = ByteBuffer.allocate(16); |
| 408 |
framer.readBytes(headerBuffer); |
| 409 |
|
| 410 |
assertEquals(FrameWriter.FRAME_HEADER_SIZE, headerBuffer.position()); |
| 411 |
|
| 412 |
headerBuffer.flip(); |
| 413 |
|
| 414 |
// Size, offset, Frame type, channel |
| 415 |
assertEquals(FrameWriter.FRAME_HEADER_SIZE + 1, headerBuffer.getInt()); |
| 416 |
assertEquals(3, headerBuffer.get()); |
| 417 |
assertEquals(4, headerBuffer.get()); |
| 418 |
assertEquals(12, headerBuffer.getShort()); |
| 419 |
} |
| 420 |
|
| 421 |
private Transfer createTransfer() { |
| 422 |
Transfer transfer = new Transfer(); |
| 423 |
transfer.setHandle(UnsignedInteger.ONE); |
| 424 |
transfer.setDeliveryTag(new Binary(new byte[] {0, 1})); |
| 425 |
transfer.setMessageFormat(UnsignedInteger.ZERO); |
| 426 |
transfer.setDeliveryId(UnsignedInteger.valueOf(127)); |
| 427 |
transfer.setAborted(false); |
| 428 |
transfer.setBatchable(false); |
| 429 |
transfer.setRcvSettleMode(ReceiverSettleMode.SECOND); |
| 430 |
|
| 431 |
return transfer; |
| 432 |
} |
| 433 |
|
| 434 |
private static final class PartialTransferHandler implements Runnable { |
| 435 |
private Transfer transfer; |
| 436 |
|
| 437 |
public PartialTransferHandler(Transfer transfer) { |
| 438 |
this.transfer = transfer; |
| 439 |
} |
| 440 |
|
| 441 |
@Override |
| 442 |
public void run() { |
| 443 |
transfer.setMore(true); |
| 444 |
} |
| 445 |
} |
| 446 |
} |