Skip to content

Commit 2c6b25e

Browse files
author
Emile Joubert
committed
Tests for 0-9-1 error codes
1 parent 66a4a5e commit 2c6b25e

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-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
@@ -63,6 +63,7 @@ public static TestSuite suite() {
6363
suite.addTestSuite(BindToDefaultExchange.class);
6464
suite.addTestSuite(UnbindAutoDeleteExchange.class);
6565
suite.addTestSuite(RecoverAfterCancel.class);
66+
suite.addTestSuite(UnexpectedFrames.class);
6667
return suite;
6768
}
6869
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
// todo: use AMQP.UNEXPECTED_FRAME as soon as
94+
// 0.9.1 codegen becomes available
95+
checkShutdownSignal(505, e);
96+
}
97+
}
98+
99+
private static class ConfusedFrameHandler extends SocketFrameHandler {
100+
public ConfusedFrameHandler(Socket socket) throws IOException {
101+
super(socket);
102+
}
103+
104+
@Override
105+
public void writeFrame(Frame frame) throws IOException {
106+
Frame confusedFrame = new Frame();
107+
confusedFrame.accumulator = frame.accumulator;
108+
confusedFrame.channel = frame.channel;
109+
confusedFrame.type = frame.type;
110+
111+
confusedFrame = confuser.confuse(confusedFrame);
112+
if (confusedFrame != null) {
113+
super.writeFrame(confusedFrame);
114+
}
115+
}
116+
117+
public Confuser confuser = new Confuser() {
118+
public Frame confuse(Frame frame) {
119+
// Do nothing to start with, we need to negotiate before the
120+
// server will send us unexpected_frame errors
121+
return frame;
122+
}
123+
};
124+
}
125+
}

0 commit comments

Comments
 (0)