2020-07-07 06:33:29 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tsaddr
|
|
|
|
|
2020-12-29 00:48:43 +00:00
|
|
|
import (
|
2022-07-26 04:55:44 +01:00
|
|
|
"net/netip"
|
2020-12-29 00:48:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-07-25 04:08:42 +01:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-12-29 00:48:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInCrostiniRange(t *testing.T) {
|
|
|
|
tests := []struct {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
ip netip.Addr
|
2020-12-29 00:48:43 +00:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{netaddr.IPv4(192, 168, 0, 1), false},
|
|
|
|
{netaddr.IPv4(100, 101, 102, 103), false},
|
|
|
|
{netaddr.IPv4(100, 115, 92, 0), true},
|
|
|
|
{netaddr.IPv4(100, 115, 92, 5), true},
|
|
|
|
{netaddr.IPv4(100, 115, 92, 255), true},
|
|
|
|
{netaddr.IPv4(100, 115, 93, 40), true},
|
|
|
|
{netaddr.IPv4(100, 115, 94, 1), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if got := ChromeOSVMRange().Contains(test.ip); got != test.want {
|
|
|
|
t.Errorf("inCrostiniRange(%q) = %v, want %v", test.ip, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 06:33:29 +01:00
|
|
|
|
2022-01-04 21:33:08 +00:00
|
|
|
func TestTailscaleServiceIPv6(t *testing.T) {
|
|
|
|
got := TailscaleServiceIPv6().String()
|
|
|
|
want := "fd7a:115c:a1e0::53"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 06:33:29 +01:00
|
|
|
func TestChromeOSVMRange(t *testing.T) {
|
|
|
|
if got, want := ChromeOSVMRange().String(), "100.115.92.0/23"; got != want {
|
|
|
|
t.Errorf("got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCGNATRange(t *testing.T) {
|
|
|
|
if got, want := CGNATRange().String(), "100.64.0.0/10"; got != want {
|
|
|
|
t.Errorf("got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 02:28:57 +01:00
|
|
|
|
2021-05-05 20:58:40 +01:00
|
|
|
func TestNewContainsIPFunc(t *testing.T) {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
f := NewContainsIPFunc([]netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")})
|
2022-07-26 04:55:44 +01:00
|
|
|
if f(netip.MustParseAddr("8.8.8.8")) {
|
2021-05-05 20:58:40 +01:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
2022-07-26 04:55:44 +01:00
|
|
|
if !f(netip.MustParseAddr("10.1.2.3")) {
|
2021-05-05 20:58:40 +01:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
f = NewContainsIPFunc([]netip.Prefix{netip.MustParsePrefix("10.1.2.3/32")})
|
2022-07-26 04:55:44 +01:00
|
|
|
if !f(netip.MustParseAddr("10.1.2.3")) {
|
2021-05-05 20:58:40 +01:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
f = NewContainsIPFunc([]netip.Prefix{
|
2022-07-26 04:55:44 +01:00
|
|
|
netip.MustParsePrefix("10.1.2.3/32"),
|
|
|
|
netip.MustParsePrefix("::2/128"),
|
2021-05-05 20:58:40 +01:00
|
|
|
})
|
2022-07-26 04:55:44 +01:00
|
|
|
if !f(netip.MustParseAddr("::2")) {
|
2021-05-05 20:58:40 +01:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
f = NewContainsIPFunc([]netip.Prefix{
|
2022-07-26 04:55:44 +01:00
|
|
|
netip.MustParsePrefix("10.1.2.3/32"),
|
|
|
|
netip.MustParsePrefix("10.1.2.4/32"),
|
|
|
|
netip.MustParsePrefix("::2/128"),
|
2021-05-05 20:58:40 +01:00
|
|
|
})
|
2022-07-26 04:55:44 +01:00
|
|
|
if !f(netip.MustParseAddr("10.1.2.4")) {
|
2021-05-05 20:58:40 +01:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 04:13:01 +01:00
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
var sinkIP netip.Addr
|
2021-05-29 04:13:01 +01:00
|
|
|
|
|
|
|
func BenchmarkTailscaleServiceAddr(b *testing.B) {
|
2021-08-31 04:43:18 +01:00
|
|
|
b.ReportAllocs()
|
2021-05-29 04:13:01 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sinkIP = TailscaleServiceIP()
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 16:47:16 +01:00
|
|
|
|
|
|
|
func TestUnmapVia(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
ip string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"1.2.3.4", "1.2.3.4"}, // unchanged v4
|
|
|
|
{"fd7a:115c:a1e0:b1a::bb:10.2.1.3", "10.2.1.3"},
|
|
|
|
{"fd7a:115c:a1e0:b1b::bb:10.2.1.4", "fd7a:115c:a1e0:b1b:0:bb:a02:104"}, // "b1b",not "bia"
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2022-07-26 04:55:44 +01:00
|
|
|
if got := UnmapVia(netip.MustParseAddr(tt.ip)).String(); got != tt.want {
|
2022-03-30 16:47:16 +01:00
|
|
|
t.Errorf("for %q: got %q, want %q", tt.ip, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|