2016-05-30 18:21:41 -04:00
|
|
|
package hub_test
|
2016-01-21 07:18:37 -05:00
|
|
|
|
|
|
|
import (
|
2016-06-03 18:38:22 -04:00
|
|
|
"net"
|
2016-01-21 07:18:37 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-21 07:27:08 -05:00
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
2016-01-21 07:18:37 -05:00
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2016-01-21 07:27:08 -05:00
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/tcp"
|
2016-05-30 18:21:41 -04:00
|
|
|
. "github.com/v2ray/v2ray-core/transport/hub"
|
2016-01-21 07:18:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialDomain(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2016-01-21 07:18:37 -05:00
|
|
|
|
2016-01-21 07:27:08 -05:00
|
|
|
server := &tcp.Server{
|
|
|
|
Port: v2nettesting.PickPort(),
|
|
|
|
}
|
|
|
|
dest, err := server.Start()
|
2016-01-21 07:18:37 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-01-21 16:45:44 -05:00
|
|
|
defer server.Close()
|
2016-01-21 07:27:08 -05:00
|
|
|
|
2016-06-03 18:38:22 -04:00
|
|
|
conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("local.v2ray.com"), dest.Port()))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.String(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port().String())
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialWithLocalAddr(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
server := &tcp.Server{
|
|
|
|
Port: v2nettesting.PickPort(),
|
|
|
|
}
|
|
|
|
dest, err := server.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
var localAddr net.IP
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
for _, addr := range addrs {
|
|
|
|
str := addr.String()
|
|
|
|
ip := net.ParseIP(str)
|
|
|
|
if ip != nil && ip.To4() != nil {
|
|
|
|
localAddr = ip.To4()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Pointer(localAddr).IsNotNil()
|
|
|
|
|
|
|
|
conn, err := Dial(v2net.IPAddress(localAddr), v2net.TCPDestination(v2net.LocalHostIP, dest.Port()))
|
2016-01-21 07:27:08 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-05-24 15:55:46 -04:00
|
|
|
assert.String(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port().String())
|
2016-01-21 07:18:37 -05:00
|
|
|
conn.Close()
|
|
|
|
}
|