2018-10-16 12:14:34 +00:00
|
|
|
package autonat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
bhost "github.com/libp2p/go-libp2p-blankhost"
|
2020-03-09 23:25:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/event"
|
2019-05-26 22:40:01 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2020-03-09 23:25:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2020-03-12 23:57:21 +00:00
|
|
|
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
|
2019-05-26 22:40:01 +00:00
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2018-10-16 12:14:34 +00:00
|
|
|
)
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
func makeAutoNATConfig(ctx context.Context, t *testing.T) *config {
|
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
dh := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
c := config{host: h, dialer: dh.Network()}
|
|
|
|
_ = defaults(&c)
|
2020-03-18 20:17:04 +00:00
|
|
|
c.forceReachability = true
|
2020-03-25 18:11:55 +00:00
|
|
|
c.dialPolicy.allowSelfDials = true
|
2020-03-12 23:57:21 +00:00
|
|
|
return &c
|
2018-10-16 12:14:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
func makeAutoNATService(ctx context.Context, t *testing.T, c *config) *autoNATService {
|
|
|
|
as, err := newAutoNATService(ctx, c)
|
2018-10-16 12:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-19 17:56:55 +00:00
|
|
|
as.Enable()
|
2018-10-16 12:14:34 +00:00
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
return as
|
2018-10-16 12:14:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
func makeAutoNATClient(ctx context.Context, t *testing.T) (host.Host, Client) {
|
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
cli := NewAutoNATClient(h, nil)
|
|
|
|
return h, cli
|
2018-10-16 12:14:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 22:40:01 +00:00
|
|
|
// Note: these tests assume that the host has only private network addresses!
|
2018-10-16 12:14:34 +00:00
|
|
|
func TestAutoNATServiceDialError(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
c := makeAutoNATConfig(ctx, t)
|
|
|
|
c.dialTimeout = 1 * time.Second
|
2020-03-25 18:11:55 +00:00
|
|
|
c.dialPolicy.allowSelfDials = false
|
2020-03-12 23:57:21 +00:00
|
|
|
_ = makeAutoNATService(ctx, t, c)
|
2018-10-16 12:14:34 +00:00
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
2020-03-12 23:57:21 +00:00
|
|
|
connect(t, c.host, hc)
|
2018-10-16 12:14:34 +00:00
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
_, err := ac.DialBack(ctx, c.host.ID())
|
2018-10-16 12:14:34 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Dial back succeeded unexpectedly!")
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
if !IsDialError(err) {
|
2018-10-16 12:14:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoNATServiceDialSuccess(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
c := makeAutoNATConfig(ctx, t)
|
|
|
|
_ = makeAutoNATService(ctx, t, c)
|
|
|
|
|
2018-10-16 12:14:34 +00:00
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
2020-03-12 23:57:21 +00:00
|
|
|
connect(t, c.host, hc)
|
2018-10-16 12:14:34 +00:00
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
_, err := ac.DialBack(ctx, c.host.ID())
|
2018-10-16 12:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial back failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoNATServiceDialRateLimiter(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
c := makeAutoNATConfig(ctx, t)
|
|
|
|
c.dialTimeout = 1 * time.Second
|
|
|
|
c.throttleResetPeriod = time.Second
|
|
|
|
c.throttleResetJitter = 0
|
|
|
|
c.throttlePeerMax = 1
|
|
|
|
_ = makeAutoNATService(ctx, t, c)
|
|
|
|
|
2018-10-16 12:14:34 +00:00
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
2020-03-12 23:57:21 +00:00
|
|
|
connect(t, c.host, hc)
|
2018-10-16 12:14:34 +00:00
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
_, err := ac.DialBack(ctx, c.host.ID())
|
2018-10-16 12:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
_, err = ac.DialBack(ctx, c.host.ID())
|
2018-10-16 12:14:34 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Dial back succeeded unexpectedly!")
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
if !IsDialRefused(err) {
|
2018-10-16 12:14:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
_, err = ac.DialBack(ctx, c.host.ID())
|
2018-10-16 12:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-13 01:04:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:25:55 +00:00
|
|
|
func TestAutoNATServiceGlobalLimiter(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
c := makeAutoNATConfig(ctx, t)
|
|
|
|
c.dialTimeout = time.Second
|
|
|
|
c.throttleResetPeriod = 10 * time.Second
|
|
|
|
c.throttleResetJitter = 0
|
|
|
|
c.throttlePeerMax = 1
|
|
|
|
c.throttleGlobalMax = 5
|
|
|
|
_ = makeAutoNATService(ctx, t, c)
|
|
|
|
|
|
|
|
hs := c.host
|
2020-03-09 23:25:55 +00:00
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
|
|
|
connect(t, hs, hc)
|
|
|
|
|
|
|
|
_, err := ac.DialBack(ctx, hs.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
|
|
|
connect(t, hs, hc)
|
|
|
|
_, err := ac.DialBack(ctx, hs.ID())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Dial back succeeded unexpectedly!")
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
if !IsDialRefused(err) {
|
2020-03-09 23:25:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:27:44 +00:00
|
|
|
func TestAutoNATServiceRateLimitJitter(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
c := makeAutoNATConfig(ctx, t)
|
|
|
|
c.throttleResetPeriod = 100 * time.Millisecond
|
|
|
|
c.throttleResetJitter = 100 * time.Millisecond
|
|
|
|
c.throttleGlobalMax = 1
|
|
|
|
svc := makeAutoNATService(ctx, t, c)
|
2020-03-06 19:27:34 +00:00
|
|
|
svc.mx.Lock()
|
2020-02-26 20:27:44 +00:00
|
|
|
svc.globalReqs = 1
|
2020-03-06 19:27:34 +00:00
|
|
|
svc.mx.Unlock()
|
2020-02-26 20:27:44 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-03-06 19:27:34 +00:00
|
|
|
|
|
|
|
svc.mx.Lock()
|
|
|
|
defer svc.mx.Unlock()
|
2020-02-26 20:27:44 +00:00
|
|
|
if svc.globalReqs != 0 {
|
|
|
|
t.Fatal("reset of rate limitter occured slower than expected")
|
|
|
|
}
|
|
|
|
|
2020-03-06 19:27:34 +00:00
|
|
|
cancel()
|
2020-02-26 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:25:55 +00:00
|
|
|
func TestAutoNATServiceStartup(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
dh := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2020-03-18 20:17:04 +00:00
|
|
|
an, err := New(ctx, h, EnableService(dh.Network()))
|
2020-03-25 18:11:55 +00:00
|
|
|
an.(*AmbientAutoNAT).config.dialPolicy.allowSelfDials = true
|
2020-03-09 23:25:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hc, ac := makeAutoNATClient(ctx, t)
|
|
|
|
connect(t, h, hc)
|
|
|
|
|
|
|
|
_, err = ac.DialBack(ctx, h.ID())
|
2020-04-07 17:55:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("autonat service be active in unknown mode.")
|
2020-03-09 23:25:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:57:21 +00:00
|
|
|
sub, _ := h.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
|
|
|
|
|
|
|
|
anc := an.(*AmbientAutoNAT)
|
|
|
|
anc.recordObservation(autoNATResult{Reachability: network.ReachabilityPublic, address: ma.StringCast("/ip4/127.0.0.1/tcp/1234")})
|
|
|
|
|
|
|
|
<-sub.Out()
|
|
|
|
|
2020-03-09 23:25:55 +00:00
|
|
|
_, err = ac.DialBack(ctx, h.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("autonat should be active, was %v", err)
|
|
|
|
}
|
2020-03-12 23:57:21 +00:00
|
|
|
if an.Status() != network.ReachabilityPublic {
|
|
|
|
t.Fatalf("autonat should report public, but didn't")
|
2020-03-09 23:25:55 +00:00
|
|
|
}
|
|
|
|
}
|