summaryrefslogtreecommitdiffhomepage
path: root/types/key/derp_test.go
blob: b91cbbf8c4e010550dbe103e5929ec0388a206dd (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
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package key

import (
	"errors"
	"testing"

	"go4.org/mem"
)

func TestDERPMeshIsValid(t *testing.T) {
	for name, tt := range map[string]struct {
		input   string
		want    string
		wantErr error
	}{
		"good": {
			input:   "0123456789012345678901234567890123456789012345678901234567890123",
			want:    "0123456789012345678901234567890123456789012345678901234567890123",
			wantErr: nil,
		},
		"hex": {
			input:   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			want:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			wantErr: nil,
		},
		"uppercase": {
			input:   "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
			want:    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			wantErr: nil,
		},
		"whitespace": {
			input:   "  0123456789012345678901234567890123456789012345678901234567890123  ",
			want:    "0123456789012345678901234567890123456789012345678901234567890123",
			wantErr: nil,
		},
		"short": {
			input:   "0123456789abcdef",
			wantErr: ErrInvalidMeshKey,
		},
		"long": {
			input:   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
			wantErr: ErrInvalidMeshKey,
		},
	} {
		t.Run(name, func(t *testing.T) {
			k, err := ParseDERPMesh(tt.input)
			if !errors.Is(err, tt.wantErr) {
				t.Errorf("err %v, want %v", err, tt.wantErr)
			}

			got := k.String()
			if got != tt.want && tt.wantErr == nil {
				t.Errorf("got %q, want %q", got, tt.want)
			}

		})
	}

}

func TestDERPMesh(t *testing.T) {
	t.Parallel()

	for name, tt := range map[string]struct {
		str   string
		hex   []byte
		equal bool // are str and hex equal?
	}{
		"zero": {
			str: "0000000000000000000000000000000000000000000000000000000000000000",
			hex: []byte{
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			},
			equal: true,
		},
		"equal": {
			str: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			hex: []byte{
				0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
				0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
				0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
				0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
			},
			equal: true,
		},
		"unequal": {
			str: "0badc0de00000000000000000000000000000000000000000000000000000000",
			hex: []byte{
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			},
			equal: false,
		},
	} {
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			k, err := ParseDERPMesh(tt.str)
			if err != nil {
				t.Fatal(err)
			}

			// string representation should round-trip
			s := k.String()
			if s != tt.str {
				t.Fatalf("string %s, want %s", s, tt.str)
			}

			// if tt.equal, then tt.hex is intended to be equal
			if k.k != [32]byte(tt.hex) && tt.equal {
				t.Fatalf("decoded %x, want %x", k.k, tt.hex)
			}

			h := DERPMeshFromRaw32(mem.B(tt.hex))
			if k.Equal(h) != tt.equal {
				if tt.equal {
					t.Fatalf("%v != %v", k, h)
				} else {
					t.Fatalf("%v == %v", k, h)
				}
			}

		})
	}
}