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

94 lines
2.9 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-05-22 16:30:21 -04:00
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dispatcher"
dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
"github.com/v2ray/v2ray-core/app/dns"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/app/router/rules"
2016-02-04 05:11:11 -05:00
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-11-01 15:32:08 -05:00
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
2016-05-22 16:30:21 -04:00
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
2016-02-04 05:11:11 -05:00
. "github.com/v2ray/v2ray-core/proxy/freedom"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
2015-10-04 14:22:52 -04:00
"github.com/v2ray/v2ray-core/testing/servers/tcp"
2016-02-04 05:11:11 -05:00
"github.com/v2ray/v2ray-core/transport/ray"
2015-09-24 12:01:02 -04:00
)
2016-02-04 05:11:11 -05:00
func TestSinglePacket(t *testing.T) {
v2testing.Current(t)
2015-11-01 15:32:08 -05:00
port := v2nettesting.PickPort()
2015-10-04 14:22:52 -04:00
tcpServer := &tcp.Server{
Port: port,
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()
freedom := NewFreedomConnection(&Config{}, space)
space.Initialize()
2016-02-04 05:11:11 -05:00
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
2015-09-24 12:01:02 -04:00
2016-05-16 14:53:18 -04:00
go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, 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) {
v2testing.Current(t)
2015-09-24 12:01:02 -04:00
2016-05-22 16:30:21 -04:00
freedom := NewFreedomConnection(&Config{}, app.NewSpace())
2016-02-04 05:11:11 -05:00
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
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) {
v2testing.Current(t)
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)
freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space)
space.Initialize()
ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
netassert.Destination(ipDest).IsTCP()
netassert.Address(ipDest.Address()).Equals(v2net.LocalHostIP)
}