Add packet padding container for packet length shaping: refactor arg names

This commit is contained in:
Shelikhoo 2025-01-28 14:19:20 +00:00
parent 1689279e95
commit fbcb9bc863
No known key found for this signature in database
GPG key ID: 4C9764E9FE80A3DC
3 changed files with 26 additions and 26 deletions

View file

@ -9,18 +9,18 @@ func New() PacketPaddingContainer {
type packetPaddingContainer struct {
}
func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, padding int) []byte {
data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, padding)...)
func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte {
data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, paddingLength)...)
dataLength := len(data_OWNERSHIP_RELINQUISHED)
data = binary.BigEndian.AppendUint16(data, uint16(dataLength))
return data
}
func (c packetPaddingContainer) Pad(padding int) []byte {
if assertPaddingLengthIsNotNegative := padding < 0; assertPaddingLengthIsNotNegative {
func (c packetPaddingContainer) Pad(paddingLength int) []byte {
if assertPaddingLengthIsNotNegative := paddingLength < 0; assertPaddingLengthIsNotNegative {
return nil
}
switch padding {
switch paddingLength {
case 0:
return []byte{}
case 1:
@ -28,7 +28,7 @@ func (c packetPaddingContainer) Pad(padding int) []byte {
case 2:
return []byte{0, 0}
default:
return append(make([]byte, padding-2), byte(padding>>8), byte(padding))
return append(make([]byte, paddingLength-2), byte(paddingLength>>8), byte(paddingLength))
}
}

View file

@ -19,7 +19,7 @@ type PacketPaddingContainer interface {
// len(data_OWNERSHIP_RELINQUISHED) + padding + 2
// @param data_OWNERSHIP_RELINQUISHED - The payload, this reference is consumed and should not be used after this call.
// @param padding - The number of padding bytes to add to the data.
Pack(data_OWNERSHIP_RELINQUISHED []byte, padding int) []byte
Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte
// Unpack extracts the data and padding from the given padded data. It
// returns the data and the number of padding bytes.
@ -30,5 +30,5 @@ type PacketPaddingContainer interface {
// Pad returns a padding packet of padding length.
// If the padding length is less than 0, nil is returned.
// @param padding - The number of padding bytes to add to the data.
Pad(padding int) []byte
Pad(paddingLength int) []byte
}

View file

@ -14,69 +14,69 @@ func TestPacketPaddingContainer(t *testing.T) {
Convey("When packing data with padding", func() {
data := []byte("testdata")
padding := 4
packedData := container.Pack(data, padding)
paddingLength := 4
packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() {
expectedLength := len(data) + padding + 2
expectedLength := len(data) + paddingLength + 2
So(len(packedData), ShouldEqual, expectedLength)
})
Convey("When unpacking the packed data", func() {
unpackedData, unpackedPadding := container.Unpack(packedData)
unpackedData, unpackedPaddingLength := container.Unpack(packedData)
Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data))
})
Convey("The unpacked padding should match the original padding", func() {
So(unpackedPadding, ShouldEqual, padding)
Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPaddingLength, ShouldEqual, paddingLength)
})
})
})
Convey("When packing empty data with padding", func() {
data := []byte("")
padding := 4
packedData := container.Pack(data, padding)
paddingLength := 4
packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() {
expectedLength := len(data) + padding + 2
expectedLength := len(data) + paddingLength + 2
So(len(packedData), ShouldEqual, expectedLength)
})
Convey("When unpacking the packed data", func() {
unpackedData, unpackedPadding := container.Unpack(packedData)
unpackedData, unpackedPaddingLength := container.Unpack(packedData)
Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data))
})
Convey("The unpacked padding should match the original padding", func() {
So(unpackedPadding, ShouldEqual, padding)
Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPaddingLength, ShouldEqual, paddingLength)
})
})
})
Convey("When packing data with zero padding", func() {
data := []byte("testdata")
padding := 0
packedData := container.Pack(data, padding)
paddingLength := 0
packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() {
expectedLength := len(data) + padding + 2
expectedLength := len(data) + paddingLength + 2
So(len(packedData), ShouldEqual, expectedLength)
})
Convey("When unpacking the packed data", func() {
unpackedData, unpackedPadding := container.Unpack(packedData)
unpackedData, unpackedPaddingLength := container.Unpack(packedData)
Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data))
})
Convey("The unpacked padding should match the original padding", func() {
So(unpackedPadding, ShouldEqual, padding)
Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPaddingLength, ShouldEqual, paddingLength)
})
})
})