-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add an integration test for muxer selection #1887
Merged
+156
−14
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
133f002
config: supply the host ID to fx
marten-seemann 9a5face
upgrader: expose the negotiated stream muxer via the ConnState
marten-seemann 4b74c65
add an integration test for muxer selection
marten-seemann d813808
upgrader: pass values through a channel instead of sharing state
marten-seemann 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package muxer_negotiation | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/libp2p/go-libp2p" | ||
"github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/protocol" | ||
"github.com/libp2p/go-libp2p/core/sec/insecure" | ||
"github.com/libp2p/go-libp2p/p2p/muxer/mplex" | ||
"github.com/libp2p/go-libp2p/p2p/muxer/yamux" | ||
"github.com/libp2p/go-libp2p/p2p/security/noise" | ||
tls "github.com/libp2p/go-libp2p/p2p/security/tls" | ||
"github.com/libp2p/go-libp2p/p2p/transport/tcp" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
yamuxOpt = libp2p.Muxer("/yamux", yamux.DefaultTransport) | ||
mplexOpt = libp2p.Muxer("/mplex", mplex.DefaultTransport) | ||
) | ||
|
||
type testcase struct { | ||
Name string | ||
ServerPreference []libp2p.Option | ||
ClientPreference []libp2p.Option | ||
|
||
Error string | ||
Expected protocol.ID | ||
} | ||
|
||
type security struct { | ||
Name string | ||
Option libp2p.Option | ||
} | ||
|
||
func TestMuxerNegotiation(t *testing.T) { | ||
testcases := []testcase{ | ||
{ | ||
Name: "server and client have the same preference", | ||
ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt}, | ||
ClientPreference: []libp2p.Option{yamuxOpt, mplexOpt}, | ||
Expected: "/yamux", | ||
}, | ||
{ | ||
Name: "client only supports one muxer", | ||
ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt}, | ||
ClientPreference: []libp2p.Option{yamuxOpt}, | ||
Expected: "/yamux", | ||
}, | ||
{ | ||
Name: "server only supports one muxer", | ||
ServerPreference: []libp2p.Option{yamuxOpt}, | ||
ClientPreference: []libp2p.Option{mplexOpt, yamuxOpt}, | ||
Expected: "/yamux", | ||
}, | ||
{ | ||
Name: "client preference preferred", | ||
ServerPreference: []libp2p.Option{yamuxOpt, mplexOpt}, | ||
ClientPreference: []libp2p.Option{mplexOpt, yamuxOpt}, | ||
Expected: "/mplex", | ||
}, | ||
{ | ||
Name: "no preference overlap", | ||
ServerPreference: []libp2p.Option{yamuxOpt}, | ||
ClientPreference: []libp2p.Option{mplexOpt}, | ||
Error: "failed to negotiate stream multiplexer: protocol not supported", | ||
}, | ||
} | ||
|
||
clientID, _, err := crypto.GenerateEd25519Key(rand.Reader) | ||
require.NoError(t, err) | ||
serverID, _, err := crypto.GenerateEd25519Key(rand.Reader) | ||
require.NoError(t, err) | ||
|
||
securities := []security{ | ||
{Name: "noise", Option: libp2p.Security("/noise", noise.New)}, | ||
{Name: "tls", Option: libp2p.Security("/tls", tls.New)}, | ||
{Name: "insecure", Option: libp2p.Security("/insecure", insecure.NewWithIdentity)}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
tc := tc | ||
|
||
for _, sec := range securities { | ||
sec := sec | ||
|
||
t.Run(fmt.Sprintf("%s: %s", sec.Name, tc.Name), func(t *testing.T) { | ||
server, err := libp2p.New( | ||
libp2p.Identity(serverID), | ||
sec.Option, | ||
libp2p.ChainOptions(tc.ServerPreference...), | ||
libp2p.Transport(tcp.NewTCPTransport), | ||
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), | ||
) | ||
require.NoError(t, err) | ||
|
||
client, err := libp2p.New( | ||
libp2p.Identity(clientID), | ||
sec.Option, | ||
libp2p.ChainOptions(tc.ClientPreference...), | ||
libp2p.Transport(tcp.NewTCPTransport), | ||
libp2p.NoListenAddrs, | ||
) | ||
require.NoError(t, err) | ||
|
||
err = client.Connect(context.Background(), peer.AddrInfo{ID: server.ID(), Addrs: server.Addrs()}) | ||
if tc.Error != "" { | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, tc.Error) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
conns := client.Network().ConnsToPeer(server.ID()) | ||
require.Len(t, conns, 1, "expected exactly one connection") | ||
require.Equal(t, tc.Expected, protocol.ID(conns[0].ConnState().NextProto)) | ||
}) | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? Did this not work before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The insecure transport consume the
peer.ID
:go-libp2p/core/sec/insecure/insecure.go
Line 43 in d8d2efa
The other transports consume the private key.