Skip to content

Commit c384a37

Browse files
author
Michael Bridgen
committed
Merge bug23093
2 parents 695efa5 + 77f07f8 commit c384a37

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

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

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,53 @@
1313
* Test that the server correctly handles us when we send it bad frames
1414
*/
1515
public class UnexpectedFrames extends BrokerTestCase {
16+
1617
private interface Confuser {
1718
public Frame confuse(Frame frame) throws IOException;
1819
}
1920

20-
@Override protected void setUp() throws IOException {}
21+
private static class ConfusedFrameHandler extends SocketFrameHandler {
22+
23+
private boolean confusedOnce = false;
24+
25+
public ConfusedFrameHandler(Socket socket) throws IOException {
26+
super(socket);
27+
}
28+
29+
@Override
30+
public void writeFrame(Frame frame) throws IOException {
31+
if (confusedOnce) {
32+
super.writeFrame(frame);
33+
} else {
34+
Frame confusedFrame = confuser.confuse(frame);
35+
if (confusedFrame != frame) confusedOnce = true;
36+
if (confusedFrame != null) {
37+
super.writeFrame(confusedFrame);
38+
}
39+
}
40+
}
41+
42+
public Confuser confuser = new Confuser() {
43+
public Frame confuse(Frame frame) {
44+
// Do nothing to start with, we need to negotiate before the
45+
// server will send us unexpected_frame errors
46+
return frame;
47+
}
48+
};
49+
}
2150

22-
@Override protected void tearDown() throws IOException {}
51+
private static class ConfusedConnectionFactory extends ConnectionFactory {
52+
53+
@Override protected FrameHandler createFrameHandler(Socket sock)
54+
throws IOException {
55+
return new ConfusedFrameHandler(sock);
56+
}
57+
}
58+
59+
public UnexpectedFrames() {
60+
super();
61+
connectionFactory = new ConfusedConnectionFactory();
62+
}
2363

2464
public void testMissingHeader() throws IOException {
2565
expectUnexpectedFrameError(new Confuser() {
@@ -38,7 +78,11 @@ public Frame confuse(Frame frame) {
3878
if (frame.type == AMQP.FRAME_METHOD) {
3979
// We can't just skip the method as that will lead us to
4080
// send 0 bytes and hang waiting for a response.
41-
frame.type = AMQP.FRAME_HEADER;
81+
Frame confusedFrame = new Frame(AMQP.FRAME_HEADER,
82+
frame.channel,
83+
frame.payload);
84+
confusedFrame.accumulator = frame.accumulator;
85+
return confusedFrame;
4286
}
4387
return frame;
4488
}
@@ -73,53 +117,16 @@ public Frame confuse(Frame frame) throws IOException {
73117
});
74118
}
75119

76-
private void expectUnexpectedFrameError(Confuser confuser) throws IOException {
77-
ConnectionFactory factory = new ConnectionFactory();
78-
Socket socket = factory.getSocketFactory().createSocket("localhost",
79-
AMQP.PROTOCOL.PORT);
80-
ConfusedFrameHandler handler = new ConfusedFrameHandler(socket);
81-
AMQConnection connection = new AMQConnection(factory, handler);
82-
connection.start();
83-
Channel channel = connection.createChannel();
84-
85-
handler.confuser = confuser;
86-
87-
try {
88-
//NB: the frame confuser relies on the encoding of the
89-
//method field to be at least 8 bytes long
90-
channel.basicPublish("", "routing key", null, "Hello".getBytes());
91-
channel.basicQos(0);
92-
fail("We should have seen an UNEXPECTED_FRAME by now");
93-
}
94-
catch (IOException e) {
95-
checkShutdownSignal(AMQP.UNEXPECTED_FRAME, e);
96-
}
97-
}
120+
private void expectUnexpectedFrameError(Confuser confuser)
121+
throws IOException {
98122

99-
private static class ConfusedFrameHandler extends SocketFrameHandler {
100-
public ConfusedFrameHandler(Socket socket) throws IOException {
101-
super(socket);
102-
}
123+
((ConfusedFrameHandler)((AMQConnection)connection).getFrameHandler()).
124+
confuser = confuser;
103125

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-
};
126+
//NB: the frame confuser relies on the encoding of the
127+
//method field to be at least 8 bytes long
128+
channel.basicPublish("", "routing key", null, "Hello".getBytes());
129+
expectError(AMQP.UNEXPECTED_FRAME);
124130
}
131+
125132
}

0 commit comments

Comments
 (0)