mirror of
https://github.com/pion/webrtc.git
synced 2025-10-06 07:37:10 +08:00

Users can now generate MulticastDNS candidates by enabling this option in the SettingEngine.
147 lines
2.9 KiB
Go
147 lines
2.9 KiB
Go
// +build !js
|
|
|
|
package webrtc
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pion/transport/test"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewICEGatherer_Success(t *testing.T) {
|
|
// Limit runtime in case of deadlocks
|
|
lim := test.TimeOut(time.Second * 20)
|
|
defer lim.Stop()
|
|
|
|
report := test.CheckRoutines(t)
|
|
defer report()
|
|
|
|
opts := ICEGatherOptions{
|
|
ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
|
|
}
|
|
|
|
gatherer, err := NewAPI().NewICEGatherer(opts)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if gatherer.State() != ICEGathererStateNew {
|
|
t.Fatalf("Expected gathering state new")
|
|
}
|
|
|
|
err = gatherer.Gather()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
params, err := gatherer.GetLocalParameters()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if len(params.UsernameFragment) == 0 ||
|
|
len(params.Password) == 0 {
|
|
t.Fatalf("Empty local username or password frag")
|
|
}
|
|
|
|
candidates, err := gatherer.GetLocalCandidates()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if len(candidates) == 0 {
|
|
t.Fatalf("No candidates gathered")
|
|
}
|
|
|
|
assert.NoError(t, gatherer.Close())
|
|
}
|
|
|
|
func TestICEGather_LocalCandidateOrder(t *testing.T) {
|
|
// Limit runtime in case of deadlocks
|
|
lim := test.TimeOut(time.Second * 20)
|
|
defer lim.Stop()
|
|
|
|
report := test.CheckRoutines(t)
|
|
defer report()
|
|
|
|
opts := ICEGatherOptions{
|
|
ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
|
|
}
|
|
|
|
gatherer, err := NewAPI().NewICEGatherer(opts)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if gatherer.State() != ICEGathererStateNew {
|
|
t.Fatalf("Expected gathering state new")
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
candidate := make(chan *ICECandidate)
|
|
gatherer.OnLocalCandidate(func(c *ICECandidate) {
|
|
candidate <- c
|
|
})
|
|
|
|
if err := gatherer.SignalCandidates(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
endGathering := false
|
|
|
|
L:
|
|
for {
|
|
select {
|
|
case c := <-candidate:
|
|
if c == nil {
|
|
endGathering = true
|
|
} else if endGathering {
|
|
t.Error("Received a candidate after the last candidate")
|
|
break L
|
|
}
|
|
case <-time.After(100 * time.Millisecond):
|
|
if !endGathering {
|
|
t.Error("Timed out before receiving the last candidate")
|
|
}
|
|
break L
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.NoError(t, gatherer.Close())
|
|
}
|
|
|
|
func TestICEGather_mDNSCandidateGathering(t *testing.T) {
|
|
// Limit runtime in case of deadlocks
|
|
lim := test.TimeOut(time.Second * 20)
|
|
defer lim.Stop()
|
|
|
|
report := test.CheckRoutines(t)
|
|
defer report()
|
|
|
|
s := SettingEngine{}
|
|
s.GenerateMulticastDNSCandidates(true)
|
|
|
|
gatherer, err := NewAPI(WithSettingEngine(s)).NewICEGatherer(ICEGatherOptions{})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
gotMulticastDNSCandidate, resolveFunc := context.WithCancel(context.Background())
|
|
gatherer.OnLocalCandidate(func(c *ICECandidate) {
|
|
if c != nil && strings.HasSuffix(c.Address, ".local") {
|
|
resolveFunc()
|
|
}
|
|
})
|
|
|
|
if err := gatherer.SignalCandidates(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
<-gotMulticastDNSCandidate.Done()
|
|
assert.NoError(t, gatherer.Close())
|
|
}
|