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
+
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
+ }
21
50
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
+ }
23
63
24
64
public void testMissingHeader () throws IOException {
25
65
expectUnexpectedFrameError (new Confuser () {
@@ -38,7 +78,11 @@ public Frame confuse(Frame frame) {
38
78
if (frame .type == AMQP .FRAME_METHOD ) {
39
79
// We can't just skip the method as that will lead us to
40
80
// 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 ;
42
86
}
43
87
return frame ;
44
88
}
@@ -73,53 +117,16 @@ public Frame confuse(Frame frame) throws IOException {
73
117
});
74
118
}
75
119
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 {
98
122
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 ;
103
125
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 );
124
130
}
131
+
125
132
}
0 commit comments