|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.handler.codec.quic; |
| 17 | + |
| 18 | +import io.netty.buffer.ByteBuf; |
| 19 | +import io.netty.buffer.Unpooled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.net.InetSocketAddress; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | + |
| 27 | +public class QuicHeaderParserTest { |
| 28 | + |
| 29 | + private static final InetSocketAddress ADDRESS = new InetSocketAddress("127.0.0.1", 9999); |
| 30 | + |
| 31 | + // Initial packet with version 1, a 2 byte dcid, a 2 byte scid and the given encoded token length. |
| 32 | + private static ByteBuf initialPacket(long tokenLengthVarint, int tokenLengthBytes, int tokenBytes) { |
| 33 | + ByteBuf packet = Unpooled.buffer(); |
| 34 | + packet.writeByte(0xc0); |
| 35 | + packet.writeInt(1); |
| 36 | + packet.writeByte(2).writeShort(0x0102); |
| 37 | + packet.writeByte(2).writeShort(0x0304); |
| 38 | + switch (tokenLengthBytes) { |
| 39 | + case 1: |
| 40 | + packet.writeByte((int) tokenLengthVarint); |
| 41 | + break; |
| 42 | + case 4: |
| 43 | + packet.writeInt((int) (tokenLengthVarint | 0x80000000L)); |
| 44 | + break; |
| 45 | + case 8: |
| 46 | + packet.writeLong(tokenLengthVarint | 0xc000000000000000L); |
| 47 | + break; |
| 48 | + default: |
| 49 | + throw new IllegalArgumentException(); |
| 50 | + } |
| 51 | + packet.writeZero(tokenBytes); |
| 52 | + return packet; |
| 53 | + } |
| 54 | + |
| 55 | + private static int parseTokenLength(ByteBuf packet) throws Exception { |
| 56 | + int[] tokenLength = { -1 }; |
| 57 | + try (QuicHeaderParser parser = new QuicHeaderParser(2)) { |
| 58 | + parser.parse(ADDRESS, ADDRESS, packet, (sender, recipient, p, type, version, scid, dcid, token) -> |
| 59 | + tokenLength[0] = token.readableBytes()); |
| 60 | + } finally { |
| 61 | + packet.release(); |
| 62 | + } |
| 63 | + return tokenLength[0]; |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testTokenLength() throws Exception { |
| 68 | + assertEquals(5, parseTokenLength(initialPacket(5, 1, 5))); |
| 69 | + assertEquals(5, parseTokenLength(initialPacket(5, 4, 5))); |
| 70 | + assertEquals(5, parseTokenLength(initialPacket(5, 8, 5))); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testTokenLengthThatDoesNotFitIntoIntIsRejected() { |
| 75 | + // 2^32 + 5 would be truncated to 5 when narrowed to an int, which is available in the packet. |
| 76 | + assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () -> |
| 77 | + parseTokenLength(initialPacket((1L << 32) + 5, 8, 5))).error()); |
| 78 | + // 2^31 would become negative when narrowed to an int. |
| 79 | + assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () -> |
| 80 | + parseTokenLength(initialPacket(1L << 31, 8, 5))).error()); |
| 81 | + assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () -> |
| 82 | + parseTokenLength(initialPacket((1L << 62) - 1, 8, 5))).error()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testTokenLengthLargerThanPacketIsRejected() { |
| 87 | + assertEquals(QuicTransportError.PROTOCOL_VIOLATION, assertThrows(QuicException.class, () -> |
| 88 | + parseTokenLength(initialPacket(6, 1, 5))).error()); |
| 89 | + } |
| 90 | +} |