| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- // Copyright 2021 the Kilo authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package wireguard
- import (
- "net"
- "testing"
- "github.com/kylelemons/godebug/pretty"
- )
- func TestNewEndpoint(t *testing.T) {
- for i, tc := range []struct {
- name string
- ip net.IP
- port int
- out *Endpoint
- }{
- {
- name: "no ip, no port",
- out: &Endpoint{
- udpAddr: &net.UDPAddr{},
- },
- },
- {
- name: "only port",
- ip: nil,
- port: 99,
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 99,
- },
- },
- },
- {
- name: "only ipv4",
- ip: net.ParseIP("10.0.0.0"),
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0").To4(),
- },
- },
- },
- {
- name: "only ipv6",
- ip: net.ParseIP("ff50::10"),
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff50::10").To16(),
- },
- },
- },
- {
- name: "ipv4",
- ip: net.ParseIP("10.0.0.0"),
- port: 1000,
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0").To4(),
- Port: 1000,
- },
- },
- },
- {
- name: "ipv6",
- ip: net.ParseIP("ff50::10"),
- port: 1000,
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff50::10").To16(),
- Port: 1000,
- },
- },
- },
- {
- name: "ipv6",
- ip: net.ParseIP("fc00:f853:ccd:e793::3"),
- port: 51820,
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("fc00:f853:ccd:e793::3").To16(),
- Port: 51820,
- },
- },
- },
- } {
- out := NewEndpoint(tc.ip, tc.port)
- if diff := pretty.Compare(out, tc.out); diff != "" {
- t.Errorf("%d %s: got diff:\n%s\n", i, tc.name, diff)
- }
- }
- }
- func TestParseEndpoint(t *testing.T) {
- for i, tc := range []struct {
- name string
- str string
- out *Endpoint
- }{
- {
- name: "no ip, no port",
- },
- {
- name: "only port",
- str: ":1000",
- },
- {
- name: "only ipv4",
- str: "10.0.0.0",
- },
- {
- name: "only ipv6",
- str: "ff50::10",
- },
- {
- name: "ipv4",
- str: "10.0.0.0:1000",
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0").To4(),
- Port: 1000,
- },
- },
- },
- {
- name: "ipv6",
- str: "[ff50::10]:1000",
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff50::10").To16(),
- Port: 1000,
- },
- },
- },
- } {
- out := ParseEndpoint(tc.str)
- if diff := pretty.Compare(out, tc.out); diff != "" {
- t.Errorf("ParseEndpoint %s(%d): got diff:\n%s\n", tc.name, i, diff)
- }
- }
- }
- func TestNewEndpointFromUDPAddr(t *testing.T) {
- for i, tc := range []struct {
- name string
- u *net.UDPAddr
- out *Endpoint
- }{
- {
- name: "no ip, no port",
- out: &Endpoint{
- addr: "",
- },
- },
- {
- name: "only port",
- u: &net.UDPAddr{
- Port: 1000,
- },
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- },
- addr: "",
- },
- },
- {
- name: "only ipv4",
- u: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0"),
- },
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0").To4(),
- },
- addr: "",
- },
- },
- {
- name: "only ipv6",
- u: &net.UDPAddr{
- IP: net.ParseIP("ff60::10"),
- },
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff60::10").To16(),
- },
- },
- },
- {
- name: "ipv4",
- u: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0"),
- Port: 1000,
- },
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0").To4(),
- Port: 1000,
- },
- },
- },
- {
- name: "ipv6",
- u: &net.UDPAddr{
- IP: net.ParseIP("ff50::10"),
- Port: 1000,
- },
- out: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff50::10").To16(),
- Port: 1000,
- },
- },
- },
- } {
- out := NewEndpointFromUDPAddr(tc.u)
- if diff := pretty.Compare(out, tc.out); diff != "" {
- t.Errorf("ParseEndpoint %s(%d): got diff:\n%s\n", tc.name, i, diff)
- }
- }
- }
- func TestReady(t *testing.T) {
- for i, tc := range []struct {
- name string
- in *Endpoint
- r bool
- }{
- {
- name: "nil",
- r: false,
- },
- {
- name: "no ip, no port",
- in: &Endpoint{
- addr: "",
- udpAddr: &net.UDPAddr{},
- },
- r: false,
- },
- {
- name: "only port",
- in: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- },
- },
- r: false,
- },
- {
- name: "only ipv4",
- in: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0"),
- },
- },
- r: false,
- },
- {
- name: "only ipv6",
- in: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff60::10"),
- },
- },
- r: false,
- },
- {
- name: "ipv4",
- in: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("10.0.0.0"),
- Port: 1000,
- },
- },
- r: true,
- },
- {
- name: "ipv6",
- in: &Endpoint{
- udpAddr: &net.UDPAddr{
- IP: net.ParseIP("ff50::10"),
- Port: 1000,
- },
- },
- r: true,
- },
- } {
- if tc.r != tc.in.Ready() {
- t.Errorf("Endpoint.Ready() %s(%d): expected=%v\tgot=%v\n", tc.name, i, tc.r, tc.in.Ready())
- }
- }
- }
- func TestEqual(t *testing.T) {
- for i, tc := range []struct {
- name string
- a *Endpoint
- b *Endpoint
- df bool
- r bool
- }{
- {
- name: "nil dns last",
- r: true,
- },
- {
- name: "nil dns first",
- df: true,
- r: true,
- },
- {
- name: "equal: only port",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- },
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- },
- },
- r: true,
- },
- {
- name: "not equal: only port",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- },
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1001,
- },
- },
- r: false,
- },
- {
- name: "equal dns first",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "example.com:1000",
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "example.com:1000",
- },
- r: true,
- },
- {
- name: "equal dns last",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "example.com:1000",
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "foo",
- },
- r: true,
- },
- {
- name: "unequal dns first",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "example.com:1000",
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "foo",
- },
- df: true,
- r: false,
- },
- {
- name: "unequal dns last",
- a: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("10.0.0.0"),
- },
- addr: "foo",
- },
- b: &Endpoint{
- udpAddr: &net.UDPAddr{
- Port: 1000,
- IP: net.ParseIP("11.0.0.0"),
- },
- addr: "foo",
- },
- r: false,
- },
- {
- name: "unequal dns last empty IP",
- a: &Endpoint{
- addr: "foo",
- },
- b: &Endpoint{
- addr: "bar",
- },
- r: false,
- },
- {
- name: "equal dns last empty IP",
- a: &Endpoint{
- addr: "foo",
- },
- b: &Endpoint{
- addr: "foo",
- },
- r: true,
- },
- } {
- if out := tc.a.Equal(tc.b, tc.df); out != tc.r {
- t.Errorf("ParseEndpoint %s(%d): expected: %v\tgot: %v\n", tc.name, i, tc.r, out)
- }
- }
- }
|