Commit 0327911b authored by Jakub Sztandera's avatar Jakub Sztandera

fix: fdcostly should take only the prefix into account

also add tests for it

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protonmail.ch>
parent 4b89cdc9
......@@ -20,6 +20,11 @@ func SubtractFilter(addrs ...ma.Multiaddr) func(ma.Multiaddr) bool {
// IsFDCostlyTransport returns true for transports that require a new file
// descriptor per connection created
func IsFDCostlyTransport(a ma.Multiaddr) bool {
mas := ma.Split(a)
if len(mas) < 2 {
return false
}
a = mas[0].Encapsulate(mas[1])
return mafmt.TCP.Matches(a)
}
......
package addrutil
import (
ma "github.com/multiformats/go-multiaddr"
"testing"
ma "github.com/multiformats/go-multiaddr"
)
func TestSubtractAndNegFilter(t *testing.T) {
......@@ -35,3 +36,50 @@ func TestIsFDCostlyTransport(t *testing.T) {
t.Errorf("Expected address %s to not need a new file descriptor per new connection", udpMa.String())
}
}
func TestIsFDCostly(t *testing.T) {
good := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234/ws"),
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/ws"),
}
bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/udp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/udp/1234/utp"),
newMultiaddr(t, "/ip6/::1/udp/1234"),
newMultiaddr(t, "/ip6/::/udp/1234"),
}
for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}
for _, a := range good {
if !IsFDCostlyTransport(a) {
t.Errorf("addr %s should be fd costly", a)
}
}
}
func TestIsFdCostlyMalformed(t *testing.T) {
bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/"),
newMultiaddr(t, "/ip4/0.0.0.0/"),
newMultiaddr(t, "/ip6/::1/"),
newMultiaddr(t, "/ip6/::/"),
}
for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}
}
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