Skip to content

Commit

Permalink
Merge pull request #90 from libp2p/fix/warn-on-useless-transport
Browse files Browse the repository at this point in the history
warn when we encounter a useless transport
  • Loading branch information
Stebalien authored Nov 16, 2018
2 parents df89bf4 + 374d8d9 commit c94e1b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions p2p/net/swarm/swarm_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions p2p/net/swarm/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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")
}
}

0 comments on commit c94e1b9

Please sign in to comment.