Browse Source

Merge pull request #398 from libp2p/feat/no-default-listen

add options for disabling the default listen address/transports
pull/369/head
Steven Allen 6 years ago
committed by GitHub
parent
commit
cacf6f42b5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      libp2p_test.go
  2. 18
      options.go

36
libp2p_test.go

@ -9,6 +9,7 @@ import (
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-tcp-transport"
)
@ -32,6 +33,41 @@ func TestBadTransportConstructor(t *testing.T) {
}
}
func TestNoListenAddrs(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, NoListenAddrs)
if err != nil {
t.Fatal(err)
}
defer h.Close()
if len(h.Addrs()) != 0 {
t.Fatal("expected no addresses")
}
}
func TestNoTransports(t *testing.T) {
ctx := context.Background()
a, err := New(ctx, NoTransports)
if err != nil {
t.Fatal(err)
}
defer a.Close()
b, err := New(ctx, ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}
defer b.Close()
err = a.Connect(ctx, pstore.PeerInfo{
ID: b.ID(),
Addrs: b.Addrs(),
})
if err == nil {
t.Error("dial should have failed as no transports have been configured")
}
}
func TestInsecure(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, NoSecurity)

18
options.go

@ -241,3 +241,21 @@ func NATManager(nm config.NATManagerC) Option {
return nil
}
}
// NoListenAddrs will configure libp2p to not listen by default.
//
// This will both clear any configured listen addrs and prevent libp2p from
// applying the default listen address option.
var NoListenAddrs = func(cfg *Config) error {
cfg.ListenAddrs = []ma.Multiaddr{}
return nil
}
// NoTransports will configure libp2p to not enable any transports.
//
// This will both clear any configured transports (specified in prior libp2p
// options) and prevent libp2p from applying the default transports.
var NoTransports = func(cfg *Config) error {
cfg.Transports = []config.TptC{}
return nil
}

Loading…
Cancel
Save