@@ -6,7 +6,91 @@ import (
6
6
ma "github.com/multiformats/go-multiaddr"
7
7
)
8
8
9
+ type testVector struct {
10
+ Pattern Pattern
11
+ Good []string
12
+ Bad []string
13
+ }
14
+
15
+ var TestVectors = map [string ]* testVector {
16
+ "IP" : {
17
+ Pattern : IP ,
18
+ Good : []string {"/ip4/0.0.0.0" , "/ip6/fc00::" },
19
+ Bad : []string {"/ip4/0.0.0.0/tcp/555" , "/udp/789/ip6/fc00::" },
20
+ },
21
+ "TCP" : {
22
+ Pattern : TCP ,
23
+ Good : []string {"/ip4/0.0.7.6/tcp/1234" , "/ip6/::/tcp/0" },
24
+ Bad : []string {"/tcp/12345" , "/ip6/fc00::/udp/5523/tcp/9543" },
25
+ },
26
+ "UDP" : {
27
+ Pattern : UDP ,
28
+ Good : []string {"/ip4/0.0.7.6/udp/1234" , "/ip6/::/udp/0" },
29
+ Bad : []string {"/udp/12345" , "/ip6/fc00::/tcp/5523/udp/9543" },
30
+ },
31
+ "UTP" : {
32
+ Pattern : UTP ,
33
+ Good : []string {"/ip4/1.2.3.4/udp/3456/utp" , "/ip6/::/udp/0/utp" },
34
+ Bad : []string {"/ip4/0.0.0.0/tcp/12345/utp" , "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/utp" , "/utp" },
35
+ },
36
+ "QUIC" : {
37
+ Pattern : QUIC ,
38
+ Good : []string {"/ip4/1.2.3.4/udp/1234/quic" , "/ip6/::/udp/1234/quic" },
39
+ Bad : []string {"/ip4/0.0.0.0/tcp/12345/quic" , "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/quic" , "/quic" },
40
+ },
41
+ "IPFS" : {
42
+ Pattern : IPFS ,
43
+ Good : []string {
44
+ "/ip4/1.2.3.4/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
45
+ "/ip6/::/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
46
+ "/ip6/::/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
47
+ "/ip4/0.0.0.0/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" },
48
+ Bad : []string {
49
+ "/ip4/1.2.3.4/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
50
+ "/ip6/::/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
51
+ "/tcp/123/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
52
+ "/ip6/::/udp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
53
+ "/ip6/::/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
54
+ "/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" }},
55
+ "DNS" : {
56
+ Pattern : DNS ,
57
+ Good : []string {"/dnsaddr/ipfs.io" , "/dns4/ipfs.io" , "/dns4/libp2p.io" , "/dns6/protocol.ai" },
58
+ Bad : []string {"/ip4/127.0.0.1" },
59
+ },
60
+ }
61
+
62
+ func TestProtocolMatching (t * testing.T ) {
63
+ for name , tc := range TestVectors {
64
+ t .Run (name , func (t * testing.T ) {
65
+ t .Parallel ()
66
+
67
+ assertMatches (t , tc .Pattern , tc .Good )
68
+
69
+ bad := [][]string {tc .Bad }
70
+ for _ , other := range TestVectors {
71
+ if other == tc {
72
+ continue
73
+ }
74
+ bad = append (bad , other .Good )
75
+ }
76
+ assertMismatches (t , tc .Pattern , bad ... )
77
+ })
78
+ }
79
+ }
80
+
81
+ func TestReliableGroup (t * testing.T ) {
82
+ assertMatches (t , Reliable , TestVectors ["UTP" ].Good , TestVectors ["TCP" ].Good , TestVectors ["QUIC" ].Good )
83
+ assertMismatches (t , Reliable , TestVectors ["IP" ].Good , TestVectors ["UDP" ].Good , TestVectors ["IPFS" ].Good )
84
+ }
85
+
86
+ func TestUnreliableGroup (t * testing.T ) {
87
+ assertMatches (t , Unreliable , TestVectors ["UDP" ].Good )
88
+ assertMismatches (t , Unreliable , TestVectors ["IP" ].Good , TestVectors ["TCP" ].Good , TestVectors ["UTP" ].Good , TestVectors ["IPFS" ].Good , TestVectors ["QUIC" ].Good )
89
+ }
90
+
9
91
func assertMatches (t * testing.T , p Pattern , args ... []string ) {
92
+ t .Helper ()
93
+
10
94
t .Logf ("testing assertions for %q" , p )
11
95
for _ , argset := range args {
12
96
for _ , s := range argset {
@@ -23,6 +107,8 @@ func assertMatches(t *testing.T, p Pattern, args ...[]string) {
23
107
}
24
108
25
109
func assertMismatches (t * testing.T , p Pattern , args ... []string ) {
110
+ t .Helper ()
111
+
26
112
for _ , argset := range args {
27
113
for _ , s := range argset {
28
114
addr , err := ma .NewMultiaddr (s )
@@ -36,125 +122,3 @@ func assertMismatches(t *testing.T, p Pattern, args ...[]string) {
36
122
}
37
123
}
38
124
}
39
-
40
- func TestBasicMatching (t * testing.T ) {
41
- good_dns := []string {
42
- "/dnsaddr/ipfs.io" ,
43
- "/dns4/ipfs.io" ,
44
- "/dns4/libp2p.io" ,
45
- "/dns6/protocol.ai" ,
46
- }
47
-
48
- bad_dns := []string {
49
- "/ip4/127.0.0.1" ,
50
- }
51
-
52
- good_ip := []string {
53
- "/ip4/0.0.0.0" ,
54
- "/ip6/fc00::" ,
55
- }
56
-
57
- bad_ip := []string {
58
- "/ip4/0.0.0.0/tcp/555" ,
59
- "/udp/789/ip6/fc00::" ,
60
- }
61
-
62
- good_tcp := []string {
63
- "/ip4/0.0.7.6/tcp/1234" ,
64
- "/ip6/::/tcp/0" ,
65
- "/dns4/protocol.ai/tcp/80" ,
66
- "/dns6/protocol.ai/tcp/80" ,
67
- "/dnsaddr/protocol.ai/tcp/8" ,
68
- }
69
-
70
- bad_tcp := []string {
71
- "/tcp/12345" ,
72
- "/ip6/fc00::/udp/5523/tcp/9543" ,
73
- }
74
-
75
- good_udp := []string {
76
- "/ip4/0.0.7.6/udp/1234" ,
77
- "/ip6/::/udp/0" ,
78
- "/dns4/protocol.ai/udp/80" ,
79
- "/dns6/protocol.ai/udp/80" ,
80
- "/dnsaddr/protocol.ai/udp/8" ,
81
- }
82
-
83
- bad_udp := []string {
84
- "/udp/12345" ,
85
- "/ip6/fc00::/tcp/5523/udp/9543" ,
86
- }
87
-
88
- good_utp := []string {
89
- "/ip4/1.2.3.4/udp/3456/utp" ,
90
- "/ip6/::/udp/0/utp" ,
91
- }
92
-
93
- bad_utp := []string {
94
- "/ip4/0.0.0.0/tcp/12345/utp" ,
95
- "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/utp" ,
96
- "/utp" ,
97
- }
98
-
99
- good_quic := []string {
100
- "/ip4/1.2.3.4/udp/1234/quic" ,
101
- "/ip6/::/udp/1234/quic" ,
102
- }
103
-
104
- bad_quic := []string {
105
- "/ip4/0.0.0.0/tcp/12345/quic" ,
106
- "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/quic" ,
107
- "/quic" ,
108
- }
109
-
110
- good_webrtcdirect := []string {
111
- "/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct" ,
112
- "/ip6/::/tcp/0/http/p2p-webrtc-direct" ,
113
- }
114
-
115
- good_ipfs := []string {
116
- "/ip4/1.2.3.4/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
117
- "/ip6/::/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
118
- "/ip6/::/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
119
- "/ip4/0.0.0.0/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
120
- }
121
-
122
- bad_ipfs := []string {
123
- "/ip4/1.2.3.4/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
124
- "/ip6/::/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
125
- "/tcp/123/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
126
- "/ip6/::/udp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
127
- "/ip6/::/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
128
- "/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
129
- }
130
-
131
- assertMatches (t , DNS , good_dns )
132
- assertMismatches (t , DNS , bad_dns , bad_ip )
133
-
134
- assertMatches (t , IP , good_ip )
135
- assertMismatches (t , IP , bad_ip , good_tcp )
136
-
137
- assertMatches (t , TCP , good_tcp )
138
- assertMismatches (t , TCP , bad_tcp , good_ip )
139
-
140
- assertMatches (t , UDP , good_udp )
141
- assertMismatches (t , UDP , bad_udp , good_ip , good_tcp , good_ipfs , good_utp , good_quic )
142
-
143
- assertMatches (t , UTP , good_utp )
144
- assertMismatches (t , UTP , bad_utp , good_ip , good_tcp , good_udp , good_quic )
145
-
146
- assertMatches (t , QUIC , good_quic )
147
- assertMismatches (t , QUIC , bad_quic , good_ip , good_tcp , good_udp , good_utp )
148
-
149
- assertMatches (t , Reliable , good_utp , good_tcp , good_quic )
150
- assertMismatches (t , Reliable , good_ip , good_udp , good_ipfs )
151
-
152
- assertMatches (t , Unreliable , good_udp )
153
- assertMismatches (t , Unreliable , good_ip , good_tcp , good_utp , good_ipfs , good_quic )
154
-
155
- assertMatches (t , WebRTCDirect , good_webrtcdirect )
156
- assertMismatches (t , WebRTCDirect , good_ip , good_udp )
157
-
158
- assertMatches (t , IPFS , good_ipfs )
159
- assertMismatches (t , IPFS , bad_ipfs , good_ip , good_tcp , good_utp , good_udp , good_quic )
160
- }
0 commit comments