@@ -6,7 +6,86 @@ import (
66 ma "github.com/multiformats/go-multiaddr"
77)
88
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+ }
56+
57+ func TestProtocolMatching (t * testing.T ) {
58+ for name , tc := range TestVectors {
59+ t .Run (name , func (t * testing.T ) {
60+ t .Parallel ()
61+
62+ assertMatches (t , tc .Pattern , tc .Good )
63+
64+ bad := [][]string {tc .Bad }
65+ for _ , other := range TestVectors {
66+ if other == tc {
67+ continue
68+ }
69+ bad = append (bad , other .Good )
70+ }
71+ assertMismatches (t , tc .Pattern , bad ... )
72+ })
73+ }
74+ }
75+
76+ func TestReliableGroup (t * testing.T ) {
77+ assertMatches (t , Reliable , TestVectors ["UTP" ].Good , TestVectors ["TCP" ].Good , TestVectors ["QUIC" ].Good )
78+ assertMismatches (t , Reliable , TestVectors ["IP" ].Good , TestVectors ["UDP" ].Good , TestVectors ["IPFS" ].Good )
79+ }
80+
81+ func TestUnreliableGroup (t * testing.T ) {
82+ assertMatches (t , Unreliable , TestVectors ["UDP" ].Good )
83+ assertMismatches (t , Unreliable , TestVectors ["IP" ].Good , TestVectors ["TCP" ].Good , TestVectors ["UTP" ].Good , TestVectors ["IPFS" ].Good , TestVectors ["QUIC" ].Good )
84+ }
85+
986func assertMatches (t * testing.T , p Pattern , args ... []string ) {
87+ t .Helper ()
88+
1089 t .Logf ("testing assertions for %q" , p )
1190 for _ , argset := range args {
1291 for _ , s := range argset {
@@ -23,6 +102,8 @@ func assertMatches(t *testing.T, p Pattern, args ...[]string) {
23102}
24103
25104func assertMismatches (t * testing.T , p Pattern , args ... []string ) {
105+ t .Helper ()
106+
26107 for _ , argset := range args {
27108 for _ , s := range argset {
28109 addr , err := ma .NewMultiaddr (s )
@@ -36,97 +117,3 @@ func assertMismatches(t *testing.T, p Pattern, args ...[]string) {
36117 }
37118 }
38119}
39-
40- func TestBasicMatching (t * testing.T ) {
41- good_ip := []string {
42- "/ip4/0.0.0.0" ,
43- "/ip6/fc00::" ,
44- }
45-
46- bad_ip := []string {
47- "/ip4/0.0.0.0/tcp/555" ,
48- "/udp/789/ip6/fc00::" ,
49- }
50-
51- good_tcp := []string {
52- "/ip4/0.0.7.6/tcp/1234" ,
53- "/ip6/::/tcp/0" ,
54- }
55-
56- bad_tcp := []string {
57- "/tcp/12345" ,
58- "/ip6/fc00::/udp/5523/tcp/9543" ,
59- }
60-
61- good_udp := []string {
62- "/ip4/0.0.7.6/udp/1234" ,
63- "/ip6/::/udp/0" ,
64- }
65-
66- bad_udp := []string {
67- "/udp/12345" ,
68- "/ip6/fc00::/tcp/5523/udp/9543" ,
69- }
70-
71- good_utp := []string {
72- "/ip4/1.2.3.4/udp/3456/utp" ,
73- "/ip6/::/udp/0/utp" ,
74- }
75-
76- bad_utp := []string {
77- "/ip4/0.0.0.0/tcp/12345/utp" ,
78- "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/utp" ,
79- "/utp" ,
80- }
81-
82- good_quic := []string {
83- "/ip4/1.2.3.4/udp/1234/quic" ,
84- "/ip6/::/udp/1234/quic" ,
85- }
86-
87- bad_quic := []string {
88- "/ip4/0.0.0.0/tcp/12345/quic" ,
89- "/ip6/1.2.3.4/ip4/0.0.0.0/udp/1234/quic" ,
90- "/quic" ,
91- }
92-
93- good_ipfs := []string {
94- "/ip4/1.2.3.4/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
95- "/ip6/::/tcp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
96- "/ip6/::/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
97- "/ip4/0.0.0.0/udp/1234/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
98- }
99-
100- bad_ipfs := []string {
101- "/ip4/1.2.3.4/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
102- "/ip6/::/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
103- "/tcp/123/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
104- "/ip6/::/udp/1234/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
105- "/ip6/::/utp/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
106- "/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ,
107- }
108-
109- assertMatches (t , IP , good_ip )
110- assertMismatches (t , IP , bad_ip , good_tcp )
111-
112- assertMatches (t , TCP , good_tcp )
113- assertMismatches (t , TCP , bad_tcp , good_ip )
114-
115- assertMatches (t , UDP , good_udp )
116- assertMismatches (t , UDP , bad_udp , good_ip , good_tcp , good_ipfs , good_utp , good_quic )
117-
118- assertMatches (t , UTP , good_utp )
119- assertMismatches (t , UTP , bad_utp , good_ip , good_tcp , good_udp , good_quic )
120-
121- assertMatches (t , QUIC , good_quic )
122- assertMismatches (t , QUIC , bad_quic , good_ip , good_tcp , good_udp , good_utp )
123-
124- assertMatches (t , Reliable , good_utp , good_tcp , good_quic )
125- assertMismatches (t , Reliable , good_ip , good_udp , good_ipfs )
126-
127- assertMatches (t , Unreliable , good_udp )
128- assertMismatches (t , Unreliable , good_ip , good_tcp , good_utp , good_ipfs , good_quic )
129-
130- assertMatches (t , IPFS , good_ipfs )
131- assertMismatches (t , IPFS , bad_ipfs , good_ip , good_tcp , good_utp , good_udp , good_quic )
132- }
0 commit comments