mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-03 04:07:35 +03:00
stop using the ExtendedHeader for writing short header packets in tests
This commit is contained in:
parent
384ad9b2ae
commit
e89fc1152b
9 changed files with 121 additions and 289 deletions
|
@ -27,7 +27,8 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
payload = []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
|
||||
)
|
||||
|
||||
getHeader := func(extHdr *wire.ExtendedHeader) (*wire.Header, []byte) {
|
||||
getLongHeader := func(extHdr *wire.ExtendedHeader) (*wire.Header, []byte) {
|
||||
ExpectWithOffset(1, extHdr.IsLongHeader).To(BeTrue())
|
||||
buf := &bytes.Buffer{}
|
||||
ExpectWithOffset(1, extHdr.Write(buf, version)).To(Succeed())
|
||||
hdrLen := buf.Len()
|
||||
|
@ -39,6 +40,12 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
return hdr, buf.Bytes()[:hdrLen]
|
||||
}
|
||||
|
||||
getShortHeader := func(connID protocol.ConnectionID, pn protocol.PacketNumber, pnLen protocol.PacketNumberLen, kp protocol.KeyPhaseBit) []byte {
|
||||
buf := &bytes.Buffer{}
|
||||
Expect(wire.WriteShortHeader(buf, connID, pn, pnLen, kp)).To(Succeed())
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
cs = mocks.NewMockCryptoSetup(mockCtrl)
|
||||
unpacker = newPacketUnpacker(cs, 4, version).(*packetUnpacker)
|
||||
|
@ -55,7 +62,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 1337,
|
||||
PacketNumberLen: protocol.PacketNumberLen2,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
data := append(hdrRaw, make([]byte, 2 /* fill up packet number */ +15 /* need 16 bytes */)...)
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
cs.EXPECT().GetHandshakeOpener().Return(opener, nil)
|
||||
|
@ -67,12 +74,9 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("errors when the packet is too small to obtain the header decryption sample, for short headers", func() {
|
||||
_, hdrRaw := getHeader(&wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
PacketNumber: 1337,
|
||||
PacketNumberLen: protocol.PacketNumberLen2,
|
||||
})
|
||||
data := append(hdrRaw, make([]byte, 2 /* fill up packet number */ +15 /* need 16 bytes */)...)
|
||||
buf := &bytes.Buffer{}
|
||||
Expect(wire.WriteShortHeader(buf, connID, 1337, protocol.PacketNumberLen2, protocol.KeyPhaseOne)).To(Succeed())
|
||||
data := append(buf.Bytes(), make([]byte, 2 /* fill up packet number */ +15 /* need 16 bytes */)...)
|
||||
opener := mocks.NewMockShortHeaderOpener(mockCtrl)
|
||||
cs.EXPECT().Get1RTTOpener().Return(opener, nil)
|
||||
_, _, _, _, err := unpacker.UnpackShortHeader(time.Now(), data)
|
||||
|
@ -92,7 +96,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 2,
|
||||
PacketNumberLen: 3,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
gomock.InOrder(
|
||||
cs.EXPECT().GetInitialOpener().Return(opener, nil),
|
||||
|
@ -118,7 +122,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 20,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
gomock.InOrder(
|
||||
cs.EXPECT().Get0RTTOpener().Return(opener, nil),
|
||||
|
@ -133,13 +137,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("opens short header packets", func() {
|
||||
extHdr := &wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
KeyPhase: protocol.KeyPhaseOne,
|
||||
PacketNumber: 99,
|
||||
PacketNumberLen: protocol.PacketNumberLen4,
|
||||
}
|
||||
_, hdrRaw := getHeader(extHdr)
|
||||
hdrRaw := getShortHeader(connID, 99, protocol.PacketNumberLen4, protocol.KeyPhaseOne)
|
||||
opener := mocks.NewMockShortHeaderOpener(mockCtrl)
|
||||
now := time.Now()
|
||||
gomock.InOrder(
|
||||
|
@ -157,12 +155,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("returns the error when getting the opener fails", func() {
|
||||
extHdr := &wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
_, hdrRaw := getHeader(extHdr)
|
||||
hdrRaw := getShortHeader(connID, 0x1337, protocol.PacketNumberLen2, protocol.KeyPhaseOne)
|
||||
cs.EXPECT().Get1RTTOpener().Return(nil, handshake.ErrKeysNotYetAvailable)
|
||||
_, _, _, _, err := unpacker.UnpackShortHeader(time.Now(), append(hdrRaw, payload...))
|
||||
Expect(err).To(MatchError(handshake.ErrKeysNotYetAvailable))
|
||||
|
@ -179,7 +172,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
KeyPhase: protocol.KeyPhaseOne,
|
||||
PacketNumberLen: protocol.PacketNumberLen4,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
gomock.InOrder(
|
||||
cs.EXPECT().GetHandshakeOpener().Return(opener, nil),
|
||||
|
@ -195,12 +188,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("errors on empty packets, for short header packets", func() {
|
||||
extHdr := &wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
KeyPhase: protocol.KeyPhaseOne,
|
||||
PacketNumberLen: protocol.PacketNumberLen4,
|
||||
}
|
||||
_, hdrRaw := getHeader(extHdr)
|
||||
hdrRaw := getShortHeader(connID, 0x42, protocol.PacketNumberLen4, protocol.KeyPhaseOne)
|
||||
opener := mocks.NewMockShortHeaderOpener(mockCtrl)
|
||||
now := time.Now()
|
||||
gomock.InOrder(
|
||||
|
@ -228,7 +216,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 2,
|
||||
PacketNumberLen: 3,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
cs.EXPECT().GetHandshakeOpener().Return(opener, nil)
|
||||
opener.EXPECT().DecryptHeader(gomock.Any(), gomock.Any(), gomock.Any())
|
||||
|
@ -250,7 +238,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
hdrRaw[0] |= 0xc
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
opener.EXPECT().DecryptHeader(gomock.Any(), gomock.Any(), gomock.Any())
|
||||
|
@ -262,12 +250,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("defends against the timing side-channel when the reserved bits are wrong, for short header packets", func() {
|
||||
extHdr := &wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
_, hdrRaw := getHeader(extHdr)
|
||||
hdrRaw := getShortHeader(connID, 0x1337, protocol.PacketNumberLen2, protocol.KeyPhaseZero)
|
||||
hdrRaw[0] |= 0x18
|
||||
opener := mocks.NewMockShortHeaderOpener(mockCtrl)
|
||||
opener.EXPECT().DecryptHeader(gomock.Any(), gomock.Any(), gomock.Any())
|
||||
|
@ -289,7 +272,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
hdrRaw[0] |= 0x18
|
||||
opener := mocks.NewMockLongHeaderOpener(mockCtrl)
|
||||
opener.EXPECT().DecryptHeader(gomock.Any(), gomock.Any(), gomock.Any())
|
||||
|
@ -301,12 +284,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
})
|
||||
|
||||
It("returns the decryption error, when unpacking a packet with wrong reserved bits fails, for short headers", func() {
|
||||
extHdr := &wire.ExtendedHeader{
|
||||
Header: wire.Header{DestConnectionID: connID},
|
||||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
_, hdrRaw := getHeader(extHdr)
|
||||
hdrRaw := getShortHeader(connID, 0x1337, protocol.PacketNumberLen2, protocol.KeyPhaseZero)
|
||||
hdrRaw[0] |= 0x18
|
||||
opener := mocks.NewMockShortHeaderOpener(mockCtrl)
|
||||
opener.EXPECT().DecryptHeader(gomock.Any(), gomock.Any(), gomock.Any())
|
||||
|
@ -329,7 +307,7 @@ var _ = Describe("Packet Unpacker", func() {
|
|||
PacketNumber: 0x1337,
|
||||
PacketNumberLen: 2,
|
||||
}
|
||||
hdr, hdrRaw := getHeader(extHdr)
|
||||
hdr, hdrRaw := getLongHeader(extHdr)
|
||||
origHdrRaw := append([]byte{}, hdrRaw...) // save a copy of the header
|
||||
firstHdrByte := hdrRaw[0]
|
||||
hdrRaw[0] ^= 0xff // invert the first byte
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue