2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-01-29 20:16:36 +00:00
|
|
|
|
|
|
|
package wgcfg
|
|
|
|
|
|
|
|
import (
|
2021-07-13 02:48:32 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-07-13 17:12:47 +00:00
|
|
|
"io"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-01-29 20:16:36 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2021-07-13 02:48:32 +00:00
|
|
|
|
2021-10-28 00:42:33 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-01-29 20:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func noError(t *testing.T, err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, fn, line, _ := runtime.Caller(1)
|
|
|
|
t.Errorf("Error at %s:%d: %#v", fn, line, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
func equal(t *testing.T, expected, actual any) bool {
|
2021-01-29 20:16:36 +00:00
|
|
|
if reflect.DeepEqual(expected, actual) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, fn, line, _ := runtime.Caller(1)
|
|
|
|
t.Errorf("Failed equals at %s:%d\nactual %#v\nexpected %#v", fn, line, actual, expected)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseEndpoint(t *testing.T) {
|
|
|
|
_, _, err := parseEndpoint("[192.168.42.0:]:51880")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Error was expected")
|
|
|
|
}
|
|
|
|
host, port, err := parseEndpoint("192.168.42.0:51880")
|
|
|
|
if noError(t, err) {
|
|
|
|
equal(t, "192.168.42.0", host)
|
|
|
|
equal(t, uint16(51880), port)
|
|
|
|
}
|
|
|
|
host, port, err = parseEndpoint("test.wireguard.com:18981")
|
|
|
|
if noError(t, err) {
|
|
|
|
equal(t, "test.wireguard.com", host)
|
|
|
|
equal(t, uint16(18981), port)
|
|
|
|
}
|
|
|
|
host, port, err = parseEndpoint("[2607:5300:60:6b0::c05f:543]:2468")
|
|
|
|
if noError(t, err) {
|
|
|
|
equal(t, "2607:5300:60:6b0::c05f:543", host)
|
|
|
|
equal(t, uint16(2468), port)
|
|
|
|
}
|
|
|
|
_, _, err = parseEndpoint("[::::::invalid:18981")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Error was expected")
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 02:48:32 +00:00
|
|
|
|
|
|
|
func BenchmarkFromUAPI(b *testing.B) {
|
2021-10-28 00:42:33 +00:00
|
|
|
newK := func() (key.NodePublic, key.NodePrivate) {
|
2021-07-13 02:48:32 +00:00
|
|
|
b.Helper()
|
2021-10-28 00:42:33 +00:00
|
|
|
k := key.NewNode()
|
|
|
|
return k.Public(), k
|
2021-07-13 02:48:32 +00:00
|
|
|
}
|
2021-10-28 00:42:33 +00:00
|
|
|
k1, pk1 := newK()
|
2022-07-26 03:55:44 +00:00
|
|
|
ip1 := netip.MustParsePrefix("10.0.0.1/32")
|
2021-07-13 02:48:32 +00:00
|
|
|
|
|
|
|
peer := Peer{
|
|
|
|
PublicKey: k1,
|
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 04:14:09 +00:00
|
|
|
AllowedIPs: []netip.Prefix{ip1},
|
2021-07-13 02:48:32 +00:00
|
|
|
}
|
|
|
|
cfg1 := &Config{
|
2021-10-28 00:42:33 +00:00
|
|
|
PrivateKey: pk1,
|
2021-07-13 02:48:32 +00:00
|
|
|
Peers: []Peer{peer, peer, peer, peer},
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := bufio.NewWriter(buf)
|
2021-11-11 02:42:16 +00:00
|
|
|
if err := cfg1.ToUAPI(b.Logf, w, &Config{}); err != nil {
|
2021-07-13 02:48:32 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
r := bytes.NewReader(buf.Bytes())
|
|
|
|
b.ReportAllocs()
|
2024-04-16 20:15:13 +00:00
|
|
|
for range b.N {
|
2021-07-13 17:12:47 +00:00
|
|
|
r.Seek(0, io.SeekStart)
|
2021-07-13 02:48:32 +00:00
|
|
|
_, err := FromUAPI(r)
|
|
|
|
if err != nil {
|
|
|
|
b.Errorf("failed from UAPI: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|