func TestSwitchAccount(t *testing.T) { v2testing.Current(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } buffer := alloc.NewBuffer().Clear() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() cmd, err := UnmarshalCommand(1, buffer.Value[2:]) assert.Error(err).IsNil() sa2, ok := cmd.(*protocol.CommandSwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
func TestRequestSerialization(t *testing.T) { v2testing.Current(t) user := protocol.NewUser( protocol.NewID(uuid.New()), protocol.UserLevelUntrusted, 0, "*****@*****.**") expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOption(0), Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), } buffer := alloc.NewBuffer().Clear() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := protocol.NewTimedUserValidator(protocol.DefaultIDHash) userValidator.Add(user) server := NewServerSession(userValidator) actualRequest, err := server.DecodeRequestHeader(buffer) assert.Error(err).IsNil() assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) netassert.Address(expectedRequest.Address).Equals(actualRequest.Address) netassert.Port(expectedRequest.Port).Equals(actualRequest.Port) }
func TestSwitchAccount(t *testing.T) { v2testing.Current(t) sa := &SwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } cmd, err := CreateResponseCommand(1) assert.Error(err).IsNil() buffer := bytes.NewBuffer(make([]byte, 0, 1024)) sa.Marshal(buffer) cmd.Unmarshal(buffer.Bytes()) sa2, ok := cmd.(*SwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
func TestSwitchAccount(t *testing.T) { v2testing.Current(t) sa := &SwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, ValidUntil: time.Now(), } cmd, err := CreateResponseCommand(1) assert.Error(err).IsNil() buffer := bytes.NewBuffer(make([]byte, 0, 1024)) nBytes, err := sa.Marshal(buffer) assert.Error(err).IsNil() assert.Int(nBytes).Equals(buffer.Len()) cmd.Unmarshal(buffer.Bytes()) sa2, ok := cmd.(*SwitchAccount) assert.Bool(ok).IsTrue() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Int64(sa.ValidUntil.Unix()).Equals(sa2.ValidUntil.Unix()) }
func TestIPv6Request(t *testing.T) { v2testing.Current(t) request, err := ReadRequest(alloc.NewBuffer().Clear().AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)) assert.Error(err).IsNil() assert.Byte(request.Command).Equals(1) assert.Bytes(request.IPv6[:]).Equals([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}) v2netassert.Port(request.Port).Equals(8) }
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 TestNormalRequestParsing(t *testing.T) { v2testing.Current(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80) request, err := ReadRequest(buffer, nil, false) assert.Error(err).IsNil() netassert.Address(request.Address).Equals(v2net.IPAddress([]byte{127, 0, 0, 1})) netassert.Port(request.Port).Equals(v2net.Port(80)) assert.Bool(request.OTA).IsFalse() }
func TestUDPRequestParsing(t *testing.T) { v2testing.Current(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80, 1, 2, 3, 4, 5, 6) request, err := ReadRequest(buffer, nil, true) assert.Error(err).IsNil() netassert.Address(request.Address).Equals(v2net.IPAddress([]byte{127, 0, 0, 1})) netassert.Port(request.Port).Equals(v2net.Port(80)) assert.Bool(request.OTA).IsFalse() assert.Bytes(request.UDPPayload.Value).Equals([]byte{1, 2, 3, 4, 5, 6}) }
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) }
func TestSocks4AuthenticationRequestRead(t *testing.T) { v2testing.Current(t) rawRequest := []byte{ 0x04, // version 0x01, // command 0x00, 0x35, 0x72, 0x72, 0x72, 0x72, } _, request4, err := ReadAuthentication(bytes.NewReader(rawRequest)) assert.Error(err).Equals(Socks4Downgrade) assert.Byte(request4.Version).Named("Version").Equals(0x04) assert.Byte(request4.Command).Named("Command").Equals(0x01) v2netassert.Port(request4.Port).Named("Port").Equals(v2net.Port(53)) assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72}) }
func TestDomainAddressRequest(t *testing.T) { v2testing.Current(t) payload := make([]byte, 0, 1024) payload = append(payload, 0, 0, 1, AddrTypeDomain, byte(len("v2ray.com"))) payload = append(payload, []byte("v2ray.com")...) payload = append(payload, 0, 80) payload = append(payload, []byte("Actual payload")...) request, err := ReadUDPRequest(payload) assert.Error(err).IsNil() assert.Byte(request.Fragment).Equals(1) assert.String(request.Address).Equals("v2ray.com") netassert.Port(request.Port).Equals(v2net.Port(80)) assert.Bytes(request.Data.Value).Equals([]byte("Actual payload")) }
func TestNormalGetRequest(t *testing.T) { v2testing.Current(t) testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) httpProxy := NewHttpProxyServer(&Config{}, testPacketDispatcher) defer httpProxy.Close() port := v2nettesting.PickPort() err := httpProxy.Listen(port) assert.Error(err).IsNil() netassert.Port(port).Equals(httpProxy.Port()) httpClient := &http.Client{} resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/") assert.Error(err).IsNil() assert.Int(resp.StatusCode).Equals(400) }
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 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 TestRequestRead(t *testing.T) { v2testing.Current(t) rawRequest := []byte{ 0x05, // version 0x01, // cmd connect 0x00, // reserved 0x01, // ipv4 type 0x72, 0x72, 0x72, 0x72, // 114.114.114.114 0x00, 0x35, // port 53 } request, err := ReadRequest(bytes.NewReader(rawRequest)) assert.Error(err).IsNil() assert.Byte(request.Version).Named("Version").Equals(0x05) assert.Byte(request.Command).Named("Command").Equals(0x01) assert.Byte(request.AddrType).Named("Address Type").Equals(0x01) assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72}) v2netassert.Port(request.Port).Named("Port").Equals(v2net.Port(53)) }