2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-30 04:54:34 +00:00
|
|
|
|
|
|
|
package disco
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2020-06-30 04:54:34 +00:00
|
|
|
"reflect"
|
2023-02-01 21:43:06 +00:00
|
|
|
"strings"
|
2020-06-30 04:54:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-10-30 00:16:20 +00:00
|
|
|
"go4.org/mem"
|
|
|
|
"tailscale.com/types/key"
|
2020-06-30 04:54:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalAndParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
|
|
|
m Message
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ping",
|
|
|
|
m: &Ping{
|
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
},
|
|
|
|
want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c",
|
|
|
|
},
|
2021-10-16 21:55:26 +00:00
|
|
|
{
|
|
|
|
name: "ping_with_nodekey_src",
|
|
|
|
m: &Ping{
|
2021-10-30 00:16:20 +00:00
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
NodeKey: key.NodePublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
|
2021-10-16 21:55:26 +00:00
|
|
|
},
|
|
|
|
want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f",
|
|
|
|
},
|
2023-08-08 12:11:28 +00:00
|
|
|
{
|
|
|
|
name: "ping_with_padding",
|
|
|
|
m: &Ping{
|
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
Padding: 3,
|
|
|
|
},
|
|
|
|
want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 00 00",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ping_with_padding_and_nodekey_src",
|
|
|
|
m: &Ping{
|
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
NodeKey: key.NodePublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
|
|
|
|
Padding: 3,
|
|
|
|
},
|
|
|
|
want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 00 00",
|
|
|
|
},
|
2020-06-30 04:54:34 +00:00
|
|
|
{
|
|
|
|
name: "pong",
|
|
|
|
m: &Pong{
|
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
Src: mustIPPort("2.3.4.5:1234"),
|
|
|
|
},
|
|
|
|
want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 00 00 00 00 00 00 00 00 00 ff ff 02 03 04 05 04 d2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pongv6",
|
|
|
|
m: &Pong{
|
|
|
|
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
|
|
|
|
Src: mustIPPort("[fed0::12]:6666"),
|
|
|
|
},
|
|
|
|
want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c fe d0 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1a 0a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "call_me_maybe",
|
2021-01-20 19:39:42 +00:00
|
|
|
m: &CallMeMaybe{},
|
2020-06-30 04:54:34 +00:00
|
|
|
want: "03 00",
|
|
|
|
},
|
2021-01-20 19:39:42 +00:00
|
|
|
{
|
|
|
|
name: "call_me_maybe_endpoints",
|
|
|
|
m: &CallMeMaybe{
|
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
|
|
|
MyNumber: []netip.AddrPort{
|
2022-07-26 03:55:44 +00:00
|
|
|
netip.MustParseAddrPort("1.2.3.4:567"),
|
|
|
|
netip.MustParseAddrPort("[2001::3456]:789"),
|
2021-01-20 19:39:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "03 00 00 00 00 00 00 00 00 00 00 00 ff ff 01 02 03 04 02 37 20 01 00 00 00 00 00 00 00 00 00 00 00 00 34 56 03 15",
|
|
|
|
},
|
2020-06-30 04:54:34 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
foo := []byte("foo")
|
|
|
|
got := string(tt.m.AppendMarshal(foo))
|
2023-02-01 21:43:06 +00:00
|
|
|
got, ok := strings.CutPrefix(got, "foo")
|
2022-11-18 18:13:14 +00:00
|
|
|
if !ok {
|
2020-06-30 04:54:34 +00:00
|
|
|
t.Fatalf("didn't start with foo: got %q", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotHex := fmt.Sprintf("% x", got)
|
|
|
|
if gotHex != tt.want {
|
|
|
|
t.Fatalf("wrong marshal\n got: %s\nwant: %s\n", gotHex, tt.want)
|
|
|
|
}
|
|
|
|
|
|
|
|
back, err := Parse([]byte(got))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parse back: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(back, tt.m) {
|
|
|
|
t.Errorf("message in %+v doesn't match Parse back result %+v", tt.m, back)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func mustIPPort(s string) netip.AddrPort {
|
2022-07-26 03:55:44 +00:00
|
|
|
ipp, err := netip.ParseAddrPort(s)
|
2020-06-30 04:54:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ipp
|
|
|
|
}
|