|
17 | 17 |
|
18 | 18 | import io.netty.buffer.ByteBuf; |
19 | 19 | import io.netty.channel.embedded.EmbeddedChannel; |
| 20 | +import io.netty.handler.codec.CorruptedFrameException; |
20 | 21 | import io.netty.handler.codec.TooLongFrameException; |
21 | 22 | import org.junit.jupiter.api.BeforeEach; |
22 | 23 | import org.junit.jupiter.api.Test; |
@@ -144,4 +145,83 @@ public void testValidFrameAfterOversized() { |
144 | 145 | expected.release(); |
145 | 146 | actual.release(); |
146 | 147 | } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testLengthPrefixSplitAtEveryPosition() { |
| 151 | + // Cover 1, 2, 3 and 4 byte prefixes, including lengths whose low 21 bits are all zero (multiples of 2 MiB). |
| 152 | + // For these the partial varint read of the first 3 prefix bytes evaluates to 0. |
| 153 | + int[] lengths = { |
| 154 | + 1, 127, |
| 155 | + 128, 16383, |
| 156 | + 16384, 2097151, |
| 157 | + 2097152, 2097152 + 5, 3 * 1024 * 1024, 4 * 1024 * 1024 |
| 158 | + }; |
| 159 | + for (int length : lengths) { |
| 160 | + ByteBuf wire = buffer(); |
| 161 | + ProtobufVarint32LengthFieldPrepender.writeRawVarint32(wire, length); |
| 162 | + int prefixLength = wire.readableBytes(); |
| 163 | + byte[] payload = new byte[length]; |
| 164 | + // Mark the last byte so a frame that starts or ends at the wrong position is detected. |
| 165 | + payload[length - 1] = 1; |
| 166 | + wire.writeBytes(payload); |
| 167 | + ByteBuf expected = wire.slice(prefixLength, length); |
| 168 | + |
| 169 | + for (int split = 1; split <= prefixLength; split++) { |
| 170 | + EmbeddedChannel channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder()); |
| 171 | + String message = "length: " + length + ", split after: " + split; |
| 172 | + assertFalse(channel.writeInbound(wire.retainedSlice(0, split)), message); |
| 173 | + assertTrue(channel.writeInbound(wire.retainedSlice(split, wire.readableBytes() - split)), message); |
| 174 | + |
| 175 | + ByteBuf actual = channel.readInbound(); |
| 176 | + try { |
| 177 | + assertEquals(length, actual.readableBytes(), message); |
| 178 | + assertEquals(expected, actual, message); |
| 179 | + } finally { |
| 180 | + actual.release(); |
| 181 | + } |
| 182 | + assertNull(channel.readInbound(), message); |
| 183 | + assertFalse(channel.finish(), message); |
| 184 | + } |
| 185 | + wire.release(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testFiveByteLengthPrefixSplitAtEveryPosition() { |
| 191 | + // 0x10000000 needs a 5 byte prefix: 80 80 80 80 01. Use a small maxFrameLength so we do not need to |
| 192 | + // allocate the whole frame, the TooLongFrameException proves the complete prefix was decoded. |
| 193 | + byte[] prefix = { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01 }; |
| 194 | + for (int split = 1; split < prefix.length; split++) { |
| 195 | + EmbeddedChannel channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder(1024)); |
| 196 | + assertFalse(channel.writeInbound(wrappedBuffer(prefix, 0, split)), "split after: " + split); |
| 197 | + final ByteBuf remaining = wrappedBuffer(prefix, split, prefix.length - split); |
| 198 | + TooLongFrameException e = assertThrows(TooLongFrameException.class, |
| 199 | + () -> channel.writeInbound(remaining), "split after: " + split); |
| 200 | + assertTrue(e.getMessage().contains(String.valueOf(0x10000000)), e.getMessage()); |
| 201 | + assertFalse(channel.finish()); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testReadRawVarint32WaitsForIncompleteVarint() { |
| 207 | + byte[][] incomplete = { |
| 208 | + { (byte) 0x80 }, |
| 209 | + { (byte) 0x80, (byte) 0x80 }, |
| 210 | + { (byte) 0x80, (byte) 0x80, (byte) 0x80 }, |
| 211 | + { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 } |
| 212 | + }; |
| 213 | + for (byte[] bytes : incomplete) { |
| 214 | + ByteBuf buf = wrappedBuffer(bytes); |
| 215 | + ProtobufVarint32FrameDecoder.readRawVarint32(buf); |
| 216 | + assertEquals(0, buf.readerIndex(), "readerIndex must not move for " + bytes.length + " incomplete bytes"); |
| 217 | + buf.release(); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testReadRawVarint32RejectsSixByteVarint() { |
| 223 | + ByteBuf buf = wrappedBuffer(new byte[] { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 }); |
| 224 | + assertThrows(CorruptedFrameException.class, () -> ProtobufVarint32FrameDecoder.readRawVarint32(buf)); |
| 225 | + buf.release(); |
| 226 | + } |
147 | 227 | } |