Compare commits

...

2 Commits

Author SHA1 Message Date
PhatPhuckDave
a68600281b Add tests for submit size 2024-07-22 22:47:06 +02:00
PhatPhuckDave
1b904b69fd Implement size in submit_pdu 2024-07-22 22:43:48 +02:00
3 changed files with 75 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ type (
EncodeInto(*[]uint8)
Encode() []uint8
Decode([]uint8)
// Size in bytes
Size() uint32
}

View File

@@ -66,5 +66,25 @@ func (p *SUBMIT_SM) EncodeInto(buf *[]byte) {
func (p *SUBMIT_SM) Decode(data []byte) {
}
func (p *SUBMIT_SM) Size() uint32 {
return 0
var size uint32
size += 16 // header
size += uint32(len(p.service_type) * 1)
size += 1 // source_addr_ton
size += 1 // source_addr_npi
size += uint32(len(p.source_addr) * 1)
size += 1 // dest_addr_ton
size += 1 // dest_addr_npi
size += uint32(len(p.destination_addr) * 1)
size += 1 // esm_class
size += 1 // protocol_id
size += 1 // priority_flag
size += uint32(len(p.schedule_delivery_time) * 1)
size += uint32(len(p.validity_period) * 1)
size += 1 // registered_delivery
size += 1 // replace_if_present
size += 1 // data_coding
size += 1 // sm_default_msg_id
size += 1 // sm_length
size += uint32(len(p.short_message) * 1)
return size
}

53
pdu/submit_test.go Normal file
View File

@@ -0,0 +1,53 @@
package pdu
import "testing"
// region size
func TestCalculateSizeTypicalInstance(t *testing.T) {
p := &SUBMIT_SM{
service_type: "test_service",
source_addr: "12345",
destination_addr: "67890",
schedule_delivery_time: "",
validity_period: "",
short_message: "Hello, World!",
}
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}
func TestCalculateSizeMaxLengths(t *testing.T) {
maxLen := 255
p := &SUBMIT_SM{
service_type: string(make([]byte, maxLen)),
source_addr: string(make([]byte, maxLen)),
destination_addr: string(make([]byte, maxLen)),
schedule_delivery_time: string(make([]byte, maxLen)),
validity_period: string(make([]byte, maxLen)),
short_message: string(make([]byte, maxLen)),
}
expectedSize := uint32(16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen)
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
p := &SUBMIT_SM{
service_type: "",
source_addr: "",
destination_addr: "",
schedule_delivery_time: "",
validity_period: "",
short_message: "",
}
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}