-
Notifications
You must be signed in to change notification settings - Fork 60
/
run_test.go
117 lines (104 loc) · 2.81 KB
/
run_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
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"io"
"net/url"
"os"
"path"
"testing"
"golang.org/x/xerrors"
"sigs.k8s.io/yaml"
)
type Item struct {
Domain string `json:"domain"`
Evidence string `json:"evidence"`
Original string `json:"original"`
Note string `json:"note"`
}
func TestDomainList(t *testing.T) {
filename := os.Getenv("YAML")
if len(filename) == 0 {
filename = "domain-list.yml"
}
tests, err := loadItems(filename)
if err != nil {
t.Fatal(err)
}
for i, tt := range tests {
i := i
tt := tt
name := fmt.Sprintf("%d-th item %s", i+1, tt.Domain)
t.Run(name, func(t *testing.T) {
t.Parallel()
// domain, evidence, and original should not be empty
if len(tt.Domain) == 0 {
t.Errorf("domain is empty")
}
if len(tt.Original) == 0 {
t.Errorf("original is empty")
}
if len(tt.Evidence) == 0 {
t.Errorf("evidence is empty")
}
// domain should match evidence
u, err := url.Parse(tt.Evidence)
if err != nil {
t.Errorf("cannot parse evidence: %+v", err)
}
if matched, err := path.Match(tt.Domain, u.Hostname()); err != nil {
t.Errorf("cannot check match: %+v", err)
} else if !matched {
t.Errorf("'%s' does not match '%s'", tt.Domain, u.Hostname())
}
// domain should not match Stack Exchange
if tt.Domain == "*.stackexchange.com" || tt.Domain == "stackexchange.com" {
t.Errorf("'%s' should not match Stack Exchange", tt.Domain)
}
if matched, err := path.Match("*.stackexchange.com", tt.Domain); err != nil {
t.Errorf("cannot check match: %+v", err)
} else if matched {
t.Errorf("'%s' should not match Stack Exchange", tt.Domain)
}
stackExchanges := []string{
"askubuntu.com",
"stackoverflow.com",
"superuser.com",
"serverfault.com",
}
for _, stackExchange := range stackExchanges {
if matched, err := path.Match(tt.Domain, stackExchange); err != nil {
t.Errorf("cannot check match: %+v", err)
} else if matched {
t.Errorf("'%s' should not match Stack Exchange", tt.Domain)
}
}
// domain should not duplicate others
for j, other := range tests {
if i == j {
continue
}
if matched, err := path.Match(tt.Domain, other.Domain); err != nil {
t.Errorf("cannot check match: %+v", err)
} else if matched {
t.Errorf("%d-th item ('%s') and %d-th item ('%s') are duplicated", i+1, tt.Domain, j+1, other.Domain)
}
}
})
}
}
func loadItems(filename string) ([]Item, error) {
f, err := os.Open(filename)
if err != nil {
return nil, xerrors.Errorf("failed to open: %w", err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, xerrors.Errorf("failed to read: %w", err)
}
domains := []Item{}
if err := yaml.UnmarshalStrict(b, &domains); err != nil {
return nil, xerrors.Errorf("failed to unmarshal: %w", err)
}
return domains, nil
}