File: version_test.go

package info (click to toggle)
golang-golang-x-net 1%3A0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 8,636 kB
  • sloc: asm: 18; makefile: 12; sh: 7
file content (110 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import (
	"bytes"
	"context"
	"crypto/tls"
	"testing"
)

func TestVersionNegotiationServerReceivesUnknownVersion(t *testing.T) {
	config := &Config{
		TLSConfig: newTestTLSConfig(serverSide),
	}
	te := newTestEndpoint(t, config)

	// Packet of unknown contents for some unrecognized QUIC version.
	dstConnID := []byte{1, 2, 3, 4}
	srcConnID := []byte{5, 6, 7, 8}
	pkt := []byte{
		0b1000_0000,
		0x00, 0x00, 0x00, 0x0f,
	}
	pkt = append(pkt, byte(len(dstConnID)))
	pkt = append(pkt, dstConnID...)
	pkt = append(pkt, byte(len(srcConnID)))
	pkt = append(pkt, srcConnID...)
	for len(pkt) < paddedInitialDatagramSize {
		pkt = append(pkt, 0)
	}

	te.write(&datagram{
		b: pkt,
	})
	gotPkt := te.read()
	if gotPkt == nil {
		t.Fatalf("got no response; want Version Negotiation")
	}
	if got := getPacketType(gotPkt); got != packetTypeVersionNegotiation {
		t.Fatalf("got packet type %v; want Version Negotiation", got)
	}
	gotDst, gotSrc, versions := parseVersionNegotiation(gotPkt)
	if got, want := gotDst, srcConnID; !bytes.Equal(got, want) {
		t.Errorf("got Destination Connection ID %x, want %x", got, want)
	}
	if got, want := gotSrc, dstConnID; !bytes.Equal(got, want) {
		t.Errorf("got Source Connection ID %x, want %x", got, want)
	}
	if got, want := versions, []byte{0, 0, 0, 1}; !bytes.Equal(got, want) {
		t.Errorf("got Supported Version %x, want %x", got, want)
	}
}

func TestVersionNegotiationClientAborts(t *testing.T) {
	tc := newTestConn(t, clientSide)
	p := tc.readPacket() // client Initial packet
	tc.endpoint.write(&datagram{
		b: appendVersionNegotiation(nil, p.srcConnID, p.dstConnID, 10),
	})
	tc.wantIdle("connection does not send a CONNECTION_CLOSE")
	if err := tc.conn.waitReady(canceledContext()); err != errVersionNegotiation {
		t.Errorf("conn.waitReady() = %v, want errVersionNegotiation", err)
	}
}

func TestVersionNegotiationClientIgnoresAfterProcessingPacket(t *testing.T) {
	tc := newTestConn(t, clientSide)
	tc.ignoreFrame(frameTypeAck)
	p := tc.readPacket() // client Initial packet
	tc.writeFrames(packetTypeInitial,
		debugFrameCrypto{
			data: tc.cryptoDataIn[tls.QUICEncryptionLevelInitial],
		})
	tc.endpoint.write(&datagram{
		b: appendVersionNegotiation(nil, p.srcConnID, p.dstConnID, 10),
	})
	if err := tc.conn.waitReady(canceledContext()); err != context.Canceled {
		t.Errorf("conn.waitReady() = %v, want context.Canceled", err)
	}
	tc.writeFrames(packetTypeHandshake,
		debugFrameCrypto{
			data: tc.cryptoDataIn[tls.QUICEncryptionLevelHandshake],
		})
	tc.wantFrameType("conn ignores Version Negotiation and continues with handshake",
		packetTypeHandshake, debugFrameCrypto{})
}

func TestVersionNegotiationClientIgnoresMismatchingSourceConnID(t *testing.T) {
	tc := newTestConn(t, clientSide)
	tc.ignoreFrame(frameTypeAck)
	p := tc.readPacket() // client Initial packet
	tc.endpoint.write(&datagram{
		b: appendVersionNegotiation(nil, p.srcConnID, []byte("mismatch"), 10),
	})
	tc.writeFrames(packetTypeInitial,
		debugFrameCrypto{
			data: tc.cryptoDataIn[tls.QUICEncryptionLevelInitial],
		})
	tc.writeFrames(packetTypeHandshake,
		debugFrameCrypto{
			data: tc.cryptoDataIn[tls.QUICEncryptionLevelHandshake],
		})
	tc.wantFrameType("conn ignores Version Negotiation and continues with handshake",
		packetTypeHandshake, debugFrameCrypto{})
}