forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog_client_session_test.go
More file actions
164 lines (136 loc) · 4.04 KB
/
dialog_client_session_test.go
File metadata and controls
164 lines (136 loc) · 4.04 KB
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
package diago
import (
"bytes"
"context"
"testing"
"github.com/emiago/diago/media"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntegrationDialogClientEarlyMedia(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
{
ua, _ := sipgo.NewUA(sipgo.WithUserAgent("server"))
defer ua.Close()
dg := NewDiago(ua, WithTransport(
Transport{
Transport: "udp",
BindHost: "127.0.0.1",
BindPort: 15060,
},
))
err := dg.ServeBackground(ctx, func(d *DialogServerSession) {
t.Log("Call received")
d.Trying()
if err := d.ProgressMedia(); err != nil {
t.Log("Failed to progress media", err)
return
}
// Write frame
w, _ := d.AudioWriter()
if _, err := w.Write(bytes.Repeat([]byte{0, 100}, 80)); err != nil {
t.Log("Failed to write frame", err)
return
}
if err := d.Answer(); err != nil {
t.Log("Failed to answer", err)
return
}
return
})
require.NoError(t, err)
}
ua, _ := sipgo.NewUA()
defer ua.Close()
dg := newDialer(ua)
err := dg.ServeBackground(context.TODO(), func(d *DialogServerSession) {})
require.NoError(t, err)
dialog, err := dg.NewDialog(sip.Uri{User: "dialer", Host: "127.0.0.1", Port: 15060}, NewDialogOptions{})
require.NoError(t, err)
defer dialog.Close()
err = dialog.Invite(ctx, InviteClientOptions{
EarlyMediaDetect: true,
})
require.ErrorIs(t, err, ErrClientEarlyMedia)
// Now we should be able to read media
r, err := dialog.AudioReader()
require.NoError(t, err)
// Read early media in background
var earlyMediaBuf []byte
doneEarly := make(chan struct{})
go func() {
defer close(doneEarly)
earlyMediaBuf, _ = media.ReadAll(r, 160)
}()
dialog.WaitAnswer(ctx, sipgo.AnswerOptions{})
dialog.Ack(ctx)
<-dialog.Context().Done()
<-doneEarly
assert.Len(t, earlyMediaBuf, 160) // 1 frame
}
func TestIntegrationDialogClientReinvite(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
{
ua, _ := sipgo.NewUA(sipgo.WithUserAgent("server"))
defer ua.Close()
dg := NewDiago(ua, WithTransport(
Transport{
Transport: "udp",
BindHost: "127.0.0.1",
BindPort: 15060,
},
))
err := dg.ServeBackground(ctx, func(d *DialogServerSession) {
t.Log("Call received")
d.AnswerOptions(AnswerOptions{OnMediaUpdate: func(d *DialogMedia) {
}})
<-d.Context().Done()
})
require.NoError(t, err)
}
ua, _ := sipgo.NewUA()
defer ua.Close()
dg := newDialer(ua)
err := dg.ServeBackground(context.TODO(), func(d *DialogServerSession) {})
require.NoError(t, err)
dialog, err := dg.Invite(ctx, sip.Uri{User: "dialer", Host: "127.0.0.1", Port: 15060}, InviteOptions{})
require.NoError(t, err)
err = dialog.ReInvite(ctx)
require.NoError(t, err)
dialog.Hangup(ctx)
}
func TestDialogClientInvite(t *testing.T) {
reqCh := make(chan *sip.Request)
dg := testDiagoClient(t, func(req *sip.Request) *sip.Response {
reqCh <- req
return sip.NewResponseFromRequest(req, 500, "", nil)
})
t.Run("WithCallerid", func(t *testing.T) {
opts := InviteClientOptions{}
opts.WithCaller("Test", "123456", "example.com")
dialog, err := dg.NewDialog(sip.Uri{User: "alice", Host: "localhost"}, NewDialogOptions{})
require.NoError(t, err)
go dialog.Invite(context.Background(), 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("WithAnonymous", func(t *testing.T) {
opts := InviteClientOptions{}
opts.WithAnonymousCaller()
dialog, err := dg.NewDialog(sip.Uri{User: "alice", Host: "localhost"}, NewDialogOptions{})
require.NoError(t, err)
go dialog.Invite(context.Background(), opts)
req := <-reqCh
assert.Equal(t, "Anonymous", req.From().DisplayName)
assert.Equal(t, "anonymous", req.From().Address.User)
assert.NotEmpty(t, req.From().Params["tag"])
})
}