-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdiago_test.go
237 lines (192 loc) · 6.33 KB
/
diago_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
package diago
import (
"context"
"net"
"sync"
"testing"
"github.com/emiago/diago/media/sdp"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testDiagoClient(t *testing.T, onRequest func(req *sip.Request) *sip.Response, opts ...DiagoOption) *Diago {
// Create client transaction request
cTxReq := &clientTxRequester{
onRequest: onRequest,
}
ua, _ := sipgo.NewUA()
client, _ := sipgo.NewClient(ua)
client.TxRequester = cTxReq
t.Cleanup(func() {
ua.Close()
})
opts = append(opts, WithClient(client))
return NewDiago(ua, opts...)
}
func TestDiagoRegister(t *testing.T) {
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
sync.OnceFunc(func() {
sip.NewResponseFromRequest(req, 100, "Trying", nil)
})()
return sip.NewResponseFromRequest(req, 200, "OK", nil)
})
ctx := context.TODO()
rtx, err := dg.RegisterTransaction(ctx, sip.Uri{User: "alice", Host: "localhost"}, RegisterOptions{})
require.NoError(t, err)
err = rtx.Register(ctx)
require.NoError(t, err)
}
func TestDiagoRegisterAuthorization(t *testing.T) {
t.Skip("Do test with sending Register and authorization returned")
}
func TestDiagoInviteCallerID(t *testing.T) {
t.Run("NoSDPInResponse", func(t *testing.T) {
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
return sip.NewResponseFromRequest(req, 200, "OK", nil)
})
_, err := dg.Invite(context.Background(), sip.Uri{User: "alice", Host: "localhost"}, InviteOptions{})
if assert.Error(t, err) {
assert.Equal(t, "no SDP in response", err.Error())
}
})
reqCh := make(chan *sip.Request)
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
reqCh <- req
return sip.NewResponseFromRequest(req, 500, "", nil)
})
t.Run("DefaultCallerID", func(t *testing.T) {
go dg.Invite(context.Background(), sip.Uri{User: "alice", Host: "localhost"}, InviteOptions{})
req := <-reqCh
assert.Equal(t, dg.ua.Name(), req.From().Address.User)
assert.Equal(t, dg.ua.Hostname(), req.From().Address.Host)
assert.NotEmpty(t, req.From().Params["tag"])
})
t.Run("SetCallerid", func(t *testing.T) {
opts := InviteOptions{}
opts.SetCaller("Test", "123456")
go dg.Invite(context.Background(), sip.Uri{User: "alice", Host: "localhost"}, opts)
req := <-reqCh
assert.Equal(t, "Test", req.From().DisplayName)
assert.Equal(t, "123456", req.From().Address.User)
assert.NotEmpty(t, req.From().Params["tag"])
})
t.Run("SetCalleridWithBridgeOriginator", func(t *testing.T) {
t.Skip("NOT IMPLEMENTED")
})
}
func TestDiagoTransportConfs(t *testing.T) {
type testCase = struct {
tran Transport
expectedContactHostPort string
expectedMediaHost string
}
doTest := func(tc testCase) {
tran := tc.tran
reqCh := make(chan *sip.Request)
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
reqCh <- req
return sip.NewResponseFromRequest(req, 200, "OK", nil)
}, WithTransport(tran))
go dg.Invite(context.TODO(), sip.Uri{User: "alice", Host: "localhost"}, InviteOptions{})
// Now check our req passed on client
req := <-reqCh
// parse SDP
sd := sdp.SessionDescription{}
require.NoError(t, sdp.Unmarshal(req.Body(), &sd))
connInfo, err := sd.ConnectionInformation()
require.NoError(t, err)
assert.Equal(t, tc.expectedContactHostPort, req.Contact().Address.HostPort())
assert.Equal(t, tc.expectedMediaHost, connInfo.IP.String())
}
t.Run("ExternalHost", func(t *testing.T) {
tc := testCase{
tran: Transport{
Transport: "udp",
BindHost: "127.0.0.111",
BindPort: 15060,
ExternalHost: "1.2.3.4",
},
expectedContactHostPort: "1.2.3.4:15060",
expectedMediaHost: "1.2.3.4",
}
doTest(tc)
})
t.Run("ExternalHostFQDN", func(t *testing.T) {
tc := testCase{
tran: Transport{
Transport: "udp",
BindHost: "127.0.0.111",
BindPort: 15060,
ExternalHost: "myhost.pbx.com",
},
expectedContactHostPort: "myhost.pbx.com:15060",
expectedMediaHost: "127.0.0.111", // Hosts are not resolved so it goes with bind
}
doTest(tc)
})
t.Run("ExternalHostFQDNExternalMedia", func(t *testing.T) {
tc := testCase{
tran: Transport{
Transport: "udp",
BindHost: "127.0.0.111",
BindPort: 15060,
ExternalHost: "myhost.pbx.com",
MediaExternalIP: net.IPv4(1, 2, 3, 4),
},
expectedContactHostPort: "myhost.pbx.com:15060",
expectedMediaHost: "1.2.3.4", // Hosts are not resolved so it goes with bind
}
doTest(tc)
})
}
func TestDiagoNewDialog(t *testing.T) {
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
body := sdp.GenerateForAudio(net.IPv4(127, 0, 0, 1), net.IPv4(127, 0, 0, 1), 34455, sdp.ModeSendrecv, []string{sdp.FORMAT_TYPE_ALAW})
return sip.NewResponseFromRequest(req, 200, "OK", body)
})
ctx := context.TODO()
t.Run("CloseNoError", func(t *testing.T) {
dialog, err := dg.NewDialog(sip.Uri{User: "alice", Host: "localhost"}, NewDialogOptions{})
require.NoError(t, err)
dialog.Close()
})
// t.Run("NoAcked", func(t *testing.T) {
// dialog, err := dg.NewDialog( sip.Uri{User: "alice", Host: "localhost"}, NewDialogOpts{})
// require.NoError(t, err)
// defer dialog.Close()
// err = dialog.Invite(ctx, InviteOptions{})
// require.NoError(t, err)
// dialog.Audio
// })
t.Run("FullDialog", func(t *testing.T) {
dialog, err := dg.NewDialog(sip.Uri{User: "alice", Host: "localhost"}, NewDialogOptions{})
require.NoError(t, err)
defer dialog.Close()
err = dialog.Invite(ctx, InviteClientOptions{})
require.NoError(t, err)
assert.NotEmpty(t, dialog.ID)
err = dialog.Ack(ctx)
require.NoError(t, err)
// assert.NotEmpty(t, dialog.ID)
})
// _, err := dg.Invite(context.Background(), sip.Uri{User: "alice", Host: "localhost"}, InviteOptions{})
// if assert.Error(t, err) {
// assert.Equal(t, "no SDP in response", err.Error())
// }
}
func TestIntegrationDiagoTransportEmpheralPort(t *testing.T) {
tran := Transport{
Transport: "udp",
BindHost: "127.0.0.1",
BindPort: 0,
}
ua, _ := sipgo.NewUA()
defer ua.Close()
dg := NewDiago(ua, WithTransport(tran))
err := dg.ServeBackground(context.TODO(), func(d *DialogServerSession) {})
require.NoError(t, err)
newTran, _ := dg.getTransport("udp")
t.Log("port assigned", newTran.BindPort)
assert.NotEmpty(t, newTran.BindPort)
}