2018-08-09 18:34:09 +00:00
|
|
|
package mnet
|
|
|
|
|
|
|
|
import (
|
2019-01-11 22:47:30 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-08-09 18:34:09 +00:00
|
|
|
"net"
|
|
|
|
. "testing"
|
|
|
|
|
2019-02-03 01:58:18 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest"
|
2018-08-09 18:34:09 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsReservedIP(t *T) {
|
|
|
|
assertReserved := func(ipStr string) massert.Assertion {
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
if ip == nil {
|
|
|
|
panic("ip:" + ipStr + " not valid")
|
|
|
|
}
|
|
|
|
return massert.Comment(massert.Equal(true, IsReservedIP(ip)),
|
|
|
|
"ip:%q", ipStr)
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2018-08-09 18:34:09 +00:00
|
|
|
assertReserved("127.0.0.1"),
|
|
|
|
assertReserved("::ffff:127.0.0.1"),
|
|
|
|
assertReserved("192.168.40.50"),
|
|
|
|
assertReserved("::1"),
|
|
|
|
assertReserved("100::1"),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-08-09 18:34:09 +00:00
|
|
|
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t, massert.None(
|
2018-08-09 18:34:09 +00:00
|
|
|
assertReserved("8.8.8.8"),
|
|
|
|
assertReserved("::ffff:8.8.8.8"),
|
|
|
|
assertReserved("2600:1700:7580:6e80:21c:25ff:fe97:44df"),
|
|
|
|
))
|
|
|
|
}
|
2019-01-11 22:47:30 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
func TestWithListener(t *T) {
|
2019-06-17 01:15:51 +00:00
|
|
|
cmp := mtest.Component()
|
|
|
|
l := InstListener(cmp)
|
|
|
|
mtest.Run(cmp, t, func() {
|
2019-02-03 01:58:18 +00:00
|
|
|
go func() {
|
|
|
|
conn, err := net.Dial("tcp", l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if _, err = fmt.Fprint(conn, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, err := l.Accept()
|
2019-01-11 22:47:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-02-03 01:58:18 +00:00
|
|
|
} else if b, err := ioutil.ReadAll(conn); err != nil {
|
2019-01-11 22:47:30 +00:00
|
|
|
t.Fatal(err)
|
2019-02-03 01:58:18 +00:00
|
|
|
} else if string(b) != "hello world" {
|
|
|
|
t.Fatalf("read %q from conn", b)
|
2019-01-11 22:47:30 +00:00
|
|
|
}
|
2019-02-03 01:58:18 +00:00
|
|
|
})
|
2019-01-11 22:47:30 +00:00
|
|
|
}
|