Add legit test for pdu encoding

This commit is contained in:
2024-07-24 20:58:12 +02:00
parent d25058fdec
commit 38976d6bc8
3 changed files with 49 additions and 26 deletions

View File

@@ -12,6 +12,7 @@ type (
Decode(*bytes.Buffer) error Decode(*bytes.Buffer) error
// Size in bytes // Size in bytes
Size() int Size() int
UpdateSize()
} }
PDU_HEADER struct { PDU_HEADER struct {
@@ -80,3 +81,6 @@ func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
func (p *PDU_HEADER) Size() int { func (p *PDU_HEADER) Size() int {
return 16 return 16
} }
func (p *PDU_HEADER) UpdateSize() {
p.command_length = uint32(p.Size())
}

View File

@@ -147,3 +147,7 @@ func (p *SUBMIT_SM) Size() int {
} }
return size return size
} }
func (p *SUBMIT_SM) UpdateSize() {
p.header.command_length = uint32(p.Size())
p.sm_length = byte(len(p.short_message))
}

View File

@@ -1,37 +1,52 @@
package pdu package pdu
import ( import (
"bytes"
"testing" "testing"
) )
// region encode // region encode
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
p := &SUBMIT_SM{
header: &PDU_HEADER{
command_length: 0,
command_id: 4,
command_status: 0,
sequence_number: 378019,
},
service_type: "OMV4",
source_addr_ton: 3,
source_addr_npi: 1,
source_addr: "80362",
dest_addr_ton: 1,
dest_addr_npi: 1,
destination_addr: "812345001000",
esm_class: 0,
protocol_id: 0,
priority_flag: 0,
schedule_delivery_time: "",
validity_period: "180105120000004+",
registered_delivery: 1,
data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
sm_default_msg_id: 0,
short_message: "Reply Yes to opt in or No to opt out.",
}
p.UpdateSize()
buf := ByteBufferPool.Get(p.Size())
err := p.Encode(buf)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := []byte{0,0,0,107,0,0,0,4,0,0,0,0,0,5,196,163,79,77,86,52,0,3,1,56,48,51,54,50,0,1,1,56,49,50,51,52,53,48,48,49,48,48,48,0,0,0,0,0,49,56,48,49,48,53,49,50,48,48,48,48,48,48,52,43,0,1,0,0,0,37,82,101,112,108,121,32,89,101,115,32,116,111,32,111,112,116,32,105,110,32,111,114,32,78,111,32,116,111,32,111,112,116,32,111,117,116,46}
if !bytes.Equal(buf.Bytes(), expected) {
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
}
}
// func TestRealScenario(t *testing.T) { // func TestRealScenario(t *testing.T) {
// expected := []byte{0, 0, 0, 54, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 67, 77, 84, 0, 1, 1, 49, 50, 51, 52, 53, 0, 1, 1, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 72, 101, 108, 108, 111, 44, 32, 83, 77, 80, 80, 33} //
// p := &SUBMIT_SM{
// header: PDU_HEADER{
// command_length: 0,
// command_id: 4,
// command_status: 0,
// sequence_number: 1,
// },
// service_type: "CMT",
// source_addr_ton: 1,
// source_addr_npi: 1,
// source_addr: "12345",
// dest_addr_ton: 1,
// dest_addr_npi: 1,
// destination_addr: "67890",
// esm_class: 0,
// protocol_id: 0,
// priority_flag: 0,
// schedule_delivery_time: "",
// validity_period: "",
// registered_delivery: 1,
// data_coding: 0,
// sm_default_msg_id: 0,
// short_message: "Hello, SMPP!",
// }
// p.header.command_length = uint32(p.Size()) // p.header.command_length = uint32(p.Size())
// p.sm_length = byte(len(p.short_message)) // p.sm_length = byte(len(p.short_message))
// buf := make([]byte, p.Size()) // buf := make([]byte, p.Size())
@@ -102,4 +117,4 @@ func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
} }
} }
// region benchmark // region benchmark