1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/freedom/freedom_test.go

67 lines
1.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 (
"testing"
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-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-02-04 05:11:11 -05:00
freedom := &FreedomConnection{}
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
packet := v2net.NewPacket(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), port), payload, false)
2015-09-24 12:01:02 -04:00
2016-02-04 05:11:11 -05:00
err = freedom.Dispatch(packet, traffic)
2015-09-24 12:01:02 -04:00
assert.Error(err).IsNil()
2016-02-04 05:11:11 -05:00
close(traffic.InboundInput())
2015-09-24 12:01:02 -04:00
2016-02-04 05:11:11 -05:00
respPayload := <-traffic.InboundOutput()
defer respPayload.Release()
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
_, open := <-traffic.InboundOutput()
assert.Bool(open).IsFalse()
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-02-04 05:11:11 -05:00
freedom := &FreedomConnection{}
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
2016-02-04 07:09:17 -05:00
packet := v2net.NewPacket(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, false)
2016-02-04 05:11:11 -05:00
err := freedom.Dispatch(packet, traffic)
assert.Error(err).IsNotNil()
2015-09-24 12:01:02 -04:00
2016-02-04 05:11:11 -05:00
_, open := <-traffic.InboundOutput()
assert.Bool(open).IsFalse()
2015-09-24 12:01:02 -04:00
}