Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7eb7d2bfee | ||
![]() |
7849f3b000 | ||
![]() |
7b4338902b | ||
![]() |
87230a215c | ||
![]() |
0e404a85b8 | ||
![]() |
7ca400ab1d | ||
![]() |
fa7131d97a | ||
![]() |
0d86fe7c12 | ||
![]() |
974a6c6be1 | ||
![]() |
fd8a58058e | ||
0b51f373d5 | |||
deb5175f94 | |||
![]() |
6ed67e27a0 | ||
![]() |
031a0a38a4 | ||
![]() |
3fe30201c9 | ||
![]() |
1c35d35b3e |
@@ -2,7 +2,8 @@ name: Benchmark BufferPool
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- 'master'
|
- '*'
|
||||||
|
- '!benchmark'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
RunBenchmarks:
|
RunBenchmarks:
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1 @@
|
|||||||
jstester/node_modules
|
jstester/node_modules
|
||||||
encoding/tmp
|
|
||||||
pdu/tmp
|
|
||||||
|
@@ -38,7 +38,7 @@ func main() {
|
|||||||
log.Fatalf("Error creating result file %s with %v", resultFile, err)
|
log.Fatalf("Error creating result file %s with %v", resultFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("go", "test", "-bench", ".", "./...")
|
cmd := exec.Command("go", "test", "-bench", ".", "./pdu")
|
||||||
cmd.Dir = "../"
|
cmd.Dir = "../"
|
||||||
cmd.Stdout = outFile
|
cmd.Stdout = outFile
|
||||||
|
|
||||||
|
@@ -0,0 +1,12 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 13663852 90.76 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 9525358 130.7 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 16205492 74.61 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8466584 143.2 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6701893 221.0 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 7069999 162.3 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.413s
|
@@ -0,0 +1,12 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16792826 71.92 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 9800792 127.2 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 13066466 79.52 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8162733 141.6 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6148492 272.3 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 7300572 170.5 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 10.059s
|
@@ -0,0 +1,12 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 433611 2688 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 1467840 683.2 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 497097 2389 ns/op
|
||||||
|
BenchmarkEncode-6 8964351 148.5 ns/op
|
||||||
|
BenchmarkEncodeInto-6 371218869 2.987 ns/op
|
||||||
|
BenchmarkDecode-6 1000000000 0.2681 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 7.851s
|
@@ -0,0 +1,13 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 457300 2609 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 2050396 561.0 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 504205 2242 ns/op
|
||||||
|
BenchmarkEncode-6 27733428 44.06 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 27795820 42.97 ns/op
|
||||||
|
BenchmarkEncodeInto-6 397897166 3.004 ns/op
|
||||||
|
BenchmarkDecode-6 1000000000 0.2653 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 10.935s
|
@@ -0,0 +1,27 @@
|
|||||||
|
? smpptester [no test files]
|
||||||
|
? smpptester/benchmark [no test files]
|
||||||
|
? smpptester/client [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/encoding
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkGSM7EncodeSimpleASCIIString-6 19589385 67.23 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplexASCIIString-6 3757233 288.3 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIString-6 2576941 532.0 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2611428 460.3 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/encoding 6.483s
|
||||||
|
? smpptester/lua [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16908814 71.62 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 8895826 125.1 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 13243089 80.48 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8433070 139.2 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 7134577 248.2 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 6587127 166.0 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.296s
|
||||||
|
? smpptester/server [no test files]
|
@@ -0,0 +1,27 @@
|
|||||||
|
? smpptester [no test files]
|
||||||
|
? smpptester/benchmark [no test files]
|
||||||
|
? smpptester/client [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/encoding
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkGSM7EncodeSimpleASCIIString-6 14935776 76.91 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplexASCIIString-6 3364554 353.1 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIString-6 1840044 591.6 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2081725 520.4 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/encoding 6.270s
|
||||||
|
? smpptester/lua [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16804492 75.91 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 11214482 134.8 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 15180892 71.99 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8587657 139.1 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 5282655 223.4 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 6659464 170.4 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 10.264s
|
||||||
|
? smpptester/server [no test files]
|
@@ -0,0 +1,27 @@
|
|||||||
|
? smpptester [no test files]
|
||||||
|
? smpptester/benchmark [no test files]
|
||||||
|
? smpptester/client [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/encoding
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkGSM7EncodeSimpleASCIIString-6 17371128 63.88 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplexASCIIString-6 4210346 332.9 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIString-6 2748054 517.4 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2576667 469.9 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/encoding 6.462s
|
||||||
|
? smpptester/lua [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16765892 70.02 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 9238833 119.7 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 15343066 75.22 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8445075 139.0 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 5464392 274.3 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 6709528 165.3 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.739s
|
||||||
|
? smpptester/server [no test files]
|
@@ -0,0 +1,27 @@
|
|||||||
|
? smpptester [no test files]
|
||||||
|
? smpptester/benchmark [no test files]
|
||||||
|
? smpptester/client [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/encoding
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkGSM7EncodeSimpleASCIIString-6 18785672 65.00 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplexASCIIString-6 4313766 332.7 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIString-6 2686035 520.9 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2613550 461.0 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/encoding 6.548s
|
||||||
|
? smpptester/lua [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16675815 69.97 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 9622903 120.8 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 15080410 84.43 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8589709 138.9 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6965332 228.4 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 7003172 165.4 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.384s
|
||||||
|
? smpptester/server [no test files]
|
@@ -0,0 +1,12 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 15687109 91.05 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 11083287 128.9 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 16576201 74.12 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 7868800 138.1 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6833810 221.2 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 7195328 166.0 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.763s
|
@@ -0,0 +1,12 @@
|
|||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 14983336 90.65 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 10671728 124.4 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 16471220 75.06 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 8545360 139.1 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6794032 224.7 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 6737445 165.3 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 8.681s
|
@@ -0,0 +1,28 @@
|
|||||||
|
? smpptester [no test files]
|
||||||
|
? smpptester/benchmark [no test files]
|
||||||
|
? smpptester/client [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/encoding
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkGSM7EncodeSimpleASCIIString-6 17190692 63.88 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplexASCIIString-6 3987712 302.8 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIString-6 2563090 523.2 ns/op
|
||||||
|
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2615463 462.5 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/encoding 6.235s
|
||||||
|
? smpptester/lua [no test files]
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: smpptester/pdu
|
||||||
|
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
|
||||||
|
BenchmarkBufferPoolManager-6 16913668 70.22 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Concurrent-6 9289608 129.8 ns/op
|
||||||
|
BenchmarkBufferPoolManager_Memory-6 13000988 83.78 ns/op
|
||||||
|
BenchmarkEncodeWithBufferPool-6 7950153 137.7 ns/op
|
||||||
|
BenchmarkEncodeWithoutBufferPool-6 6290456 172.9 ns/op
|
||||||
|
BenchmarkDecodeBufferPool-6 7194349 162.7 ns/op
|
||||||
|
PASS
|
||||||
|
ok smpptester/pdu 7.717s
|
||||||
|
? smpptester/server [no test files]
|
||||||
|
? smpptester/utils [no test files]
|
@@ -1,51 +0,0 @@
|
|||||||
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
|
|
@@ -1,22 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type ASCIICoder struct{}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
@@ -1,74 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestASCIIEncodeSimpleASCIIString(t *testing.T) {
|
|
||||||
coder := &ASCIICoder{}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
input := "Hello, World!"
|
|
||||||
|
|
||||||
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
|
||||||
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 TestASCIIDecodeSimpleASCIIString(t *testing.T) {
|
|
||||||
coder := &ASCIICoder{}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
input := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
|
||||||
|
|
||||||
expected := "Hello, World!"
|
|
||||||
buf.Write(input)
|
|
||||||
output, 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestASCIIEncodeEmptyString(t *testing.T) {
|
|
||||||
coder := &ASCIICoder{}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
input := ""
|
|
||||||
|
|
||||||
expected := []byte{}
|
|
||||||
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 TestASCIIDecodeEmptyString(t *testing.T) {
|
|
||||||
coder := &ASCIICoder{}
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
|
||||||
|
|
||||||
expected := ""
|
|
||||||
output, 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)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"ucs2": {
|
|
||||||
"link": "http://www.columbia.edu/kermit/ucs2.html"
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,10 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type Coder interface {
|
|
||||||
Encode(s *string, buf *bytes.Buffer) error
|
|
||||||
Decode(buf *bytes.Buffer) (string, error)
|
|
||||||
EncodesInto(s *string) int
|
|
||||||
DecodesInto(buf *bytes.Buffer) int
|
|
||||||
}
|
|
136
encoding/gsm7.go
136
encoding/gsm7.go
@@ -1,136 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GSM7Coder struct{}
|
|
||||||
|
|
||||||
// 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 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)
|
|
||||||
}
|
|
||||||
if index == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
bitshift++
|
|
||||||
// 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) {
|
|
||||||
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
|
|
||||||
}
|
|
@@ -1,369 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"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 := short8nString
|
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGSM7EncodeComplexASCIIString(t *testing.T) {
|
|
||||||
coder := &GSM7Coder{}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
input := longNot8nString
|
|
||||||
|
|
||||||
expected := longNot8nStringEncodedBytes
|
|
||||||
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 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGSM7EncodeEmptyString(t *testing.T) {
|
|
||||||
coder := &GSM7Coder{}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
input := ""
|
|
||||||
|
|
||||||
expected := []byte{}
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGSM7DecodeEmptyString(t *testing.T) {
|
|
||||||
coder := &GSM7Coder{}
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
|
||||||
|
|
||||||
expected := ""
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
package encoding
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type UCS2Coder struct{}
|
|
||||||
|
|
||||||
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) EncodesInto(s *string) int {
|
|
||||||
return len(*s) * 2
|
|
||||||
}
|
|
||||||
func (c UCS2Coder) DecodesInto(buf *bytes.Buffer) int {
|
|
||||||
return buf.Len() / 2
|
|
||||||
}
|
|
58
main.go
58
main.go
@@ -6,12 +6,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"smpptester/pdu"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
log.SetFlags(log.Lmicroseconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to create a submit_sm PDU
|
// Function to create a submit_sm PDU
|
||||||
@@ -31,7 +29,7 @@ func createSubmitSMPDU() []byte {
|
|||||||
validityPeriod := ""
|
validityPeriod := ""
|
||||||
registeredDelivery := byte(0x01)
|
registeredDelivery := byte(0x01)
|
||||||
replaceIfPresentFlag := byte(0x00)
|
replaceIfPresentFlag := byte(0x00)
|
||||||
dataCoding := byte(0x01)
|
dataCoding := byte(0x00)
|
||||||
smDefaultMsgID := byte(0x00)
|
smDefaultMsgID := byte(0x00)
|
||||||
shortMessage := "Hello, SMPP!"
|
shortMessage := "Hello, SMPP!"
|
||||||
|
|
||||||
@@ -39,6 +37,7 @@ func createSubmitSMPDU() []byte {
|
|||||||
headerLength := 16
|
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)
|
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
|
totalLength := headerLength + bodyLength
|
||||||
|
log.Printf("Total length: %d", totalLength)
|
||||||
|
|
||||||
// Create a buffer to hold the PDU
|
// Create a buffer to hold the PDU
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
@@ -137,55 +136,6 @@ func main() {
|
|||||||
// go handleConnection(conn)
|
// 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 := &sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@@ -199,7 +149,7 @@ func main() {
|
|||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
conn.Write(buf.Bytes())
|
conn.Write(createSubmitSMPDU())
|
||||||
data, err := conn.Read(make([]byte, 1024))
|
data, err := conn.Read(make([]byte, 1024))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to read from connection: %+v", err)
|
log.Printf("Failed to read from connection: %+v", err)
|
||||||
|
@@ -1,51 +0,0 @@
|
|||||||
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
|
|
26
pdu/bind.go
26
pdu/bind.go
@@ -2,19 +2,19 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
BIND struct {
|
BIND struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
System_id string
|
system_id string
|
||||||
Password string
|
password string
|
||||||
System_type string
|
system_type string
|
||||||
Interface_version byte
|
interface_version uint8
|
||||||
Addr_ton byte
|
addr_ton uint8
|
||||||
Addr_npi byte
|
addr_npi uint8
|
||||||
Address_range string
|
address_range string
|
||||||
}
|
}
|
||||||
BIND_RESP struct {
|
BIND_RESP struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
System_id string
|
system_id string
|
||||||
Sc_interface_version byte
|
sc_interface_version uint8
|
||||||
}
|
}
|
||||||
BIND_RECVEIVER struct {
|
BIND_RECVEIVER struct {
|
||||||
BIND
|
BIND
|
||||||
@@ -36,9 +36,9 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
UNBIND struct {
|
UNBIND struct {
|
||||||
Header *PDU_HEADER
|
PDU_HEADER
|
||||||
}
|
}
|
||||||
UNBIND_RESP struct {
|
UNBIND_RESP struct {
|
||||||
Header *PDU_HEADER
|
PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -1,30 +1,21 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"log"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BufferPoolManager struct {
|
type BufferPoolManager struct {
|
||||||
pools map[int]*sync.Pool
|
pools map[uint]*sync.Pool
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
debug bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bpm *BufferPoolManager) logf(format string, args ...interface{}) {
|
|
||||||
if bpm.debug {
|
|
||||||
log.Printf(format, args...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBufferPoolManager() *BufferPoolManager {
|
func NewBufferPoolManager() *BufferPoolManager {
|
||||||
return &BufferPoolManager{
|
return &BufferPoolManager{
|
||||||
pools: make(map[int]*sync.Pool),
|
pools: make(map[uint]*sync.Pool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
|
func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
|
||||||
bpm.mu.RLock()
|
bpm.mu.RLock()
|
||||||
pool, exists := bpm.pools[size]
|
pool, exists := bpm.pools[size]
|
||||||
bpm.mu.RUnlock()
|
bpm.mu.RUnlock()
|
||||||
@@ -34,13 +25,10 @@ func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
|
|||||||
// Double-check if another goroutine added the pool while we were waiting
|
// Double-check if another goroutine added the pool while we were waiting
|
||||||
pool, exists = bpm.pools[size]
|
pool, exists = bpm.pools[size]
|
||||||
if !exists {
|
if !exists {
|
||||||
bpm.logf("Creating new pool for size %d\n", size)
|
|
||||||
pool = &sync.Pool{
|
pool = &sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
bpm.logf("Creating new buffer of size %d\n", size)
|
buf := make([]uint8, size)
|
||||||
buf := bytes.NewBuffer(make([]byte, size))
|
return &buf
|
||||||
buf.Reset()
|
|
||||||
return buf
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
bpm.pools[size] = pool
|
bpm.pools[size] = pool
|
||||||
@@ -48,23 +36,23 @@ func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
|
|||||||
bpm.mu.Unlock()
|
bpm.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := pool.Get().(*bytes.Buffer)
|
return pool.Get().(*[]uint8)
|
||||||
bpm.logf("Returning buffer of size %d: %p\n", buf.Cap(), buf)
|
|
||||||
return buf
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bpm *BufferPoolManager) Put(buf *bytes.Buffer) {
|
func (bpm *BufferPoolManager) Put(buf *[]uint8) {
|
||||||
size := buf.Cap()
|
size := uint(len(*buf))
|
||||||
bpm.mu.RLock()
|
bpm.mu.RLock()
|
||||||
pool, exists := bpm.pools[size]
|
pool, exists := bpm.pools[size]
|
||||||
bpm.mu.RUnlock()
|
bpm.mu.RUnlock()
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
bpm.logf("Cannot return %p, No pool for size %d\n", buf, size)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.Reset()
|
// Clear buffer
|
||||||
|
for i := range *buf {
|
||||||
|
(*buf)[i] = 0
|
||||||
|
}
|
||||||
|
|
||||||
pool.Put(buf)
|
pool.Put(buf)
|
||||||
bpm.logf("Returned buffer of size %d: %p\n", size, buf)
|
|
||||||
}
|
}
|
||||||
|
@@ -10,14 +10,14 @@ func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
|||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 1024
|
size := 1024
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer, got nil")
|
t.Fatalf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,14 +25,14 @@ func TestRequestBufferSizeZero(t *testing.T) {
|
|||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 0
|
size := 0
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer, got nil")
|
t.Fatalf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,12 +47,12 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Errorf("Expected buffer, got nil")
|
t.Errorf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -64,14 +64,14 @@ func TestGetBufferLockUnlock(t *testing.T) {
|
|||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 1024
|
size := 1024
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer, got nil")
|
t.Fatalf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,14 +79,14 @@ func TestVerifyPoolCreationForNewSizes(t *testing.T) {
|
|||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 512
|
size := 512
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer, got nil")
|
t.Fatalf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,14 +94,14 @@ func TestBufferPoolManagerGetBuffer(t *testing.T) {
|
|||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 1024
|
size := 1024
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer, got nil")
|
t.Fatalf("Expected buffer, got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,14 +110,14 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
|
|||||||
|
|
||||||
sizes := []int{512, 1024, 2048}
|
sizes := []int{512, 1024, 2048}
|
||||||
for _, size := range sizes {
|
for _, size := range sizes {
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(uint(size))
|
||||||
|
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
t.Fatalf("Expected buffer for size %d, got nil", size)
|
t.Fatalf("Expected buffer for size %d, got nil", size)
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if len(*buffer) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
|
|||||||
func TestGetBufferIsAlwaysZero(t *testing.T) {
|
func TestGetBufferIsAlwaysZero(t *testing.T) {
|
||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
|
|
||||||
size := 1024 * 64
|
var size uint = 1024 * 64
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
buffer := bpm.Get(size)
|
buffer := bpm.Get(size)
|
||||||
|
|
||||||
@@ -133,11 +133,11 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
|
|||||||
t.Fatalf("Expected buffer for size %d, got nil", size)
|
t.Fatalf("Expected buffer for size %d, got nil", size)
|
||||||
}
|
}
|
||||||
|
|
||||||
if buffer.Cap() != size {
|
if uint(len(*buffer)) != size {
|
||||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, b := range buffer.Bytes() {
|
for _, b := range *buffer {
|
||||||
if b != 0 {
|
if b != 0 {
|
||||||
t.Errorf("Expected buffer to be zero, got %d", b)
|
t.Errorf("Expected buffer to be zero, got %d", b)
|
||||||
}
|
}
|
||||||
@@ -147,26 +147,9 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPoolReusesBuffers(t *testing.T) {
|
|
||||||
bpm := NewBufferPoolManager()
|
|
||||||
|
|
||||||
size := 1024
|
|
||||||
buffer := bpm.Get(size)
|
|
||||||
initialPtr := buffer
|
|
||||||
bpm.Put(buffer)
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
buffer = bpm.Get(size)
|
|
||||||
if buffer != initialPtr {
|
|
||||||
t.Errorf("Expected initial buffer (%p) to be reused, got %p", initialPtr, buffer)
|
|
||||||
}
|
|
||||||
bpm.Put(buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// region benchmark
|
|
||||||
func BenchmarkBufferPoolManager(b *testing.B) {
|
func BenchmarkBufferPoolManager(b *testing.B) {
|
||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is way too large
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
@@ -180,7 +163,7 @@ func BenchmarkBufferPoolManager(b *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
@@ -203,17 +186,17 @@ func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkBufferPoolManager_Memory(b *testing.B) {
|
func BenchmarkBufferPoolManager_Memory(b *testing.B) {
|
||||||
bpm := NewBufferPoolManager()
|
bpm := NewBufferPoolManager()
|
||||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
var i byte
|
var i uint8
|
||||||
buf := bpm.Get(bufSize)
|
buf := bpm.Get(bufSize)
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
|
|
||||||
// Simulate some work
|
// Simulate some work
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
for range buf.Bytes() {
|
for k := range *buf {
|
||||||
buf.WriteByte(i % 255)
|
(*buf)[k] = i % 255
|
||||||
}
|
}
|
||||||
|
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
@@ -2,17 +2,17 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
CANCEL_SM struct {
|
CANCEL_SM struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
Service_type string
|
service_type string
|
||||||
Message_id string
|
message_id string
|
||||||
Source_addr_ton byte
|
source_addr_ton uint8
|
||||||
Source_addr_npi byte
|
source_addr_npi uint8
|
||||||
Source_addr string
|
source_addr string
|
||||||
Dest_addr_ton byte
|
dest_addr_ton uint8
|
||||||
Dest_addr_npi byte
|
dest_addr_npi uint8
|
||||||
Destination_addr string
|
destination_addr string
|
||||||
}
|
}
|
||||||
CANCEL_SM_RESP struct {
|
CANCEL_SM_RESP struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -2,11 +2,11 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
DELIVER_SM struct {
|
DELIVER_SM struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
SUBMIT_SM
|
SUBMIT_SM
|
||||||
}
|
}
|
||||||
DELIVER_SM_RESP struct {
|
DELIVER_SM_RESP struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
SUBMIT_SM_RESP
|
SUBMIT_SM_RESP
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -2,9 +2,9 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
ENQUIRE_LINK struct {
|
ENQUIRE_LINK struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
}
|
}
|
||||||
ENQUIRE_LINK_RESP struct {
|
ENQUIRE_LINK_RESP struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -1,5 +1,3 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
var ByteBufferPool = NewBufferPoolManager()
|
var ByteBufferPool = NewBufferPoolManager()
|
||||||
const NULL = byte(0x00)
|
|
||||||
var NULL_ARR = []byte{NULL}
|
|
65
pdu/pdu.go
65
pdu/pdu.go
@@ -1,29 +1,28 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
PDU interface {
|
PDU interface {
|
||||||
Encode(*bytes.Buffer) error
|
EncodeInto(*[]uint8)
|
||||||
Decode(*bytes.Buffer) error
|
Encode() []uint8
|
||||||
|
Decode([]uint8)
|
||||||
// Size in bytes
|
// Size in bytes
|
||||||
Size() int
|
Size() uint32
|
||||||
UpdateSize()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PDU_HEADER struct {
|
PDU_HEADER struct {
|
||||||
Command_length uint32
|
command_length uint32
|
||||||
Command_id uint32
|
command_id uint32
|
||||||
Command_status uint32
|
command_status uint32
|
||||||
Sequence_number uint32
|
sequence_number uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
GENERIC_NACK struct {
|
GENERIC_NACK struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,29 +57,41 @@ type (
|
|||||||
// What are the other 0s?
|
// What are the other 0s?
|
||||||
// Don't know
|
// Don't know
|
||||||
|
|
||||||
func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
|
func (p *PDU_HEADER) Encode() (*[]uint8, error) {
|
||||||
|
buf := ByteBufferPool.Get(uint(p.Size()))
|
||||||
|
err := p.EncodeInto(buf)
|
||||||
|
return buf, err
|
||||||
|
}
|
||||||
|
func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot encode into nil buffer")
|
return fmt.Errorf("cannot encode PDU_HEADER, buffer is nil")
|
||||||
}
|
}
|
||||||
binary.Write(buf, binary.BigEndian, p.Command_length)
|
if len(*buf) < 16 {
|
||||||
binary.Write(buf, binary.BigEndian, p.Command_id)
|
return fmt.Errorf("cannot encode PDU_HEADER, buffer too small (%d, required 16)", len(*buf))
|
||||||
binary.Write(buf, binary.BigEndian, p.Command_status)
|
}
|
||||||
binary.Write(buf, binary.BigEndian, p.Sequence_number)
|
bufVal := *buf
|
||||||
|
binary.BigEndian.PutUint32(bufVal[0:4], p.command_length)
|
||||||
|
binary.BigEndian.PutUint32(bufVal[4:8], p.command_id)
|
||||||
|
binary.BigEndian.PutUint32(bufVal[8:12], p.command_status)
|
||||||
|
binary.BigEndian.PutUint32(bufVal[12:16], p.sequence_number)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
func (p *PDU_HEADER) Decode(data []uint8) error {
|
||||||
if buf == nil {
|
if len(data) >= 4 {
|
||||||
return fmt.Errorf("cannot decode nil buffer")
|
p.command_length = binary.BigEndian.Uint32(data[0:4])
|
||||||
|
}
|
||||||
|
if len(data) >= 8 {
|
||||||
|
p.command_id = binary.BigEndian.Uint32(data[4:8])
|
||||||
|
}
|
||||||
|
if len(data) >= 12 {
|
||||||
|
p.command_status = binary.BigEndian.Uint32(data[8:12])
|
||||||
|
|
||||||
|
}
|
||||||
|
if len(data) >= 16 {
|
||||||
|
p.sequence_number = binary.BigEndian.Uint32(data[12:16])
|
||||||
}
|
}
|
||||||
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
|
return nil
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) Size() int {
|
func (p *PDU_HEADER) Size() uint32 {
|
||||||
return 16
|
return 16
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) UpdateSize() {
|
|
||||||
p.Command_length = uint32(p.Size())
|
|
||||||
}
|
|
||||||
|
420
pdu/pdu_test.go
420
pdu/pdu_test.go
@@ -1,7 +1,6 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -11,41 +10,37 @@ import (
|
|||||||
// region encode
|
// region encode
|
||||||
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 1,
|
command_length: 1,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 1,
|
command_status: 1,
|
||||||
Sequence_number: 1,
|
sequence_number: 1,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
result, err := p.Encode()
|
||||||
buf.Reset()
|
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
if buf.Cap() != 16 {
|
if len(*result) != 16 {
|
||||||
t.Errorf("Expected byte slice of length 16, got %d", buf.Cap())
|
t.Errorf("Expected byte slice of length 16, got %d", len(*result))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeHandlesZeroValues(t *testing.T) {
|
func TestEncodeHandlesZeroValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 0,
|
command_length: 0,
|
||||||
Command_id: 0,
|
command_id: 0,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 0,
|
sequence_number: 0,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
result, err := p.Encode()
|
||||||
buf.Reset()
|
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
expected := make([]byte, 16)
|
expected := make([]uint8, 16)
|
||||||
for i, v := range buf.Bytes() {
|
for i, v := range *result {
|
||||||
if v != expected[i] {
|
if v != expected[i] {
|
||||||
t.Errorf("Expected byte slice with zero values, got %v", buf.Bytes())
|
t.Errorf("Expected byte slice with zero values, got %v", *result)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,23 +48,21 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeEncodesProperly(t *testing.T) {
|
func TestEncodeEncodesProperly(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 1,
|
command_length: 1,
|
||||||
Command_id: 2,
|
command_id: 2,
|
||||||
Command_status: 3,
|
command_status: 3,
|
||||||
Sequence_number: 4,
|
sequence_number: 4,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
result, err := p.Encode()
|
||||||
buf.Reset()
|
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
|
expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
|
||||||
for i, v := range buf.Bytes() {
|
for i, v := range *result {
|
||||||
if v != expected[i] {
|
if v != expected[i] {
|
||||||
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
|
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,23 +70,21 @@ func TestEncodeEncodesProperly(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 13426724,
|
command_length: 13426724,
|
||||||
Command_id: 254352,
|
command_id: 254352,
|
||||||
Command_status: 35634264,
|
command_status: 35634264,
|
||||||
Sequence_number: 476543523,
|
sequence_number: 476543523,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
result, err := p.Encode()
|
||||||
buf.Reset()
|
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []byte{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
|
expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
|
||||||
for i, v := range buf.Bytes() {
|
for i, v := range *result {
|
||||||
if v != expected[i] {
|
if v != expected[i] {
|
||||||
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
|
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,198 +92,217 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := make([]uint8, 16)
|
||||||
buf.Reset()
|
err := p.EncodeInto(&buf)
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeIntoHandlesNilBuffer(t *testing.T) {
|
||||||
|
p := &PDU_HEADER{
|
||||||
|
command_length: 16,
|
||||||
|
command_id: 1,
|
||||||
|
command_status: 0,
|
||||||
|
sequence_number: 12345,
|
||||||
|
}
|
||||||
|
var buf *[]uint8 = nil
|
||||||
|
|
||||||
|
err := p.EncodeInto(buf)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error when buffer is nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeIntoHandlesSmallerBuffer(t *testing.T) {
|
||||||
|
p := &PDU_HEADER{
|
||||||
|
command_length: 16,
|
||||||
|
command_id: 1,
|
||||||
|
command_status: 0,
|
||||||
|
sequence_number: 12345,
|
||||||
|
}
|
||||||
|
buf := make([]uint8, 12) // smaller buffer size
|
||||||
|
|
||||||
|
err := p.EncodeInto(&buf)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error when buffer is too small")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
|
buf := make([]uint8, 20)
|
||||||
buf.Reset()
|
err := p.EncodeInto(&buf)
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := make([]uint8, 16)
|
||||||
buf.Reset()
|
err := p.EncodeInto(&buf)
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := make([]uint8, 16)
|
||||||
buf.Reset()
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
p.Encode(buf)
|
p.EncodeInto(&buf)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: math.MaxUint32,
|
command_length: math.MaxUint32,
|
||||||
Command_id: math.MaxUint32,
|
command_id: math.MaxUint32,
|
||||||
Command_status: math.MaxUint32,
|
command_status: math.MaxUint32,
|
||||||
Sequence_number: math.MaxUint32,
|
sequence_number: math.MaxUint32,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := make([]uint8, 16)
|
||||||
buf.Reset()
|
err := p.EncodeInto(&buf)
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 0,
|
command_length: 0,
|
||||||
Command_id: 0,
|
command_id: 0,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 0,
|
sequence_number: 0,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := make([]uint8, 16)
|
||||||
buf.Reset()
|
err := p.EncodeInto(&buf)
|
||||||
err := p.Encode(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||||
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 {
|
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// region decode
|
// region decode
|
||||||
func TestDecodeHandlesShortByteSlice(t *testing.T) {
|
func TestDecodeHandlesShortByteSlice(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{0, 0, 0, 10}
|
data := []uint8{0, 0, 0, 10}
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
t.Errorf("Decode panicked with short byte slice")
|
t.Errorf("Decode panicked with short byte slice")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
@@ -300,36 +310,36 @@ func TestDecodeHandlesShortByteSlice(t *testing.T) {
|
|||||||
|
|
||||||
func TestDecodeParsesValidByteSlice(t *testing.T) {
|
func TestDecodeParsesValidByteSlice(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Command_length != 16 {
|
if p.command_length != 16 {
|
||||||
t.Errorf("Expected command_length to be 16, got %d", p.Command_length)
|
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
|
||||||
}
|
}
|
||||||
if p.Command_id != 1 {
|
if p.command_id != 1 {
|
||||||
t.Errorf("Expected command_id to be 1, got %d", p.Command_id)
|
t.Errorf("Expected command_id to be 1, got %d", p.command_id)
|
||||||
}
|
}
|
||||||
if p.Command_status != 2 {
|
if p.command_status != 2 {
|
||||||
t.Errorf("Expected command_status to be 2, got %d", p.Command_status)
|
t.Errorf("Expected command_status to be 2, got %d", p.command_status)
|
||||||
}
|
}
|
||||||
if p.Sequence_number != 3 {
|
if p.sequence_number != 3 {
|
||||||
t.Errorf("Expected sequence_number to be 3, got %d", p.Sequence_number)
|
t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
|
func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := make([]byte, 20)
|
data := make([]uint8, 20)
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
t.Errorf("Decode panicked with long byte slice")
|
t.Errorf("Decode panicked with long byte slice")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
@@ -339,54 +349,54 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
|
|||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
err := p.Decode(nil)
|
err := p.Decode(nil)
|
||||||
|
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Errorf("Expected error, got none")
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Command_length != 0 {
|
if p.command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||||
}
|
}
|
||||||
if p.Command_id != 0 {
|
if p.command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||||
}
|
}
|
||||||
if p.Command_status != 0 {
|
if p.command_status != 0 {
|
||||||
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
||||||
}
|
}
|
||||||
if p.Sequence_number != 0 {
|
if p.sequence_number != 0 {
|
||||||
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
|
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{}
|
data := []uint8{}
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Command_length != 0 {
|
if p.command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||||
}
|
}
|
||||||
if p.Command_id != 0 {
|
if p.command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||||
}
|
}
|
||||||
if p.Command_status != 0 {
|
if p.command_status != 0 {
|
||||||
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
||||||
}
|
}
|
||||||
if p.Sequence_number != 0 {
|
if p.sequence_number != 0 {
|
||||||
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
|
func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||||
originalData := make([]byte, len(data))
|
originalData := make([]uint8, len(data))
|
||||||
copy(originalData, data)
|
copy(originalData, data)
|
||||||
|
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
@@ -401,35 +411,35 @@ func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
|
|||||||
|
|
||||||
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
|
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{255, 255, 255, 255, 255, 255, 255, 255}
|
data := []uint8{255, 255, 255, 255, 255, 255, 255, 255}
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Command_length != math.MaxUint32 {
|
if p.command_length != math.MaxUint32 {
|
||||||
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.Command_length)
|
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
|
||||||
}
|
}
|
||||||
if p.Command_id != math.MaxUint32 {
|
if p.command_id != math.MaxUint32 {
|
||||||
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.Command_id)
|
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
|
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
|
||||||
var p PDU_HEADER
|
var p PDU_HEADER
|
||||||
data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
|
data := []uint8{0, 0, 0, 0, 0, 0, 0, 0}
|
||||||
err := p.Decode(bytes.NewBuffer(data))
|
err := p.Decode(data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Command_length != 0 {
|
if p.command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||||
}
|
}
|
||||||
if p.Command_id != 0 {
|
if p.command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,45 +452,41 @@ func TestSizeReturns16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// region benchmarks
|
// region benchmarks
|
||||||
|
|
||||||
// With buffer pool
|
// With buffer pool
|
||||||
func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
func BenchmarkEncode(b *testing.B) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
buf := ByteBufferPool.Get(p.Size())
|
p.Encode()
|
||||||
p.Encode(buf)
|
|
||||||
ByteBufferPool.Put(buf)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Without buffer pool
|
// Without buffer pool
|
||||||
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
|
func BenchmarkEncodeInto(b *testing.B) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
Command_length: 16,
|
command_length: 16,
|
||||||
Command_id: 1,
|
command_id: 1,
|
||||||
Command_status: 0,
|
command_status: 0,
|
||||||
Sequence_number: 12345,
|
sequence_number: 12345,
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
buf := bytes.Buffer{}
|
buf := make([]uint8, 16)
|
||||||
p.Encode(&buf)
|
p.EncodeInto(&buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDecodeBufferPool(b *testing.B) {
|
func BenchmarkDecode(b *testing.B) {
|
||||||
p := &PDU_HEADER{}
|
p := &PDU_HEADER{}
|
||||||
|
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
p.Decode(data)
|
||||||
buf := ByteBufferPool.Get(len(data))
|
|
||||||
buf.Write(data)
|
|
||||||
p.Decode(buf)
|
|
||||||
ByteBufferPool.Put(buf)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
176
pdu/submit.go
176
pdu/submit.go
@@ -1,154 +1,104 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"smpptester/encoding"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
SUBMIT_SM struct {
|
SUBMIT_SM struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
Service_type string
|
service_type string
|
||||||
Source_addr_ton byte
|
source_addr_ton uint8
|
||||||
Source_addr_npi byte
|
source_addr_npi uint8
|
||||||
Source_addr string
|
source_addr string
|
||||||
Dest_addr_ton byte
|
dest_addr_ton uint8
|
||||||
Dest_addr_npi byte
|
dest_addr_npi uint8
|
||||||
Destination_addr string
|
destination_addr string
|
||||||
Esm_class byte
|
esm_class uint8
|
||||||
Protocol_id byte
|
protocol_id uint8
|
||||||
Priority_flag byte
|
priority_flag uint8
|
||||||
Schedule_delivery_time string
|
schedule_delivery_time string
|
||||||
Validity_period string
|
validity_period string
|
||||||
Registered_delivery byte
|
registered_delivery uint8
|
||||||
Replace_if_present byte
|
replace_if_present uint8
|
||||||
Data_coding byte
|
data_coding uint8
|
||||||
Sm_default_msg_id byte
|
sm_default_msg_id uint8
|
||||||
Sm_length byte
|
sm_length uint8
|
||||||
Short_message string
|
short_message string
|
||||||
// user_message_reference uint16
|
// user_message_reference uint16
|
||||||
// source_port uint16
|
// source_port uint16
|
||||||
// source_addr_subunit byte
|
// source_addr_subunit uint8
|
||||||
// destination_port uint16
|
// destination_port uint16
|
||||||
// dest_addr_subunit byte
|
// dest_addr_subunit uint8
|
||||||
// sar_msg_ref_num uint16
|
// sar_msg_ref_num uint16
|
||||||
// sar_total_segments byte
|
// sar_total_segments uint8
|
||||||
// sar_segment_seqnum byte
|
// sar_segment_seqnum uint8
|
||||||
// more_messages_to_send byte
|
// more_messages_to_send uint8
|
||||||
// payload_type byte
|
// payload_type uint8
|
||||||
// message_payload string
|
// message_payload string
|
||||||
// privacy_indicator byte
|
// privacy_indicator uint8
|
||||||
// callback_num string
|
// callback_num string
|
||||||
// callback_num_pres byte
|
// callback_num_pres uint8
|
||||||
// callback_num_atag string
|
// callback_num_atag string
|
||||||
// source_subaddress string
|
// source_subaddress string
|
||||||
// dest_subaddress string
|
// dest_subaddress string
|
||||||
// user_response_code byte
|
// user_response_code uint8
|
||||||
// display_time byte
|
// display_time uint8
|
||||||
// sms_signal byte
|
// sms_signal uint8
|
||||||
// ms_validity byte
|
// ms_validity uint8
|
||||||
// ms_msg_wait_facilities byte
|
// ms_msg_wait_facilities uint8
|
||||||
// number_of_messages byte
|
// number_of_messages uint8
|
||||||
// alert_on_message_delivery byte
|
// alert_on_message_delivery uint8
|
||||||
// language_indicator byte
|
// language_indicator uint8
|
||||||
// its_reply_type byte
|
// its_reply_type uint8
|
||||||
// its_session_info byte
|
// its_session_info uint8
|
||||||
// ussd_service_op byte
|
// ussd_service_op uint8
|
||||||
}
|
}
|
||||||
SUBMIT_SM_RESP struct {
|
SUBMIT_SM_RESP struct {
|
||||||
Header *PDU_HEADER
|
header PDU_HEADER
|
||||||
Message_id string
|
message_id string
|
||||||
}
|
}
|
||||||
SUBMIT_MULTI struct{}
|
SUBMIT_MULTI struct{}
|
||||||
SUBMIT_MULTI_RESP struct{}
|
SUBMIT_MULTI_RESP struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
func (p *SUBMIT_SM) Encode() (*[]byte, error) {
|
||||||
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
buf := ByteBufferPool.Get(uint(p.Size()))
|
||||||
|
err := p.EncodeInto(buf)
|
||||||
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
return buf, err
|
||||||
|
}
|
||||||
|
func (p *SUBMIT_SM) EncodeInto(buf *[]byte) error {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot encode into nil buffer")
|
return fmt.Errorf("cannot encode SUBMIT_SM, buffer is nil")
|
||||||
|
}
|
||||||
|
if len(*buf) < int(p.Size()) {
|
||||||
|
return fmt.Errorf("cannot encode SUBMIT_SM, buffer too small (%d, required %d)", len(*buf), p.Size())
|
||||||
}
|
}
|
||||||
messageEncoder := p.GetEncoder()
|
|
||||||
|
|
||||||
p.Header.Encode(buf)
|
p.header.EncodeInto(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
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
func (p *SUBMIT_SM) Decode(data []byte) {
|
||||||
if buf == nil {
|
|
||||||
return fmt.Errorf("cannot decode nil buffer")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) Size() int {
|
func (p *SUBMIT_SM) Size() uint32 {
|
||||||
var size int
|
var size uint32
|
||||||
size += p.Header.Size()
|
size += p.header.Size()
|
||||||
size += 1 + len(p.Service_type)
|
size += uint32(len(p.service_type) * 1)
|
||||||
size += 1 // source_addr_ton
|
size += 1 // source_addr_ton
|
||||||
size += 1 // source_addr_npi
|
size += 1 // source_addr_npi
|
||||||
size += 1 + len(p.Source_addr)
|
size += uint32(len(p.source_addr) * 1)
|
||||||
size += 1 // dest_addr_ton
|
size += 1 // dest_addr_ton
|
||||||
size += 1 // dest_addr_npi
|
size += 1 // dest_addr_npi
|
||||||
size += 1 + len(p.Destination_addr)
|
size += uint32(len(p.destination_addr) * 1)
|
||||||
size += 1 // esm_class
|
size += 1 // esm_class
|
||||||
size += 1 // protocol_id
|
size += 1 // protocol_id
|
||||||
size += 1 // priority_flag
|
size += 1 // priority_flag
|
||||||
size += 1 + len(p.Schedule_delivery_time)
|
size += uint32(len(p.schedule_delivery_time) * 1)
|
||||||
size += 1 + len(p.Validity_period)
|
size += uint32(len(p.validity_period) * 1)
|
||||||
size += 1 // registered_delivery
|
size += 1 // registered_delivery
|
||||||
size += 1 // replace_if_present
|
size += 1 // replace_if_present
|
||||||
size += 1 // data_coding
|
size += 1 // data_coding
|
||||||
size += 1 // sm_default_msg_id
|
size += 1 // sm_default_msg_id
|
||||||
size += 1 // sm_length
|
size += 1 // sm_length
|
||||||
size += p.GetEncoder().EncodesInto(&p.Short_message)
|
size += uint32(len(p.short_message) * 1)
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) UpdateSize() {
|
|
||||||
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{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -1,119 +1,20 @@
|
|||||||
package pdu
|
package pdu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"testing"
|
"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, 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 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
|
// region size
|
||||||
func TestCalculateSizeTypicalInstance(t *testing.T) {
|
func TestCalculateSizeTypicalInstance(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
Service_type: "test_service",
|
service_type: "test_service",
|
||||||
Source_addr: "12345",
|
source_addr: "12345",
|
||||||
Destination_addr: "67890",
|
destination_addr: "67890",
|
||||||
Schedule_delivery_time: "",
|
schedule_delivery_time: "",
|
||||||
Validity_period: "",
|
validity_period: "",
|
||||||
Short_message: "Hello, World!",
|
short_message: "Hello, World!",
|
||||||
Data_coding: 1,
|
|
||||||
}
|
}
|
||||||
expectedSize := 68
|
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 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()
|
actualSize := p.Size()
|
||||||
if actualSize != expectedSize {
|
if actualSize != expectedSize {
|
||||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||||
@@ -123,15 +24,14 @@ func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
|
|||||||
func TestCalculateSizeMaxLengths(t *testing.T) {
|
func TestCalculateSizeMaxLengths(t *testing.T) {
|
||||||
maxLen := 255
|
maxLen := 255
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
Service_type: string(make([]byte, maxLen)),
|
service_type: string(make([]byte, maxLen)),
|
||||||
Source_addr: string(make([]byte, maxLen)),
|
source_addr: string(make([]byte, maxLen)),
|
||||||
Destination_addr: string(make([]byte, maxLen)),
|
destination_addr: string(make([]byte, maxLen)),
|
||||||
Schedule_delivery_time: string(make([]byte, maxLen)),
|
schedule_delivery_time: string(make([]byte, maxLen)),
|
||||||
Validity_period: string(make([]byte, maxLen)),
|
validity_period: string(make([]byte, maxLen)),
|
||||||
Short_message: string(make([]byte, maxLen)),
|
short_message: string(make([]byte, maxLen)),
|
||||||
Data_coding: 1,
|
|
||||||
}
|
}
|
||||||
expectedSize := 1563
|
expectedSize := uint32(16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen)
|
||||||
actualSize := p.Size()
|
actualSize := p.Size()
|
||||||
if actualSize != expectedSize {
|
if actualSize != expectedSize {
|
||||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||||
@@ -140,18 +40,16 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
|
|||||||
|
|
||||||
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
Service_type: "",
|
service_type: "",
|
||||||
Source_addr: "",
|
source_addr: "",
|
||||||
Destination_addr: "",
|
destination_addr: "",
|
||||||
Schedule_delivery_time: "",
|
schedule_delivery_time: "",
|
||||||
Validity_period: "",
|
validity_period: "",
|
||||||
Short_message: "",
|
short_message: "",
|
||||||
}
|
}
|
||||||
expectedSize := 33
|
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()
|
actualSize := p.Size()
|
||||||
if actualSize != expectedSize {
|
if actualSize != expectedSize {
|
||||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// region benchmark
|
|
||||||
|
Reference in New Issue
Block a user