forked from ajankovic/smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
264 lines (250 loc) · 6.46 KB
/
session_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package smpp_test
import (
"bytes"
"context"
"testing"
"time"
"github.com/ajankovic/smpp"
"github.com/ajankovic/smpp/internal/mock"
"github.com/ajankovic/smpp/pdu"
)
type testSequencer struct {
seq uint32
skip bool
}
func (ts *testSequencer) Next() uint32 {
if !ts.skip {
ts.seq++
} else {
ts.skip = false
}
return ts.seq
}
func (ts *testSequencer) skipNext() {
ts.skip = true
}
type testEncoder struct {
buf *bytes.Buffer
enc *pdu.Encoder
seq *testSequencer
}
func newTestEncoder(i int) *testEncoder {
buf := bytes.NewBuffer(nil)
seq := &testSequencer{seq: uint32(i)}
return &testEncoder{
buf: buf,
seq: seq,
enc: pdu.NewEncoder(buf, seq),
}
}
// Encode by incrementing counter.
func (te *testEncoder) i(p pdu.PDU, status ...pdu.Status) []byte {
te.buf.Reset()
st := pdu.StatusOK
if len(status) > 0 {
st = status[0]
}
_, err := te.enc.Encode(p, pdu.EncodeStatus(st))
if err != nil {
panic(err.Error())
}
out := make([]byte, te.buf.Len())
copy(out, te.buf.Bytes())
return out
}
// Encode by skipping increment.
func (te *testEncoder) s(p pdu.PDU, status ...pdu.Status) []byte {
te.buf.Reset()
st := pdu.StatusOK
if len(status) > 0 {
st = status[0]
}
te.seq.skipNext()
_, err := te.enc.Encode(p, pdu.EncodeStatus(st))
if err != nil {
panic(err.Error())
}
out := make([]byte, te.buf.Len())
copy(out, te.buf.Bytes())
return out
}
func TestESMESession(t *testing.T) {
bindTRx := &pdu.BindTRx{
SystemID: "ESME",
Password: "password",
SystemType: "type",
InterfaceVersion: smpp.Version,
AddressRange: "111111",
}
bindTRxResp := bindTRx.Response("SMSC")
bindTRxResp.Options = pdu.NewOptions().SetScInterfaceVersion(smpp.Version)
submitSm := &pdu.SubmitSm{
SourceAddr: "source",
DestinationAddr: "destination",
ShortMessage: "this is the message",
}
submitSmResp := submitSm.Response("id0")
unbind := pdu.Unbind{}
unbindResp := pdu.UnbindResp{}
e := newTestEncoder(0)
conn := mock.NewConn().
ByteWrite(e.i(bindTRx)).ByteRead(e.s(bindTRxResp)).
ByteWrite(e.i(submitSm)).ByteRead(e.s(submitSmResp)).
Wait(1).
ByteWrite(e.i(unbind)).ByteRead(e.s(unbindResp)).
Wait(1).
Closed()
conf := smpp.SessionConf{
SystemID: "TestingESME",
}
sess := smpp.NewSession(conn, conf)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
resp, err := sess.Send(ctx, bindTRx)
if err != nil {
t.Fatal(err)
}
if resp.CommandID() != pdu.BindTransceiverRespID {
t.Errorf("expected BindTransceiverRespID got %d", resp.CommandID())
}
resp, err = sess.Send(ctx, submitSm)
if err != nil {
t.Fatal(err)
}
if resp.CommandID() != pdu.SubmitSmRespID {
t.Errorf("expected SubmitSmRespID got %d", resp.CommandID())
}
resp, err = sess.Send(ctx, unbind)
if err != nil {
t.Fatal(err)
}
if resp.CommandID() != pdu.UnbindRespID {
t.Errorf("expected UnbindRespID got %d", resp.CommandID())
}
if err := sess.Close(); err != nil {
t.Errorf("Got error during session close %+v", err)
}
errors := conn.Validate()
if errors != nil {
for _, err := range errors {
t.Error(err)
}
}
}
func TestESMESessionInvalidStatus(t *testing.T) {
bindTRx := &pdu.BindTRx{
SystemID: "ESME",
}
bindTRxResp := bindTRx.Response("SMSC")
submitSm := &pdu.SubmitSm{
SourceAddr: "source",
DestinationAddr: "destination",
ShortMessage: "this is the message",
}
submitSmResp := submitSm.Response("id0")
e := newTestEncoder(0)
conn := mock.NewConn().
ByteWrite(e.i(bindTRx)).ByteRead(e.s(bindTRxResp)).
ByteWrite(e.i(submitSm)).ByteRead(e.s(submitSmResp, pdu.StatusInvDstAdr)).
Wait(1).
Closed()
conf := smpp.SessionConf{}
sess := smpp.NewSession(conn, conf)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
resp, err := sess.Send(ctx, bindTRx)
if err != nil {
t.Fatal(err)
}
if resp.CommandID() != pdu.BindTransceiverRespID {
t.Errorf("expected BindTransceiverRespID got %d", resp.CommandID())
}
resp, err = sess.Send(ctx, submitSm)
if err == nil {
t.Errorf("Expected status error got nil")
}
if resp.CommandID() != pdu.SubmitSmRespID {
t.Errorf("expected SubmitSmRespID got %d", resp.CommandID())
}
if serr, ok := err.(smpp.StatusError); !ok {
t.Errorf("Expected StatusError type")
} else {
expected := "Invalid Destination Address '0xB'"
if serr.Error() != expected {
t.Errorf("Status error: %v, expected %s", err, expected)
}
}
if err := sess.Close(); err != nil {
t.Errorf("Got error during session close %+v", err)
}
errors := conn.Validate()
if errors != nil {
for _, err := range errors {
t.Error(err)
}
}
}
func TestSMSCSession(t *testing.T) {
bindTRx := &pdu.BindTRx{
SystemID: "ESME",
Password: "password",
SystemType: "type",
InterfaceVersion: smpp.Version,
AddressRange: "111111",
}
bindTRxResp := bindTRx.Response("SMSC")
bindTRxResp.Options = pdu.NewOptions().SetScInterfaceVersion(smpp.Version)
submitSm := &pdu.SubmitSm{
SourceAddr: "source",
DestinationAddr: "destination",
ShortMessage: "this is the message",
}
submitSmResp := submitSm.Response("id0")
sync := make(chan struct{})
e := newTestEncoder(0)
conn := mock.NewConn().
ByteRead(e.i(bindTRx, pdu.StatusOK)).ByteWrite(e.s(bindTRxResp, pdu.StatusOK)).
ByteRead(e.i(submitSm, pdu.StatusOK)).ByteWrite(e.s(submitSmResp, pdu.StatusOK)).Wait(1).
Closed()
conf := smpp.SessionConf{
SystemID: "TestingSMSC",
Type: smpp.SMSC,
Handler: smpp.HandlerFunc(func(ctx *smpp.Context) {
switch ctx.CommandID() {
case pdu.BindTransceiverID:
btrx, err := ctx.BindTRx()
if err != nil {
t.Errorf("Handler can't get BindTRx request %v", err)
}
resp := btrx.Response("SMSC")
resp.Options = pdu.NewOptions().SetScInterfaceVersion(smpp.Version)
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
t.Errorf("Handler can't respond to bind request %v", err)
}
case pdu.SubmitSmID:
defer close(sync)
sm, err := ctx.SubmitSm()
if err != nil {
t.Errorf("Handler can't get BindTRx request %v", err)
}
resp := sm.Response("id0")
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
t.Errorf("Handler can't respond to SubmitSm request %v", err)
}
}
}),
}
sess := smpp.NewSession(conn, conf)
select {
case <-time.After(50 * time.Millisecond):
t.Fatal("timeout waiting for response")
case <-sync:
}
sess.Close()
errors := conn.Validate()
if errors != nil {
for _, err := range errors {
t.Error(err)
}
}
}