use an array instead of a byte slice for Connection IDs

This commit is contained in:
Marten Seemann 2022-08-28 16:05:56 +03:00
parent 9e0f9e62ff
commit 1aced95d41
47 changed files with 530 additions and 487 deletions

View file

@ -93,8 +93,8 @@ var _ = Describe("Packet Handler Map", func() {
conn1.EXPECT().destroy(testErr)
conn2 := NewMockPacketHandler(mockCtrl)
conn2.EXPECT().destroy(testErr)
handler.Add(protocol.ConnectionID{1, 1, 1, 1}, conn1)
handler.Add(protocol.ConnectionID{2, 2, 2, 2}, conn2)
handler.Add(protocol.ParseConnectionID([]byte{1, 1, 1, 1}), conn1)
handler.Add(protocol.ParseConnectionID([]byte{2, 2, 2, 2}), conn2)
mockMultiplexer.EXPECT().RemoveConn(gomock.Any())
handler.close(testErr)
close(packetChan)
@ -123,8 +123,8 @@ var _ = Describe("Packet Handler Map", func() {
})
It("handles packets for different packet handlers on the same packet conn", func() {
connID1 := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID2 := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
connID1 := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
connID2 := protocol.ParseConnectionID([]byte{8, 7, 6, 5, 4, 3, 2, 1})
packetHandler1 := NewMockPacketHandler(mockCtrl)
packetHandler2 := NewMockPacketHandler(mockCtrl)
handledPacket1 := make(chan struct{})
@ -162,7 +162,7 @@ var _ = Describe("Packet Handler Map", func() {
It("deletes removed connections immediately", func() {
handler.deleteRetiredConnsAfter = time.Hour
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
handler.Add(connID, NewMockPacketHandler(mockCtrl))
handler.Remove(connID)
handler.handlePacket(&receivedPacket{data: getPacket(connID)})
@ -171,7 +171,7 @@ var _ = Describe("Packet Handler Map", func() {
It("deletes retired connection entries after a wait time", func() {
handler.deleteRetiredConnsAfter = scaleDuration(10 * time.Millisecond)
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
conn := NewMockPacketHandler(mockCtrl)
handler.Add(connID, conn)
handler.Retire(connID)
@ -182,7 +182,7 @@ var _ = Describe("Packet Handler Map", func() {
It("passes packets arriving late for closed connections to that connection", func() {
handler.deleteRetiredConnsAfter = time.Hour
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
packetHandler := NewMockPacketHandler(mockCtrl)
handled := make(chan struct{})
packetHandler.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
@ -195,7 +195,7 @@ var _ = Describe("Packet Handler Map", func() {
})
It("drops packets for unknown receivers", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
handler.handlePacket(&receivedPacket{data: getPacket(connID)})
})
@ -206,14 +206,14 @@ var _ = Describe("Packet Handler Map", func() {
Expect(e).To(HaveOccurred())
close(done)
})
handler.Add(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, packetHandler)
handler.Add(protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}), packetHandler)
packetChan <- packetToRead{err: errors.New("read failed")}
Eventually(done).Should(BeClosed())
})
It("continues listening for temporary errors", func() {
packetHandler := NewMockPacketHandler(mockCtrl)
handler.Add(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, packetHandler)
handler.Add(protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}), packetHandler)
err := deadlineError{}
Expect(err.Temporary()).To(BeTrue())
packetChan <- packetToRead{err: err}
@ -222,15 +222,15 @@ var _ = Describe("Packet Handler Map", func() {
})
It("says if a connection ID is already taken", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
Expect(handler.Add(connID, NewMockPacketHandler(mockCtrl))).To(BeTrue())
Expect(handler.Add(connID, NewMockPacketHandler(mockCtrl))).To(BeFalse())
})
It("says if a connection ID is already taken, for AddWithConnID", func() {
clientDestConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
newConnID1 := protocol.ConnectionID{1, 2, 3, 4}
newConnID2 := protocol.ConnectionID{4, 3, 2, 1}
clientDestConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8})
newConnID1 := protocol.ParseConnectionID([]byte{1, 2, 3, 4})
newConnID2 := protocol.ParseConnectionID([]byte{4, 3, 2, 1})
Expect(handler.AddWithConnID(clientDestConnID, newConnID1, func() packetHandler { return NewMockPacketHandler(mockCtrl) })).To(BeTrue())
Expect(handler.AddWithConnID(clientDestConnID, newConnID2, func() packetHandler { return NewMockPacketHandler(mockCtrl) })).To(BeFalse())
})
@ -238,7 +238,7 @@ var _ = Describe("Packet Handler Map", func() {
Context("running a server", func() {
It("adds a server", func() {
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
connID := protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
p := getPacket(connID)
server := NewMockUnknownPacketHandler(mockCtrl)
server.EXPECT().handlePacket(gomock.Any()).Do(func(p *receivedPacket) {
@ -258,13 +258,13 @@ var _ = Describe("Packet Handler Map", func() {
serverConn.EXPECT().getPerspective().Return(protocol.PerspectiveServer)
serverConn.EXPECT().shutdown()
handler.Add(protocol.ConnectionID{1, 1, 1, 1}, clientConn)
handler.Add(protocol.ConnectionID{2, 2, 2, 2}, serverConn)
handler.Add(protocol.ParseConnectionID([]byte{1, 1, 1, 1}), clientConn)
handler.Add(protocol.ParseConnectionID([]byte{2, 2, 2, 2}), serverConn)
handler.CloseServer()
})
It("stops handling packets with unknown connection IDs after the server is closed", func() {
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
connID := protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
p := getPacket(connID)
server := NewMockUnknownPacketHandler(mockCtrl)
// don't EXPECT any calls to server.handlePacket
@ -286,7 +286,7 @@ var _ = Describe("Packet Handler Map", func() {
server := NewMockUnknownPacketHandler(mockCtrl)
// don't EXPECT any calls to server.handlePacket
handler.SetServer(server)
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
connID := protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
p1 := &receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 1)}
p2 := &receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 2)}
p3 := &receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 3)}
@ -300,14 +300,14 @@ var _ = Describe("Packet Handler Map", func() {
conn.EXPECT().handlePacket(p2),
conn.EXPECT().handlePacket(p3).Do(func(packet *receivedPacket) { close(done) }),
)
handler.AddWithConnID(connID, protocol.ConnectionID{1, 2, 3, 4}, func() packetHandler { return conn })
handler.AddWithConnID(connID, protocol.ParseConnectionID([]byte{1, 2, 3, 4}), func() packetHandler { return conn })
Eventually(done).Should(BeClosed())
})
It("directs 0-RTT packets to existing connections", func() {
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
connID := protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
conn := NewMockPacketHandler(mockCtrl)
handler.AddWithConnID(connID, protocol.ConnectionID{1, 2, 3, 4}, func() packetHandler { return conn })
handler.AddWithConnID(connID, protocol.ParseConnectionID([]byte{1, 2, 3, 4}), func() packetHandler { return conn })
p1 := &receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 1)}
conn.EXPECT().handlePacket(p1)
handler.handlePacket(p1)
@ -315,17 +315,21 @@ var _ = Describe("Packet Handler Map", func() {
It("limits the number of 0-RTT queues", func() {
for i := 0; i < protocol.Max0RTTQueues; i++ {
connID := make(protocol.ConnectionID, 8)
rand.Read(connID)
p := &receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 1)}
b := make([]byte, 8)
rand.Read(b)
p := &receivedPacket{data: getPacketWithPacketType(
protocol.ParseConnectionID(b),
protocol.PacketType0RTT,
1,
)}
handler.handlePacket(p)
}
// We're already storing the maximum number of queues. This packet will be dropped.
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9}
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9})
handler.handlePacket(&receivedPacket{data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 1)})
// Don't EXPECT any handlePacket() calls.
conn := NewMockPacketHandler(mockCtrl)
handler.AddWithConnID(connID, protocol.ConnectionID{1, 2, 3, 4}, func() packetHandler { return conn })
handler.AddWithConnID(connID, protocol.ParseConnectionID([]byte{1, 2, 3, 4}), func() packetHandler { return conn })
time.Sleep(20 * time.Millisecond)
})
@ -336,7 +340,7 @@ var _ = Describe("Packet Handler Map", func() {
server := NewMockUnknownPacketHandler(mockCtrl)
// don't EXPECT any calls to server.handlePacket
handler.SetServer(server)
connID := protocol.ConnectionID{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
connID := protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
p1 := &receivedPacket{
data: getPacketWithPacketType(connID, protocol.PacketType0RTT, 1),
buffer: getPacketBuffer(),
@ -351,7 +355,7 @@ var _ = Describe("Packet Handler Map", func() {
time.Sleep(queueDuration * 3)
// Don't EXPECT any handlePacket() calls.
conn := NewMockPacketHandler(mockCtrl)
handler.AddWithConnID(connID, protocol.ConnectionID{1, 2, 3, 4}, func() packetHandler { return conn })
handler.AddWithConnID(connID, protocol.ParseConnectionID([]byte{1, 2, 3, 4}), func() packetHandler { return conn })
time.Sleep(20 * time.Millisecond)
})
})
@ -404,7 +408,7 @@ var _ = Describe("Packet Handler Map", func() {
})
It("removes reset tokens", func() {
connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0x42}
connID := protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef, 0x42})
packetHandler := NewMockPacketHandler(mockCtrl)
handler.Add(connID, packetHandler)
token := protocol.StatelessResetToken{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
@ -442,8 +446,8 @@ var _ = Describe("Packet Handler Map", func() {
})
It("generates stateless reset tokens", func() {
connID1 := []byte{0xde, 0xad, 0xbe, 0xef}
connID2 := []byte{0xde, 0xca, 0xfb, 0xad}
connID1 := protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef})
connID2 := protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad})
Expect(handler.GetStatelessResetToken(connID1)).ToNot(Equal(handler.GetStatelessResetToken(connID2)))
})