-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
146 lines (138 loc) · 3.43 KB
/
validate_test.go
File metadata and controls
146 lines (138 loc) · 3.43 KB
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
139
140
141
142
143
144
145
146
package main
import (
"strings"
"testing"
)
func TestValidEmail(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"valid address", "user@example.com", true},
{"empty string", "", false},
{"no at sign", "noat", false},
{"no dot in domain", "no@dot", false},
{"over 254 chars", strings.Repeat("a", 243) + "@example.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validEmail(tt.input); got != tt.want {
t.Errorf("validEmail(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestMaskEmail(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"normal address", "user@example.com", "*@example.com"},
{"no at sign", "noat", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maskEmail(tt.input); got != tt.want {
t.Errorf("maskEmail(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestValidPassword(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"too short", "short", false},
{"valid 9 chars", "validpass", true},
{"valid 8 digits", "12345678", true},
{"too long 65 chars", strings.Repeat("a", 65), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validPassword(tt.input); got != tt.want {
t.Errorf("validPassword(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidLabel(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"valid hyphenated", "my-host", true},
{"empty string", "", false},
{"has space", "has space", false},
{"has underscore", "has_underscore", false},
{"has dot", "has.dot", false},
{"too long 33 chars", strings.Repeat("a", 33), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validLabel(tt.input); got != tt.want {
t.Errorf("validLabel(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidEndpoint(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"valid ip:port", "1.2.3.4:51820", true},
{"no port", "noport", false},
{"empty string", "", false},
{"too long 254 chars", strings.Repeat("a", 254), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validEndpoint(tt.input); got != tt.want {
t.Errorf("validEndpoint(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidAllowedIPs(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"single host /32", "10.0.0.2/32", true},
{"subnet /24", "10.0.0.0/24", true},
{"not CIDR", "notcidr", false},
{"missing prefix length", "10.0.0.2", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validAllowedIPs(tt.input); got != tt.want {
t.Errorf("validAllowedIPs(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidWGPubkey(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"valid base64 key", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=", true},
{"too short", "short", false},
{"44 chars no trailing =", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validWGPubkey(tt.input); got != tt.want {
t.Errorf("validWGPubkey(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}