Files
webrtc/rtcicecandidate_test.go
Michael MacDonald 2b2c09fc3e Add remote TURN support
Allow connection to remote relay candidates when the peer is
configured as a TURN client. NB: Does not yet add support for
local relay candidates.
2019-01-28 11:27:15 -05:00

163 lines
4.0 KiB
Go

package webrtc
import (
"net"
"testing"
"github.com/pions/sdp"
"github.com/pions/webrtc/pkg/ice"
"github.com/stretchr/testify/assert"
)
func TestRTCIceCandidate_Convert(t *testing.T) {
testCases := []struct {
native RTCIceCandidate
ice *ice.Candidate
sdp sdp.ICECandidate
}{
{
RTCIceCandidate{
Foundation: "foundation",
Priority: 128,
IP: "1.0.0.1",
Protocol: RTCIceProtocolUDP,
Port: 1234,
Typ: RTCIceCandidateTypeHost,
Component: 1,
}, &ice.Candidate{
IP: net.ParseIP("1.0.0.1"),
NetworkType: ice.NetworkTypeUDP4,
Port: 1234,
Type: ice.CandidateTypeHost,
Component: 1,
LocalPreference: 65535,
},
sdp.ICECandidate{
Foundation: "foundation",
Priority: 128,
IP: "1.0.0.1",
Protocol: "udp",
Port: 1234,
Typ: "host",
Component: 1,
},
},
{
RTCIceCandidate{
Foundation: "foundation",
Priority: 128,
IP: "::1",
Protocol: RTCIceProtocolUDP,
Port: 1234,
Typ: RTCIceCandidateTypeSrflx,
Component: 1,
RelatedAddress: "1.0.0.1",
RelatedPort: 4321,
}, &ice.Candidate{
IP: net.ParseIP("::1"),
NetworkType: ice.NetworkTypeUDP6,
Port: 1234,
Type: ice.CandidateTypeServerReflexive,
Component: 1,
LocalPreference: 65535,
RelatedAddress: &ice.CandidateRelatedAddress{
Address: "1.0.0.1",
Port: 4321,
},
},
sdp.ICECandidate{
Foundation: "foundation",
Priority: 128,
IP: "::1",
Protocol: "udp",
Port: 1234,
Typ: "srflx",
Component: 1,
RelatedAddress: "1.0.0.1",
RelatedPort: 4321,
},
},
{
RTCIceCandidate{
Foundation: "foundation",
Priority: 128,
IP: "::1",
Protocol: RTCIceProtocolUDP,
Port: 1234,
Typ: RTCIceCandidateTypePrflx,
Component: 1,
RelatedAddress: "1.0.0.1",
RelatedPort: 4321,
}, &ice.Candidate{
IP: net.ParseIP("::1"),
NetworkType: ice.NetworkTypeUDP6,
Port: 1234,
Type: ice.CandidateTypePeerReflexive,
Component: 1,
LocalPreference: 65535,
RelatedAddress: &ice.CandidateRelatedAddress{
Address: "1.0.0.1",
Port: 4321,
},
},
sdp.ICECandidate{
Foundation: "foundation",
Priority: 128,
IP: "::1",
Protocol: "udp",
Port: 1234,
Typ: "prflx",
Component: 1,
RelatedAddress: "1.0.0.1",
RelatedPort: 4321,
},
},
}
for i, testCase := range testCases {
actualSDP := testCase.native.toSDP()
assert.Equal(t,
testCase.sdp,
actualSDP,
"testCase: %d sdp not equal %v", i, actualSDP,
)
actualICE, err := testCase.native.toICE()
assert.Nil(t, err)
assert.Equal(t,
testCase.ice,
actualICE,
"testCase: %d ice not equal %v", i, actualSDP,
)
}
}
func TestConvertTypeFromICE(t *testing.T) {
t.Run("host", func(t *testing.T) {
ct, err := convertTypeFromICE(ice.CandidateTypeHost)
if err != nil {
t.Fatal("failed coverting ice.CandidateTypeHost")
}
if ct != RTCIceCandidateTypeHost {
t.Fatal("should be coverted to RTCIceCandidateTypeHost")
}
})
t.Run("srflx", func(t *testing.T) {
ct, err := convertTypeFromICE(ice.CandidateTypeServerReflexive)
if err != nil {
t.Fatal("failed coverting ice.CandidateTypeServerReflexive")
}
if ct != RTCIceCandidateTypeSrflx {
t.Fatal("should be coverted to RTCIceCandidateTypeSrflx")
}
})
t.Run("prflx", func(t *testing.T) {
ct, err := convertTypeFromICE(ice.CandidateTypePeerReflexive)
if err != nil {
t.Fatal("failed coverting ice.CandidateTypePeerReflexive")
}
if ct != RTCIceCandidateTypePrflx {
t.Fatal("should be coverted to RTCIceCandidateTypePrflx")
}
})
}