golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/conn_send_test.go (about)

     1  // Copyright 2023 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  //go:build go1.21
     6  
     7  package quic
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestAckElicitingAck(t *testing.T) {
    15  	// "A receiver that sends only non-ack-eliciting packets [...] might not receive
    16  	// an acknowledgment for a long period of time.
    17  	// [...] a receiver could send a [...] ack-eliciting frame occasionally [...]
    18  	// to elicit an ACK from the peer."
    19  	// https://www.rfc-editor.org/rfc/rfc9000#section-13.2.4-2
    20  	//
    21  	// Send a bunch of ack-eliciting packets, verify that the conn doesn't just
    22  	// send ACKs in response.
    23  	tc := newTestConn(t, clientSide, permissiveTransportParameters)
    24  	tc.handshake()
    25  	const count = 100
    26  	for i := 0; i < count; i++ {
    27  		tc.advance(1 * time.Millisecond)
    28  		tc.writeFrames(packetType1RTT,
    29  			debugFramePing{},
    30  		)
    31  		got, _ := tc.readFrame()
    32  		switch got.(type) {
    33  		case debugFrameAck:
    34  			continue
    35  		case debugFramePing:
    36  			return
    37  		}
    38  	}
    39  	t.Errorf("after sending %v PINGs, got no ack-eliciting response", count)
    40  }
    41  
    42  func TestSendPacketNumberSize(t *testing.T) {
    43  	tc := newTestConn(t, clientSide, permissiveTransportParameters)
    44  	tc.handshake()
    45  
    46  	recvPing := func() *testPacket {
    47  		t.Helper()
    48  		tc.conn.ping(appDataSpace)
    49  		p := tc.readPacket()
    50  		if p == nil {
    51  			t.Fatalf("want packet containing PING, got none")
    52  		}
    53  		return p
    54  	}
    55  
    56  	// Desynchronize the packet numbers the conn is sending and the ones it is receiving,
    57  	// by having the conn send a number of unacked packets.
    58  	for i := 0; i < 16; i++ {
    59  		recvPing()
    60  	}
    61  
    62  	// Establish the maximum packet number the conn has received an ACK for.
    63  	maxAcked := recvPing().num
    64  	tc.writeAckForAll()
    65  
    66  	// Make the conn send a sequence of packets.
    67  	// Check that the packet number is encoded with two bytes once the difference between the
    68  	// current packet and the max acked one is sufficiently large.
    69  	for want := maxAcked + 1; want < maxAcked+0x100; want++ {
    70  		p := recvPing()
    71  		if p.num != want {
    72  			t.Fatalf("received packet number %v, want %v", p.num, want)
    73  		}
    74  		gotPnumLen := int(p.header&0x03) + 1
    75  		wantPnumLen := 1
    76  		if p.num-maxAcked >= 0x80 {
    77  			wantPnumLen = 2
    78  		}
    79  		if gotPnumLen != wantPnumLen {
    80  			t.Fatalf("packet number 0x%x encoded with %v bytes, want %v (max acked = %v)", p.num, gotPnumLen, wantPnumLen, maxAcked)
    81  		}
    82  	}
    83  }