Skip to content
This repository has been archived by the owner on Jan 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from petroav/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
Stebalien authored Jan 22, 2019
2 parents 43abb5c + 19cebc1 commit 798f99d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 25 additions & 3 deletions addr_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
37 changes: 37 additions & 0 deletions filter_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 798f99d

Please sign in to comment.