Skip to content

Commit

Permalink
Add test that NetworkTypes is respected
Browse files Browse the repository at this point in the history
Assert that a remote Passive TCP candidate doesn't cause a TCP
connection to be started.

Resolves pion/webrtc#2782
  • Loading branch information
Sean-Der committed Aug 14, 2024
1 parent 3051b4a commit b559a79
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions active_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package ice

import (
"fmt"
"net"
"net/netip"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -216,3 +218,67 @@ func TestActiveTCP_NonBlocking(t *testing.T) {

<-isConnected
}

// Assert that we ignore remote TCP candidates when running a UDP Only Agent
func TestActiveTCP_Respect_NetworkTypes(t *testing.T) {
defer test.CheckRoutines(t)()
defer test.TimeOut(time.Second * 5).Stop()

tcpListener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)

_, port, err := net.SplitHostPort(tcpListener.Addr().String())
require.NoError(t, err)

var incomingTCPCount uint64
go func() {
for {
conn, listenErr := tcpListener.Accept()
if listenErr != nil {
return
}

require.NoError(t, conn.Close())
atomic.AddUint64(&incomingTCPCount, ^uint64(0))
}
}()

cfg := &AgentConfig{
NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6},
InterfaceFilter: problematicNetworkInterfaces,
IncludeLoopback: true,
}

aAgent, err := NewAgent(cfg)
require.NoError(t, err)

defer func() {
require.NoError(t, aAgent.Close())
}()

bAgent, err := NewAgent(cfg)
require.NoError(t, err)

defer func() {
require.NoError(t, bAgent.Close())
}()

isConnected := make(chan interface{})
err = aAgent.OnConnectionStateChange(func(c ConnectionState) {
if c == ConnectionStateConnected {
close(isConnected)
}
})
require.NoError(t, err)

invalidCandidate, err := UnmarshalCandidate(fmt.Sprintf("1052353102 1 tcp 1675624447 127.0.0.1 %s typ host tcptype passive", port))
require.NoError(t, err)
require.NoError(t, aAgent.AddRemoteCandidate(invalidCandidate))
require.NoError(t, bAgent.AddRemoteCandidate(invalidCandidate))

connect(aAgent, bAgent)

<-isConnected
require.NoError(t, tcpListener.Close())
require.Equal(t, uint64(0), atomic.LoadUint64(&incomingTCPCount))
}

0 comments on commit b559a79

Please sign in to comment.