From 73c7266a2fc1425ae519898022959eb55a9411db Mon Sep 17 00:00:00 2001 From: PhatPhuckDave <> Date: Mon, 22 Jul 2024 22:28:59 +0200 Subject: [PATCH] Refactor the decode tests --- pdu/pdu.go | 3 ++- pdu/pdu_test.go | 53 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/pdu/pdu.go b/pdu/pdu.go index 4a0b437..b91f046 100644 --- a/pdu/pdu.go +++ b/pdu/pdu.go @@ -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 } diff --git a/pdu/pdu_test.go b/pdu/pdu_test.go index 8570f5d..1e13a53 100644 --- a/pdu/pdu_test.go +++ b/pdu/pdu_test.go @@ -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) }