net: avoid TCP self-connect

Fixes #2690.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5650071
This commit is contained in:
Russ Cox 2012-02-12 23:25:55 -05:00
parent f7a3683928
commit cbe7d8db24
2 changed files with 61 additions and 0 deletions

View file

@ -84,3 +84,34 @@ func TestDialTimeout(t *testing.T) {
}
}
}
func TestSelfConnect(t *testing.T) {
// Test that Dial does not honor self-connects.
// See the comment in DialTCP.
// Find a port that would be used as a local address.
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
c, err := Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
addr := c.LocalAddr().String()
c.Close()
l.Close()
// Try to connect to that address repeatedly.
n := 100000
if testing.Short() {
n = 1000
}
for i := 0; i < n; i++ {
c, err := Dial("tcp", addr)
if err == nil {
c.Close()
t.Errorf("#%d: Dial %q succeeded", i, addr)
}
}
}