|
| 1 | +// Copyright (c) 2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | +// info@rabbitmq.com. |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +import com.rabbitmq.client.AMQP; |
| 22 | +import com.rabbitmq.client.MalformedFrameException; |
| 23 | +import com.rabbitmq.client.impl.AMQCommand; |
| 24 | +import com.rabbitmq.client.impl.Frame; |
| 25 | +import com.rabbitmq.client.impl.SocketFrameHandler; |
| 26 | +import java.io.DataOutputStream; |
| 27 | +import java.net.InetAddress; |
| 28 | +import java.net.ServerSocket; |
| 29 | +import java.net.Socket; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +/** |
| 33 | + * Verifies that once the negotiated frame_max is applied to the inbound reader, a frame whose total |
| 34 | + * size exceeds frame_max is rejected, while a frame of exactly frame_max is still accepted. |
| 35 | + */ |
| 36 | +public class NegotiatedFrameMaxInboundTest { |
| 37 | + |
| 38 | + private static final int FRAME_MAX = 4096; |
| 39 | + |
| 40 | + // Same limit AMQConnection applies once frame_max is negotiated. |
| 41 | + private static final int INBOUND_PAYLOAD_LIMIT = FRAME_MAX - AMQCommand.EMPTY_FRAME_SIZE + 1; |
| 42 | + |
| 43 | + @Test |
| 44 | + void frameOfExactlyFrameMaxIsAccepted() throws Exception { |
| 45 | + int payloadSize = FRAME_MAX - AMQCommand.EMPTY_FRAME_SIZE; |
| 46 | + Frame frame = readSingleFrame(payloadSize); |
| 47 | + assertThat(frame.getPayload()).hasSize(payloadSize); |
| 48 | + assertThat(frame.size()).isEqualTo(FRAME_MAX); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void frameLargerThanFrameMaxIsRejected() { |
| 53 | + int payloadSize = FRAME_MAX - AMQCommand.EMPTY_FRAME_SIZE + 1; // total frame size = frame_max + 1 |
| 54 | + assertThatThrownBy(() -> readSingleFrame(payloadSize)) |
| 55 | + .isInstanceOf(MalformedFrameException.class); |
| 56 | + } |
| 57 | + |
| 58 | + private static Frame readSingleFrame(int payloadSize) throws Exception { |
| 59 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 60 | + try (ServerSocket server = new ServerSocket(0, 1, loopback); |
| 61 | + Socket client = new Socket(loopback, server.getLocalPort()); |
| 62 | + Socket peer = server.accept()) { |
| 63 | + DataOutputStream out = new DataOutputStream(peer.getOutputStream()); |
| 64 | + out.writeByte(AMQP.FRAME_METHOD); |
| 65 | + out.writeShort(0); |
| 66 | + out.writeInt(payloadSize); |
| 67 | + out.write(new byte[payloadSize]); |
| 68 | + out.writeByte(AMQP.FRAME_END); |
| 69 | + out.flush(); |
| 70 | + |
| 71 | + SocketFrameHandler handler = new SocketFrameHandler(client); |
| 72 | + handler.setMaxInboundFramePayloadSize(INBOUND_PAYLOAD_LIMIT); |
| 73 | + return handler.readFrame(); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments