1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-31 08:18:50 -04:00
v2fly/proxy/freedom/freedom_test.go

115 lines
3.1 KiB
Go
Raw Normal View History

2016-02-04 05:11:11 -05:00
package freedom_test
2015-09-24 12:01:02 -04:00
import (
2016-05-22 16:30:21 -04:00
"net"
2015-09-24 12:01:02 -04:00
"testing"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/app/router/rules"
"v2ray.com/core/common/alloc"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
. "v2ray.com/core/proxy/freedom"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-24 12:01:02 -04:00
)
2016-02-04 05:11:11 -05:00
func TestSinglePacket(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-10-04 14:22:52 -04:00
tcpServer := &tcp.Server{
MsgProcessor: func(data []byte) []byte {
buffer := make([]byte, 0, 2048)
buffer = append(buffer, []byte("Processed: ")...)
buffer = append(buffer, data...)
return buffer
},
}
_, err := tcpServer.Start()
assert.Error(err).IsNil()
2015-09-24 12:01:02 -04:00
2016-05-22 16:30:21 -04:00
space := app.NewSpace()
2016-06-14 16:54:08 -04:00
freedom := NewFreedomConnection(
&Config{},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
},
})
2016-05-22 16:30:21 -04:00
space.Initialize()
2016-02-04 05:11:11 -05:00
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
2016-08-01 16:59:18 -04:00
payload := alloc.NewLocalBuffer(2048).Clear().Append([]byte(data2Send))
2015-09-24 12:01:02 -04:00
2016-08-15 17:02:03 -04:00
go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic)
2016-04-18 12:44:10 -04:00
traffic.InboundInput().Close()
2015-09-24 12:01:02 -04:00
2016-04-18 12:44:10 -04:00
respPayload, err := traffic.InboundOutput().Read()
assert.Error(err).IsNil()
2016-02-04 05:11:11 -05:00
assert.Bytes(respPayload.Value).Equals([]byte("Processed: Data to be sent to remote"))
2015-09-24 12:01:02 -04:00
2016-02-04 05:11:11 -05:00
tcpServer.Close()
}
2015-09-24 12:01:02 -04:00
2016-02-04 05:11:11 -05:00
func TestUnreachableDestination(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-24 12:01:02 -04:00
2016-06-14 16:54:08 -04:00
freedom := NewFreedomConnection(
&Config{},
app.NewSpace(),
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
},
})
2016-02-04 05:11:11 -05:00
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
2016-08-01 16:59:18 -04:00
payload := alloc.NewLocalBuffer(2048).Clear().Append([]byte(data2Send))
2016-02-04 05:11:11 -05:00
2016-04-25 18:13:26 -04:00
err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)
2016-02-04 05:11:11 -05:00
assert.Error(err).IsNotNil()
2015-09-24 12:01:02 -04:00
}
2016-05-22 16:30:21 -04:00
func TestIPResolution(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-05-22 16:30:21 -04:00
space := app.NewSpace()
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space)
space.BindApp(router.APP_ID, r)
dnsServer := dns.NewCacheServer(space, &dns.Config{
Hosts: map[string]net.IP{
"v2ray.com": net.IP([]byte{127, 0, 0, 1}),
},
})
space.BindApp(dns.APP_ID, dnsServer)
2016-06-04 08:25:13 -04:00
freedom := NewFreedomConnection(
2016-08-25 15:55:49 -04:00
&Config{DomainStrategy: Config_USE_IP},
2016-06-04 08:25:13 -04:00
space,
2016-06-14 16:54:08 -04:00
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
},
})
2016-05-22 16:30:21 -04:00
space.Initialize()
ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
2016-05-24 09:29:08 -04:00
assert.Destination(ipDest).IsTCP()
2016-09-20 05:53:05 -04:00
assert.Address(ipDest.Address).Equals(v2net.LocalHostIP)
2016-05-22 16:30:21 -04:00
}