File: packet_header_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 (145 lines) | stat: -rw-r--r-- 3,736 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package qlog

import (
	"bytes"
	"encoding/json"
	"testing"

	"github.com/francoispqt/gojay"
	"github.com/quic-go/quic-go/internal/protocol"
	"github.com/quic-go/quic-go/internal/wire"
	"github.com/quic-go/quic-go/logging"
	"github.com/stretchr/testify/require"
)

func TestPacketTypeFromEncryptionLevel(t *testing.T) {
	tests := []struct {
		name  string
		level protocol.EncryptionLevel
		want  logging.PacketType
	}{
		{"Initial", protocol.EncryptionInitial, logging.PacketTypeInitial},
		{"Handshake", protocol.EncryptionHandshake, logging.PacketTypeHandshake},
		{"0-RTT", protocol.Encryption0RTT, logging.PacketType0RTT},
		{"1-RTT", protocol.Encryption1RTT, logging.PacketType1RTT},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := getPacketTypeFromEncryptionLevel(tt.level)
			require.Equal(t, tt.want, got)
		})
	}
}

func checkHeader(t *testing.T, hdr *wire.ExtendedHeader, expected map[string]interface{}) {
	buf := &bytes.Buffer{}
	enc := gojay.NewEncoder(buf)
	require.NoError(t, enc.Encode(transformLongHeader(hdr)))
	data := buf.Bytes()
	require.True(t, json.Valid(data))
	checkEncoding(t, data, expected)
}

func TestMarshalHeaderWithPayloadLength(t *testing.T) {
	checkHeader(t,
		&wire.ExtendedHeader{
			PacketNumber: 42,
			Header: wire.Header{
				Type:    protocol.PacketTypeInitial,
				Length:  123,
				Version: protocol.Version(0xdecafbad),
			},
		},
		map[string]interface{}{
			"packet_type":   "initial",
			"packet_number": 42,
			"dcil":          0,
			"scil":          0,
			"version":       "decafbad",
		},
	)
}

func TestMarshalInitialWithToken(t *testing.T) {
	checkHeader(t,
		&wire.ExtendedHeader{
			PacketNumber: 4242,
			Header: wire.Header{
				Type:    protocol.PacketTypeInitial,
				Length:  123,
				Version: protocol.Version(0xdecafbad),
				Token:   []byte{0xde, 0xad, 0xbe, 0xef},
			},
		},
		map[string]interface{}{
			"packet_type":   "initial",
			"packet_number": 4242,
			"dcil":          0,
			"scil":          0,
			"version":       "decafbad",
			"token":         map[string]interface{}{"data": "deadbeef"},
		},
	)
}

func TestMarshalRetryPacket(t *testing.T) {
	checkHeader(t,
		&wire.ExtendedHeader{
			Header: wire.Header{
				Type:            protocol.PacketTypeRetry,
				SrcConnectionID: protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44}),
				Version:         protocol.Version(0xdecafbad),
				Token:           []byte{0xde, 0xad, 0xbe, 0xef},
			},
		},
		map[string]interface{}{
			"packet_type": "retry",
			"dcil":        0,
			"scil":        4,
			"scid":        "11223344",
			"token":       map[string]interface{}{"data": "deadbeef"},
			"version":     "decafbad",
		},
	)
}

func TestMarshalPacketWithPacketNumber0(t *testing.T) {
	checkHeader(t,
		&wire.ExtendedHeader{
			PacketNumber: 0,
			Header: wire.Header{
				Type:    protocol.PacketTypeHandshake,
				Version: protocol.Version(0xdecafbad),
			},
		},
		map[string]interface{}{
			"packet_type":   "handshake",
			"packet_number": 0,
			"dcil":          0,
			"scil":          0,
			"version":       "decafbad",
		},
	)
}

func TestMarshalHeaderWithSourceConnectionID(t *testing.T) {
	checkHeader(t,
		&wire.ExtendedHeader{
			PacketNumber: 42,
			Header: wire.Header{
				Type:            protocol.PacketTypeHandshake,
				SrcConnectionID: protocol.ParseConnectionID([]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}),
				Version:         protocol.Version(0xdecafbad),
			},
		},
		map[string]interface{}{
			"packet_type":   "handshake",
			"packet_number": 42,
			"dcil":          0,
			"scil":          16,
			"scid":          "00112233445566778899aabbccddeeff",
			"version":       "decafbad",
		},
	)
}