|
17 | 17 | package com.rabbitmq.client.test.functional;
|
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertEquals;
|
| 20 | +import static org.junit.Assert.assertTrue; |
20 | 21 | import static org.junit.Assert.fail;
|
21 | 22 |
|
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.DataOutputStream; |
22 | 25 | import java.io.IOException;
|
23 | 26 | import java.net.Socket;
|
| 27 | +import java.util.HashMap; |
24 | 28 | import java.util.List;
|
| 29 | +import java.util.Map; |
25 | 30 | import java.util.concurrent.ExecutorService;
|
26 | 31 | import java.util.concurrent.TimeoutException;
|
27 | 32 |
|
|
35 | 40 | import com.rabbitmq.client.GetResponse;
|
36 | 41 | import com.rabbitmq.client.impl.AMQCommand;
|
37 | 42 | import com.rabbitmq.client.impl.AMQConnection;
|
| 43 | +import com.rabbitmq.client.impl.ContentHeaderPropertyWriter; |
38 | 44 | import com.rabbitmq.client.impl.Frame;
|
39 | 45 | import com.rabbitmq.client.impl.FrameHandler;
|
40 | 46 | import com.rabbitmq.client.impl.LongStringHelper;
|
@@ -104,6 +110,47 @@ public FrameMax() {
|
104 | 110 | expectError(AMQP.FRAME_ERROR);
|
105 | 111 | }
|
106 | 112 |
|
| 113 | + /* client should throw exception if headers exceed negotiated |
| 114 | + * frame size */ |
| 115 | + @Test public void rejectHeadersExceedingFrameMax() |
| 116 | + throws IOException, TimeoutException { |
| 117 | + declareTransientTopicExchange("x"); |
| 118 | + String queueName = channel.queueDeclare().getQueue(); |
| 119 | + channel.queueBind(queueName, "x", "foobar"); |
| 120 | + |
| 121 | + Map<String, Object> headers = new HashMap<String, Object>(); |
| 122 | + String headerName = "x-huge-header"; |
| 123 | + |
| 124 | + // create headers with zero-length value to calculate maximum header value size before exceeding frame_max |
| 125 | + headers.put(headerName, LongStringHelper.asLongString(new byte[0])); |
| 126 | + AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().headers(headers).build(); |
| 127 | + Frame minimalHeaderFrame = properties.toFrame(0, 0); |
| 128 | + int maxHeaderValueSize = FRAME_MAX - minimalHeaderFrame.size(); |
| 129 | + |
| 130 | + // create headers with maximum header value size (frame size equals frame_max) |
| 131 | + headers.put(headerName, LongStringHelper.asLongString(new byte[maxHeaderValueSize])); |
| 132 | + properties = new AMQP.BasicProperties.Builder().headers(headers).build(); |
| 133 | + |
| 134 | + basicPublishVolatile(new byte[100], "x", "foobar", properties); |
| 135 | + assertDelivered(queueName, 1); |
| 136 | + |
| 137 | + // create headers with frame size exceeding frame_max by 1 |
| 138 | + headers.put(headerName, LongStringHelper.asLongString(new byte[maxHeaderValueSize + 1])); |
| 139 | + properties = new AMQP.BasicProperties.Builder().headers(headers).build(); |
| 140 | + try { |
| 141 | + basicPublishVolatile(new byte[100], "x", "foobar", properties); |
| 142 | + fail("expected rejectHeadersExceedingFrameMax to throw"); |
| 143 | + } catch (IllegalArgumentException iae) { |
| 144 | + assertTrue(iae.getMessage().startsWith("Content headers exceeded max frame size")); |
| 145 | + // check that the channel is still operational |
| 146 | + assertDelivered(queueName, 0); |
| 147 | + } |
| 148 | + |
| 149 | + // cleanup |
| 150 | + deleteExchange("x"); |
| 151 | + } |
| 152 | + |
| 153 | + |
107 | 154 | /* ConnectionFactory that uses MyFrameHandler rather than
|
108 | 155 | * SocketFrameHandler. */
|
109 | 156 | private static class MyConnectionFactory extends ConnectionFactory {
|
|
0 commit comments