1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-03 16:57:38 -05:00
v2fly/proxy/freedom/freedom_test.go

118 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 (
"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,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
},
})
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,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
},
})
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{
2016-09-20 10:05:35 -04:00
Hosts: map[string]*v2net.AddressPB{
"v2ray.com": &v2net.AddressPB{
Address: &v2net.AddressPB_Ip{
Ip: []byte{127, 0, 0, 1},
},
},
2016-05-22 16:30:21 -04:00
},
})
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,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
},
})
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
}