File: retransmission_queue_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (131 lines) | stat: -rw-r--r-- 4,655 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package quic

import (
	"testing"

	"github.com/quic-go/quic-go/internal/protocol"
	"github.com/quic-go/quic-go/internal/wire"

	"github.com/stretchr/testify/require"
)

func TestRetransmissionQueueFrames(t *testing.T) {
	t.Run("Initial", func(t *testing.T) {
		testRetransmissionQueueFrames(t, protocol.EncryptionInitial)
	})
	t.Run("Handshake", func(t *testing.T) {
		testRetransmissionQueueFrames(t, protocol.EncryptionHandshake)
	})
	t.Run("1-RTT", func(t *testing.T) {
		testRetransmissionQueueFrames(t, protocol.Encryption1RTT)
	})
}

func testRetransmissionQueueFrames(t *testing.T, encLevel protocol.EncryptionLevel) {
	q := newRetransmissionQueue()

	require.False(t, q.HasData(encLevel))
	require.Nil(t, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))

	ah := q.AckHandler(encLevel)
	require.NotNil(t, ah)
	ah.OnLost(&wire.PingFrame{})
	require.True(t, q.HasData(encLevel))
	require.Equal(t, &wire.PingFrame{}, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))
	require.False(t, q.HasData(encLevel))
	require.Nil(t, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))

	f := &wire.PathChallengeFrame{Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}}
	ah.OnLost(f)
	require.True(t, q.HasData(encLevel))
	require.Nil(t, q.GetFrame(encLevel, f.Length(protocol.Version1)-1, protocol.Version1))
	require.Equal(t, f, q.GetFrame(encLevel, f.Length(protocol.Version1), protocol.Version1))
	require.False(t, q.HasData(encLevel))

	if encLevel == protocol.Encryption1RTT {
		require.Panics(t, func() { ah.OnLost(&wire.StreamFrame{}) })
	}
}

func TestRetransmissionQueueCryptoFrames(t *testing.T) {
	t.Run("Initial", func(t *testing.T) {
		testRetransmissionQueueCryptoFrames(t, protocol.EncryptionInitial)
	})
	t.Run("Handshake", func(t *testing.T) {
		testRetransmissionQueueCryptoFrames(t, protocol.EncryptionHandshake)
	})
	t.Run("1-RTT", func(t *testing.T) {
		testRetransmissionQueueCryptoFrames(t, protocol.Encryption1RTT)
	})
}

func testRetransmissionQueueCryptoFrames(t *testing.T, encLevel protocol.EncryptionLevel) {
	q := newRetransmissionQueue()

	var otherEncLevel protocol.EncryptionLevel
	switch encLevel {
	case protocol.EncryptionInitial:
		otherEncLevel = protocol.EncryptionHandshake
	case protocol.EncryptionHandshake:
		otherEncLevel = protocol.Encryption1RTT
	case protocol.Encryption1RTT:
		otherEncLevel = protocol.EncryptionInitial
	}

	ah := q.AckHandler(encLevel)
	require.NotNil(t, ah)
	ah.OnLost(&wire.CryptoFrame{Data: []byte("foobar")})
	require.True(t, q.HasData(encLevel))
	require.False(t, q.HasData(otherEncLevel))
	require.Equal(t, &wire.CryptoFrame{Data: []byte("foobar")}, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))
	require.False(t, q.HasData(encLevel))
	require.Nil(t, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))

	f := &wire.CryptoFrame{Offset: 100, Data: []byte("foobar")}
	ah.OnLost(f)
	ah.OnLost(&wire.PingFrame{})
	require.True(t, q.HasData(encLevel))
	require.False(t, q.HasData(otherEncLevel))
	// the CRYPTO frame wouldn't fit, not even if it was split
	require.IsType(t, &wire.PingFrame{}, q.GetFrame(encLevel, 2, protocol.Version1))

	f1 := q.GetFrame(encLevel, f.Length(protocol.Version1)-3, protocol.Version1)
	require.NotNil(t, f1)
	require.IsType(t, &wire.CryptoFrame{}, f1)
	require.Equal(t, &wire.CryptoFrame{Offset: 100, Data: []byte("foo")}, f1)
	f2 := q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1)
	require.NotNil(t, f2)
	require.IsType(t, &wire.CryptoFrame{}, f2)
	require.Equal(t, &wire.CryptoFrame{Offset: 103, Data: []byte("bar")}, f2)
}

func TestRetransmissionQueueDropEncLevel(t *testing.T) {
	q := newRetransmissionQueue()
	require.Panics(t, func() { q.DropPackets(protocol.Encryption0RTT) })
	require.Panics(t, func() { q.DropPackets(protocol.Encryption1RTT) })

	t.Run("Initial", func(t *testing.T) {
		testRetransmissionQueueDropEncLevel(t, protocol.EncryptionInitial)
	})
	t.Run("Handshake", func(t *testing.T) {
		testRetransmissionQueueDropEncLevel(t, protocol.EncryptionHandshake)
	})
}

func testRetransmissionQueueDropEncLevel(t *testing.T, encLevel protocol.EncryptionLevel) {
	q := newRetransmissionQueue()

	ah := q.AckHandler(encLevel)
	require.NotNil(t, ah)
	ah.OnLost(&wire.PingFrame{})
	ah.OnLost(&wire.CryptoFrame{Data: []byte("foobar")})
	require.True(t, q.HasData(encLevel))
	q.DropPackets(encLevel)
	require.False(t, q.HasData(encLevel))
	require.Nil(t, q.GetFrame(encLevel, protocol.MaxByteCount, protocol.Version1))

	// losing more frame is a no-op
	ah.OnLost(&wire.CryptoFrame{Data: []byte("foobar")})
	ah.OnLost(&wire.PingFrame{})
	require.False(t, q.HasData(encLevel))
}