forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiago_test.go
163 lines (135 loc) · 4.31 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
package diago
import (
"context"
"net"
"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 {
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 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)
})
}