Export all fields in pdu

Oopsie!
This commit is contained in:
2024-07-31 15:10:46 +02:00
parent 4771ffa154
commit f1eb6b065d
8 changed files with 290 additions and 289 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -10,25 +10,25 @@ import (
type (
SUBMIT_SM struct {
header *PDU_HEADER
service_type string
source_addr_ton byte
source_addr_npi byte
source_addr string
dest_addr_ton byte
dest_addr_npi byte
destination_addr string
esm_class byte
protocol_id byte
priority_flag byte
schedule_delivery_time string
validity_period string
registered_delivery byte
replace_if_present byte
data_coding byte
sm_default_msg_id byte
sm_length byte
short_message string
Header *PDU_HEADER
Service_type string
Source_addr_ton byte
Source_addr_npi byte
Source_addr string
Dest_addr_ton byte
Dest_addr_npi byte
Destination_addr string
Esm_class byte
Protocol_id byte
Priority_flag byte
Schedule_delivery_time string
Validity_period string
Registered_delivery byte
Replace_if_present byte
Data_coding byte
Sm_default_msg_id byte
Sm_length byte
Short_message string
// user_message_reference uint16
// source_port uint16
// source_addr_subunit byte
@@ -59,12 +59,13 @@ type (
// ussd_service_op byte
}
SUBMIT_SM_RESP struct {
header *PDU_HEADER
message_id string
Header *PDU_HEADER
Message_id string
}
SUBMIT_MULTI struct{}
SUBMIT_MULTI_RESP struct{}
)
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
@@ -74,31 +75,31 @@ func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
}
messageEncoder := p.GetEncoder()
p.header.Encode(buf)
p.Header.Encode(buf)
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
buf.WriteString(p.service_type)
buf.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)
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)
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)
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.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)
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
}
@@ -112,33 +113,33 @@ func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
}
func (p *SUBMIT_SM) Size() int {
var size int
size += p.header.Size()
size += 1 + len(p.service_type)
size += p.Header.Size()
size += 1 + len(p.Service_type)
size += 1 // source_addr_ton
size += 1 // source_addr_npi
size += 1 + len(p.source_addr)
size += 1 + len(p.Source_addr)
size += 1 // dest_addr_ton
size += 1 // dest_addr_npi
size += 1 + len(p.destination_addr)
size += 1 + len(p.Destination_addr)
size += 1 // esm_class
size += 1 // protocol_id
size += 1 // priority_flag
size += 1 + len(p.schedule_delivery_time)
size += 1 + len(p.validity_period)
size += 1 + len(p.Schedule_delivery_time)
size += 1 + len(p.Validity_period)
size += 1 // registered_delivery
size += 1 // replace_if_present
size += 1 // data_coding
size += 1 // sm_default_msg_id
size += 1 // sm_length
size += p.GetEncoder().EncodesInto(&p.short_message)
size += p.GetEncoder().EncodesInto(&p.Short_message)
return size
}
func (p *SUBMIT_SM) UpdateSize() {
p.header.command_length = uint32(p.Size())
p.sm_length = byte(p.GetEncoder().EncodesInto(&p.short_message))
p.Header.Command_length = uint32(p.Size())
p.Sm_length = byte(p.GetEncoder().EncodesInto(&p.Short_message))
}
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
switch p.data_coding {
switch p.Data_coding {
case 0b00000000: // GSM7
return &encoding.GSM7Coder{}
case 0b00000001: // ASCII

View File

@@ -9,28 +9,28 @@ import (
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
p := &SUBMIT_SM{
header: &PDU_HEADER{
command_length: 0,
command_id: 4,
command_status: 0,
sequence_number: 378019,
Header: &PDU_HEADER{
Command_length: 0,
Command_id: 4,
Command_status: 0,
Sequence_number: 378019,
},
service_type: "OMV4",
source_addr_ton: 3,
source_addr_npi: 1,
source_addr: "80362",
dest_addr_ton: 1,
dest_addr_npi: 1,
destination_addr: "812345001000",
esm_class: 0,
protocol_id: 0,
priority_flag: 0,
schedule_delivery_time: "",
validity_period: "180105120000004+",
registered_delivery: 1,
data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
sm_default_msg_id: 0,
short_message: "Reply Yes to opt in or No to opt out.",
Service_type: "OMV4",
Source_addr_ton: 3,
Source_addr_npi: 1,
Source_addr: "80362",
Dest_addr_ton: 1,
Dest_addr_npi: 1,
Destination_addr: "812345001000",
Esm_class: 0,
Protocol_id: 0,
Priority_flag: 0,
Schedule_delivery_time: "",
Validity_period: "180105120000004+",
Registered_delivery: 1,
Data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
Sm_default_msg_id: 0,
Short_message: "Reply Yes to opt in or No to opt out.",
}
p.UpdateSize()
buf := ByteBufferPool.Get(p.Size())
@@ -47,28 +47,28 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
p := &SUBMIT_SM{
header: &PDU_HEADER{
command_length: 0,
command_id: 4,
command_status: 0,
sequence_number: 378019,
Header: &PDU_HEADER{
Command_length: 0,
Command_id: 4,
Command_status: 0,
Sequence_number: 378019,
},
service_type: "OMV4",
source_addr_ton: 3,
source_addr_npi: 1,
source_addr: "80362",
dest_addr_ton: 1,
dest_addr_npi: 1,
destination_addr: "812345001000",
esm_class: 0,
protocol_id: 0,
priority_flag: 0,
schedule_delivery_time: "",
validity_period: "180105120000004+",
registered_delivery: 1,
data_coding: 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.",
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())
@@ -88,13 +88,13 @@ func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
// region size
func TestCalculateSizeTypicalInstance(t *testing.T) {
p := &SUBMIT_SM{
service_type: "test_service",
source_addr: "12345",
destination_addr: "67890",
schedule_delivery_time: "",
validity_period: "",
short_message: "Hello, World!",
data_coding: 1,
Service_type: "test_service",
Source_addr: "12345",
Destination_addr: "67890",
Schedule_delivery_time: "",
Validity_period: "",
Short_message: "Hello, World!",
Data_coding: 1,
}
expectedSize := 68
actualSize := p.Size()
@@ -105,13 +105,13 @@ func TestCalculateSizeTypicalInstance(t *testing.T) {
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,
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()
@@ -123,13 +123,13 @@ func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
func TestCalculateSizeMaxLengths(t *testing.T) {
maxLen := 255
p := &SUBMIT_SM{
service_type: string(make([]byte, maxLen)),
source_addr: string(make([]byte, maxLen)),
destination_addr: string(make([]byte, maxLen)),
schedule_delivery_time: string(make([]byte, maxLen)),
validity_period: string(make([]byte, maxLen)),
short_message: string(make([]byte, maxLen)),
data_coding: 1,
Service_type: string(make([]byte, maxLen)),
Source_addr: string(make([]byte, maxLen)),
Destination_addr: string(make([]byte, maxLen)),
Schedule_delivery_time: string(make([]byte, maxLen)),
Validity_period: string(make([]byte, maxLen)),
Short_message: string(make([]byte, maxLen)),
Data_coding: 1,
}
expectedSize := 1563
actualSize := p.Size()
@@ -140,12 +140,12 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
p := &SUBMIT_SM{
service_type: "",
source_addr: "",
destination_addr: "",
schedule_delivery_time: "",
validity_period: "",
short_message: "",
Service_type: "",
Source_addr: "",
Destination_addr: "",
Schedule_delivery_time: "",
Validity_period: "",
Short_message: "",
}
expectedSize := 33
actualSize := p.Size()