Compare commits

...

3 Commits

Author SHA1 Message Date
31efd3fdef Remove null terminator from short message
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 18s
Run Tests / Test (push) Failing after 16s
AApparently the message does not use it
2024-07-24 21:00:42 +02:00
38976d6bc8 Add legit test for pdu encoding 2024-07-24 20:58:52 +02:00
d25058fdec Add null to terminate strings 2024-07-24 20:58:40 +02:00
6 changed files with 108 additions and 46 deletions

5
go.mod
View File

@@ -2,4 +2,7 @@ module smpptester
go 1.22.4
require github.com/yuin/gopher-lua v1.1.1
require (
github.com/warthog618/sms v0.3.0
github.com/yuin/gopher-lua v1.1.1
)

14
go.sum
View File

@@ -1,2 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/warthog618/sms v0.3.0 h1:LYAb5ngmu2qjNExgji3B7xi2tIZ9+DsuE9pC5xs4wwc=
github.com/warthog618/sms v0.3.0/go.mod h1:+bYZGeBxu003sxD5xhzsrIPBAjPBzTABsRTwSpd7ld4=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@@ -1,3 +1,5 @@
package pdu
var ByteBufferPool = NewBufferPoolManager()
const NULL = byte(0x00)
var NULL_ARR = []byte{NULL}

View File

@@ -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())
}

View File

@@ -5,6 +5,8 @@ import (
"encoding/ascii85"
"encoding/binary"
"fmt"
"github.com/warthog618/sms/encoding/gsm7"
)
type (
@@ -69,32 +71,40 @@ func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot encode into nil buffer")
}
asciiEncoder := ascii85.NewEncoder(buf)
// TODO: Implement encodings bsed on p.data_coding
messageEncoder := gsm7.NewEncoder()
p.header.Encode(buf)
n := ascii85.Encode(buf.Bytes(), []byte(p.service_type))
buf.Truncate(n)
asciiEncoder.Write([]byte(p.service_type))
buf.Write(NULL_ARR)
asciiEncoder.Write([]byte(p.service_type))
buf.Write(NULL_ARR)
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
n = ascii85.Encode(buf.Bytes(), []byte(p.source_addr))
buf.Truncate(n)
asciiEncoder.Write([]byte(p.source_addr))
buf.Write(NULL_ARR)
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
n = ascii85.Encode(buf.Bytes(), []byte(p.destination_addr))
buf.Truncate(n)
asciiEncoder.Write([]byte(p.destination_addr))
buf.Write(NULL_ARR)
binary.Write(buf, binary.BigEndian, p.esm_class)
binary.Write(buf, binary.BigEndian, p.protocol_id)
binary.Write(buf, binary.BigEndian, p.priority_flag)
n = ascii85.Encode(buf.Bytes(), []byte(p.schedule_delivery_time))
buf.Truncate(n)
n = ascii85.Encode(buf.Bytes(), []byte(p.validity_period))
buf.Truncate(n)
asciiEncoder.Write([]byte(p.schedule_delivery_time))
buf.Write(NULL_ARR)
asciiEncoder.Write([]byte(p.validity_period))
buf.Write(NULL_ARR)
binary.Write(buf, binary.BigEndian, p.registered_delivery)
binary.Write(buf, binary.BigEndian, p.replace_if_present)
binary.Write(buf, binary.BigEndian, p.data_coding)
binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
binary.Write(buf, binary.BigEndian, p.sm_length)
// TODO: Implement encodings bsed on p.data_coding
n = ascii85.Encode(buf.Bytes(), []byte(p.short_message))
buf.Truncate(n)
encodedMsg, err := messageEncoder.Encode([]byte(p.short_message))
if err != nil {
return err
}
buf.Write(encodedMsg)
return nil
}
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
@@ -106,23 +116,37 @@ func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
func (p *SUBMIT_SM) Size() int {
var size int
size += p.header.Size()
size += len(p.service_type) * 1
size += 1 + len(p.service_type)
size += 1 // source_addr_ton
size += 1 // source_addr_npi
size += len(p.source_addr) * 1
size += 1 + len(p.source_addr)
size += 1 // dest_addr_ton
size += 1 // dest_addr_npi
size += len(p.destination_addr) * 1
size += 1 + len(p.destination_addr)
size += 1 // esm_class
size += 1 // protocol_id
size += 1 // priority_flag
size += len(p.schedule_delivery_time) * 1
size += len(p.validity_period) * 1
size += 1 + len(p.schedule_delivery_time)
size += 1 + len(p.validity_period)
size += 1 // registered_delivery
size += 1 // replace_if_present
size += 1 // data_coding
size += 1 // sm_default_msg_id
size += 1 // sm_length
size += len(p.short_message) * 1
// TODO: Handle encoding based on p.data_coding
switch p.data_coding {
case 0b00000000: // GSM7
size += (len(p.short_message)*7 + 8 - 1) / 8
case 0b00000001: // ASCII
size += len(p.short_message)
case 0b00000011: // LATIN1
size += len(p.short_message)
case 0b00001000: // UCS2
size += len(p.short_message) * 2
}
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
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
// region benchmark