| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 | package freedom_testimport (	"net"	"testing"	"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"	"github.com/v2ray/v2ray-core/common/alloc"	v2net "github.com/v2ray/v2ray-core/common/net"	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"	. "github.com/v2ray/v2ray-core/proxy/freedom"	"github.com/v2ray/v2ray-core/testing/assert"	"github.com/v2ray/v2ray-core/testing/servers/tcp"	"github.com/v2ray/v2ray-core/transport/ray")func TestSinglePacket(t *testing.T) {	assert := assert.On(t)	port := v2nettesting.PickPort()	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()	space := app.NewSpace()	freedom := NewFreedomConnection(&Config{}, space, v2net.AnyIP)	space.Initialize()	traffic := ray.NewRay()	data2Send := "Data to be sent to remote"	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))	go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, port), payload, traffic)	traffic.InboundInput().Close()	respPayload, err := traffic.InboundOutput().Read()	assert.Error(err).IsNil()	assert.Bytes(respPayload.Value).Equals([]byte("Processed: Data to be sent to remote"))	tcpServer.Close()}func TestUnreachableDestination(t *testing.T) {	assert := assert.On(t)	freedom := NewFreedomConnection(&Config{}, app.NewSpace(), v2net.AnyIP)	traffic := ray.NewRay()	data2Send := "Data to be sent to remote"	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))	err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)	assert.Error(err).IsNotNil()}func TestIPResolution(t *testing.T) {	assert := assert.On(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, v2net.AnyIP)	space.Initialize()	ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))	assert.Destination(ipDest).IsTCP()	assert.Address(ipDest.Address()).Equals(v2net.LocalHostIP)}
 |