From 38976d6bc8b9590115acca5fa9ccad496c42260b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 24 Jul 2024 20:58:12 +0200 Subject: [PATCH] Add legit test for pdu encoding --- pdu/pdu.go | 4 +++ pdu/submit.go | 4 +++ pdu/submit_test.go | 67 ++++++++++++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/pdu/pdu.go b/pdu/pdu.go index 2511a1e..cc2a301 100644 --- a/pdu/pdu.go +++ b/pdu/pdu.go @@ -12,6 +12,7 @@ type ( Decode(*bytes.Buffer) error // Size in bytes Size() int + UpdateSize() } PDU_HEADER struct { @@ -80,3 +81,6 @@ func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error { func (p *PDU_HEADER) Size() int { return 16 } +func (p *PDU_HEADER) UpdateSize() { + p.command_length = uint32(p.Size()) +} \ No newline at end of file diff --git a/pdu/submit.go b/pdu/submit.go index 254a039..dcea9b5 100644 --- a/pdu/submit.go +++ b/pdu/submit.go @@ -147,3 +147,7 @@ func (p *SUBMIT_SM) Size() int { } return size } +func (p *SUBMIT_SM) UpdateSize() { + p.header.command_length = uint32(p.Size()) + p.sm_length = byte(len(p.short_message)) +} diff --git a/pdu/submit_test.go b/pdu/submit_test.go index 57decaa..797b9e9 100644 --- a/pdu/submit_test.go +++ b/pdu/submit_test.go @@ -1,37 +1,52 @@ package pdu import ( + "bytes" "testing" ) // 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) { -// 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.sm_length = byte(len(p.short_message)) // buf := make([]byte, p.Size()) @@ -102,4 +117,4 @@ func TestHandlesEmptyStringsForAllStringFields(t *testing.T) { } } -// region benchmark \ No newline at end of file +// region benchmark