Skip to content

Commit 5ad7880

Browse files
shamisc-bik
authored andcommitted
SMPP 3.4 compatibility for (un)pack with tests (#115)
* compatibility PDU tests * #114 test cases re-ordered * pack - ignoring body if the status is not ok * not parsing body on non OK status for pack and unpack * removed unused macro M_MSG_ID * fixed indentation 💄 * tester de-factoring
1 parent ac23359 commit 5ad7880

5 files changed

Lines changed: 76 additions & 44 deletions

File tree

src/smpp.erl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -823,28 +823,26 @@ err(undefined) -> err(?ESME_RUNKNOWNERR).
823823
command_status => statusstr(?ESME_ROK),
824824
sequence_number => 0}).
825825
-define(M_SYS_ID(_Id), ?BASE(_Id)#{system_id => <<>>}).
826-
-define(M_MSG_ID(_Id), ?BASE(_Id)#{message_id => <<>>}).
827826
-define(M_DST_ADDR(_Id), ?BASE(_Id)#{destination_addr => <<>>}).
828827

829828
info() ->
830829
#{templates =>
831830
#{unbind => ?BASE(cmd(unbind)),
832-
query_sm => ?M_MSG_ID(cmd(query_sm)),
833-
replace_sm => ?M_MSG_ID(cmd(replace_sm)),
831+
query_sm => ?BASE(cmd(query_sm)),
832+
replace_sm => ?BASE(cmd(replace_sm)),
834833
outbind => ?M_SYS_ID(cmd(outbind)),
835834
enquire_link => ?BASE(cmd(enquire_link)),
836835
data_sm => ?M_DST_ADDR(cmd(data_sm)),
837-
cancel_sm => ?M_DST_ADDR(cmd(cancel_sm))
838-
#{message_id => <<>>},
836+
cancel_sm => ?M_DST_ADDR(cmd(cancel_sm)),
839837
submit_sm => ?M_DST_ADDR(cmd(submit_sm)),
840838
deliver_sm => ?M_DST_ADDR(cmd(deliver_sm)),
841839
bind_receiver => ?M_SYS_ID(cmd(bind_receiver)),
842840
alert_notification => ?BASE(cmd(alert_notification)),
843-
query_broadcast_sm => ?M_MSG_ID(cmd(query_broadcast_sm)),
844-
cancel_broadcast_sm => ?M_MSG_ID(cmd(cancel_broadcast_sm)),
841+
query_broadcast_sm => ?BASE(cmd(query_broadcast_sm)),
842+
cancel_broadcast_sm => ?BASE(cmd(cancel_broadcast_sm)),
845843
bind_transmitter => ?M_SYS_ID(cmd(bind_transmitter)),
846844
bind_transceiver => ?M_SYS_ID(cmd(bind_transceiver)),
847-
broadcast_sm => ?M_MSG_ID(cmd(broadcast_sm))
845+
broadcast_sm => ?BASE(cmd(broadcast_sm))
848846
#{broadcast_area_identifier =>
849847
[#{details => <<>>, format => 0}],
850848
broadcast_content_type =>
@@ -856,22 +854,22 @@ info() ->
856854
#{dest_address => <<>>},
857855

858856
unbind_resp => ?BASE(cmd(unbind_resp)),
859-
data_sm_resp => ?M_MSG_ID(cmd(data_sm_resp)),
857+
data_sm_resp => ?BASE(cmd(data_sm_resp)),
860858
generic_nack => ?BASE(cmd(generic_nack)),
861-
query_sm_resp => ?M_MSG_ID(cmd(query_sm_resp)),
862-
submit_sm_resp => ?M_MSG_ID(cmd(submit_sm_resp)),
859+
query_sm_resp => ?BASE(cmd(query_sm_resp)),
860+
submit_sm_resp => ?BASE(cmd(submit_sm_resp)),
863861
cancel_sm_resp => ?BASE(cmd(cancel_sm_resp)),
864-
deliver_sm_resp => ?M_MSG_ID(cmd(deliver_sm_resp)),
862+
deliver_sm_resp => ?BASE(cmd(deliver_sm_resp)),
865863
replace_sm_resp => ?BASE(cmd(replace_sm_resp)),
866-
broadcast_sm_resp => ?M_MSG_ID(cmd(broadcast_sm_resp)),
864+
broadcast_sm_resp => ?BASE(cmd(broadcast_sm_resp)),
867865
enquire_link_resp => ?BASE(cmd(enquire_link_resp)),
868866
bind_receiver_resp => ?M_SYS_ID(cmd(bind_receiver_resp)),
869867
cancel_broadcast_sm_resp => ?BASE(cmd(cancel_broadcast_sm_resp)),
870868
bind_transceiver_resp => ?M_SYS_ID(cmd(bind_transceiver_resp)),
871869
bind_transmitter_resp => ?M_SYS_ID(cmd(bind_transmitter_resp)),
872-
submit_multi_resp => ?M_MSG_ID(cmd(submit_multi_resp))
870+
submit_multi_resp => ?BASE(cmd(submit_multi_resp))
873871
#{unsuccess_sme => <<>>},
874-
query_broadcast_sm_resp => ?M_MSG_ID(cmd(query_broadcast_sm_resp))
872+
query_broadcast_sm_resp => ?BASE(cmd(query_broadcast_sm_resp))
875873
#{broadcast_area_identifier =>
876874
[#{details => <<>>, format => 0}],
877875
broadcast_area_success => [0],

src/smpp_param_syntax.erl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ encode_std(undefined, #standard{default = undefined, error = undefined}) ->
173173
{error, ?ESME_RUNKNOWNERR};
174174
encode_std(undefined, #standard{default = undefined, error = Error}) ->
175175
{error, Error};
176-
% SMPP Protocol Specification v3.4 compatibility
177-
% Sections : 4.1.2, 4.1.4 and 4.4.2
178-
% Empty/null message_id (00) is not added if not found in body
179-
encode_std(undefined, #standard{name = message_id}) ->
180-
{ok, <<>>};
181176
encode_std(undefined, #standard{domain = Domain, default = Default}) ->
182177
% Assumes default values should always be OK...if not, it'll be easier to
183178
% review the default definitions rather than touching this clause.

src/smpp_pdu_syntax.erl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,35 @@ sequence_number({_CmdId, _Status, SeqNum, _Body}) ->
6464
new_pdu(CmdId, Status, SeqNum, Body) ->
6565
{CmdId, Status, SeqNum, Body}.
6666

67-
pack({CmdId, Status, SeqNum, Body}, PduType) ->
67+
pack({CmdId, ?ESME_ROK, SeqNum, Body}, PduType) ->
6868
case pack_body(Body, PduType) of
6969
{ok, BodyL} ->
7070
BodyBin = list_to_binary(BodyL),
7171
Len = size(BodyBin) + 16,
72-
{ok, <<Len:32, CmdId:32, Status:32, SeqNum:32, BodyBin/binary>>};
72+
{ok, <<Len:32, CmdId:32, ?ESME_ROK:32, SeqNum:32, BodyBin/binary>>};
7373
{error, Error} ->
7474
{error, CmdId, Error, SeqNum}
75-
end.
75+
end;
76+
% SMPP Protocol Specification v3.4 compatibility
77+
% Sections : 4.1.2, 4.1.4 and 4.4.2
78+
% Note: The PDU Body is not returned if the command_status field contains a non-zero value
79+
pack({CmdId, Status, SeqNum, _Body}, _PduType) ->
80+
{ok, <<16:32, CmdId:32, Status:32, SeqNum:32>>}.
7681

77-
unpack(<<Len:32, CmdId:32, Status:32, SeqNum:32, Body/binary>>, PduType)
82+
unpack(<<Len:32, CmdId:32, ?ESME_ROK:32, SeqNum:32, Body/binary>>, PduType)
7883
when Len == size(Body) + 16 ->
7984
case unpack_body(Body, PduType) of
8085
{ok, BodyParams} ->
81-
{ok, new_pdu(CmdId, Status, SeqNum, BodyParams)};
86+
{ok, new_pdu(CmdId, ?ESME_ROK, SeqNum, BodyParams)};
8287
{error, Error} ->
83-
% SMPP Protocol Specification v3.4 compatibility
84-
% Sections : 4.1.2, 4.1.4 and 4.4.2
85-
% Note: The PDU Body is not returned if the command_status field contains a non-zero value
86-
case Status of
87-
?ESME_ROK -> {error, CmdId, Error, SeqNum};
88-
Status -> {ok, new_pdu(CmdId, Status, SeqNum, [])}
89-
end
88+
{error, CmdId, Error, SeqNum}
9089
end;
90+
% SMPP Protocol Specification v3.4 compatibility
91+
% Sections : 4.1.2, 4.1.4 and 4.4.2
92+
% Note: The PDU Body is not returned if the command_status field contains a non-zero value
93+
unpack(<<Len:32, CmdId:32, Status:32, SeqNum:32, Body/binary>>, _PduType)
94+
when Len == size(Body) + 16 ->
95+
{ok, new_pdu(CmdId, Status, SeqNum, [])};
9196
unpack(<<_Len:32, CmdId:32, _Status:32, SeqNum:32, _Body/binary>>, _PduType) ->
9297
{error, CmdId, ?ESME_RINVCMDLEN, SeqNum}.
9398

test/smpp_3_4.erl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-module(smpp_3_4).
2+
3+
-include_lib("eunit/include/eunit.hrl").
4+
-include("../src/smpp_globals.hrl").
5+
6+
-define(TESTS, [
7+
{"submit_sm_resp(ESME_RINVMSGLEN)",
8+
"00000010800000040000000100000001",
9+
{?COMMAND_ID_SUBMIT_SM_RESP, ?ESME_RINVMSGLEN, 1, []}},
10+
{"submit_sm_resp(ESME_RTHROTTLED)",
11+
"00000010800000040000005800000001",
12+
{?COMMAND_ID_SUBMIT_SM_RESP, ?ESME_RTHROTTLED, 1, []}},
13+
{"submit_sm_resp(ESME_RMSGQFUL)",
14+
"00000010800000040000001400000001",
15+
{?COMMAND_ID_SUBMIT_SM_RESP, ?ESME_RMSGQFUL, 1, []}},
16+
{"bind_transceiver_resp(ESME_RALYBND)",
17+
"00000010800000090000000500000001",
18+
{?COMMAND_ID_BIND_TRANSCEIVER_RESP, ?ESME_RALYBND, 1, []}},
19+
{"bind_transceiver_resp(ESME_RBINDFAIL)",
20+
"00000010800000090000000D00000001",
21+
{?COMMAND_ID_BIND_TRANSCEIVER_RESP, ?ESME_RBINDFAIL, 1, []}}
22+
]).
23+
24+
packunpack_test_() ->
25+
{inparallel,
26+
[{T,
27+
fun() ->
28+
Bin = hex2bin(Pdu),
29+
?assertEqual({ok, Parsed}, smpp_operation:unpack(Bin)),
30+
?assertEqual({ok, Bin}, smpp_operation:pack(Parsed))
31+
end}
32+
|| {T, Pdu, Parsed} <- ?TESTS]
33+
}.
34+
35+
hex2bin([]) -> <<>>;
36+
hex2bin([A,B|Rest]) ->
37+
<<(list_to_integer([A,B], 16)), (hex2bin(Rest))/binary>>.

test/tests.hrl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
-define(PDU(_Id), ?PDU(_Id, "")).
88
-define(PDU_SYSID(_Id), ?PDU(_Id, ",\"system_id\":\"\",\"sc_interface_version\":80")).
99
-define(PDU_DSTADDR(_Id), ?PDU(_Id, ",\"destination_addr\":\"\"")).
10-
-define(PDU_MSGID(_Id), ?PDU(_Id, ",\"message_id\":\"\"")).
11-
-define(PDU_DSTADDR_MSGID(_Id), ?PDU(_Id, ",\"destination_addr\":\"\",\"message_id\":\"\"")).
1210

1311
-define(TESTS,
1412
[{"bind_transceiver",
@@ -950,32 +948,31 @@
950948
message_id => <<"this_could_be_a_message_id">>,
951949
sequence_number => 1}},
952950
{"deliver_sm_resp_error_status_code_#107",
953-
"00 00 00 17 80 00 01 03 00 00 00 05 00 00 00 00 31 32 33 34 35 36 00",
954-
#{command_id => <<"data_sm_resp">>, command_length => 23,
955-
command_status => <<"ESME_RALYBND">>, message_id => <<"123456">>,
956-
sequence_number => 0}}
951+
"00 00 00 10 80 00 01 03 00 00 00 05 00 00 00 01",
952+
#{command_id => <<"data_sm_resp">>, command_length => 16,
953+
command_status => <<"ESME_RALYBND">>, sequence_number => 1}}
957954
]).
958955

959956
-define(TESTS2,
960957
[% requests
961958
{"bind_receiver", 16#00000001, ?PDU_SYSID(16#00000001)},
962959
{"bind_transmitter", 16#00000002, ?PDU_SYSID(16#00000002)},
963-
{"query_sm", 16#00000003, ?PDU_DSTADDR_MSGID(16#00000003)},
960+
{"query_sm", 16#00000003, ?PDU_DSTADDR(16#00000003)},
964961
{"submit_sm", 16#00000004, ?PDU_DSTADDR(16#00000004)},
965962
{"deliver_sm", 16#00000005, ?PDU_DSTADDR(16#00000005)},
966963
{"unbind", 16#00000006, ?PDU_DSTADDR(16#00000006)},
967-
{"replace_sm", 16#00000007, ?PDU_DSTADDR_MSGID(16#00000007)},
968-
{"cancel_sm", 16#00000008, ?PDU_DSTADDR_MSGID(16#00000008)},
964+
{"replace_sm", 16#00000007, ?PDU_DSTADDR(16#00000007)},
965+
{"cancel_sm", 16#00000008, ?PDU_DSTADDR(16#00000008)},
969966
{"bind_transceiver", 16#00000009, ?PDU_SYSID(16#00000009)},
970967
{"outbind", 16#0000000B, ?PDU_SYSID(16#0000000B)},
971968
{"enquire_link", 16#00000015, ?PDU(16#000000015)},
972969

973970
% responses
974971
{"bind_receiver_resp", 16#80000001, ?PDU_SYSID(16#80000001)},
975972
{"bind_transmitter_resp", 16#80000002, ?PDU_SYSID(16#80000002)},
976-
{"query_sm_resp", 16#80000003, ?PDU_MSGID(16#80000003)},
977-
{"submit_sm_resp", 16#80000004, ?PDU_MSGID(16#80000004)},
978-
{"deliver_sm_resp", 16#80000005, ?PDU_MSGID(16#80000005)},
973+
{"query_sm_resp", 16#80000003, ?PDU(16#80000003)},
974+
{"submit_sm_resp", 16#80000004, ?PDU(16#80000004)},
975+
{"deliver_sm_resp", 16#80000005, ?PDU(16#80000005)},
979976
{"unbind_resp", 16#80000006, ?PDU(16#80000006)},
980977
{"replace_sm_resp", 16#80000007, ?PDU(16#80000007)},
981978
{"cancel_sm_resp", 16#80000008, ?PDU(16#80000008)},

0 commit comments

Comments
 (0)