summaryrefslogtreecommitdiffhomepage
path: root/net/packet/tsmp_test.go
blob: 01bb836d7697196c5816f9369153ae1469128130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package packet

import (
	"bytes"
	"encoding/hex"
	"net/netip"
	"slices"
	"testing"

	"go4.org/mem"
	"tailscale.com/types/key"
)

func TestTailscaleRejectedHeader(t *testing.T) {
	tests := []struct {
		h       TailscaleRejectedHeader
		wantStr string
	}{
		{
			h: TailscaleRejectedHeader{
				IPSrc:  netip.MustParseAddr("5.5.5.5"),
				IPDst:  netip.MustParseAddr("1.2.3.4"),
				Src:    netip.MustParseAddrPort("1.2.3.4:567"),
				Dst:    netip.MustParseAddrPort("5.5.5.5:443"),
				Proto:  TCP,
				Reason: RejectedDueToACLs,
			},
			wantStr: "TSMP-reject-flow{TCP 1.2.3.4:567 > 5.5.5.5:443}: acl",
		},
		{
			h: TailscaleRejectedHeader{
				IPSrc:  netip.MustParseAddr("2::2"),
				IPDst:  netip.MustParseAddr("1::1"),
				Src:    netip.MustParseAddrPort("[1::1]:567"),
				Dst:    netip.MustParseAddrPort("[2::2]:443"),
				Proto:  UDP,
				Reason: RejectedDueToShieldsUp,
			},
			wantStr: "TSMP-reject-flow{UDP [1::1]:567 > [2::2]:443}: shields",
		},
		{
			h: TailscaleRejectedHeader{
				IPSrc:       netip.MustParseAddr("2::2"),
				IPDst:       netip.MustParseAddr("1::1"),
				Src:         netip.MustParseAddrPort("[1::1]:567"),
				Dst:         netip.MustParseAddrPort("[2::2]:443"),
				Proto:       UDP,
				Reason:      RejectedDueToIPForwarding,
				MaybeBroken: true,
			},
			wantStr: "TSMP-reject-flow{UDP [1::1]:567 > [2::2]:443}: host-ip-forwarding-unavailable",
		},
	}
	for i, tt := range tests {
		gotStr := tt.h.String()
		if gotStr != tt.wantStr {
			t.Errorf("%v. String = %q; want %q", i, gotStr, tt.wantStr)
			continue
		}
		pkt := make([]byte, tt.h.Len())
		tt.h.Marshal(pkt)

		var p Parsed
		p.Decode(pkt)
		t.Logf("Parsed: %+v", p)
		t.Logf("Parsed: %s", p.String())
		back, ok := p.AsTailscaleRejectedHeader()
		if !ok {
			t.Errorf("%v. %q (%02x) didn't parse back", i, gotStr, pkt)
			continue
		}
		if back != tt.h {
			t.Errorf("%v. %q parsed back as %q", i, tt.h, back)
		}
	}
}

func TestTSMPDiscoKeyAdvertisementMarshal(t *testing.T) {
	var (
		// IPv4: Ver(4)Len(5), TOS, Len(53), ID, Flags, TTL(64), Proto(99), Cksum
		headerV4, _ = hex.DecodeString("45000035000000004063705d")
		// IPv6: Ver(6)TCFlow, Len(33), NextHdr(99), HopLim(64)
		headerV6, _ = hex.DecodeString("6000000000216340")

		packetType = []byte{'a'}
		testKey    = bytes.Repeat([]byte{'a'}, 32)

		// IPs
		srcV4 = netip.MustParseAddr("1.2.3.4")
		dstV4 = netip.MustParseAddr("4.3.2.1")
		srcV6 = netip.MustParseAddr("2001:db8::1")
		dstV6 = netip.MustParseAddr("2001:db8::2")
	)

	join := func(parts ...[]byte) []byte {
		return bytes.Join(parts, nil)
	}

	tests := []struct {
		name string
		tka  TSMPDiscoKeyAdvertisement
		want []byte
	}{
		{
			name: "v4Header",
			tka: TSMPDiscoKeyAdvertisement{
				Src: srcV4,
				Dst: dstV4,
				Key: key.DiscoPublicFromRaw32(mem.B(testKey)),
			},
			want: join(headerV4, srcV4.AsSlice(), dstV4.AsSlice(), packetType, testKey),
		},
		{
			name: "v6Header",
			tka: TSMPDiscoKeyAdvertisement{
				Src: srcV6,
				Dst: dstV6,
				Key: key.DiscoPublicFromRaw32(mem.B(testKey)),
			},
			want: join(headerV6, srcV6.AsSlice(), dstV6.AsSlice(), packetType, testKey),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := tt.tka.Marshal()
			if err != nil {
				t.Errorf("error mashalling TSMPDiscoAdvertisement: %s", err)
			}
			if !slices.Equal(got, tt.want) {
				t.Errorf("error mashalling TSMPDiscoAdvertisement, expected: \n%x, \ngot:\n%x", tt.want, got)
			}
		})
	}
}