|
| 1 | +// Copyright (c) 2025 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 5 | +// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
| 6 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 7 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 8 | +// please see LICENSE-APACHE2. |
| 9 | +// |
| 10 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 11 | +// either express or implied. See the LICENSE file for specific language governing |
| 12 | +// rights and limitations of this software. |
| 13 | +// |
| 14 | +// If you have any questions regarding licensing, please contact us at |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 19 | + |
| 20 | +import com.rabbitmq.client.ConnectionFactory; |
| 21 | +import com.rabbitmq.client.MalformedFrameException; |
| 22 | +import io.netty.bootstrap.ServerBootstrap; |
| 23 | +import io.netty.buffer.ByteBuf; |
| 24 | +import io.netty.channel.ChannelFutureListener; |
| 25 | +import io.netty.channel.ChannelHandlerContext; |
| 26 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 27 | +import io.netty.channel.ChannelInitializer; |
| 28 | +import io.netty.channel.EventLoopGroup; |
| 29 | +import io.netty.channel.MultiThreadIoEventLoopGroup; |
| 30 | +import io.netty.channel.nio.NioIoHandler; |
| 31 | +import io.netty.channel.socket.SocketChannel; |
| 32 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 33 | +import io.netty.util.ReferenceCountUtil; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import org.junit.jupiter.params.ParameterizedTest; |
| 36 | +import org.junit.jupiter.params.provider.MethodSource; |
| 37 | + |
| 38 | +public class ProtocolVersionMismatch { |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @MethodSource("com.rabbitmq.client.test.TestUtils#ioLayers") |
| 42 | + void connectionShouldFailWithProtocolVersionMismatch(String ioLayer) throws Exception { |
| 43 | + int port = TestUtils.randomNetworkPort(); |
| 44 | + try (SimpleServer ignored = new SimpleServer(port)) { |
| 45 | + ConnectionFactory cf = TestUtils.connectionFactory(); |
| 46 | + TestUtils.setIoLayer(cf, ioLayer); |
| 47 | + cf.setPort(port); |
| 48 | + assertThatThrownBy(cf::newConnection).hasRootCauseInstanceOf(MalformedFrameException.class); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static class SimpleServer implements AutoCloseable { |
| 53 | + |
| 54 | + private final EventLoopGroup elp = |
| 55 | + new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()); |
| 56 | + |
| 57 | + private static final byte[] AMQP_HEADER = new byte[] {'A', 'M', 'Q', 'P', 0, 1, 0, 0}; |
| 58 | + |
| 59 | + private SimpleServer(int port) throws InterruptedException { |
| 60 | + ServerBootstrap b = new ServerBootstrap(); |
| 61 | + b.group(elp); |
| 62 | + b.channel(NioServerSocketChannel.class); |
| 63 | + b.childHandler( |
| 64 | + new ChannelInitializer<SocketChannel>() { |
| 65 | + @Override |
| 66 | + protected void initChannel(SocketChannel ch) { |
| 67 | + ch.pipeline() |
| 68 | + .addLast( |
| 69 | + new ChannelInboundHandlerAdapter() { |
| 70 | + @Override |
| 71 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 72 | + // discard the data |
| 73 | + ReferenceCountUtil.release(msg); |
| 74 | + ByteBuf b = ctx.alloc().buffer(AMQP_HEADER.length); |
| 75 | + b.writeBytes(AMQP_HEADER); |
| 76 | + ctx.channel() |
| 77 | + .writeAndFlush(b) |
| 78 | + .addListener(ChannelFutureListener.CLOSE); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + }) |
| 83 | + .validate(); |
| 84 | + b.bind(port).sync(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void close() { |
| 89 | + this.elp.shutdownGracefully(0, 0, TimeUnit.SECONDS); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments