-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_test.go
108 lines (104 loc) · 1.98 KB
/
domain_test.go
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
package hsts
import (
"testing"
"time"
)
func TestDomain_Check(t *testing.T) {
now := time.Now().Unix()
fiveHours := int64(5 * 60 * 60)
tenHours := int64(fiveHours * 2)
tests := []struct {
name string
host string
domain Domain
expected bool
}{
{
name: "subdomain enabled",
host: "z.a.example.com",
domain: Domain{
Host: "a.example.com",
Subdomains: true,
Created: now,
MaxAge: tenHours,
},
expected: true,
},
{
name: "subdomain disabled",
host: "z.b.example.com",
domain: Domain{
Host: "a.example.com",
Subdomains: true,
Created: now,
MaxAge: tenHours,
},
expected: false,
},
{
name: "exact match",
host: "b.example.com",
domain: Domain{
Host: "b.example.com",
Subdomains: true,
Created: now,
MaxAge: tenHours,
},
expected: true,
},
{
name: "exact match with ports",
host: "b.example.com:8443",
domain: Domain{
Host: "b.example.com:8443",
Subdomains: true,
Created: now,
MaxAge: tenHours,
},
expected: true,
},
{
name: "missmatch ports",
host: "b.example.com:8443",
domain: Domain{
Host: "b.example.com:8442",
Subdomains: true,
Created: now,
MaxAge: tenHours,
},
expected: false,
},
{
name: "expired",
host: "z.example.com",
domain: Domain{
Host: "z.example.com",
Subdomains: true,
Created: now - tenHours,
MaxAge: fiveHours,
},
expected: false,
},
{
name: "permanent",
host: "z.example.com",
domain: Domain{
Host: "z.example.com",
Subdomains: true,
Permanent: true,
Created: 0,
MaxAge: 0,
},
expected: true,
},
}
for _, test := range tests {
out := test.domain.Check(test.host)
if out != test.expected {
t.Logf("host: %s", test.host)
t.Logf("want:%v", test.expected)
t.Logf("got:%v", out)
t.Fatalf("test case failed: %s", test.name)
}
}
}