Commit 04044c22 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

AddrMatch

parent 97da87cf
......@@ -266,3 +266,30 @@ func InterfaceMultiaddrs() ([]ma.Multiaddr, error) {
}
return maddrs, nil
}
// AddrMatch returns the Multiaddrs that match the protocol stack on addr
func AddrMatch(match ma.Multiaddr, addrs []ma.Multiaddr) []ma.Multiaddr {
// we should match transports entirely.
p1s := match.Protocols()
out := make([]ma.Multiaddr, 0, len(addrs))
for _, a := range addrs {
p2s := a.Protocols()
if len(p1s) != len(p2s) {
continue
}
match := true
for i, p2 := range p2s {
if p1s[i].Code != p2.Code {
match = false
break
}
}
if match {
out = append(out, a)
}
}
return out
}
......@@ -364,3 +364,75 @@ func TestIP6LinkLocal(t *testing.T) {
}
}
}
func TestAddrMatch(t *testing.T) {
test := func(m ma.Multiaddr, input, expect []ma.Multiaddr) {
actual := AddrMatch(m, input)
testSliceEqual(t, expect, actual)
}
a := []ma.Multiaddr{
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/2345"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/2345"),
newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
}
test(a[0], a, []ma.Multiaddr{
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/2345"),
})
test(a[2], a, []ma.Multiaddr{
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
})
test(a[4], a, []ma.Multiaddr{
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
})
test(a[6], a, []ma.Multiaddr{
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
})
test(a[8], a, []ma.Multiaddr{
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/2345"),
})
test(a[10], a, []ma.Multiaddr{
newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
})
test(a[12], a, []ma.Multiaddr{
newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
})
test(a[14], a, []ma.Multiaddr{
newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
})
}
func testSliceEqual(t *testing.T, a, b []ma.Multiaddr) {
if len(a) != len(b) {
t.Error("differ", a, b)
}
for i, addrA := range a {
if !addrA.Equal(b[i]) {
t.Error("differ", a, b)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment