Skip to content

Commit bd8fcbe

Browse files
author
Simon MacMullen
committed
Test unexpected frame errors on the server side.
1 parent 60ba8fd commit bd8fcbe

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

test/src/com/rabbitmq/client/test/functional/FunctionalTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static TestSuite suite() {
5858
suite.addTestSuite(QueueExclusivity.class);
5959
suite.addTestSuite(InvalidAcks.class);
6060
suite.addTestSuite(InvalidAcksTx.class);
61+
suite.addTestSuite(UnexpectedFrames.class);
6162
return suite;
6263
}
6364
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
connection.start();
79+
Channel channel = connection.createChannel();
80+
81+
handler.confuser = confuser;
82+
83+
try {
84+
String queue = channel.queueDeclare().getQueue();
85+
channel.basicPublish("", queue, null, "Hello".getBytes());
86+
GetResponse result = channel.basicGet(queue, false);
87+
channel.basicAck(result.getEnvelope().getDeliveryTag(), false);
88+
fail("We should have seen an UNEXPECTED_FRAME by now");
89+
}
90+
catch (IOException e) {
91+
checkShutdownSignal(AMQP.UNEXPECTED_FRAME, e);
92+
}
93+
}
94+
95+
private static class ConfusedFrameHandler extends SocketFrameHandler {
96+
public ConfusedFrameHandler(Socket socket) throws IOException {
97+
super(socket);
98+
}
99+
100+
@Override
101+
public void writeFrame(Frame frame) throws IOException {
102+
Frame confusedFrame = new Frame();
103+
confusedFrame.accumulator = frame.accumulator;
104+
confusedFrame.channel = frame.channel;
105+
confusedFrame.type = frame.type;
106+
107+
confusedFrame = confuser.confuse(confusedFrame);
108+
if (confusedFrame != null) {
109+
super.writeFrame(confusedFrame);
110+
}
111+
}
112+
113+
public Confuser confuser = new Confuser() {
114+
public Frame confuse(Frame frame) {
115+
// Do nothing to start with, we need to negotiate before the
116+
// server will send us unexpected_frame errors
117+
return frame;
118+
}
119+
};
120+
}
121+
}

0 commit comments

Comments
 (0)