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 { type packetPaddingContainer struct {
} }
func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, padding int) []byte { func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte {
data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, padding)...) data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, paddingLength)...)
dataLength := len(data_OWNERSHIP_RELINQUISHED) dataLength := len(data_OWNERSHIP_RELINQUISHED)
data = binary.BigEndian.AppendUint16(data, uint16(dataLength)) data = binary.BigEndian.AppendUint16(data, uint16(dataLength))
return data return data
} }
func (c packetPaddingContainer) Pad(padding int) []byte { func (c packetPaddingContainer) Pad(paddingLength int) []byte {
if assertPaddingLengthIsNotNegative := padding < 0; assertPaddingLengthIsNotNegative { if assertPaddingLengthIsNotNegative := paddingLength < 0; assertPaddingLengthIsNotNegative {
return nil return nil
} }
switch padding { switch paddingLength {
case 0: case 0:
return []byte{} return []byte{}
case 1: case 1:
@ -28,7 +28,7 @@ func (c packetPaddingContainer) Pad(padding int) []byte {
case 2: case 2:
return []byte{0, 0} return []byte{0, 0}
default: 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 // 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 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. // @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 // Unpack extracts the data and padding from the given padded data. It
// returns the data and the number of padding bytes. // returns the data and the number of padding bytes.
@ -30,5 +30,5 @@ type PacketPaddingContainer interface {
// Pad returns a padding packet of padding length. // Pad returns a padding packet of padding length.
// If the padding length is less than 0, nil is returned. // If the padding length is less than 0, nil is returned.
// @param padding - The number of padding bytes to add to the data. // @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() { Convey("When packing data with padding", func() {
data := []byte("testdata") data := []byte("testdata")
padding := 4 paddingLength := 4
packedData := container.Pack(data, padding) packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() { 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) So(len(packedData), ShouldEqual, expectedLength)
}) })
Convey("When unpacking the packed data", func() { 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() { Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data)) So(string(unpackedData), ShouldEqual, string(data))
}) })
Convey("The unpacked padding should match the original padding", func() { Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPadding, ShouldEqual, padding) So(unpackedPaddingLength, ShouldEqual, paddingLength)
}) })
}) })
}) })
Convey("When packing empty data with padding", func() { Convey("When packing empty data with padding", func() {
data := []byte("") data := []byte("")
padding := 4 paddingLength := 4
packedData := container.Pack(data, padding) packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() { 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) So(len(packedData), ShouldEqual, expectedLength)
}) })
Convey("When unpacking the packed data", func() { 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() { Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data)) So(string(unpackedData), ShouldEqual, string(data))
}) })
Convey("The unpacked padding should match the original padding", func() { Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPadding, ShouldEqual, padding) So(unpackedPaddingLength, ShouldEqual, paddingLength)
}) })
}) })
}) })
Convey("When packing data with zero padding", func() { Convey("When packing data with zero padding", func() {
data := []byte("testdata") data := []byte("testdata")
padding := 0 paddingLength := 0
packedData := container.Pack(data, padding) packedData := container.Pack(data, paddingLength)
Convey("The packed data should have the correct length", func() { 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) So(len(packedData), ShouldEqual, expectedLength)
}) })
Convey("When unpacking the packed data", func() { 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() { Convey("The unpacked data should match the original data", func() {
So(string(unpackedData), ShouldEqual, string(data)) So(string(unpackedData), ShouldEqual, string(data))
}) })
Convey("The unpacked padding should match the original padding", func() { Convey("The unpacked padding length should match the original padding length", func() {
So(unpackedPadding, ShouldEqual, padding) So(unpackedPaddingLength, ShouldEqual, paddingLength)
}) })
}) })
}) })