mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-30 05:25:35 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package wgcfg
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
"net/netip"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"tailscale.com/types/key"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func equal(t *testing.T, expected, actual any) bool {
|
|
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")
|
|
}
|
|
}
|
|
|
|
func BenchmarkFromUAPI(b *testing.B) {
|
|
newK := func() (key.NodePublic, key.NodePrivate) {
|
|
b.Helper()
|
|
k := key.NewNode()
|
|
return k.Public(), k
|
|
}
|
|
k1, pk1 := newK()
|
|
ip1 := netip.MustParsePrefix("10.0.0.1/32")
|
|
|
|
peer := Peer{
|
|
PublicKey: k1,
|
|
AllowedIPs: []netip.Prefix{ip1},
|
|
}
|
|
cfg1 := &Config{
|
|
PrivateKey: pk1,
|
|
Peers: []Peer{peer, peer, peer, peer},
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
w := bufio.NewWriter(buf)
|
|
if err := cfg1.ToUAPI(b.Logf, w, &Config{}); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
w.Flush()
|
|
r := bytes.NewReader(buf.Bytes())
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
r.Seek(0, io.SeekStart)
|
|
_, err := FromUAPI(r)
|
|
if err != nil {
|
|
b.Errorf("failed from UAPI: %v", err)
|
|
}
|
|
}
|
|
}
|