| 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.engine.impl; |
| 20 |
|
| 21 |
import static org.apache.qpid.proton.engine.Transport.DEFAULT_MAX_FRAME_SIZE; |
| 22 |
import static org.apache.qpid.proton.engine.impl.AmqpHeader.HEADER; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertNotNull; |
| 25 |
import static org.mockito.ArgumentMatchers.any; |
| 26 |
import static org.mockito.ArgumentMatchers.argThat; |
| 27 |
import static org.mockito.Mockito.inOrder; |
| 28 |
import static org.mockito.Mockito.mock; |
| 29 |
import static org.mockito.Mockito.never; |
| 30 |
import static org.mockito.Mockito.verify; |
| 31 |
import static org.mockito.Mockito.when; |
| 32 |
|
| 33 |
import java.nio.ByteBuffer; |
| 34 |
|
| 35 |
import org.apache.qpid.proton.amqp.transport.Close; |
| 36 |
import org.apache.qpid.proton.amqp.transport.FrameBody; |
| 37 |
import org.apache.qpid.proton.amqp.transport.Open; |
| 38 |
import org.apache.qpid.proton.codec.AMQPDefinedTypes; |
| 39 |
import org.apache.qpid.proton.codec.DecoderImpl; |
| 40 |
import org.apache.qpid.proton.codec.EncoderImpl; |
| 41 |
import org.apache.qpid.proton.engine.Transport; |
| 42 |
import org.apache.qpid.proton.engine.TransportException; |
| 43 |
import org.apache.qpid.proton.framing.TransportFrame; |
| 44 |
import org.junit.Before; |
| 45 |
import org.junit.Test; |
| 46 |
import org.mockito.ArgumentMatcher; |
| 47 |
import org.mockito.InOrder; |
| 48 |
|
| 49 |
// TODO test a frame with a payload (potentially followed by another frame) |
| 50 |
public class FrameParserTest |
| 51 |
{ |
| 52 |
private FrameHandler _mockFrameHandler = mock(FrameHandler.class); |
| 53 |
private DecoderImpl _decoder = new DecoderImpl(); |
| 54 |
private EncoderImpl _encoder = new EncoderImpl(_decoder); |
| 55 |
private final FrameParser _frameParser = new FrameParser(_mockFrameHandler, _decoder, DEFAULT_MAX_FRAME_SIZE, new TransportImpl()); |
| 56 |
|
| 57 |
private final AmqpFramer _amqpFramer = new AmqpFramer(); |
| 58 |
|
| 59 |
@Before |
| 60 |
public void setUp() |
| 61 |
{ |
| 62 |
AMQPDefinedTypes.registerAllTypes(_decoder, _encoder); |
| 63 |
|
| 64 |
when(_mockFrameHandler.isHandlingFrames()).thenReturn(true); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void testInputOfInvalidProtocolHeader_causesErrorAndRefusesFurtherInput() |
| 69 |
{ |
| 70 |
ByteBuffer buffer = _frameParser.tail(); |
| 71 |
buffer.put("hello".getBytes()); |
| 72 |
_frameParser.process(); |
| 73 |
verify(_mockFrameHandler).closed(any(TransportException.class)); |
| 74 |
assertEquals(_frameParser.capacity(), Transport.END_OF_STREAM); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void testInputOfValidProtocolHeader() |
| 79 |
{ |
| 80 |
ByteBuffer buffer = _frameParser.tail(); |
| 81 |
buffer.put(HEADER); |
| 82 |
_frameParser.process(); |
| 83 |
|
| 84 |
assertNotNull(_frameParser.tail()); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void testInputOfValidProtocolHeaderInMultipleChunks() |
| 89 |
{ |
| 90 |
{ |
| 91 |
ByteBuffer buffer = _frameParser.tail(); |
| 92 |
buffer.put(HEADER, 0, 2); |
| 93 |
_frameParser.process(); |
| 94 |
} |
| 95 |
|
| 96 |
{ |
| 97 |
ByteBuffer buffer = _frameParser.tail(); |
| 98 |
buffer.put(HEADER, 2, HEADER.length - 2); |
| 99 |
_frameParser.process(); |
| 100 |
} |
| 101 |
|
| 102 |
assertNotNull(_frameParser.tail()); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void testInputOfValidFrame_invokesFrameTransportCallback() |
| 107 |
{ |
| 108 |
sendHeader(); |
| 109 |
|
| 110 |
// now send an open frame |
| 111 |
ByteBuffer buffer = _frameParser.tail(); |
| 112 |
|
| 113 |
Open openFrame = generateOpenFrame(); |
| 114 |
int channel = 0; |
| 115 |
byte[] frame = _amqpFramer.generateFrame(channel, openFrame); |
| 116 |
buffer.put(frame); |
| 117 |
|
| 118 |
_frameParser.process(); |
| 119 |
verify(_mockFrameHandler).handleFrame(frameMatching(channel, openFrame)); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void testInputOfFrameInMultipleChunks_invokesFrameTransportCallback() |
| 124 |
{ |
| 125 |
sendHeader(); |
| 126 |
|
| 127 |
Open openFrame = generateOpenFrame(); |
| 128 |
int channel = 0; |
| 129 |
byte[] frame = _amqpFramer.generateFrame(channel, openFrame); |
| 130 |
int lengthOfFirstChunk = 2; |
| 131 |
int lengthOfSecondChunk = (frame.length - lengthOfFirstChunk)/2; |
| 132 |
int lengthOfThirdChunk = frame.length - lengthOfFirstChunk - lengthOfSecondChunk; |
| 133 |
|
| 134 |
// send the first chunk |
| 135 |
{ |
| 136 |
ByteBuffer buffer = _frameParser.tail(); |
| 137 |
|
| 138 |
buffer.put(frame, 0, lengthOfFirstChunk); |
| 139 |
|
| 140 |
_frameParser.process(); |
| 141 |
|
| 142 |
verify(_mockFrameHandler, never()).handleFrame(any(TransportFrame.class)); |
| 143 |
} |
| 144 |
|
| 145 |
// send the second chunk |
| 146 |
{ |
| 147 |
ByteBuffer buffer = _frameParser.tail(); |
| 148 |
|
| 149 |
int secondChunkOffset = lengthOfFirstChunk; |
| 150 |
buffer.put(frame, secondChunkOffset, lengthOfSecondChunk); |
| 151 |
|
| 152 |
_frameParser.process(); |
| 153 |
verify(_mockFrameHandler, never()).handleFrame(any(TransportFrame.class)); |
| 154 |
} |
| 155 |
|
| 156 |
// send the third and final chunk |
| 157 |
{ |
| 158 |
ByteBuffer buffer = _frameParser.tail(); |
| 159 |
|
| 160 |
int thirdChunkOffset = lengthOfFirstChunk + lengthOfSecondChunk; |
| 161 |
buffer.put(frame, thirdChunkOffset, lengthOfThirdChunk); |
| 162 |
|
| 163 |
_frameParser.process(); |
| 164 |
verify(_mockFrameHandler).handleFrame(frameMatching(channel, openFrame)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void testInputOfTwoFrames_invokesFrameTransportTwice() |
| 170 |
{ |
| 171 |
sendHeader(); |
| 172 |
|
| 173 |
int channel = 0; |
| 174 |
Open openFrame = generateOpenFrame(); |
| 175 |
byte[] openFrameBytes = _amqpFramer.generateFrame(channel, openFrame); |
| 176 |
|
| 177 |
Close closeFrame = generateCloseFrame(); |
| 178 |
byte[] closeFrameBytes = _amqpFramer.generateFrame(channel, closeFrame); |
| 179 |
|
| 180 |
_frameParser.tail() |
| 181 |
.put(openFrameBytes) |
| 182 |
.put(closeFrameBytes); |
| 183 |
|
| 184 |
_frameParser.process(); |
| 185 |
|
| 186 |
InOrder inOrder = inOrder(_mockFrameHandler); |
| 187 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, openFrame)); |
| 188 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, closeFrame)); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void testFrameTransportTemporarilyRefusesOpenFrame() |
| 193 |
{ |
| 194 |
when(_mockFrameHandler.isHandlingFrames()).thenReturn(false); |
| 195 |
|
| 196 |
sendHeader(); |
| 197 |
|
| 198 |
// now send an open frame |
| 199 |
int channel = 0; |
| 200 |
Open openFrame = generateOpenFrame(); |
| 201 |
{ |
| 202 |
ByteBuffer buffer = _frameParser.tail(); |
| 203 |
|
| 204 |
byte[] frame = _amqpFramer.generateFrame(channel, openFrame); |
| 205 |
buffer.put(frame); |
| 206 |
|
| 207 |
_frameParser.process(); |
| 208 |
} |
| 209 |
|
| 210 |
verify(_mockFrameHandler, never()).handleFrame(any(TransportFrame.class)); |
| 211 |
|
| 212 |
when(_mockFrameHandler.isHandlingFrames()).thenReturn(true); |
| 213 |
|
| 214 |
// now ensure that the held frame gets sent on second input |
| 215 |
Close closeFrame = generateCloseFrame(); |
| 216 |
{ |
| 217 |
ByteBuffer buffer = _frameParser.tail(); |
| 218 |
|
| 219 |
byte[] frame = _amqpFramer.generateFrame(channel, closeFrame); |
| 220 |
buffer.put(frame); |
| 221 |
|
| 222 |
_frameParser.process(); |
| 223 |
} |
| 224 |
|
| 225 |
InOrder inOrder = inOrder(_mockFrameHandler); |
| 226 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, openFrame)); |
| 227 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, closeFrame)); |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void testFrameTransportTemporarilyRefusesOpenAndCloseFrame() |
| 232 |
{ |
| 233 |
when(_mockFrameHandler.isHandlingFrames()).thenReturn(false); |
| 234 |
|
| 235 |
sendHeader(); |
| 236 |
|
| 237 |
// now send an open frame |
| 238 |
int channel = 0; |
| 239 |
Open openFrame = generateOpenFrame(); |
| 240 |
{ |
| 241 |
ByteBuffer buffer = _frameParser.tail(); |
| 242 |
|
| 243 |
byte[] frame = _amqpFramer.generateFrame(channel, openFrame); |
| 244 |
buffer.put(frame); |
| 245 |
|
| 246 |
_frameParser.process(); |
| 247 |
} |
| 248 |
verify(_mockFrameHandler, never()).handleFrame(any(TransportFrame.class)); |
| 249 |
|
| 250 |
// now send a close frame |
| 251 |
Close closeFrame = generateCloseFrame(); |
| 252 |
{ |
| 253 |
ByteBuffer buffer = _frameParser.tail(); |
| 254 |
|
| 255 |
byte[] frame = _amqpFramer.generateFrame(channel, closeFrame); |
| 256 |
buffer.put(frame); |
| 257 |
|
| 258 |
_frameParser.process(); |
| 259 |
} |
| 260 |
verify(_mockFrameHandler, never()).handleFrame(any(TransportFrame.class)); |
| 261 |
|
| 262 |
when(_mockFrameHandler.isHandlingFrames()).thenReturn(true); |
| 263 |
|
| 264 |
_frameParser.flush(); |
| 265 |
|
| 266 |
InOrder inOrder = inOrder(_mockFrameHandler); |
| 267 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, openFrame)); |
| 268 |
inOrder.verify(_mockFrameHandler).handleFrame(frameMatching(channel, closeFrame)); |
| 269 |
} |
| 270 |
|
| 271 |
private void sendHeader() throws TransportException |
| 272 |
{ |
| 273 |
ByteBuffer buffer = _frameParser.tail(); |
| 274 |
buffer.put(HEADER); |
| 275 |
_frameParser.process(); |
| 276 |
} |
| 277 |
|
| 278 |
private Open generateOpenFrame() |
| 279 |
{ |
| 280 |
Open open = new Open(); |
| 281 |
open.setContainerId("containerid"); |
| 282 |
return open; |
| 283 |
} |
| 284 |
|
| 285 |
private Close generateCloseFrame() |
| 286 |
{ |
| 287 |
Close close = new Close(); |
| 288 |
return close; |
| 289 |
} |
| 290 |
|
| 291 |
private TransportFrame frameMatching(int channel, FrameBody frameBody) |
| 292 |
{ |
| 293 |
return argThat(new TransportFrameMatcher(channel, frameBody)); |
| 294 |
} |
| 295 |
|
| 296 |
private class TransportFrameMatcher implements ArgumentMatcher<TransportFrame> |
| 297 |
{ |
| 298 |
private final TransportFrame _expectedTransportFrame; |
| 299 |
|
| 300 |
TransportFrameMatcher(int expectedChannel, FrameBody expectedFrameBody) |
| 301 |
{ |
| 302 |
_expectedTransportFrame = new TransportFrame(expectedChannel, expectedFrameBody, null); |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public boolean matches(TransportFrame transportFrame) |
| 307 |
{ |
| 308 |
if(transportFrame == null) |
| 309 |
{ |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
FrameBody actualFrame = transportFrame.getBody(); |
| 314 |
|
| 315 |
int _expectedChannel = _expectedTransportFrame.getChannel(); |
| 316 |
FrameBody expectedFrame = _expectedTransportFrame.getBody(); |
| 317 |
|
| 318 |
return _expectedChannel == transportFrame.getChannel() |
| 319 |
&& expectedFrame.getClass().equals(actualFrame.getClass()); |
| 320 |
} |
| 321 |
|
| 322 |
@Override |
| 323 |
public String toString() |
| 324 |
{ |
| 325 |
return "TransportFrameMatcher, Expected: " + _expectedTransportFrame; |
| 326 |
} |
| 327 |
} |
| 328 |
} |