Fix size calculation and add another test for encoding a gsm7 message
All checks were successful
Run Tests / Test (push) Successful in 16s
Benchmark BufferPool / RunBenchmarks (push) Successful in 21s

This commit is contained in:
2024-07-31 14:19:12 +02:00
parent 85e674753e
commit c56d441891
2 changed files with 38 additions and 20 deletions

View File

@@ -135,7 +135,7 @@ func (p *SUBMIT_SM) Size() int {
} }
func (p *SUBMIT_SM) UpdateSize() { func (p *SUBMIT_SM) UpdateSize() {
p.header.command_length = uint32(p.Size()) p.header.command_length = uint32(p.Size())
p.sm_length = byte(len(p.short_message)) p.sm_length = byte(p.GetEncoder().EncodesInto(&p.short_message))
} }
func (p *SUBMIT_SM) GetEncoder() encoding.Coder { func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
switch p.data_coding { switch p.data_coding {

View File

@@ -45,25 +45,43 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
} }
} }
// func TestRealScenario(t *testing.T) { func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
// p := &SUBMIT_SM{
// p.header.command_length = uint32(p.Size()) header: &PDU_HEADER{
// p.sm_length = byte(len(p.short_message)) command_length: 0,
// buf := make([]byte, p.Size()) command_id: 4,
// err := p.EncodeInto(&buf) command_status: 0,
// if err != nil { sequence_number: 378019,
// t.Errorf("Expected no error, got %v", err) },
// } service_type: "OMV4",
// if len(buf) != len(expected) { source_addr_ton: 3,
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf)) source_addr_npi: 1,
// } source_addr: "80362",
// for i, v := range buf { dest_addr_ton: 1,
// if v != expected[i] { dest_addr_npi: 1,
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf) destination_addr: "812345001000",
// break 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 decode