Compare commits

...

29 Commits

Author SHA1 Message Date
38af7b146d Test with the smpp center
All checks were successful
Run Tests / Test (push) Successful in 20s
Benchmark BufferPool / RunBenchmarks (push) Successful in 34s
2024-07-31 15:19:53 +02:00
f1eb6b065d Export all fields in pdu
Oopsie!
2024-07-31 15:12:25 +02:00
4771ffa154 Make benchmarks run only on master
All checks were successful
Run Tests / Test (push) Successful in 22s
Benchmark BufferPool / RunBenchmarks (push) Successful in 32s
2024-07-31 14:42:29 +02:00
76bdc5214b Merge branch 'dev'
All checks were successful
Run Tests / Test (push) Successful in 16s
Benchmark BufferPool / RunBenchmarks (push) Successful in 29s
2024-07-31 14:21:27 +02:00
9088bc14bc Make benchmark everything 2024-07-31 14:21:24 +02:00
c56d441891 Fix size calculation and add another test for encoding a gsm7 message
All checks were successful
Run Tests / Test (push) Successful in 16s
Benchmark BufferPool / RunBenchmarks (push) Successful in 21s
2024-07-31 14:19:12 +02:00
85e674753e Fix size tests for submit_sm
All checks were successful
Run Tests / Test (push) Successful in 15s
Benchmark BufferPool / RunBenchmarks (push) Successful in 23s
2024-07-31 14:05:54 +02:00
b74a7c842f Remove dependencies
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 16s
Run Tests / Test (push) Failing after 14s
2024-07-31 13:15:57 +02:00
d477951fb4 Implement submit message encoding 2024-07-31 13:15:47 +02:00
4c92723df0 Implement blank ucs2 coder 2024-07-31 13:14:37 +02:00
d0c868ca5c Add coder/decoder size to coder 2024-07-31 13:14:37 +02:00
7001f2c51a Refactor encoding to use *string instead of string as input
I don't want to clone the whole input string
2024-07-31 13:14:37 +02:00
7b4fcf3de1 Add air for pdu tests
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 14s
Run Tests / Test (push) Failing after 13s
2024-07-31 12:47:53 +02:00
ad8eaff930 Fully implement GSM7 decode
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 16s
2024-07-31 12:45:31 +02:00
f847588a24 Grow buffer when encoding when necessary
Some checks failed
Run Tests / Test (push) Failing after 15s
Benchmark BufferPool / RunBenchmarks (push) Failing after 16s
To maybe hopefully prevent multiple allocations
2024-07-30 23:34:50 +02:00
ee4f8ecfd6 Add memory allocation test
To make sure none is happening
2024-07-30 23:31:54 +02:00
f5e263752e little refactor
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 15s
2024-07-30 22:58:51 +02:00
39c7876ed1 Clean up tests to reduce repeating code
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 16s
Run Tests / Test (push) Failing after 15s
2024-07-30 22:49:22 +02:00
b2a588e667 Add "len" methods to encode and decode
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 18s
Run Tests / Test (push) Failing after 16s
To get length of encoded/decoded string
2024-07-30 22:45:56 +02:00
db61da9fc2 A lil optimize 2024-07-30 22:15:36 +02:00
9fbcdd86cc Add encode benchmarks 2024-07-30 22:14:19 +02:00
76d7102e72 Implement basic decoding 2024-07-30 22:10:57 +02:00
74928d9f7a Add some sort of work on decode 2024-07-30 22:03:57 +02:00
a6339587d4 Add tests for insertat and decode 2024-07-30 22:03:57 +02:00
48d28def47 Add air config 2024-07-30 22:02:00 +02:00
e6199da5d6 Add more tests
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 1m15s
Run Tests / Test (push) Failing after 1m13s
2024-07-28 21:47:51 +02:00
a2f4e9af25 Fix packing for larger strings
Some checks are pending
Benchmark BufferPool / RunBenchmarks (push) Waiting to run
Run Tests / Test (push) Waiting to run
2024-07-28 17:35:19 +02:00
b26c64d0d9 Add more complex test case
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Has been cancelled
Run Tests / Test (push) Has been cancelled
2024-07-28 14:54:30 +02:00
930e5b9b4f Generate mask on the fly 2024-07-28 14:51:25 +02:00
22 changed files with 950 additions and 392 deletions

View File

@@ -2,8 +2,7 @@ name: Benchmark BufferPool
on:
push:
branches:
- '*'
- '!benchmark'
- 'master'
jobs:
RunBenchmarks:

2
.gitignore vendored
View File

@@ -1 +1,3 @@
jstester/node_modules
encoding/tmp
pdu/tmp

View File

@@ -38,7 +38,7 @@ func main() {
log.Fatalf("Error creating result file %s with %v", resultFile, err)
}
cmd := exec.Command("go", "test", "-bench", ".", "./pdu")
cmd := exec.Command("go", "test", "-bench", ".", "./...")
cmd.Dir = "../"
cmd.Stdout = outFile

51
encoding/.air.toml Normal file
View File

@@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "tmp\\main.exe"
cmd = "go test ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = []
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 100
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = true
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = true
keep_scroll = true

View File

@@ -4,11 +4,19 @@ import "bytes"
type ASCIICoder struct{}
func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) error {
buf.WriteString(s)
func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error {
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
buf.WriteString(*s)
return nil
}
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
return buf.String(), nil
}
func (c ASCIICoder) EncodesInto(s *string) int {
return len(*s)
}
func (c ASCIICoder) DecodesInto(buf *bytes.Buffer) int {
return buf.Len()
}

View File

@@ -11,7 +11,7 @@ func TestASCIIEncodeSimpleASCIIString(t *testing.T) {
input := "Hello, World!"
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
err := coder.Encode(input, &buf)
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
@@ -46,7 +46,7 @@ func TestASCIIEncodeEmptyString(t *testing.T) {
input := ""
expected := []byte{}
err := coder.Encode(input, &buf)
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)

View File

@@ -3,6 +3,8 @@ package encoding
import "bytes"
type Coder interface {
Encode(s string, buf *bytes.Buffer) error
Encode(s *string, buf *bytes.Buffer) error
Decode(buf *bytes.Buffer) (string, error)
EncodesInto(s *string) int
DecodesInto(buf *bytes.Buffer) int
}

View File

@@ -7,47 +7,130 @@ import (
type GSM7Coder struct{}
var masks = []byte{
0b00000000,
0b00000001,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
}
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
// Make sure buffer can fit EncodesInto bytes
// Otherwise Encode will allocate memory as it sees fit
// Which is fine but not optimal
// Preallocate the buffer with the size of EncodesInto bytes
func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error {
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
utf8 := []byte(s)
var offset byte = 1
var bitshift byte = 1
utf8 := []byte(*s)
var (
offset int = 1
bitshift byte = 0
leap, shift bool
)
encodedSize := c.EncodesInto(s)
cap := buf.Cap()
if cap < encodedSize {
buf.Grow(encodedSize - cap)
}
for index, septet := range utf8 {
if septet > 0b01111111 {
return fmt.Errorf("invalid character at index %d", index)
}
bindex := byte(index)
if bindex == 0 {
if index == 0 {
continue
}
mask := masks[bitshift]
masked := (mask & septet) << (8 - bitshift)
utf8[bindex-offset] |= masked
utf8[bindex] >>= bitshift
buf.WriteByte(utf8[bindex-offset])
bitshift++
if utf8[bindex] == 0 {
offset++
bitshift = 1
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte:%08b (%-3d) Leap:%5v", index, offset, bitshift, utf8[index], utf8[index], utf8[index-offset], utf8[index-offset], leap)
mask := byte(255 >> (8 - bitshift))
masked := (mask & septet) << (8 - bitshift)
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d Mask:%08b Masked:%08b", index, offset, bitshift, mask, masked)
if leap {
masked >>= 1
}
utf8[index-offset] |= masked
utf8[index] >>= bitshift
if !leap {
buf.WriteByte(utf8[index-offset])
}
if index == len(utf8)-1 && utf8[index] > 0 {
buf.WriteByte(utf8[index])
}
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte:%08b (%-3d) Leap:%5v", index, offset, bitshift, utf8[index], utf8[index], utf8[index-offset], utf8[index-offset], leap)
if bitshift >= 7 {
if leap {
// log.Printf("Shift at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
leap = false
bitshift = 0
offset++
shift = true
continue
}
// log.Printf("Leap at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
leap = true
bitshift = 6
}
if shift {
offset = 1
}
}
return nil
}
func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
return buf.String(), nil
gsm7 := buf.Bytes()
var (
offset int
bitshift byte = 0
leap bool
)
outLength := c.DecodesInto(buf)
lengthDiff := outLength - len(gsm7)
gsm7 = append(gsm7, make([]byte, lengthDiff)...)
start := len(gsm7) - 2
// We don't care about the last byte
// Unless it's the %8.......
// We'll deal with that later
for index := start; index >= 0; index-- {
octet := gsm7[index]
bitshift = byte((index % 7) + 1)
if bitshift == 7 {
leap = true
}
offset = 1
// log.Println(offset, index, index+offset)
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
mask := byte(255 << (8 - bitshift))
masked := (mask & octet) >> (8 - bitshift)
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d Mask:%08b Masked:%08b", index, offset, bitshift, mask, masked)
if leap {
InsertAt(&gsm7, index+offset, masked)
} else {
gsm7[index+offset] |= masked
}
// Remove last bitshift bits
gsm7[index] <<= bitshift
// Move the remaining bit once to the right to form septet instead of octet
gsm7[index] >>= 1
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
leap = false
}
return string(gsm7), nil
}
// Allocation free
// Which means data MUST have space for value
func InsertAt(data *[]byte, index int, value byte) {
copy((*data)[index+1:], (*data)[index:])
(*data)[index] = value
}
func (c GSM7Coder) EncodesInto(s *string) int {
slen := len(*s)
enclen := slen * 7 / 8
if slen%8 != 0 {
enclen++
}
return enclen
}
func (c GSM7Coder) DecodesInto(buf *bytes.Buffer) int {
blen := buf.Len()
declen := blen * 8 / 7
return declen
}

View File

@@ -5,38 +5,103 @@ import (
"testing"
)
var (
short8nString = "Sunshine"
longNot8nString = "Golden rays play, Chasing night away."
long8nString = "Ducks are fucking great, they quacks, O quackers, what the fuck."
short8nStringEncodedBytes = []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
longNot8nStringEncodedBytes = []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
long8nStringEncodedBytes = []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
)
// region encode
func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := "Sunshine"
input := short8nString
expected := []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
err := coder.Encode(input, &buf)
expected := short8nStringEncodedBytes
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
}
}
func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
func TestGSM7EncodeComplexASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
input := longNot8nString
expected := "Sunshine"
buf.Write(input)
output, err := coder.Decode(&buf)
expected := longNot8nStringEncodedBytes
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
}
}
func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
expected := long8nStringEncodedBytes
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
}
}
func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
coder := &GSM7Coder{}
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
buf.Reset()
expected := buf.Cap()
err := coder.Encode(&input, buf)
actual := buf.Cap()
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if actual != expected {
t.Errorf("Expected buffer of size %v, but got %v", expected, actual)
}
}
func TestGSM7EncodeDoesAllocateWhenNecessary(t *testing.T) {
coder := &GSM7Coder{}
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
var buf bytes.Buffer
buf.Reset()
original := buf.Cap()
err := coder.Encode(&input, &buf)
modified := buf.Cap()
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !(modified > original) {
t.Errorf("Expected buffer to grow but buffer changed from %v to %v", original, modified)
}
}
@@ -46,14 +111,69 @@ func TestGSM7EncodeEmptyString(t *testing.T) {
input := ""
expected := []byte{}
err := coder.Encode(input, &buf)
err := coder.Encode(&input, &buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
}
}
// region decode
func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := short8nStringEncodedBytes
expected := short8nString
buf.Write(input)
actual, err := coder.Decode(&buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if actual != expected {
t.Errorf("Expected '%v', but got '%v'", expected, actual)
}
}
func TestGSM7DecodeComplexASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := longNot8nStringEncodedBytes
expected := longNot8nString
buf.Write(input)
actual, err := coder.Decode(&buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if actual != expected {
t.Errorf("Expected '%v', but got '%v'", expected, actual)
}
}
func TestGSM7DecodeComplex8nASCIIString(t *testing.T) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := long8nStringEncodedBytes
expected := long8nString
buf.Write(input)
actual, err := coder.Decode(&buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if actual != expected {
t.Errorf("Expected '%v', but got '%v'", expected, actual)
}
}
@@ -62,13 +182,188 @@ func TestGSM7DecodeEmptyString(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
expected := ""
output, err := coder.Decode(buf)
actual, err := coder.Decode(buf)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if output != expected {
t.Errorf("Expected %v, but got %v", expected, output)
if actual != expected {
t.Errorf("Expected '%v', but got '%v'", expected, actual)
}
}
// region insertat
func TestInsertAtBeginning(t *testing.T) {
data := []byte{2, 3, 4, 0}
InsertAt(&data, 0, 1)
expected := []byte{1, 2, 3, 4}
if !bytes.Equal(data, expected) {
t.Errorf("expected %v, got %v", expected, data)
}
}
func TestInsertInMiddle(t *testing.T) {
data := []byte{2, 3, 4, 0}
InsertAt(&data, 1, 5)
expected := []byte{2, 5, 3, 4}
if !bytes.Equal(data, expected) {
t.Errorf("expected %v, got %v", expected, data)
}
}
func TestInsertAtEnd(t *testing.T) {
data := []byte{1, 2, 3, 0}
InsertAt(&data, 3, 4)
expected := []byte{1, 2, 3, 4}
if !bytes.Equal(data, expected) {
t.Errorf("expected %v, got %v", expected, data)
}
}
func TestIndexOutOfBounds(t *testing.T) {
data := []byte{2, 3, 4}
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
InsertAt(&data, 4, 5)
}
func TestNegativeIndex(t *testing.T) {
data := []byte{2, 3, 4}
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
InsertAt(&data, -1, 1)
}
func TestMaintainsOrderAfterInsertion(t *testing.T) {
data := []byte{2, 3, 4, 0}
InsertAt(&data, 1, 1)
expected := []byte{2, 1, 3, 4}
if !bytes.Equal(data, expected) {
t.Errorf("expected %v, got %v", expected, data)
}
}
func TestMaintainsLength(t *testing.T) {
data := []byte{2, 3, 4, 0}
InsertAt(&data, 1, 1)
if len(data) != 4 {
t.Errorf("expected length 4, got %d", len(data))
}
}
func TestDeletesLastValue(t *testing.T) {
data := []byte{2, 3, 4, 5}
InsertAt(&data, 0, 1)
expected := []byte{1, 2, 3, 4}
if !bytes.Equal(data, expected) {
t.Errorf("expected %v, got %v", expected, data)
}
}
// region misc tests
func TestGSM7EncodesIntoSmallString(t *testing.T) {
input := short8nString
expected := 7
actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
input := longNot8nString
expected := 33
actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
input := long8nString
expected := 56
actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
func TestGSM7DecodesIntoSmallString(t *testing.T) {
input := short8nStringEncodedBytes
expected := 8
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
input := longNot8nStringEncodedBytes
expected := 37
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
input := long8nStringEncodedBytes
expected := 64
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual)
}
}
// region benchmark
func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := short8nString
b.ResetTimer()
for i := 0; i < b.N; i++ {
coder.Encode(&input, &buf)
}
}
func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := longNot8nString
b.ResetTimer()
for i := 0; i < b.N; i++ {
coder.Encode(&input, &buf)
}
}
func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
coder := &GSM7Coder{}
var buf bytes.Buffer
input := long8nString
b.ResetTimer()
for i := 0; i < b.N; i++ {
coder.Encode(&input, &buf)
}
}
func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
coder := &GSM7Coder{}
input := long8nString
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
buf.Reset()
coder.Encode(&input, buf)
}
}

View File

@@ -4,10 +4,16 @@ import "bytes"
type UCS2Coder struct{}
func (c *UCS2Coder) Encode(s string, buf *bytes.Buffer) {
func (c *UCS2Coder) Encode(s *string, buf *bytes.Buffer) error {
panic("UCS2 not implemented yet")
}
func (c *UCS2Coder) Decode(buf *bytes.Buffer) (string, error) {
panic("UCS2 not implemented yet")
}
func (c *UCS2Coder) Decode(buf *bytes.Buffer) string {
panic("UCS2 not implemented yet")
func (c UCS2Coder) EncodesInto(s *string) int {
return len(*s) * 2
}
func (c UCS2Coder) DecodesInto(buf *bytes.Buffer) int {
return buf.Len() / 2
}

5
go.mod
View File

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

14
go.sum
View File

@@ -1,16 +1,2 @@
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=

57
main.go
View File

@@ -6,10 +6,12 @@ import (
"log"
"net"
"sync"
"smpptester/pdu"
)
func init() {
log.SetFlags(log.Lmicroseconds)
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
}
// Function to create a submit_sm PDU
@@ -29,7 +31,7 @@ func createSubmitSMPDU() []byte {
validityPeriod := ""
registeredDelivery := byte(0x01)
replaceIfPresentFlag := byte(0x00)
dataCoding := byte(0x00)
dataCoding := byte(0x01)
smDefaultMsgID := byte(0x00)
shortMessage := "Hello, SMPP!"
@@ -37,7 +39,6 @@ func createSubmitSMPDU() []byte {
headerLength := 16
bodyLength := 1 + len(serviceType) + 1 + 1 + len(sourceAddr) + 1 + 1 + len(destinationAddr) + 1 + 1 + len(scheduleDeliveryTime) + 1 + len(validityPeriod) + 1 + 1 + 1 + 1 + 1 + len(shortMessage)
totalLength := headerLength + bodyLength
log.Printf("Total length: %d", totalLength)
// Create a buffer to hold the PDU
var buffer bytes.Buffer
@@ -136,7 +137,55 @@ func main() {
// go handleConnection(conn)
// }
// }()
submit := &pdu.SUBMIT_SM{
Header: &pdu.PDU_HEADER{
Command_length: 0,
Command_id: 4,
Command_status: 0,
Sequence_number: 1,
},
Service_type: "hehe",
Source_addr_ton: 0x01,
Source_addr_npi: 0x01,
Source_addr: "12345",
Dest_addr_ton: 0x01,
Dest_addr_npi: 0x01,
Destination_addr: "67890",
Esm_class: 0x00,
Protocol_id: 0x00,
Priority_flag: 0x00,
Schedule_delivery_time: "",
Validity_period: "",
Registered_delivery: 0x01,
Replace_if_present: 0x00,
Data_coding: 0x01,
Sm_default_msg_id: 0x00,
Short_message: "Hello, SMPP!",
}
submit.UpdateSize()
buf := pdu.ByteBufferPool.Get(submit.Size())
defer pdu.ByteBufferPool.Put(buf)
submit.Encode(buf)
log.Println(buf.Bytes())
log.Println(createSubmitSMPDU())
// log.Println(pdu.Encode())
// serviceType := "CMT"
// sourceAddrTON := byte(0x01)
// sourceAddrNPI := byte(0x01)
// sourceAddr := "12345"
// destAddrTON := byte(0x01)
// destAddrNPI := byte(0x01)
// destinationAddr := "67890"
// esmClass := byte(0x00)
// protocolID := byte(0x00)
// priorityFlag := byte(0x00)
// scheduleDeliveryTime := ""
// validityPeriod := ""
// registeredDelivery := byte(0x01)
// replaceIfPresentFlag := byte(0x00)
// dataCoding := byte(0x01)
// smDefaultMsgID := byte(0x00)
// shortMessage := "Hello, SMPP!"
wg := &sync.WaitGroup{}
wg.Add(1)
@@ -150,7 +199,7 @@ func main() {
defer conn.Close()
go func() {
conn.Write(createSubmitSMPDU())
conn.Write(buf.Bytes())
data, err := conn.Read(make([]byte, 1024))
if err != nil {
log.Printf("Failed to read from connection: %+v", err)

51
pdu/.air.toml Normal file
View File

@@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "tmp\\main.exe"
cmd = "go test ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = []
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true

View File

@@ -2,19 +2,19 @@ package pdu
type (
BIND struct {
header *PDU_HEADER
system_id string
password string
system_type string
interface_version byte
addr_ton byte
addr_npi byte
address_range string
Header *PDU_HEADER
System_id string
Password string
System_type string
Interface_version byte
Addr_ton byte
Addr_npi byte
Address_range string
}
BIND_RESP struct {
header *PDU_HEADER
system_id string
sc_interface_version byte
Header *PDU_HEADER
System_id string
Sc_interface_version byte
}
BIND_RECVEIVER struct {
BIND
@@ -36,9 +36,9 @@ type (
}
UNBIND struct {
header *PDU_HEADER
Header *PDU_HEADER
}
UNBIND_RESP struct {
header *PDU_HEADER
Header *PDU_HEADER
}
)

View File

@@ -2,17 +2,17 @@ package pdu
type (
CANCEL_SM struct {
header *PDU_HEADER
service_type string
message_id string
source_addr_ton byte
source_addr_npi byte
source_addr string
dest_addr_ton byte
dest_addr_npi byte
destination_addr string
Header *PDU_HEADER
Service_type string
Message_id string
Source_addr_ton byte
Source_addr_npi byte
Source_addr string
Dest_addr_ton byte
Dest_addr_npi byte
Destination_addr string
}
CANCEL_SM_RESP struct {
header *PDU_HEADER
Header *PDU_HEADER
}
)

View File

@@ -2,11 +2,11 @@ package pdu
type (
DELIVER_SM struct {
header *PDU_HEADER
Header *PDU_HEADER
SUBMIT_SM
}
DELIVER_SM_RESP struct {
header *PDU_HEADER
Header *PDU_HEADER
SUBMIT_SM_RESP
}
)

View File

@@ -2,9 +2,9 @@ package pdu
type (
ENQUIRE_LINK struct {
header *PDU_HEADER
Header *PDU_HEADER
}
ENQUIRE_LINK_RESP struct {
header *PDU_HEADER
Header *PDU_HEADER
}
)

View File

@@ -16,14 +16,14 @@ type (
}
PDU_HEADER struct {
command_length uint32
command_id uint32
command_status uint32
sequence_number uint32
Command_length uint32
Command_id uint32
Command_status uint32
Sequence_number uint32
}
GENERIC_NACK struct {
header *PDU_HEADER
Header *PDU_HEADER
}
)
@@ -62,25 +62,25 @@ func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot encode into nil buffer")
}
binary.Write(buf, binary.BigEndian, p.command_length)
binary.Write(buf, binary.BigEndian, p.command_id)
binary.Write(buf, binary.BigEndian, p.command_status)
binary.Write(buf, binary.BigEndian, p.sequence_number)
binary.Write(buf, binary.BigEndian, p.Command_length)
binary.Write(buf, binary.BigEndian, p.Command_id)
binary.Write(buf, binary.BigEndian, p.Command_status)
binary.Write(buf, binary.BigEndian, p.Sequence_number)
return nil
}
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot decode nil buffer")
}
binary.Read(buf, binary.BigEndian, &p.command_length)
binary.Read(buf, binary.BigEndian, &p.command_id)
binary.Read(buf, binary.BigEndian, &p.command_status)
binary.Read(buf, binary.BigEndian, &p.sequence_number)
binary.Read(buf, binary.BigEndian, &p.Command_length)
binary.Read(buf, binary.BigEndian, &p.Command_id)
binary.Read(buf, binary.BigEndian, &p.Command_status)
binary.Read(buf, binary.BigEndian, &p.Sequence_number)
return nil
}
func (p *PDU_HEADER) Size() int {
return 16
}
func (p *PDU_HEADER) UpdateSize() {
p.command_length = uint32(p.Size())
p.Command_length = uint32(p.Size())
}

View File

@@ -11,10 +11,10 @@ import (
// region encode
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 1,
command_status: 1,
sequence_number: 1,
Command_length: 1,
Command_id: 1,
Command_status: 1,
Sequence_number: 1,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -30,10 +30,10 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
func TestEncodeHandlesZeroValues(t *testing.T) {
p := &PDU_HEADER{
command_length: 0,
command_id: 0,
command_status: 0,
sequence_number: 0,
Command_length: 0,
Command_id: 0,
Command_status: 0,
Sequence_number: 0,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -53,10 +53,10 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
func TestEncodeEncodesProperly(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 2,
command_status: 3,
sequence_number: 4,
Command_length: 1,
Command_id: 2,
Command_status: 3,
Sequence_number: 4,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -77,10 +77,10 @@ func TestEncodeEncodesProperly(t *testing.T) {
func TestEncodeEncodesProperlyComplex(t *testing.T) {
p := &PDU_HEADER{
command_length: 13426724,
command_id: 254352,
command_status: 35634264,
sequence_number: 476543523,
Command_length: 13426724,
Command_id: 254352,
Command_status: 35634264,
Sequence_number: 476543523,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -101,10 +101,10 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -115,26 +115,26 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
}
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
buf.Reset()
@@ -145,26 +145,26 @@ func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
}
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -175,26 +175,26 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
}
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
func TestEncodeIntoConcurrencySafety(t *testing.T) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -209,26 +209,26 @@ func TestEncodeIntoConcurrencySafety(t *testing.T) {
wg.Wait()
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
func TestEncodeIntoWithMaximumValues(t *testing.T) {
p := &PDU_HEADER{
command_length: math.MaxUint32,
command_id: math.MaxUint32,
command_status: math.MaxUint32,
sequence_number: math.MaxUint32,
Command_length: math.MaxUint32,
Command_id: math.MaxUint32,
Command_status: math.MaxUint32,
Sequence_number: math.MaxUint32,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -239,26 +239,26 @@ func TestEncodeIntoWithMaximumValues(t *testing.T) {
}
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
p := &PDU_HEADER{
command_length: 0,
command_id: 0,
command_status: 0,
sequence_number: 0,
Command_length: 0,
Command_id: 0,
Command_status: 0,
Sequence_number: 0,
}
buf := bytes.NewBuffer(make([]byte, 16))
buf.Reset()
@@ -269,17 +269,17 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
}
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -307,17 +307,17 @@ func TestDecodeParsesValidByteSlice(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 16 {
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
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_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.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)
if p.Sequence_number != 3 {
t.Errorf("Expected sequence_number to be 3, got %d", p.Sequence_number)
}
}
@@ -343,17 +343,17 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
t.Errorf("Expected error, got none")
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
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_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.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)
if p.Sequence_number != 0 {
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
}
}
@@ -366,17 +366,17 @@ func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
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_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.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)
if p.Sequence_number != 0 {
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
}
}
@@ -408,11 +408,11 @@ func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != math.MaxUint32 {
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
if p.Command_length != math.MaxUint32 {
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.Command_length)
}
if p.command_id != math.MaxUint32 {
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
if p.Command_id != math.MaxUint32 {
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.Command_id)
}
}
@@ -425,11 +425,11 @@ func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
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_id != 0 {
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
}
}
@@ -445,10 +445,10 @@ func TestSizeReturns16(t *testing.T) {
// With buffer pool
func BenchmarkEncodeWithBufferPool(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
@@ -461,10 +461,10 @@ func BenchmarkEncodeWithBufferPool(b *testing.B) {
// Without buffer pool
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
Command_length: 16,
Command_id: 1,
Command_status: 0,
Sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {

View File

@@ -2,35 +2,33 @@ package pdu
import (
"bytes"
"encoding/ascii85"
"encoding/binary"
"fmt"
"log"
"github.com/warthog618/sms/encoding/gsm7"
"smpptester/encoding"
)
type (
SUBMIT_SM struct {
header *PDU_HEADER
service_type string
source_addr_ton byte
source_addr_npi byte
source_addr string
dest_addr_ton byte
dest_addr_npi byte
destination_addr string
esm_class byte
protocol_id byte
priority_flag byte
schedule_delivery_time string
validity_period string
registered_delivery byte
replace_if_present byte
data_coding byte
sm_default_msg_id byte
sm_length byte
short_message string
Header *PDU_HEADER
Service_type string
Source_addr_ton byte
Source_addr_npi byte
Source_addr string
Dest_addr_ton byte
Dest_addr_npi byte
Destination_addr string
Esm_class byte
Protocol_id byte
Priority_flag byte
Schedule_delivery_time string
Validity_period string
Registered_delivery byte
Replace_if_present byte
Data_coding byte
Sm_default_msg_id byte
Sm_length byte
Short_message string
// user_message_reference uint16
// source_port uint16
// source_addr_subunit byte
@@ -61,60 +59,50 @@ type (
// ussd_service_op byte
}
SUBMIT_SM_RESP struct {
header *PDU_HEADER
message_id string
Header *PDU_HEADER
Message_id string
}
SUBMIT_MULTI struct{}
SUBMIT_MULTI_RESP struct{}
)
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot encode into nil buffer")
}
// This won't do...
// TODO: Implement your own encoders and shit
// ASCII is easy
// UCS2 should also be fairly easy, use uint16 or something
// GSM7 will not be easy
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
asciiEncoder := ascii85.NewEncoder(buf)
// TODO: Implement encodings bsed on p.data_coding
messageEncoder := gsm7.NewEncoder()
messageEncoder := p.GetEncoder()
p.header.Encode(buf)
n, err := asciiEncoder.Write([]byte("OOO"))
p.Header.Encode(buf)
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
buf.WriteString(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)
buf.WriteString(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)
buf.WriteString(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)
buf.WriteString(p.Schedule_delivery_time)
buf.Write(NULL_ARR)
buf.WriteString(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)
err := messageEncoder.Encode(&p.Short_message, buf)
if err != nil {
return err
}
log.Println(n)
// 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)
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)
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)
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)
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 {
@@ -125,38 +113,42 @@ func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
}
func (p *SUBMIT_SM) Size() int {
var size int
size += p.header.Size()
size += 1 + len(p.service_type)
size += p.Header.Size()
size += 1 + len(p.Service_type)
size += 1 // source_addr_ton
size += 1 // source_addr_npi
size += 1 + len(p.source_addr)
size += 1 + len(p.Source_addr)
size += 1 // dest_addr_ton
size += 1 // dest_addr_npi
size += 1 + len(p.destination_addr)
size += 1 + len(p.Destination_addr)
size += 1 // esm_class
size += 1 // protocol_id
size += 1 // priority_flag
size += 1 + len(p.schedule_delivery_time)
size += 1 + len(p.validity_period)
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
// 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
}
size += p.GetEncoder().EncodesInto(&p.Short_message)
return size
}
func (p *SUBMIT_SM) UpdateSize() {
p.header.command_length = uint32(p.Size())
p.sm_length = byte(len(p.short_message))
p.Header.Command_length = uint32(p.Size())
p.Sm_length = byte(p.GetEncoder().EncodesInto(&p.Short_message))
}
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
switch p.Data_coding {
case 0b00000000: // GSM7
return &encoding.GSM7Coder{}
case 0b00000001: // ASCII
return &encoding.ASCIICoder{}
// case 0b00000011: // LATIN1
// return &encoding.LATIN1Coder{}
case 0b00001000: // UCS2
return &encoding.UCS2Coder{}
default:
return &encoding.ASCIICoder{}
}
}

View File

@@ -9,28 +9,28 @@ import (
// 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,
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.",
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())
@@ -39,45 +39,81 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
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}
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, 1, 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) {
//
// p.header.command_length = uint32(p.Size())
// p.sm_length = byte(len(p.short_message))
// buf := make([]byte, p.Size())
// err := p.EncodeInto(&buf)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// if len(buf) != len(expected) {
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
// }
// for i, v := range buf {
// if v != expected[i] {
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
// break
// }
// }
// }
func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(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: 0, // 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, 103, 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, 33, 210, 50, 156, 157, 7, 101, 203, 115, 16, 253, 13, 122, 195, 233, 160, 180, 27, 244, 150, 131, 156, 111, 16, 253, 13, 122, 195, 233, 160, 119, 157, 238, 2}
if !bytes.Equal(buf.Bytes(), expected) {
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
}
}
// region decode
// 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!",
Service_type: "test_service",
Source_addr: "12345",
Destination_addr: "67890",
Schedule_delivery_time: "",
Validity_period: "",
Short_message: "Hello, World!",
Data_coding: 1,
}
expectedSize := 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)
expectedSize := 68
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}
func TestCalculateSizeTypicalGSM7Instance(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!",
Data_coding: 0,
}
expectedSize := 67
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
@@ -87,14 +123,15 @@ func TestCalculateSizeTypicalInstance(t *testing.T) {
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)),
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)),
Data_coding: 1,
}
expectedSize := 16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen
expectedSize := 1563
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
@@ -103,14 +140,14 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
p := &SUBMIT_SM{
service_type: "",
source_addr: "",
destination_addr: "",
schedule_delivery_time: "",
validity_period: "",
short_message: "",
Service_type: "",
Source_addr: "",
Destination_addr: "",
Schedule_delivery_time: "",
Validity_period: "",
Short_message: "",
}
expectedSize := 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)
expectedSize := 33
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)