func TestHopByHopHeadersStrip(t *testing.T) { v2testing.Current(t) rawRequest := `GET /pkg/net/http/ HTTP/1.1 Host: golang.org Connection: keep-alive,Foo, Bar Foo: foo Bar: bar Proxy-Connection: keep-alive Proxy-Authenticate: abc User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10 Accept-Encoding: gzip Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7 Cache-Control: no-cache Accept-Language: de,en;q=0.7,en-us;q=0.3 ` b := bufio.NewReader(strings.NewReader(rawRequest)) req, err := http.ReadRequest(b) assert.Error(err).IsNil() assert.StringLiteral(req.Header.Get("Foo")).Equals("foo") assert.StringLiteral(req.Header.Get("Bar")).Equals("bar") assert.StringLiteral(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar") assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("keep-alive") assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("abc") StripHopByHopHeaders(req) assert.StringLiteral(req.Header.Get("Connection")).Equals("close") assert.StringLiteral(req.Header.Get("Foo")).Equals("") assert.StringLiteral(req.Header.Get("Bar")).Equals("") assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("") assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("") }
func TestStreamLogger(t *testing.T) { v2testing.Current(t) buffer := bytes.NewBuffer(make([]byte, 0, 1024)) infoLogger = &stdOutLogWriter{ logger: log.New(buffer, "", 0), } Info("Test ", "Stream Logger", " Format") assert.StringLiteral(string(buffer.Bytes())).Equals("[Info]Test Stream Logger Format" + platform.LineSeparator()) buffer.Reset() errorLogger = infoLogger Error("Test ", serial.StringLiteral("literal"), " Format") assert.StringLiteral(string(buffer.Bytes())).Equals("[Error]Test literal Format" + platform.LineSeparator()) }
func TestBuildAndRun(t *testing.T) { v2testing.Current(t) gopath := os.Getenv("GOPATH") goOS := parseOS(runtime.GOOS) goArch := parseArch(runtime.GOARCH) target := filepath.Join(gopath, "src", "v2ray_test") if goOS == Windows { target += ".exe" } err := buildV2Ray(target, "v1.0", goOS, goArch) assert.Error(err).IsNil() outBuffer := bytes.NewBuffer(make([]byte, 0, 1024)) errBuffer := bytes.NewBuffer(make([]byte, 0, 1024)) configFile := filepath.Join(gopath, "src", "github.com", "v2ray", "v2ray-core", "release", "config", "vpoint_socks_vmess.json") cmd := exec.Command(target, "--config="+configFile) cmd.Stdout = outBuffer cmd.Stderr = errBuffer cmd.Start() <-time.After(1 * time.Second) cmd.Process.Kill() outStr := string(outBuffer.Bytes()) errStr := string(errBuffer.Bytes()) assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue() assert.StringLiteral(errStr).Equals("") os.Remove(target) }
func TestSocksUdpSend(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnInput: bytes.NewReader(connInput), ConnOutput: connOutput, } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) { return och, nil }) assert.Error(err).IsNil() config := &point.Config{ Port: port, InboundConfig: &point.ConnectionConfig{ Protocol: "socks", Settings: []byte(`{"auth": "noauth", "udp": true}`), }, OutboundConfig: &point.ConnectionConfig{ Protocol: protocol, Settings: nil, }, } point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), Zone: "", }) assert.Error(err).IsNil() data2Send := []byte("Fake DNS request") buffer := make([]byte, 0, 1024) buffer = append(buffer, 0, 0, 0) buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53) buffer = append(buffer, data2Send...) conn.Write(buffer) response := make([]byte, 1024) nBytes, err := conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[10:nBytes]).Equals(connInput) assert.Bytes(data2Send).Equals(connOutput.Bytes()) assert.StringLiteral(och.Destination.String()).Equals("udp:8.8.4.4:53") }
func TestPubsub(t *testing.T) { v2testing.Current(t) messages := make(map[string]app.PubsubMessage) pubsub := New() pubsub.Subscribe(&apptesting.Context{}, "t1", func(message app.PubsubMessage) { messages["t1"] = message }) pubsub.Subscribe(&apptesting.Context{}, "t2", func(message app.PubsubMessage) { messages["t2"] = message }) message := app.PubsubMessage([]byte("This is a pubsub message.")) pubsub.Publish(&apptesting.Context{}, "t2", message) <-time.Tick(time.Second) _, found := messages["t1"] assert.Bool(found).IsFalse() actualMessage, found := messages["t2"] assert.Bool(found).IsTrue() assert.StringLiteral(string(actualMessage)).Equals(string(message)) }
func TestDokodemoUDP(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() data2Send := "Data to be sent to remote." udpServer := &udp.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 := udpServer.Start() assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() networkList := v2netjson.NetworkList([]string{"udp"}) config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "dokodemo-door", SettingsValue: &json.DokodemoConfig{ Host: v2netjson.NewIPHost(net.ParseIP("127.0.0.1")), PortValue: port, NetworkList: &networkList, TimeoutValue: 0, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", SettingsValue: nil, }, } point, err := point.NewPoint(&config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(pointPort), Zone: "", }) assert.Error(err).IsNil() udpClient.Write([]byte(data2Send)) response := make([]byte, 1024) nBytes, err := udpClient.Read(response) assert.Error(err).IsNil() udpClient.Close() assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) }
func TestHttpProxy(t *testing.T) { v2testing.Current(t) httpServer := &v2http.Server{ Port: v2net.Port(50042), PathHandler: make(map[string]http.HandlerFunc), } _, err := httpServer.Start() assert.Error(err).IsNil() defer httpServer.Close() assert.Error(InitializeServerSetOnce("test_5")).IsNil() transport := &http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse("http://127.0.0.1:50040/") }, } client := &http.Client{ Transport: transport, } resp, err := client.Get("http://127.0.0.1:50042/") assert.Error(err).IsNil() assert.Int(resp.StatusCode).Equals(200) content, err := ioutil.ReadAll(resp.Body) assert.Error(err).IsNil() assert.StringLiteral(string(content)).Equals("Home") CloseAllServers() }
func TestDokodemoTCP(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() data2Send := "Data to be sent to remote." 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() pointPort := v2nettesting.PickPort() config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "dokodemo-door", SettingsValue: []byte(`{ "address": "127.0.0.1", "port": ` + port.String() + `, "network": "tcp", "timeout": 0 }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", SettingsValue: nil, }, } point, err := point.NewPoint(&config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(pointPort), Zone: "", }) assert.Error(err).IsNil() tcpClient.Write([]byte(data2Send)) tcpClient.CloseWrite() response := make([]byte, 1024) nBytes, err := tcpClient.Read(response) assert.Error(err).IsNil() tcpClient.Close() assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) }
func TestSocksUdpSend(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnInput: bytes.NewReader(connInput), ConnOutput: connOutput, } connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och) config := mocks.Config{ PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", SettingsValue: &json.SocksConfig{ AuthMethod: "noauth", UDP: true, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_och", SettingsValue: nil, }, } point, err := point.NewPoint(&config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), Zone: "", }) assert.Error(err).IsNil() data2Send := []byte("Fake DNS request") buffer := make([]byte, 0, 1024) buffer = append(buffer, 0, 0, 0) buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53) buffer = append(buffer, data2Send...) conn.Write(buffer) response := make([]byte, 1024) nBytes, err := conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[10:nBytes]).Equals(connInput) assert.Bytes(data2Send).Equals(connOutput.Bytes()) assert.StringLiteral(och.Destination.String()).Equals("udp:8.8.4.4:53") }
func TestSocksTcpConnect(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnOutput: connOutput, ConnInput: bytes.NewReader(connInput), } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) { return och, nil }) assert.Error(err).IsNil() config := mocks.Config{ PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", SettingsValue: []byte(` { "auth": "noauth" }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, SettingsValue: nil, }, } point, err := point.NewPoint(&config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct) assert.Error(err).IsNil() targetServer := "google.com:80" conn, err := socks5Client.Dial("tcp", targetServer) assert.Error(err).IsNil() data2Send := "The data to be sent to remote server." conn.Write([]byte(data2Send)) if tcpConn, ok := conn.(*net.TCPConn); ok { tcpConn.CloseWrite() } dataReturned, err := ioutil.ReadAll(conn) assert.Error(err).IsNil() conn.Close() assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes()) assert.Bytes(dataReturned).Equals(connInput) assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr()) }
func TestRandom(t *testing.T) { v2testing.Current(t) uuid := New() uuid2 := New() assert.StringLiteral(uuid.String()).NotEquals(uuid2.String()) assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes()) }
func TestSocksTcpConnectWithUserPass(t *testing.T) { v2testing.Current(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnInput: bytes.NewReader(connInput), ConnOutput: connOutput, } connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och) config := mocks.Config{ PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", SettingsValue: &json.SocksConfig{ AuthMethod: "password", Accounts: json.SocksAccountMap{ "userx": "passy", }, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_och", SettingsValue: nil, }, } point, err := point.NewPoint(&config) assert.Error(err).IsNil() err = point.Start() assert.Error(err).IsNil() socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passy"}, proxy.Direct) assert.Error(err).IsNil() targetServer := "1.2.3.4:443" conn, err := socks5Client.Dial("tcp", targetServer) assert.Error(err).IsNil() data2Send := "The data to be sent to remote server." conn.Write([]byte(data2Send)) if tcpConn, ok := conn.(*net.TCPConn); ok { tcpConn.CloseWrite() } dataReturned, err := ioutil.ReadAll(conn) assert.Error(err).IsNil() conn.Close() assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes()) assert.Bytes(dataReturned).Equals(connInput) assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr()) }
func TestServerSampleConfig(t *testing.T) { v2testing.Current(t) // TODO: fix for Windows baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) assert.Error(err).IsNil() netassert.Port(pointConfig.Port).IsValid() assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func TestNewUUID(t *testing.T) { v2testing.Current(t) uuid := New() uuid2, err := ParseString(uuid.String()) assert.Error(err).IsNil() assert.StringLiteral(uuid.String()).Equals(uuid2.String()) assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes()) }
func TestServerSampleConfig(t *testing.T) { v2testing.Current(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config") pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) assert.Error(err).IsNil() netassert.Port(pointConfig.Port).IsValid() assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func (subject *AddressSubject) Equals(another v2net.Address) { if subject.value.IsIPv4() && another.IsIPv4() { IP(subject.value.IP()).Equals(another.IP()) } else if subject.value.IsIPv6() && another.IsIPv6() { IP(subject.value.IP()).Equals(another.IP()) } else if subject.value.IsDomain() && another.IsDomain() { assert.StringLiteral(subject.value.Domain()).Equals(another.Domain()) } else { subject.Fail(subject.DisplayString(), "equals to", another) } }
func TestDomainParsing(t *testing.T) { v2testing.Current(t) rawJson := "\"v2ray.com\"" var address AddressJson err := json.Unmarshal([]byte(rawJson), &address) assert.Error(err).IsNil() assert.Bool(address.Address.IsIPv4()).IsFalse() assert.Bool(address.Address.IsDomain()).IsTrue() assert.StringLiteral(address.Address.Domain()).Equals("v2ray.com") }
func TestDomainParsing(t *testing.T) { v2testing.Current(t) rawJson := "\"v2ray.com\"" host := &Host{} err := json.Unmarshal([]byte(rawJson), host) assert.Error(err).IsNil() assert.Bool(host.IsIP()).IsFalse() assert.Bool(host.IsDomain()).IsTrue() assert.StringLiteral(host.Domain()).Equals("v2ray.com") }
func TestDomainAddress(t *testing.T) { v2testing.Current(t) domain := "v2ray.com" addr := v2net.DomainAddress(domain) v2netassert.Address(addr).IsDomain() v2netassert.Address(addr).IsNotIPv6() v2netassert.Address(addr).IsNotIPv4() assert.StringLiteral(addr.Domain()).Equals(domain) assert.String(addr).Equals("v2ray.com") }
func TestSimpleRouter(t *testing.T) { v2testing.Current(t) router := NewRouter().AddRule( &Rule{ Tag: "test", Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()), }) tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() assert.StringLiteral(tag).Equals("test") }
func TestSimpleRouter(t *testing.T) { v2testing.Current(t) router := NewRouter().AddRule( &testinconfig.TestRule{ TagValue: "test", Function: func(dest v2net.Destination) bool { return dest.IsTCP() }, }) tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() assert.StringLiteral(tag).Equals("test") }
func TestChinaIPJson(t *testing.T) { v2testing.Current(t) rule := ParseRule([]byte(`{ "type": "chinaip", "outboundTag": "x" }`)) assert.StringLiteral(rule.Tag).Equals("x") assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com assert.Bool(rule.Apply(makeDestination("120.135.126.1"))).IsTrue() assert.Bool(rule.Apply(makeDestination("8.8.8.8"))).IsFalse() }
func TestChinaSitesJson(t *testing.T) { v2testing.Current(t) rule := ParseRule([]byte(`{ "type": "chinasites", "outboundTag": "y" }`)) assert.StringLiteral(rule.Tag).Equals("y") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("12306.cn"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("v2ray.com"))).IsFalse() }
func TestDialDomain(t *testing.T) { v2testing.Current(t) server := &tcp.Server{ Port: v2nettesting.PickPort(), } dest, err := server.Start() assert.Error(err).IsNil() defer server.Close() conn, err := Dial(v2net.TCPDestination(v2net.DomainAddress("local.v2ray.com"), dest.Port())) assert.Error(err).IsNil() assert.StringLiteral(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port().String()) conn.Close() }
func TestRouter(t *testing.T) { v2testing.Current(t) baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" pointConfig, err := point.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) assert.Error(err).IsNil() router, err := CreateRouter(pointConfig.RouterConfig.Strategy, pointConfig.RouterConfig.Settings) assert.Error(err).IsNil() dest := v2net.TCPDestination(v2net.IPAddress(net.ParseIP("120.135.126.1")), 80) tag, err := router.TakeDetour(dest) assert.Error(err).IsNil() assert.StringLiteral(tag).Equals("direct") }
func TestDokodemoTCP(t *testing.T) { v2testing.Current(t) tcpServer := &tcp.Server{ Port: v2net.Port(50016), 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() defer tcpServer.Close() assert.Error(InitializeServerSetOnce("test_2")).IsNil() dokodemoPortStart := v2net.Port(50011) dokodemoPortEnd := v2net.Port(50015) for port := dokodemoPortStart; port <= dokodemoPortEnd; port++ { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), }) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) conn.CloseWrite() response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() } CloseAllServers() }
func TestDefaultValueOfRandomAllocation(t *testing.T) { v2testing.Current(t) rawJson := `{ "protocol": "vmess", "port": 1, "settings": {}, "allocate": { "strategy": "random" } }` inboundDetourConfig := new(InboundDetourConfig) err := json.Unmarshal([]byte(rawJson), inboundDetourConfig) assert.Error(err).IsNil() assert.StringLiteral(inboundDetourConfig.Allocation.Strategy).Equals(AllocationStrategyRandom) assert.Int(inboundDetourConfig.Allocation.Concurrency).Equals(3) assert.Int(inboundDetourConfig.Allocation.Refresh).Equals(5) }
func TestDokodemoTCP(t *testing.T) { v2testing.Current(t) testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) data2Send := "Data to be sent to remote." dokodemo := NewDokodemoDoor(&Config{ Address: v2net.IPAddress([]byte{1, 2, 3, 4}), Port: 128, Network: v2net.TCPNetwork.AsList(), Timeout: 600, }, testPacketDispatcher) defer dokodemo.Close() port := v2nettesting.PickPort() err := dokodemo.Listen(port) assert.Error(err).IsNil() netassert.Port(port).Equals(dokodemo.Port()) tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), Zone: "", }) assert.Error(err).IsNil() tcpClient.Write([]byte(data2Send)) tcpClient.CloseWrite() lastPacket := <-testPacketDispatcher.LastPacket response := make([]byte, 1024) nBytes, err := tcpClient.Read(response) assert.Error(err).IsNil() tcpClient.Close() assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) assert.Bool(lastPacket.Destination().IsTCP()).IsTrue() netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4})) netassert.Port(lastPacket.Destination().Port()).Equals(128) }
func TestShadowsocksTCP(t *testing.T) { v2testing.Current(t) tcpServer := &tcp.Server{ Port: v2net.Port(50052), 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() defer tcpServer.Close() assert.Error(InitializeServerServer("test_6")).IsNil() cipher, err := ssclient.NewCipher("aes-256-cfb", "v2ray-password") assert.Error(err).IsNil() rawAddr := []byte{1, 127, 0, 0, 1, 0xc3, 0x84} // 127.0.0.1:50052 conn, err := ssclient.DialWithRawAddr(rawAddr, "127.0.0.1:50051", cipher) assert.Error(err).IsNil() payload := "shadowsocks request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) conn.Conn.(*net.TCPConn).CloseWrite() response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() CloseAllServers() }
func TestDokodemoUDP(t *testing.T) { v2testing.Current(t) testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) data2Send := "Data to be sent to remote." dokodemo := NewDokodemoDoor(&Config{ Address: v2net.IPAddress([]byte{5, 6, 7, 8}), Port: 256, Network: v2net.UDPNetwork.AsList(), Timeout: 600, }, testPacketDispatcher) defer dokodemo.Close() port := v2nettesting.PickPort() err := dokodemo.Listen(port) assert.Error(err).IsNil() netassert.Port(port).Equals(dokodemo.Port()) udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), Zone: "", }) assert.Error(err).IsNil() udpClient.Write([]byte(data2Send)) udpClient.Close() lastPacket := <-testPacketDispatcher.LastPacket assert.StringLiteral(data2Send).Equals(string(lastPacket.Chunk().Value)) assert.Bool(lastPacket.Destination().IsUDP()).IsTrue() netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8})) netassert.Port(lastPacket.Destination().Port()).Equals(256) }