diff --git a/README.md b/README.md index 035fd0b..b6a4b86 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ go-addr-util ================== -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) -[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://github.com/libp2p/libp2p) -[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) -[![Coverage Status](https://coveralls.io/repos/github/libp2p/go-addr-util/badge.svg?branch=master)](https://coveralls.io/github/libp2p/go-addr-util?branch=master) +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai) +[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://libp2p.io/) +[![](https://img.shields.io/badge/freenode-%23libp2p-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p) +[![codecov](https://codecov.io/gh/libp2p/go-addr-util/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/go-addr-util) [![Travis CI](https://travis-ci.org/libp2p/go-addr-util.svg?branch=master)](https://travis-ci.org/libp2p/go-addr-util) > Address utilities for libp2p swarm. diff --git a/addr.go b/addr.go index f27e5b7..3a922a6 100644 --- a/addr.go +++ b/addr.go @@ -11,8 +11,8 @@ import ( var log = logging.Logger("addrutil") -// FilterAddrs is a filter that removes certain addresses, according the given filters. -// if all filters return true, the address is kept. +// FilterAddrs is a filter that removes certain addresses, according to the given filters. +// If all filters return true, the address is kept. func FilterAddrs(a []ma.Multiaddr, filters ...func(ma.Multiaddr) bool) []ma.Multiaddr { b := make([]ma.Multiaddr, 0, len(a)) for _, addr := range a { diff --git a/addr_test.go b/addr_test.go index e071468..3707ad6 100644 --- a/addr_test.go +++ b/addr_test.go @@ -1,10 +1,9 @@ package addrutil import ( - "testing" - ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" + "testing" ) func newMultiaddr(t *testing.T, s string) ma.Multiaddr { @@ -17,10 +16,10 @@ func newMultiaddr(t *testing.T, s string) ma.Multiaddr { } func TestFilterAddrs(t *testing.T) { - bad := []ma.Multiaddr{ newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), // link local newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local + newMultiaddr(t, ""), } good := []ma.Multiaddr{ @@ -211,3 +210,26 @@ func TestSubtract(t *testing.T) { } } } + +func TestAddrInList(t *testing.T) { + addrs := []ma.Multiaddr{ + newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"), + newMultiaddr(t, "/ip6/::/tcp/1234"), + newMultiaddr(t, "/ip6/fe80::/tcp/1234"), + } + if in := AddrInList(newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), addrs); in { + t.Errorf("Expected address %s to not be in list", "/ip6/fe80::1/tcp/1234") + } + if in := AddrInList(newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"), addrs); !in { + t.Errorf("Expected address %s to be in list", "/ip4/0.0.0.0/tcp/1234") + } +} + +func TestCheckNATWarning(t *testing.T) { + multiAddr := newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234") + listen := []ma.Multiaddr{ + multiAddr, + } + CheckNATWarning(multiAddr, multiAddr, listen) + CheckNATWarning(newMultiaddr(t, "/ip6/fe80::/tcp/1234"), multiAddr, listen) +} diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..d5f57d1 --- /dev/null +++ b/filter_test.go @@ -0,0 +1,37 @@ +package addrutil + +import ( + ma "github.com/multiformats/go-multiaddr" + "testing" +) + +func TestSubtractAndNegFilter(t *testing.T) { + localhost := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234") + private := newMultiaddr(t, "/ip4/192.168.1.1/tcp/1234") + toRemoveFilter := SubtractFilter( + localhost, + newMultiaddr(t, "/ip6/::1/tcp/1234"), + newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"), + ) + result := FilterAddrs([]ma.Multiaddr{localhost, private}, toRemoveFilter) + if len(result) != 1 || !result[0].Equal(private) { + t.Errorf("Expected only one remaining address: %s", private.String()) + } + + // Negate original filter + result = FilterAddrs([]ma.Multiaddr{localhost, private}, FilterNeg(toRemoveFilter)) + if len(result) != 1 || !result[0].Equal(localhost) { + t.Errorf("Expected only one remaining address: %s", localhost.String()) + } +} + +func TestIsFDCostlyTransport(t *testing.T) { + tcpMa := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234") + if ok := IsFDCostlyTransport(tcpMa); !ok { + t.Errorf("Expected address %s to need a new file descriptor per new connection", tcpMa.String()) + } + udpMa := newMultiaddr(t, "/ip4/127.0.0.1/udp/1234") + if ok := IsFDCostlyTransport(udpMa); ok { + t.Errorf("Expected address %s to not need a new file descriptor per new connection", udpMa.String()) + } +}