Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 9e6ea7e

Browse files
authored
Merge pull request #90 from libp2p/fix/warn-on-useless-transport
warn when we encounter a useless transport
2 parents ebb79a9 + adaa228 commit 9e6ea7e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

swarm_transport.go

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (s *Swarm) TransportForListening(a ma.Multiaddr) transport.Transport {
7070
func (s *Swarm) AddTransport(t transport.Transport) error {
7171
protocols := t.Protocols()
7272

73+
if len(protocols) == 0 {
74+
return fmt.Errorf("useless transport handles no protocols: %T", t)
75+
}
76+
7377
s.transports.Lock()
7478
defer s.transports.Unlock()
7579
var registered []string

transport_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package swarm_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
8+
9+
peer "github.com/libp2p/go-libp2p-peer"
10+
transport "github.com/libp2p/go-libp2p-transport"
11+
ma "github.com/multiformats/go-multiaddr"
12+
)
13+
14+
type dummyTransport struct {
15+
protocols []int
16+
proxy bool
17+
}
18+
19+
func (dt *dummyTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.Conn, error) {
20+
panic("unimplemented")
21+
}
22+
23+
func (dt *dummyTransport) CanDial(addr ma.Multiaddr) bool {
24+
panic("unimplemented")
25+
}
26+
27+
func (dt *dummyTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
28+
panic("unimplemented")
29+
}
30+
31+
func (dt *dummyTransport) Proxy() bool {
32+
return dt.proxy
33+
}
34+
35+
func (dt *dummyTransport) Protocols() []int {
36+
return dt.protocols
37+
}
38+
39+
func TestUselessTransport(t *testing.T) {
40+
ctx, cancel := context.WithCancel(context.Background())
41+
defer cancel()
42+
swarm := swarmt.GenSwarm(t, ctx)
43+
err := swarm.AddTransport(new(dummyTransport))
44+
if err == nil {
45+
t.Fatal("adding a transport that supports no protocols should have failed")
46+
}
47+
}

0 commit comments

Comments
 (0)