-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpatterns_test.go
164 lines (149 loc) · 7.07 KB
/
patterns_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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mafmt
import (
"testing"
ma "github.com/multiformats/go-multiaddr"
)
type testVector struct {
Pattern Pattern
Good []string
Bad []string
}
var TestVectors = map[string]*testVector{
"IP": {
Pattern: IP,
Good: []string{"/ip4/0.0.0.0", "/ip6/fc00::"},
Bad: []string{"/ip4/0.0.0.0/tcp/555", "/udp/789/ip6/fc00::"},
},
"TCP": {
Pattern: TCP,
Good: []string{"/ip4/0.0.7.6/tcp/1234", "/ip6/::/tcp/0"},
Bad: []string{"/tcp/12345", "/ip6/fc00::/udp/5523/tcp/9543"},
},
"UDP": {
Pattern: UDP,
Good: []string{"/ip4/0.0.7.6/udp/1234", "/ip6/::/udp/0"},
Bad: []string{"/udp/12345", "/ip6/fc00::/tcp/5523/udp/9543"},
},
"UTP": {
Pattern: UTP,
Good: []string{"/ip4/1.2.3.4/udp/3456/utp", "/ip6/::/udp/0/utp"},
Bad: []string{"/ip4/0.0.0.0/tcp/12345/utp", "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/utp", "/utp"},
},
"QUIC": {
Pattern: QUIC,
Good: []string{"/ip4/1.2.3.4/udp/1234/quic", "/ip6/::/udp/1234/quic"},
Bad: []string{"/ip4/0.0.0.0/tcp/12345/quic", "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/quic", "/quic"},
},
"SAM3": {
Pattern: SAM3,
Good: []string{"/ip4/1.2.3.4/udp/1234/sam3/3|3|6|6|2|2", "/ip6/::1/tcp/1234/sam3/3|3|6|6|2|2"},
Bad: []string{"/ip4/0.0.0.0/sam3/3|3|6|6|2|2", "/sam3/3|3|6|6|2|2", "/sam3"},
},
"GARLIC": {
Pattern: GARLIC,
Good: []string{
"/garlic32/6zlio2ipbnmir26nrspdbt4cqnhu52ekw2gw6kfca4p77tdtsmja",
"/garlic64/Z1XdSy9zNg1IaFfDqWhpzoSjlf-lAWT4YkrRkq3rsnQyY69QXc~-2Xeyoj7~36swjrHjlH-OqvTbM0jFAc2RzIQ1VEf9uDUAz1sso9~or1xIDxsEUmgK~TQvHMPNFwVGhMxZ7K4LkLbi~sN2WlZB49FhhWVgD-nV-WfuRi~aByMUGtM1SqBML-Ok1bI6Pw9o6VIM-aFG7RLj1s8RsJOCzkugKLMsW-k5gXze6QJlRJKlcHI23bf-LPqhTfVe2HpEjS5DAj1fohtY63V3Kksd34Ejh86I-njBYGG66enBHvM-sRKAUQIKAt7eJEuk7l5BcidGD4HqodRl1nYBrHoP0MFDtw4ZTLcefVCwZ~OttpnUdH~9RBPBxc9Da-mVOe9dh608anxuXTBaKzdN6FQjxnMPEMQe5pBPzjGrkUpgCJLduybMltVymlYIMR98dPpBskqJQjuEm5tHBSWJfiKD9POVKIV0yyjlakoHm4Y~Zrl14GdFTIf2BGHMjf9GP0Q5BQAEAAcAAA==",
},
Bad: []string{
"/ip4/0.0.0.0/tcp/12345/garlic32/6zlio2ipbnmir26nrspdbt4cqnhu52ekw2gw6kfca4p77tdtsmja",
"/ip4/0.0.0.0/tcp/12345/garlic64/Z1XdSy9zNg1IaFfDqWhpzoSjlf-lAWT4YkrRkq3rsnQyY69QXc~-2Xeyoj7~36swjrHjlH-OqvTbM0jFAc2RzIQ1VEf9uDUAz1sso9~or1xIDxsEUmgK~TQvHMPNFwV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ERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/tcp/123/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip6/::/udp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip6/::/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
},
},
"DNS": {
Pattern: DNS,
Good: []string{"/dnsaddr/example.io", "/dns4/example.io", "/dns6/example.io"},
Bad: []string{"/ip4/127.0.0.1"},
},
"WebRTCDirect": {
Pattern: WebRTCDirect,
Good: []string{"/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct", "/ip6/::/tcp/0/http/p2p-webrtc-direct"},
Bad: []string{"/ip4/0.0.0.0", "/ip6/fc00::", "/udp/12345", "/ip6/fc00::/tcp/5523/udp/9543"},
},
"HTTP": {
Pattern: HTTP,
Good: []string{"/ip4/1.2.3.4/http", "/dns4/example.io/http", "/dns6/::/tcp/7011/http", "/dnsaddr/example.io/http", "/ip6/fc00::/http"},
Bad: []string{"/ip4/1.2.3.4/https", "/ip4/0.0.0.0/tcp/12345/quic", "/ip6/fc00::/tcp/5523"},
},
"HTTPS": {
Pattern: HTTPS,
Good: []string{"/ip4/1.2.3.4/https", "/dns4/example.io/https", "/dns6/::/tcp/7011/https", "/ip6/fc00::/https"},
Bad: []string{"/ip4/1.2.3.4/http", "/ip4/0.0.0.0/tcp/12345/quic", "/ip6/fc00::/tcp/5523"},
},
}
func TestProtocolMatching(t *testing.T) {
for name, tc := range TestVectors {
t.Run(name, func(t *testing.T) {
t.Parallel()
assertMatches(t, tc.Pattern, tc.Good)
bad := [][]string{tc.Bad}
for _, other := range TestVectors {
if other == tc {
continue
}
bad = append(bad, other.Good)
}
assertMismatches(t, tc.Pattern, bad...)
})
}
}
func TestReliableGroup(t *testing.T) {
assertMatches(t, Reliable, TestVectors["UTP"].Good, TestVectors["TCP"].Good, TestVectors["QUIC"].Good)
assertMismatches(t, Reliable, TestVectors["IP"].Good, TestVectors["UDP"].Good, TestVectors["IPFS"].Good)
}
func TestUnreliableGroup(t *testing.T) {
assertMatches(t, Unreliable, TestVectors["UDP"].Good)
assertMismatches(t, Unreliable, TestVectors["IP"].Good, TestVectors["TCP"].Good, TestVectors["UTP"].Good, TestVectors["IPFS"].Good, TestVectors["QUIC"].Good)
}
func assertMatches(t *testing.T, p Pattern, args ...[]string) {
t.Helper()
t.Logf("testing assertions for %q", p)
for _, argset := range args {
for _, s := range argset {
addr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if !p.Matches(addr) {
t.Fatal("mismatch!", s, p)
}
}
}
}
func assertMismatches(t *testing.T, p Pattern, args ...[]string) {
t.Helper()
for _, argset := range args {
for _, s := range argset {
addr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if p.Matches(addr) {
t.Fatal("incorrect match!", s, p)
}
}
}
}