|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package quic |
| 6 | + |
| 7 | +import "testing" |
| 8 | + |
| 9 | +func TestSkipPackets(t *testing.T) { |
| 10 | + tc, s := newTestConnAndLocalStream(t, serverSide, uniStream, permissiveTransportParameters) |
| 11 | + connWritesPacket := func() { |
| 12 | + s.WriteByte(0) |
| 13 | + s.Flush() |
| 14 | + tc.wantFrameType("conn sends STREAM data", |
| 15 | + packetType1RTT, debugFrameStream{}) |
| 16 | + tc.writeAckForLatest() |
| 17 | + tc.wantIdle("conn is idle") |
| 18 | + } |
| 19 | + connWritesPacket() |
| 20 | + |
| 21 | +expectSkip: |
| 22 | + for maxUntilSkip := 256; maxUntilSkip <= 1024; maxUntilSkip *= 2 { |
| 23 | + for range maxUntilSkip + 1 { |
| 24 | + nextNum := tc.lastPacket.num + 1 |
| 25 | + |
| 26 | + connWritesPacket() |
| 27 | + |
| 28 | + if tc.lastPacket.num == nextNum+1 { |
| 29 | + // A packet number was skipped, as expected. |
| 30 | + continue expectSkip |
| 31 | + } |
| 32 | + if tc.lastPacket.num != nextNum { |
| 33 | + t.Fatalf("got packet number %v, want %v or %v+1", tc.lastPacket.num, nextNum, nextNum) |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + t.Fatalf("no numbers skipped after %v packets", maxUntilSkip) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestSkipAckForSkippedPacket(t *testing.T) { |
| 42 | + tc, s := newTestConnAndLocalStream(t, serverSide, uniStream, permissiveTransportParameters) |
| 43 | + |
| 44 | + // Cause the connection to send packets until it skips a packet number. |
| 45 | + for { |
| 46 | + // Cause the connection to send a packet. |
| 47 | + last := tc.lastPacket |
| 48 | + s.WriteByte(0) |
| 49 | + s.Flush() |
| 50 | + tc.wantFrameType("conn sends STREAM data", |
| 51 | + packetType1RTT, debugFrameStream{}) |
| 52 | + |
| 53 | + if tc.lastPacket.num > 256 { |
| 54 | + t.Fatalf("no numbers skipped after 256 packets") |
| 55 | + } |
| 56 | + |
| 57 | + // Acknowledge everything up to the packet before the one we just received. |
| 58 | + // We don't acknowledge the most-recently-received packet, because doing |
| 59 | + // so will cause the connection to drop state for the skipped packet number. |
| 60 | + // (We only retain state up to the oldest in-flight packet.) |
| 61 | + // |
| 62 | + // If the conn has skipped a packet number, then this ack will improperly |
| 63 | + // acknowledge the unsent packet. |
| 64 | + t.Log(tc.lastPacket.num) |
| 65 | + tc.writeFrames(tc.lastPacket.ptype, debugFrameAck{ |
| 66 | + ranges: []i64range[packetNumber]{{0, tc.lastPacket.num}}, |
| 67 | + }) |
| 68 | + |
| 69 | + if last != nil && tc.lastPacket.num == last.num+2 { |
| 70 | + // The connection has skipped a packet number. |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // We wrote an ACK for a skipped packet number. |
| 76 | + // The connection should close. |
| 77 | + tc.wantFrame("ACK for skipped packet causes CONNECTION_CLOSE", |
| 78 | + packetType1RTT, debugFrameConnectionCloseTransport{ |
| 79 | + code: errProtocolViolation, |
| 80 | + }) |
| 81 | +} |
0 commit comments