Skip to content

Commit

Permalink
Merge pull request #3104 from lucas-clemente/clean-up-testutils
Browse files Browse the repository at this point in the history
clean up the testutils
  • Loading branch information
marten-seemann authored Apr 2, 2021
2 parents f60306c + 7c8f729 commit f8b847f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 40 deletions.
4 changes: 2 additions & 2 deletions integrationtests/self/mitm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ var _ = Describe("MITM test", func() {
// expects hdr from an Initial packet intercepted from client
sendForgedInitialPacketWithAck := func(conn net.PacketConn, remoteAddr net.Addr, hdr *wire.Header) {
// Fake Initial with ACK for packet 2 (unsent)
ackFrame := testutils.ComposeAckFrame(2, 2)
initialPacket := testutils.ComposeInitialPacket(hdr.DestConnectionID, hdr.SrcConnectionID, hdr.Version, hdr.DestConnectionID, []wire.Frame{ackFrame})
ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
initialPacket := testutils.ComposeInitialPacket(hdr.DestConnectionID, hdr.SrcConnectionID, hdr.Version, hdr.DestConnectionID, []wire.Frame{ack})
_, err := conn.WriteTo(initialPacket, remoteAddr)
Expect(err).ToNot(HaveOccurred())
}
Expand Down
35 changes: 0 additions & 35 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
// Utilities for simulating packet injection and man-in-the-middle (MITM) attacker tests.
// Do not use for non-testing purposes.

// CryptoFrameType uses same types as messageType in crypto_setup.go
type CryptoFrameType uint8

// writePacket returns a new raw packet with the specified header and payload
func writePacket(hdr *wire.ExtendedHeader, data []byte) []byte {
buf := &bytes.Buffer{}
Expand All @@ -30,38 +27,6 @@ func packRawPayload(version protocol.VersionNumber, frames []wire.Frame) []byte
return buf.Bytes()
}

// ComposeCryptoFrame returns a new empty crypto frame of the specified
// type padded to size bytes with zeroes
func ComposeCryptoFrame(cft CryptoFrameType, size int) *wire.CryptoFrame {
data := make([]byte, size)
data[0] = byte(cft)
return &wire.CryptoFrame{
Offset: 0,
Data: data,
}
}

// ComposeConnCloseFrame returns a new Connection Close frame with a generic error
func ComposeConnCloseFrame() *wire.ConnectionCloseFrame {
return &wire.ConnectionCloseFrame{
IsApplicationError: true,
ErrorCode: 0,
ReasonPhrase: "mitm attacker",
}
}

// ComposeAckFrame returns a new Ack Frame that acknowledges all packets between smallest and largest
func ComposeAckFrame(smallest protocol.PacketNumber, largest protocol.PacketNumber) *wire.AckFrame {
ackRange := wire.AckRange{
Smallest: smallest,
Largest: largest,
}
return &wire.AckFrame{
AckRanges: []wire.AckRange{ackRange},
DelayTime: 0,
}
}

// ComposeInitialPacket returns an Initial packet encrypted under key
// (the original destination connection ID) containing specified frames
func ComposeInitialPacket(srcConnID protocol.ConnectionID, destConnID protocol.ConnectionID, version protocol.VersionNumber, key protocol.ConnectionID, frames []wire.Frame) []byte {
Expand Down
9 changes: 6 additions & 3 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2956,16 +2956,19 @@ var _ = Describe("Client Session", func() {
// Illustrates that an injected Initial with an ACK frame for an unsent packet causes
// the connection to immediately break down
It("fails on Initial-level ACK for unsent packet", func() {
ackFrame := testutils.ComposeAckFrame(0, 0)
initialPacket := testutils.ComposeInitialPacket(destConnID, srcConnID, sess.version, destConnID, []wire.Frame{ackFrame})
ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
initialPacket := testutils.ComposeInitialPacket(destConnID, srcConnID, sess.version, destConnID, []wire.Frame{ack})
tracer.EXPECT().ReceivedPacket(gomock.Any(), gomock.Any(), gomock.Any())
Expect(sess.handlePacketImpl(wrapPacket(initialPacket))).To(BeFalse())
})

// Illustrates that an injected Initial with a CONNECTION_CLOSE frame causes
// the connection to immediately break down
It("fails on Initial-level CONNECTION_CLOSE frame", func() {
connCloseFrame := testutils.ComposeConnCloseFrame()
connCloseFrame := &wire.ConnectionCloseFrame{
IsApplicationError: true,
ReasonPhrase: "mitm attacker",
}
initialPacket := testutils.ComposeInitialPacket(destConnID, srcConnID, sess.version, destConnID, []wire.Frame{connCloseFrame})
tracer.EXPECT().ReceivedPacket(gomock.Any(), gomock.Any(), gomock.Any())
Expect(sess.handlePacketImpl(wrapPacket(initialPacket))).To(BeTrue())
Expand Down

0 comments on commit f8b847f

Please sign in to comment.