2018-03-10 01:56:02 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-08-21 16:25:52 +00:00
|
|
|
"context"
|
2018-03-10 01:56:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-05-26 21:55:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2018-08-21 16:25:52 +00:00
|
|
|
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
|
|
|
|
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
2019-05-26 21:55:46 +00:00
|
|
|
|
|
|
|
mux "github.com/libp2p/go-libp2p-core/mux"
|
|
|
|
yamux "github.com/libp2p/go-libp2p-yamux"
|
2018-03-10 01:56:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMuxerSimple(t *testing.T) {
|
|
|
|
// single
|
2019-05-26 21:55:46 +00:00
|
|
|
_, err := MuxerConstructor(func(_ peer.ID) mux.Multiplexer { return nil })
|
2018-03-10 01:56:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxerByValue(t *testing.T) {
|
|
|
|
_, err := MuxerConstructor(yamux.DefaultTransport)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestMuxerDuplicate(t *testing.T) {
|
2019-05-26 21:55:46 +00:00
|
|
|
_, err := MuxerConstructor(func(_ peer.ID, _ peer.ID) mux.Multiplexer { return nil })
|
2018-03-10 01:56:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxerError(t *testing.T) {
|
2019-05-26 21:55:46 +00:00
|
|
|
_, err := MuxerConstructor(func() (mux.Multiplexer, error) { return nil, nil })
|
2018-03-10 01:56:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxerBadTypes(t *testing.T) {
|
|
|
|
for i, f := range []interface{}{
|
|
|
|
func() error { return nil },
|
|
|
|
func() string { return "" },
|
|
|
|
func() {},
|
2019-05-26 21:55:46 +00:00
|
|
|
func(string) mux.Multiplexer { return nil },
|
|
|
|
func(string) (mux.Multiplexer, error) { return nil, nil },
|
2018-03-10 01:56:02 +00:00
|
|
|
nil,
|
|
|
|
"testing",
|
|
|
|
} {
|
|
|
|
|
|
|
|
if _, err := MuxerConstructor(f); err == nil {
|
|
|
|
t.Fatalf("constructor %d with type %T should have failed", i, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 16:25:52 +00:00
|
|
|
|
|
|
|
func TestCatchDuplicateTransportsMuxer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
h := bhost.New(swarmt.GenSwarm(t, ctx))
|
|
|
|
yamuxMuxer, err := MuxerConstructor(yamux.DefaultTransport)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = map[string]struct {
|
|
|
|
h host.Host
|
|
|
|
transports []MsMuxC
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
"no duplicate transports": {
|
|
|
|
h: h,
|
|
|
|
transports: []MsMuxC{MsMuxC{yamuxMuxer, "yamux"}},
|
|
|
|
expectedError: "",
|
|
|
|
},
|
|
|
|
"duplicate transports": {
|
|
|
|
h: h,
|
|
|
|
transports: []MsMuxC{
|
|
|
|
MsMuxC{yamuxMuxer, "yamux"},
|
|
|
|
MsMuxC{yamuxMuxer, "yamux"},
|
|
|
|
},
|
|
|
|
expectedError: "duplicate muxer transport: yamux",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for testName, test := range tests {
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
_, err = makeMuxer(test.h, test.transports)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() != test.expectedError {
|
|
|
|
t.Errorf(
|
|
|
|
"\nexpected: [%v]\nactual: [%v]\n",
|
|
|
|
test.expectedError,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|