From 500cb11235b9a8c6bb3c33d24ec2164de9c145e5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 24 Jul 2024 19:12:05 +0200 Subject: [PATCH] Fix the rest of pdu tests --- pdu/pdu.go | 4 + pdu/pdu_test.go | 275 ++++++++++++++++++++++++------------------------ 2 files changed, 141 insertions(+), 138 deletions(-) diff --git a/pdu/pdu.go b/pdu/pdu.go index 2a34f80..aec003b 100644 --- a/pdu/pdu.go +++ b/pdu/pdu.go @@ -3,6 +3,7 @@ package pdu import ( "bytes" "encoding/binary" + "fmt" ) type ( @@ -64,6 +65,9 @@ func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error { return nil } func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error { + if buf == nil { + return fmt.Errorf("cannot decode nil buffer") + } binary.Read(buf, binary.BigEndian, &p.command_length) binary.Read(buf, binary.BigEndian, &p.command_id) binary.Read(buf, binary.BigEndian, &p.command_status) diff --git a/pdu/pdu_test.go b/pdu/pdu_test.go index a66bca9..0ed90bb 100644 --- a/pdu/pdu_test.go +++ b/pdu/pdu_test.go @@ -327,166 +327,165 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) { // } // } -// // region decode -// func TestDecodeHandlesShortByteSlice(t *testing.T) { -// var p PDU_HEADER -// data := []byte{0, 0, 0, 10} -// defer func() { -// if r := recover(); r != nil { -// t.Errorf("Decode panicked with short byte slice") -// } -// }() -// err := p.Decode(data) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } -// } +// region decode +func TestDecodeHandlesShortByteSlice(t *testing.T) { + var p PDU_HEADER + data := []byte{0, 0, 0, 10} + defer func() { + if r := recover(); r != nil { + t.Errorf("Decode panicked with short byte slice") + } + }() + err := p.Decode(bytes.NewBuffer(data)) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} -// func TestDecodeParsesValidByteSlice(t *testing.T) { -// var p PDU_HEADER -// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} -// err := p.Decode(data) +func TestDecodeParsesValidByteSlice(t *testing.T) { + var p PDU_HEADER + data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} + err := p.Decode(bytes.NewBuffer(data)) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + 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) -// } -// if p.command_id != 1 { -// t.Errorf("Expected command_id to be 1, got %d", p.command_id) -// } -// if p.command_status != 2 { -// t.Errorf("Expected command_status to be 2, got %d", p.command_status) -// } -// if p.sequence_number != 3 { -// t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number) -// } -// } + if p.command_length != 16 { + t.Errorf("Expected command_length to be 16, got %d", p.command_length) + } + if p.command_id != 1 { + t.Errorf("Expected command_id to be 1, got %d", p.command_id) + } + if p.command_status != 2 { + t.Errorf("Expected command_status to be 2, got %d", p.command_status) + } + if p.sequence_number != 3 { + t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number) + } +} -// func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) { -// var p PDU_HEADER -// data := make([]byte, 20) -// defer func() { -// if r := recover(); r != nil { -// t.Errorf("Decode panicked with long byte slice") -// } -// }() -// err := p.Decode(data) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } -// } +func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) { + var p PDU_HEADER + data := make([]byte, 20) + defer func() { + if r := recover(); r != nil { + t.Errorf("Decode panicked with long byte slice") + } + }() + err := p.Decode(bytes.NewBuffer(data)) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} -// func TestDecodeHandlesNilDataInput(t *testing.T) { -// var p PDU_HEADER -// err := p.Decode(nil) +func TestDecodeHandlesNilDataInput(t *testing.T) { + var p PDU_HEADER + err := p.Decode(nil) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + if err == nil { + t.Errorf("Expected error, got none") + } -// if p.command_length != 0 { -// t.Errorf("Expected command_length to be 0, got %d", p.command_length) -// } -// if p.command_id != 0 { -// t.Errorf("Expected command_id to be 0, got %d", p.command_id) -// } -// if p.command_status != 0 { -// t.Errorf("Expected command_status to be 0, got %d", p.command_status) -// } -// if p.sequence_number != 0 { -// t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number) -// } -// } + if p.command_length != 0 { + t.Errorf("Expected command_length to be 0, got %d", p.command_length) + } + if p.command_id != 0 { + t.Errorf("Expected command_id to be 0, got %d", p.command_id) + } + if p.command_status != 0 { + t.Errorf("Expected command_status to be 0, got %d", p.command_status) + } + if p.sequence_number != 0 { + t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number) + } +} -// func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) { -// var p PDU_HEADER -// data := []byte{} -// err := p.Decode(data) +func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) { + var p PDU_HEADER + data := []byte{} + err := p.Decode(bytes.NewBuffer(data)) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + 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) -// } -// if p.command_id != 0 { -// t.Errorf("Expected command_id to be 0, got %d", p.command_id) -// } -// if p.command_status != 0 { -// t.Errorf("Expected command_status to be 0, got %d", p.command_status) -// } -// if p.sequence_number != 0 { -// t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number) -// } -// } + if p.command_length != 0 { + t.Errorf("Expected command_length to be 0, got %d", p.command_length) + } + if p.command_id != 0 { + t.Errorf("Expected command_id to be 0, got %d", p.command_id) + } + if p.command_status != 0 { + t.Errorf("Expected command_status to be 0, got %d", p.command_status) + } + if p.sequence_number != 0 { + t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number) + } +} -// func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) { -// var p PDU_HEADER -// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} -// originalData := make([]byte, len(data)) -// copy(originalData, data) +func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) { + var p PDU_HEADER + data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3} + originalData := make([]byte, len(data)) + copy(originalData, data) -// err := p.Decode(data) + err := p.Decode(bytes.NewBuffer(data)) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + if err != nil { + t.Errorf("Expected no error, got %v", err) + } -// for i := range data { -// if data[i] != originalData[i] { -// t.Errorf("Expected data at index %d to be %d, got %d", i, originalData[i], data[i]) -// } -// } -// } + for i := range data { + if data[i] != originalData[i] { + t.Errorf("Expected data at index %d to be %d, got %d", i, originalData[i], data[i]) + } + } +} -// func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) { -// var p PDU_HEADER -// data := []byte{255, 255, 255, 255, 255, 255, 255, 255} -// err := p.Decode(data) +func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) { + var p PDU_HEADER + data := []byte{255, 255, 255, 255, 255, 255, 255, 255} + err := p.Decode(bytes.NewBuffer(data)) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + 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) -// } -// if p.command_id != math.MaxUint32 { -// t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id) -// } -// } + if p.command_length != math.MaxUint32 { + t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length) + } + if p.command_id != math.MaxUint32 { + t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id) + } +} -// func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) { -// var p PDU_HEADER -// data := []byte{0, 0, 0, 0, 0, 0, 0, 0} -// err := p.Decode(data) +func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) { + var p PDU_HEADER + data := []byte{0, 0, 0, 0, 0, 0, 0, 0} + err := p.Decode(bytes.NewBuffer(data)) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } + 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) -// } -// if p.command_id != 0 { -// t.Errorf("Expected command_id to be 0, got %d", p.command_id) -// } -// } + if p.command_length != 0 { + t.Errorf("Expected command_length to be 0, got %d", p.command_length) + } + if p.command_id != 0 { + t.Errorf("Expected command_id to be 0, got %d", p.command_id) + } +} -// // region size -// func TestSizeReturns16(t *testing.T) { -// var p PDU_HEADER -// if p.Size() != 16 { -// t.Errorf("Expected size to be 16, got %d", p.Size()) -// } -// } - -// // region benchmarks +// region size +func TestSizeReturns16(t *testing.T) { + var p PDU_HEADER + if p.Size() != 16 { + t.Errorf("Expected size to be 16, got %d", p.Size()) + } +} +// region benchmarks // With buffer pool func BenchmarkEncodeWithBufferPool(b *testing.B) { p := &PDU_HEADER{