Files
smpp-tester/pdu/pdu_test.go
2024-07-22 22:15:51 +02:00

186 lines
4.8 KiB
Go

package pdu
import (
"testing"
)
// region encode
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 1,
command_status: 1,
sequence_number: 1,
}
result := p.Encode()
if len(result) != 16 {
t.Errorf("Expected byte slice of length 16, got %d", len(result))
}
}
func TestEncodeHandlesZeroValues(t *testing.T) {
p := &PDU_HEADER{
command_length: 0,
command_id: 0,
command_status: 0,
sequence_number: 0,
}
result := p.Encode()
expected := make([]uint8, 16)
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with zero values, got %v", result)
break
}
}
}
func TestEncodeEncodesProperly(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 2,
command_status: 3,
sequence_number: 4,
}
result := p.Encode()
expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
break
}
}
}
func TestEncodeEncodesProperlyComplex(t *testing.T) {
p := &PDU_HEADER{
command_length: 13426724,
command_id: 254352,
command_status: 35634264,
sequence_number: 476543523,
}
result := p.Encode()
expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
break
}
}
}
// region decode
func TestDecodeHandlesShortByteSlice(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 10}
defer func() {
if r := recover(); r != nil {
t.Errorf("Decode panicked with short byte slice")
}
}()
p.Decode(data)
}
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)
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([]uint8, 20)
defer func() {
if r := recover(); r != nil {
t.Errorf("Decode panicked with long byte slice")
}
}()
p.Decode(data)
}
func TestDecodeHandlesNilDataInput(t *testing.T) {
var p PDU_HEADER
p.Decode(nil)
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 := []uint8{}
p.Decode(data)
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 := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
originalData := make([]uint8, len(data))
copy(originalData, data)
p.Decode(data)
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 := []uint8{255, 255, 255, 255, 255, 255, 255, 255}
p.Decode(data)
if p.command_length != 4294967295 {
t.Errorf("Expected command_length to be 4294967295, got %d", p.command_length)
}
if p.command_id != 4294967295 {
t.Errorf("Expected command_id to be 4294967295, got %d", p.command_id)
}
}
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 0, 0, 0, 0, 0}
p.Decode(data)
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)
}
}