Commit adaa228f authored by Steven Allen's avatar Steven Allen

warn when we encounter a useless transport

parent ebb79a95
......@@ -70,6 +70,10 @@ func (s *Swarm) TransportForListening(a ma.Multiaddr) transport.Transport {
func (s *Swarm) AddTransport(t transport.Transport) error {
protocols := t.Protocols()
if len(protocols) == 0 {
return fmt.Errorf("useless transport handles no protocols: %T", t)
}
s.transports.Lock()
defer s.transports.Unlock()
var registered []string
......
package swarm_test
import (
"context"
"testing"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
peer "github.com/libp2p/go-libp2p-peer"
transport "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)
type dummyTransport struct {
protocols []int
proxy bool
}
func (dt *dummyTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.Conn, error) {
panic("unimplemented")
}
func (dt *dummyTransport) CanDial(addr ma.Multiaddr) bool {
panic("unimplemented")
}
func (dt *dummyTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
panic("unimplemented")
}
func (dt *dummyTransport) Proxy() bool {
return dt.proxy
}
func (dt *dummyTransport) Protocols() []int {
return dt.protocols
}
func TestUselessTransport(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
swarm := swarmt.GenSwarm(t, ctx)
err := swarm.AddTransport(new(dummyTransport))
if err == nil {
t.Fatal("adding a transport that supports no protocols should have failed")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment