Commit d0bf3e16 authored by elcore's avatar elcore Committed by Matt Holt

Fix network listener address comparison, fixes #1258 (#1273)

* Fix issue #1258

* address comments

* add a test

* address comments 2
parent 7dc23b18
......@@ -232,7 +232,7 @@ func HasListenerWithAddress(addr string) bool {
func listenerAddrEqual(ln net.Listener, addr string) bool {
lnAddr := ln.Addr().String()
hostname, port, err := net.SplitHostPort(addr)
if err != nil || hostname != "" {
if err != nil {
return lnAddr == addr
}
if lnAddr == net.JoinHostPort("::", port) {
......@@ -241,7 +241,7 @@ func listenerAddrEqual(ln net.Listener, addr string) bool {
if lnAddr == net.JoinHostPort("0.0.0.0", port) {
return true
}
return false
return hostname != "" && lnAddr == addr
}
// TCPServer is a type that can listen and serve connections.
......
package caddy
import "testing"
import (
"net"
"strconv"
"testing"
)
/*
// TODO
......@@ -56,3 +60,39 @@ func TestIsLoopback(t *testing.T) {
}
}
}
func TestListenerAddrEqual(t *testing.T) {
ln1, err := net.Listen("tcp", "[::]:0")
if err != nil {
t.Fatal(err)
}
defer ln1.Close()
ln1port := strconv.Itoa(ln1.Addr().(*net.TCPAddr).Port)
ln2, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln2.Close()
ln2port := strconv.Itoa(ln2.Addr().(*net.TCPAddr).Port)
for i, test := range []struct {
ln net.Listener
addr string
expect bool
}{
{ln1, ":1234", false},
{ln1, "0.0.0.0:1234", false},
{ln1, ":" + ln1port + "", true},
{ln1, "0.0.0.0:" + ln1port + "", true},
{ln2, "127.0.0.1:1234", false},
{ln2, ":" + ln2port + "", false},
{ln2, "127.0.0.1:" + ln2port + "", true},
} {
if got, want := listenerAddrEqual(test.ln, test.addr), test.expect; got != want {
t.Errorf("Test %d (%s == %s): expected %v but was %v", i, test.addr, test.ln.Addr().String(), want, got)
}
}
}
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