-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement active TCP candidate type (RFC6544)
By default TCP candidate type priority is UDP one minus 27 (except relay), so that UDP+srlfx priority > TCP+host priority. That priority offset can be configured using AgentConfig. IPv6 TCP candidates are also supported.
- Loading branch information
1 parent
898746c
commit 1d502ca
Showing
11 changed files
with
358 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build !js | ||
// +build !js | ||
|
||
package ice | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pion/logging" | ||
"github.com/pion/transport/v2/stdnet" | ||
"github.com/pion/transport/v2/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func getLocalIPAddress(t *testing.T, networkType NetworkType) net.IP { | ||
net, err := stdnet.NewNet() | ||
require.NoError(t, err) | ||
localIPs, err := localInterfaces(net, nil, nil, []NetworkType{networkType}, false) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, localIPs) | ||
return localIPs[0] | ||
} | ||
|
||
func ipv6Available(t *testing.T) bool { | ||
net, err := stdnet.NewNet() | ||
require.NoError(t, err) | ||
localIPs, err := localInterfaces(net, nil, nil, []NetworkType{NetworkTypeTCP6}, false) | ||
require.NoError(t, err) | ||
return len(localIPs) > 0 | ||
} | ||
|
||
func TestAgentActiveTCP(t *testing.T) { | ||
report := test.CheckRoutines(t) | ||
defer report() | ||
|
||
lim := test.TimeOut(time.Second * 5) | ||
defer lim.Stop() | ||
|
||
const listenPort = 7686 | ||
type testCase struct { | ||
name string | ||
networkTypes []NetworkType | ||
listenIPAddress net.IP | ||
selectedPairNetworkType string | ||
} | ||
testCases := []testCase{ | ||
{ | ||
name: "TCP4 connection", | ||
networkTypes: []NetworkType{NetworkTypeTCP4}, | ||
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP4), | ||
selectedPairNetworkType: tcp, | ||
}, | ||
{ | ||
name: "UDP is preferred over TCP4", // fails some time | ||
networkTypes: supportedNetworkTypes(), | ||
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP4), | ||
selectedPairNetworkType: udp, | ||
}, | ||
} | ||
|
||
if ipv6Available(t) { | ||
tcpv6Cases := []testCase{ | ||
{ | ||
name: "TCP6 connection", | ||
networkTypes: []NetworkType{NetworkTypeTCP6}, | ||
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6), | ||
selectedPairNetworkType: tcp, | ||
}, | ||
{ | ||
name: "UDP is preferred over TCP6", // fails some time | ||
networkTypes: supportedNetworkTypes(), | ||
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6), | ||
selectedPairNetworkType: udp, | ||
}, | ||
} | ||
testCases = append(testCases, tcpv6Cases...) | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
r := require.New(t) | ||
|
||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{ | ||
IP: testCase.listenIPAddress, | ||
Port: listenPort, | ||
}) | ||
r.NoError(err) | ||
defer func() { | ||
_ = listener.Close() | ||
}() | ||
|
||
loggerFactory := logging.NewDefaultLoggerFactory() | ||
loggerFactory.DefaultLogLevel.Set(logging.LogLevelTrace) | ||
|
||
tcpMux := NewTCPMuxDefault(TCPMuxParams{ | ||
Listener: listener, | ||
Logger: loggerFactory.NewLogger("passive-ice-tcp-mux"), | ||
ReadBufferSize: 20, | ||
}) | ||
|
||
defer func() { | ||
_ = tcpMux.Close() | ||
}() | ||
|
||
r.NotNil(tcpMux.LocalAddr(), "tcpMux.LocalAddr() is nil") | ||
|
||
hostAcceptanceMinWait := 100 * time.Millisecond | ||
passiveAgent, err := NewAgent(&AgentConfig{ | ||
TCPMux: tcpMux, | ||
CandidateTypes: []CandidateType{CandidateTypeHost}, | ||
NetworkTypes: testCase.networkTypes, | ||
LoggerFactory: loggerFactory, | ||
IncludeLoopback: true, | ||
HostAcceptanceMinWait: &hostAcceptanceMinWait, | ||
}) | ||
r.NoError(err) | ||
r.NotNil(passiveAgent) | ||
|
||
activeAgent, err := NewAgent(&AgentConfig{ | ||
CandidateTypes: []CandidateType{CandidateTypeHost}, | ||
NetworkTypes: testCase.networkTypes, | ||
LoggerFactory: loggerFactory, | ||
HostAcceptanceMinWait: &hostAcceptanceMinWait, | ||
}) | ||
r.NoError(err) | ||
r.NotNil(activeAgent) | ||
|
||
passiveAgentConn, activeAgenConn := connect(passiveAgent, activeAgent) | ||
r.NotNil(passiveAgentConn) | ||
r.NotNil(activeAgenConn) | ||
|
||
pair := passiveAgent.getSelectedPair() | ||
r.NotNil(pair) | ||
r.Equal(testCase.selectedPairNetworkType, pair.Local.NetworkType().NetworkShort()) | ||
|
||
foo := []byte("foo") | ||
_, err = passiveAgentConn.Write(foo) | ||
r.NoError(err) | ||
|
||
buffer := make([]byte, 1024) | ||
n, err := activeAgenConn.Read(buffer) | ||
r.NoError(err) | ||
r.Equal(foo, buffer[:n]) | ||
|
||
bar := []byte("bar") | ||
_, err = activeAgenConn.Write(bar) | ||
r.NoError(err) | ||
|
||
n, err = passiveAgentConn.Read(buffer) | ||
r.NoError(err) | ||
r.Equal(bar, buffer[:n]) | ||
|
||
r.NoError(activeAgenConn.Close()) | ||
r.NoError(passiveAgentConn.Close()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.