2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-02-20 06:15:41 +00:00
|
|
|
|
|
|
|
package portmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2021-08-09 19:52:15 +00:00
|
|
|
"reflect"
|
2021-02-20 06:15:41 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-09-11 19:03:39 +00:00
|
|
|
|
|
|
|
"tailscale.com/control/controlknobs"
|
2021-02-20 06:15:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateOrGetMapping(t *testing.T) {
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
|
|
|
|
t.Skip("skipping test without HIT_NETWORK=1")
|
|
|
|
}
|
2023-09-11 19:03:39 +00:00
|
|
|
c := NewClient(t.Logf, nil, nil, new(controlknobs.Knobs), nil)
|
2021-06-22 22:29:01 +00:00
|
|
|
defer c.Close()
|
2021-02-20 06:15:41 +00:00
|
|
|
c.SetLocalPort(1234)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2021-07-09 17:01:50 +00:00
|
|
|
ext, err := c.createOrGetMapping(context.Background())
|
2021-02-20 06:15:41 +00:00
|
|
|
t.Logf("Got: %v, %v", ext, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientProbe(t *testing.T) {
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
|
|
|
|
t.Skip("skipping test without HIT_NETWORK=1")
|
|
|
|
}
|
2023-09-11 19:03:39 +00:00
|
|
|
c := NewClient(t.Logf, nil, nil, new(controlknobs.Knobs), nil)
|
2021-06-22 22:29:01 +00:00
|
|
|
defer c.Close()
|
|
|
|
for i := 0; i < 3; i++ {
|
2021-02-20 06:15:41 +00:00
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
res, err := c.Probe(context.Background())
|
2021-06-22 22:29:01 +00:00
|
|
|
t.Logf("Got(t=%dms): %+v, %v", i*100, res, err)
|
2021-02-20 06:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientProbeThenMap(t *testing.T) {
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
|
|
|
|
t.Skip("skipping test without HIT_NETWORK=1")
|
|
|
|
}
|
2023-09-11 19:03:39 +00:00
|
|
|
c := NewClient(t.Logf, nil, nil, new(controlknobs.Knobs), nil)
|
2021-06-22 22:29:01 +00:00
|
|
|
defer c.Close()
|
2021-02-20 06:15:41 +00:00
|
|
|
c.SetLocalPort(1234)
|
|
|
|
res, err := c.Probe(context.Background())
|
|
|
|
t.Logf("Probe: %+v, %v", res, err)
|
2021-07-09 17:01:50 +00:00
|
|
|
ext, err := c.createOrGetMapping(context.Background())
|
|
|
|
t.Logf("createOrGetMapping: %v, %v", ext, err)
|
2021-02-20 06:15:41 +00:00
|
|
|
}
|
2021-08-05 17:32:13 +00:00
|
|
|
|
|
|
|
func TestProbeIntegration(t *testing.T) {
|
2021-08-06 19:01:23 +00:00
|
|
|
igd, err := NewTestIGD(t.Logf, TestIGDOptions{PMP: true, PCP: true, UPnP: true})
|
2021-08-05 17:32:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer igd.Close()
|
|
|
|
|
2021-08-06 19:01:23 +00:00
|
|
|
c := newTestClient(t, igd)
|
2021-08-09 19:52:15 +00:00
|
|
|
t.Logf("Listening on pxp=%v, upnp=%v", c.testPxPPort, c.testUPnPPort)
|
2021-08-06 19:01:23 +00:00
|
|
|
defer c.Close()
|
2021-08-05 17:32:13 +00:00
|
|
|
|
|
|
|
res, err := c.Probe(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Probe: %v", err)
|
|
|
|
}
|
2021-08-09 19:52:15 +00:00
|
|
|
if !res.UPnP {
|
|
|
|
t.Errorf("didn't detect UPnP")
|
|
|
|
}
|
|
|
|
st := igd.stats()
|
|
|
|
want := igdCounters{
|
|
|
|
numUPnPDiscoRecv: 1,
|
|
|
|
numPMPRecv: 1,
|
|
|
|
numPCPRecv: 1,
|
2021-08-06 19:01:23 +00:00
|
|
|
numPCPDiscoRecv: 1,
|
2021-08-09 19:52:15 +00:00
|
|
|
numPMPPublicAddrRecv: 1,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(st, want) {
|
|
|
|
t.Errorf("unexpected stats:\n got: %+v\nwant: %+v", st, want)
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:32:13 +00:00
|
|
|
t.Logf("Probe: %+v", res)
|
2021-08-09 19:52:15 +00:00
|
|
|
t.Logf("IGD stats: %+v", st)
|
2021-08-05 17:32:13 +00:00
|
|
|
// TODO(bradfitz): finish
|
|
|
|
}
|
2021-08-06 19:01:23 +00:00
|
|
|
|
|
|
|
func TestPCPIntegration(t *testing.T) {
|
|
|
|
igd, err := NewTestIGD(t.Logf, TestIGDOptions{PMP: false, PCP: true, UPnP: false})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer igd.Close()
|
|
|
|
|
|
|
|
c := newTestClient(t, igd)
|
|
|
|
defer c.Close()
|
|
|
|
res, err := c.Probe(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("probe failed: %v", err)
|
|
|
|
}
|
|
|
|
if res.UPnP || res.PMP {
|
|
|
|
t.Errorf("probe unexpectedly saw upnp or pmp: %+v", res)
|
|
|
|
}
|
|
|
|
if !res.PCP {
|
|
|
|
t.Fatalf("probe did not see pcp: %+v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
external, err := c.createOrGetMapping(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get mapping: %v", err)
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if !external.IsValid() {
|
2021-08-06 19:01:23 +00:00
|
|
|
t.Errorf("got zero IP, expected non-zero")
|
|
|
|
}
|
|
|
|
if c.mapping == nil {
|
|
|
|
t.Errorf("got nil mapping after successful createOrGetMapping")
|
|
|
|
}
|
|
|
|
}
|