Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/engine/impl/SaslFrameParserTest.java

raw
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.hamcrest.CoreMatchers.containsString;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.junit.Assert.fail;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.isA;
26 import static org.mockito.ArgumentMatchers.isNull;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.nio.ByteBuffer;
33 import java.util.Arrays;
34
35 import org.apache.qpid.proton.amqp.Binary;
36 import org.apache.qpid.proton.amqp.Symbol;
37 import org.apache.qpid.proton.amqp.security.SaslFrameBody;
38 import org.apache.qpid.proton.amqp.security.SaslInit;
39 import org.apache.qpid.proton.amqp.transport.FrameBody;
40 import org.apache.qpid.proton.amqp.transport.Open;
41 import org.apache.qpid.proton.codec.AMQPDefinedTypes;
42 import org.apache.qpid.proton.codec.ByteBufferDecoder;
43 import org.apache.qpid.proton.codec.DecodeException;
44 import org.apache.qpid.proton.codec.DecoderImpl;
45 import org.apache.qpid.proton.codec.EncoderImpl;
46 import org.apache.qpid.proton.engine.Transport;
47 import org.apache.qpid.proton.engine.TransportException;
48 import org.junit.Test;
49
50 /**
51 * TODO test case where input provides frame and half etc
52 */
53 public class SaslFrameParserTest
54 {
55 private final SaslFrameHandler _mockSaslFrameHandler = mock(SaslFrameHandler.class);
56 private final ByteBufferDecoder _mockDecoder = mock(ByteBufferDecoder.class);
57 private final TransportImpl mockTransport = mock(TransportImpl.class);
58 private final SaslFrameParser _frameParser;
59 private final SaslFrameParser _frameParserWithMockDecoder = new SaslFrameParser(_mockSaslFrameHandler, _mockDecoder, Transport.MIN_MAX_FRAME_SIZE, mockTransport);
60 private final AmqpFramer _amqpFramer = new AmqpFramer();
61
62 private final SaslInit _saslFrameBody;
63 private final ByteBuffer _saslFrameBytes;
64
65 public SaslFrameParserTest()
66 {
67 DecoderImpl decoder = new DecoderImpl();
68 EncoderImpl encoder = new EncoderImpl(decoder);
69 AMQPDefinedTypes.registerAllTypes(decoder,encoder);
70
71 _frameParser = new SaslFrameParser(_mockSaslFrameHandler, decoder, Transport.MIN_MAX_FRAME_SIZE, mockTransport);
72 _saslFrameBody = new SaslInit();
73 _saslFrameBody.setMechanism(Symbol.getSymbol("unused"));
74 _saslFrameBytes = ByteBuffer.wrap(_amqpFramer.generateSaslFrame(0, new byte[0], _saslFrameBody));
75 }
76
77 @Test
78 public void testInputOfValidFrame()
79 {
80 sendAmqpSaslHeader(_frameParser);
81
82 when(_mockSaslFrameHandler.isDone()).thenReturn(false);
83
84 _frameParser.input(_saslFrameBytes);
85
86 verify(_mockSaslFrameHandler).handle(isA(SaslInit.class), (Binary)isNull());
87 }
88
89 /*
90 * Test that SASL frames indicating they are over 512 bytes (the minimum max frame size, and the
91 * largest usable during SASL before max frame size is later determined by the Open frames) causes an error.
92 */
93 @Test
94 public void testInputOfFrameWithInvalidSizeExceedingMinMaxFrameSize()
95 {
96 sendAmqpSaslHeader(_frameParserWithMockDecoder);
97
98 // http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-security-v1.0-os.html#doc-idp43536
99 // Description: '513byte sized' SASL frame header
100 byte[] oversizedSaslFrameHeader = new byte[] { (byte) 0x00, 0x00, 0x02, 0x01, 0x02, 0x01, 0x00, 0x00 };
101
102 try {
103 _frameParserWithMockDecoder.input(ByteBuffer.wrap(oversizedSaslFrameHeader));
104 fail("expected exception");
105 } catch (TransportException e) {
106 assertThat(e.getMessage(), containsString("frame size 513 larger than maximum"));
107 }
108 }
109
110 /*
111 * Test that when the frame parser is created with a size limit above the 512 byte min-max-frame-size, frames
112 * arriving with headers indicating they are over this size causes an exception.
113 */
114 @Test
115 public void testInputOfFrameWithInvalidSizeWhenSpecifyingLargeMaxFrameSize()
116 {
117 SaslFrameParser frameParserWithLargeMaxSize = new SaslFrameParser(_mockSaslFrameHandler, _mockDecoder, 2017, mockTransport);
118 sendAmqpSaslHeader(frameParserWithLargeMaxSize);
119
120 // http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-security-v1.0-os.html#doc-idp43536
121 // Description: '2057byte sized' SASL frame header
122 byte[] oversizedSaslFrameHeader = new byte[] { (byte) 0x00, 0x00, 0x08, 0x09, 0x02, 0x01, 0x00, 0x00 };
123
124 try {
125 frameParserWithLargeMaxSize.input(ByteBuffer.wrap(oversizedSaslFrameHeader));
126 fail("expected exception");
127 } catch (TransportException e) {
128 assertThat(e.getMessage(), containsString("frame size 2057 larger than maximum SASL frame size 2017"));
129 }
130 }
131
132 /*
133 * Test that SASL frames indicating they are under 8 bytes (the minimum size of the frame header) causes an error.
134 */
135 @Test
136 public void testInputOfFrameWithInvalidSizeBelowMinimumPossible()
137 {
138 sendAmqpSaslHeader(_frameParserWithMockDecoder);
139
140 // http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-security-v1.0-os.html#doc-idp43536
141 // Description: '7byte sized' SASL frame header
142 byte[] undersizedSaslFrameHeader = new byte[] { (byte) 0x00, 0x00, 0x00, 0x07, 0x02, 0x01, 0x00, 0x00 };
143
144 try {
145 _frameParserWithMockDecoder.input(ByteBuffer.wrap(undersizedSaslFrameHeader));
146 fail("expected exception");
147 } catch (TransportException e) {
148 assertThat(e.getMessage(), containsString("frame size 7 smaller than minimum"));
149 }
150 }
151
152 /*
153 * Test that SASL frames indicating a doff under 8 bytes (the minimum size of the frame header) causes an error.
154 */
155 @Test
156 public void testInputOfFrameWithInvalidDoffBelowMinimumPossible()
157 {
158 sendAmqpSaslHeader(_frameParserWithMockDecoder);
159
160 // http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-security-v1.0-os.html#doc-idp43536
161 // Description: '8byte sized' SASL frame header with invalid doff of 1[*4 = 4bytes]
162 byte[] underMinDoffSaslFrameHeader = new byte[] { (byte) 0x00, 0x00, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00 };
163
164 try {
165 _frameParserWithMockDecoder.input(ByteBuffer.wrap(underMinDoffSaslFrameHeader));
166 fail("expected exception");
167 } catch (TransportException e) {
168 assertThat(e.getMessage(), containsString("data offset 4 smaller than minimum"));
169 }
170 }
171
172 /*
173 * Test that SASL frames indicating a doff larger than the frame size causes an error.
174 */
175 @Test
176 public void testInputOfFrameWithInvalidDoffAboveMaximumPossible()
177 {
178 sendAmqpSaslHeader(_frameParserWithMockDecoder);
179
180 // http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-security-v1.0-os.html#doc-idp43536
181 // Description: '8byte sized' SASL frame header with invalid doff of 3[*4 = 12bytes]
182 byte[] overFrameSizeDoffSaslFrameHeader = new byte[] { (byte) 0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0x00, 0x00 };
183
184 try {
185 _frameParserWithMockDecoder.input(ByteBuffer.wrap(overFrameSizeDoffSaslFrameHeader));
186 fail("expected exception");
187 } catch (TransportException e) {
188 assertThat(e.getMessage(), containsString("data offset 12 larger than the frame size 8"));
189 }
190 }
191
192 @Test
193 public void testInputOfInvalidFrame_causesErrorAndRefusesFurtherInput()
194 {
195 sendAmqpSaslHeader(_frameParserWithMockDecoder);
196
197 String exceptionMessage = "dummy decode exception";
198 when(_mockDecoder.readObject()).thenThrow(new DecodeException(exceptionMessage));
199
200 // We send a valid frame but the mock decoder has been configured to reject it
201 try {
202 _frameParserWithMockDecoder.input(_saslFrameBytes);
203 fail("expected exception");
204 } catch (TransportException e) {
205 assertThat(e.getMessage(), containsString(exceptionMessage));
206 }
207
208 verify(_mockSaslFrameHandler, never()).handle(any(SaslFrameBody.class), any(Binary.class));
209
210 // Check that any further interaction causes an error TransportResult.
211 try {
212 _frameParserWithMockDecoder.input(ByteBuffer.wrap("".getBytes()));
213 fail("expected exception");
214 } catch (TransportException e) {
215 // this is expected
216 }
217 }
218
219 @Test
220 public void testInputOfNonSaslFrame_causesErrorAndRefusesFurtherInput()
221 {
222 sendAmqpSaslHeader(_frameParserWithMockDecoder);
223
224 FrameBody nonSaslFrame = new Open();
225 when(_mockDecoder.readObject()).thenReturn(nonSaslFrame);
226
227 // We send a valid frame but the mock decoder has been configured to reject it
228 try {
229 _frameParserWithMockDecoder.input(_saslFrameBytes);
230 fail("expected exception");
231 } catch (TransportException e) {
232 assertThat(e.getMessage(), containsString("Unexpected frame type encountered."));
233 }
234
235 verify(_mockSaslFrameHandler, never()).handle(any(SaslFrameBody.class), any(Binary.class));
236
237 // Check that any further interaction causes an error TransportResult.
238 try {
239 _frameParserWithMockDecoder.input(ByteBuffer.wrap("".getBytes()));
240 fail("expected exception");
241 } catch (TransportException e) {
242 // this is expected
243 }
244 }
245
246 /*
247 * Test that if the first 8 bytes don't match the AMQP SASL header, it causes an error.
248 */
249 @Test
250 public void testInputOfInvalidHeader() {
251 for (int invalidIndex = 0; invalidIndex < 8; invalidIndex++) {
252 doInputOfInvalidHeaderTestImpl(invalidIndex);
253 }
254 }
255
256 private void doInputOfInvalidHeaderTestImpl(int invalidIndex) {
257 SaslFrameHandler mockSaslFrameHandler = mock(SaslFrameHandler.class);
258 ByteBufferDecoder mockDecoder = mock(ByteBufferDecoder.class);
259 TransportImpl mockTransport = mock(TransportImpl.class);
260
261 SaslFrameParser saslFrameParser = new SaslFrameParser(mockSaslFrameHandler, mockDecoder, Transport.MIN_MAX_FRAME_SIZE, mockTransport);
262
263 byte[] header = Arrays.copyOf(AmqpHeader.SASL_HEADER, AmqpHeader.SASL_HEADER.length);
264 header[invalidIndex] = 'X';
265
266 try {
267 saslFrameParser.input(ByteBuffer.wrap(header));
268 fail("expected exception");
269 } catch (TransportException e) {
270 assertThat(e.getMessage(), containsString("AMQP SASL header mismatch"));
271 assertThat(e.getMessage(), containsString("In state: HEADER" + invalidIndex));
272 }
273
274 // Check that further interaction throws TransportException.
275 try {
276 saslFrameParser.input(ByteBuffer.wrap(new byte[0]));
277 fail("expected exception");
278 } catch (TransportException e) {
279 // Expected
280 }
281 }
282
283 /*
284 * Test that if the first 8 bytes, fed in one at a time, don't match the AMQP SASL header, it causes an error.
285 */
286 @Test
287 public void testInputOfValidHeaderInSegments()
288 {
289 sendAmqpSaslHeaderInSegments();
290
291 // Try feeding in an actual frame now to check we get past the header parsing ok
292 when(_mockSaslFrameHandler.isDone()).thenReturn(false);
293
294 _frameParser.input(_saslFrameBytes);
295
296 verify(_mockSaslFrameHandler).handle(isA(SaslInit.class), (Binary)isNull());
297 }
298
299 private void sendAmqpSaslHeaderInSegments()
300 {
301 for (int headerIndex = 0; headerIndex < 8; headerIndex++)
302 {
303 byte headerPart = AmqpHeader.SASL_HEADER[headerIndex];
304 _frameParser.input(ByteBuffer.wrap(new byte[] { headerPart }));
305 }
306 }
307
308 private void sendAmqpSaslHeader(SaslFrameParser saslFrameParser)
309 {
310 saslFrameParser.input(ByteBuffer.wrap(AmqpHeader.SASL_HEADER));
311 }
312
313 }