Commit ecf27b59 authored by Abhishek Upperwal's avatar Abhishek Upperwal
Browse files

strict DefaultListenAddrs test w/ regex

parent 6372f928
...@@ -3,6 +3,7 @@ package libp2p ...@@ -3,6 +3,7 @@ package libp2p
import ( import (
"context" "context"
"fmt" "fmt"
"regexp"
"strings" "strings"
"testing" "testing"
...@@ -43,14 +44,19 @@ func TestInsecure(t *testing.T) { ...@@ -43,14 +44,19 @@ func TestInsecure(t *testing.T) {
func TestDefaultListenAddrs(t *testing.T) { func TestDefaultListenAddrs(t *testing.T) {
ctx := context.Background() ctx := context.Background()
re := regexp.MustCompile("/ip[4|6]/0.0.0.0/tcp/")
// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil // Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
h, err := New(ctx) h, err := New(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(h.Addrs()) != 2 { for _, addr := range h.Network().ListenAddresses() {
t.Error("expected 2 default listen addrs") if re.FindStringSubmatchIndex(addr.String()) == nil {
t.Error("expected ip4 or ip6 interface")
}
} }
h.Close() h.Close()
// Test 2: Listen addr should not set if user defined transport is passed. // Test 2: Listen addr should not set if user defined transport is passed.
...@@ -62,25 +68,10 @@ func TestDefaultListenAddrs(t *testing.T) { ...@@ -62,25 +68,10 @@ func TestDefaultListenAddrs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if len(h.Addrs()) != 0 { if len(h.Network().ListenAddresses()) != 0 {
t.Error("expected zero listen addrs as none is set with user defined transport") t.Error("expected zero listen addrs as none is set with user defined transport")
} }
h.Close() h.Close()
// Test 3: User defined listener addrs should overwrite the default options.
h, err = New(
ctx,
Transport(tcp.NewTCPTransport),
ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
if err != nil {
t.Fatal(err)
}
if len(h.Addrs()) != 1 {
t.Error("expected one listen addr")
}
h.Close()
} }
func makeRandomHost(t *testing.T, port int) (host.Host, error) { func makeRandomHost(t *testing.T, port int) (host.Host, error) {
......
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