-
Notifications
You must be signed in to change notification settings - Fork 183
Support active TCP candidates (RFC6544) #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.