-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
short_header_test.go
96 lines (84 loc) · 2.42 KB
/
short_header_test.go
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
package wire
import (
"bytes"
"io"
"log"
"os"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Short Header", func() {
Context("Parsing", func() {
It("parses", func() {
data := []byte{
0b01000110,
0xde, 0xad, 0xbe, 0xef,
0x13, 0x37, 0x99,
}
l, pn, pnLen, kp, err := ParseShortHeader(data, 4)
Expect(err).ToNot(HaveOccurred())
Expect(l).To(Equal(len(data)))
Expect(kp).To(Equal(protocol.KeyPhaseOne))
Expect(pn).To(Equal(protocol.PacketNumber(0x133799)))
Expect(pnLen).To(Equal(protocol.PacketNumberLen3))
})
It("errors when the QUIC bit is not set", func() {
data := []byte{
0b00000101,
0xde, 0xad, 0xbe, 0xef,
0x13, 0x37,
}
_, _, _, _, err := ParseShortHeader(data, 4)
Expect(err).To(MatchError("not a QUIC packet"))
})
It("errors, but returns the header, when the reserved bits are set", func() {
data := []byte{
0b01010101,
0xde, 0xad, 0xbe, 0xef,
0x13, 0x37,
}
_, pn, _, _, err := ParseShortHeader(data, 4)
Expect(err).To(MatchError(ErrInvalidReservedBits))
Expect(pn).To(Equal(protocol.PacketNumber(0x1337)))
})
It("errors when passed a long header packet", func() {
_, _, _, _, err := ParseShortHeader([]byte{0x80}, 4)
Expect(err).To(MatchError("not a short header packet"))
})
It("errors on EOF", func() {
data := []byte{
0b01000110,
0xde, 0xad, 0xbe, 0xef,
0x13, 0x37, 0x99,
}
_, _, _, _, err := ParseShortHeader(data, 4)
Expect(err).ToNot(HaveOccurred())
for i := range data {
_, _, _, _, err := ParseShortHeader(data[:i], 4)
Expect(err).To(MatchError(io.EOF))
}
})
})
Context("logging", func() {
var (
buf *bytes.Buffer
logger utils.Logger
)
BeforeEach(func() {
buf = &bytes.Buffer{}
logger = utils.DefaultLogger
logger.SetLogLevel(utils.LogLevelDebug)
log.SetOutput(buf)
})
AfterEach(func() {
log.SetOutput(os.Stdout)
})
It("logs Short Headers containing a connection ID", func() {
connID := protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37})
LogShortHeader(logger, connID, 1337, protocol.PacketNumberLen4, protocol.KeyPhaseOne)
Expect(buf.String()).To(ContainSubstring("Short Header{DestConnectionID: deadbeefcafe1337, PacketNumber: 1337, PacketNumberLen: 4, KeyPhase: 1}"))
})
})
})