Refactor the decode tests

This commit is contained in:
PhatPhuckDave
2024-07-22 22:28:59 +02:00
parent 4a819f1563
commit 73c7266a2f
2 changed files with 46 additions and 10 deletions

View File

@@ -47,7 +47,7 @@ func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error {
binary.BigEndian.PutUint32(bufVal[12:16], p.sequence_number)
return nil
}
func (p *PDU_HEADER) Decode(data []uint8) {
func (p *PDU_HEADER) Decode(data []uint8) error {
if len(data) >= 4 {
p.command_length = binary.BigEndian.Uint32(data[0:4])
}
@@ -61,4 +61,5 @@ func (p *PDU_HEADER) Decode(data []uint8) {
if len(data) >= 16 {
p.sequence_number = binary.BigEndian.Uint32(data[12:16])
}
return nil
}

View File

@@ -185,7 +185,7 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
}
buf := make([]uint8, 16)
err := p.EncodeInto(&buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
@@ -301,13 +301,21 @@ func TestDecodeHandlesShortByteSlice(t *testing.T) {
t.Errorf("Decode panicked with short byte slice")
}
}()
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDecodeParsesValidByteSlice(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 16 {
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
}
@@ -330,12 +338,20 @@ func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
t.Errorf("Decode panicked with long byte slice")
}
}()
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestDecodeHandlesNilDataInput(t *testing.T) {
var p PDU_HEADER
p.Decode(nil)
err := p.Decode(nil)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}
@@ -353,7 +369,12 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
var p PDU_HEADER
data := []uint8{}
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}
@@ -374,7 +395,11 @@ func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
originalData := make([]uint8, len(data))
copy(originalData, data)
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
for i := range data {
if data[i] != originalData[i] {
@@ -386,7 +411,12 @@ func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
var p PDU_HEADER
data := []uint8{255, 255, 255, 255, 255, 255, 255, 255}
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != math.MaxUint32 {
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
}
@@ -398,7 +428,12 @@ func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 0, 0, 0, 0, 0}
p.Decode(data)
err := p.Decode(data)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}