|
| 1 | +package com.rabbitmq.client.test.functional; |
| 2 | + |
| 3 | +import com.rabbitmq.client.*; |
| 4 | +import com.rabbitmq.client.impl.*; |
| 5 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.DataInputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.Socket; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test that the server correctly handles us when we send it bad frames |
| 14 | + */ |
| 15 | +public class UnexpectedFrames extends BrokerTestCase { |
| 16 | + private interface Confuser { |
| 17 | + public Frame confuse(Frame frame) throws IOException; |
| 18 | + } |
| 19 | + |
| 20 | + public void testMissingHeader() throws IOException { |
| 21 | + expectUnexpectedFrameError(new Confuser() { |
| 22 | + public Frame confuse(Frame frame) { |
| 23 | + if (frame.type == AMQP.FRAME_HEADER) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + return frame; |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + public void testMissingMethod() throws IOException { |
| 32 | + expectUnexpectedFrameError(new Confuser() { |
| 33 | + public Frame confuse(Frame frame) { |
| 34 | + if (frame.type == AMQP.FRAME_METHOD) { |
| 35 | + // We can't just skip the method as that will lead us to |
| 36 | + // send 0 bytes and hang waiting for a response. |
| 37 | + frame.type = AMQP.FRAME_HEADER; |
| 38 | + } |
| 39 | + return frame; |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + public void testMissingBody() throws IOException { |
| 45 | + expectUnexpectedFrameError(new Confuser() { |
| 46 | + public Frame confuse(Frame frame) { |
| 47 | + if (frame.type == AMQP.FRAME_BODY) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + return frame; |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public void testWrongClassInHeader() throws IOException { |
| 56 | + expectUnexpectedFrameError(new Confuser() { |
| 57 | + public Frame confuse(Frame frame) throws IOException { |
| 58 | + if (frame.type == AMQP.FRAME_HEADER) { |
| 59 | + byte[] payload = frame.accumulator.toByteArray(); |
| 60 | + // First two bytes = class ID, must match class ID from |
| 61 | + // method. |
| 62 | + payload[0] = 12; |
| 63 | + payload[1] = 34; |
| 64 | + frame.accumulator = new ByteArrayOutputStream(); |
| 65 | + frame.accumulator.write(payload, 0, payload.length); |
| 66 | + } |
| 67 | + return frame; |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + private void expectUnexpectedFrameError(Confuser confuser) throws IOException { |
| 73 | + ConnectionFactory factory = new ConnectionFactory(); |
| 74 | + Socket socket = factory.getSocketFactory().createSocket("localhost", |
| 75 | + AMQP.PROTOCOL.PORT); |
| 76 | + ConfusedFrameHandler handler = new ConfusedFrameHandler(socket); |
| 77 | + AMQConnection connection = new AMQConnection(factory, handler); |
| 78 | + try { |
| 79 | + connection.start(false); |
| 80 | + } catch (RedirectException e) {} |
| 81 | + Channel channel = connection.createChannel(); |
| 82 | + |
| 83 | + handler.confuser = confuser; |
| 84 | + |
| 85 | + try { |
| 86 | + String queue = channel.queueDeclare().getQueue(); |
| 87 | + channel.basicPublish("", queue, null, "Hello".getBytes()); |
| 88 | + GetResponse result = channel.basicGet(queue, false); |
| 89 | + channel.basicAck(result.getEnvelope().getDeliveryTag(), false); |
| 90 | + fail("We should have seen an UNEXPECTED_FRAME by now"); |
| 91 | + } |
| 92 | + catch (IOException e) { |
| 93 | + checkShutdownSignal(AMQP.UNEXPECTED_FRAME, e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static class ConfusedFrameHandler extends SocketFrameHandler { |
| 98 | + public ConfusedFrameHandler(Socket socket) throws IOException { |
| 99 | + super(socket); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void writeFrame(Frame frame) throws IOException { |
| 104 | + Frame confusedFrame = new Frame(); |
| 105 | + confusedFrame.accumulator = frame.accumulator; |
| 106 | + confusedFrame.channel = frame.channel; |
| 107 | + confusedFrame.type = frame.type; |
| 108 | + |
| 109 | + confusedFrame = confuser.confuse(confusedFrame); |
| 110 | + if (confusedFrame != null) { |
| 111 | + super.writeFrame(confusedFrame); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public Confuser confuser = new Confuser() { |
| 116 | + public Frame confuse(Frame frame) { |
| 117 | + // Do nothing to start with, we need to negotiate before the |
| 118 | + // server will send us unexpected_frame errors |
| 119 | + return frame; |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | +} |
0 commit comments