|
13 | 13 | * Test that the server correctly handles us when we send it bad frames |
14 | 14 | */ |
15 | 15 | public class UnexpectedFrames extends BrokerTestCase { |
| 16 | + |
16 | 17 | private interface Confuser { |
17 | 18 | public Frame confuse(Frame frame) throws IOException; |
18 | 19 | } |
19 | 20 |
|
20 | | - @Override protected void setUp() throws IOException {} |
| 21 | + private static class ConfusedFrameHandler extends SocketFrameHandler { |
| 22 | + public ConfusedFrameHandler(Socket socket) throws IOException { |
| 23 | + super(socket); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void writeFrame(Frame frame) throws IOException { |
| 28 | + Frame confusedFrame = new Frame(); |
| 29 | + confusedFrame.accumulator = frame.accumulator; |
| 30 | + confusedFrame.channel = frame.channel; |
| 31 | + confusedFrame.type = frame.type; |
| 32 | + |
| 33 | + confusedFrame = confuser.confuse(confusedFrame); |
| 34 | + if (confusedFrame != null) { |
| 35 | + super.writeFrame(confusedFrame); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public Confuser confuser = new Confuser() { |
| 40 | + public Frame confuse(Frame frame) { |
| 41 | + // Do nothing to start with, we need to negotiate before the |
| 42 | + // server will send us unexpected_frame errors |
| 43 | + return frame; |
| 44 | + } |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + private static class ConfusedConnectionFactory extends ConnectionFactory { |
| 49 | + |
| 50 | + @Override protected FrameHandler createFrameHandler(Socket sock) |
| 51 | + throws IOException { |
| 52 | + return new ConfusedFrameHandler(sock); |
| 53 | + } |
| 54 | + } |
21 | 55 |
|
22 | | - @Override protected void tearDown() throws IOException {} |
| 56 | + public UnexpectedFrames() { |
| 57 | + super(); |
| 58 | + connectionFactory = new ConfusedConnectionFactory(); |
| 59 | + } |
23 | 60 |
|
24 | 61 | public void testMissingHeader() throws IOException { |
25 | 62 | expectUnexpectedFrameError(new Confuser() { |
@@ -73,53 +110,16 @@ public Frame confuse(Frame frame) throws IOException { |
73 | 110 | }); |
74 | 111 | } |
75 | 112 |
|
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 | | - } |
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; |
| 113 | + private void expectUnexpectedFrameError(Confuser confuser) |
| 114 | + throws IOException { |
110 | 115 |
|
111 | | - confusedFrame = confuser.confuse(confusedFrame); |
112 | | - if (confusedFrame != null) { |
113 | | - super.writeFrame(confusedFrame); |
114 | | - } |
115 | | - } |
| 116 | + ((ConfusedFrameHandler)((AMQConnection)connection).getFrameHandler()). |
| 117 | + confuser = confuser; |
116 | 118 |
|
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 | | - }; |
| 119 | + //NB: the frame confuser relies on the encoding of the |
| 120 | + //method field to be at least 8 bytes long |
| 121 | + channel.basicPublish("", "routing key", null, "Hello".getBytes()); |
| 122 | + expectError(AMQP.UNEXPECTED_FRAME); |
124 | 123 | } |
| 124 | + |
125 | 125 | } |
0 commit comments