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 TestIPv4Address(t *testing.T) { v2testing.Current(t) ip := []byte{byte(1), byte(2), byte(3), byte(4)} addr := v2net.IPAddress(ip) v2netassert.Address(addr).IsIPv4() v2netassert.Address(addr).IsNotIPv6() v2netassert.Address(addr).IsNotDomain() assert.Bytes(addr.IP()).Equals(ip) assert.String(addr).Equals("1.2.3.4") }
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 TestNetIPv4Address(t *testing.T) { v2testing.Current(t) ip := net.IPv4(1, 2, 3, 4) addr := v2net.IPAddress(ip) v2netassert.Address(addr).IsIPv4() assert.String(addr).Equals("1.2.3.4") }
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 TestOTARequest(t *testing.T) { v2testing.Current(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 0) auth := NewAuthenticator(HeaderKeyGenerator( []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5})) request, err := ReadRequest(buffer, auth, false) assert.Error(err).IsNil() netassert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com")) assert.Bool(request.OTA).IsTrue() }
func TestCacheDnsIPv4(t *testing.T) { v2testing.Current(t) cd := &CacheDns{ Address: v2net.IPAddress([]byte{1, 2, 3, 4}), } buffer := alloc.NewBuffer().Clear() defer buffer.Release() cd.Marshal(buffer) cd2 := &CacheDns{} err := cd2.Unmarshal(buffer.Value) assert.Error(err).IsNil() netassert.Address(cd.Address).Equals(cd2.Address) }
func TestUDPRequestWithOTA(t *testing.T) { v2testing.Current(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes( 0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 58, 32, 223, 30, 57, 199, 50, 139, 143, 101) auth := NewAuthenticator(HeaderKeyGenerator( []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5})) request, err := ReadRequest(buffer, auth, true) assert.Error(err).IsNil() netassert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com")) assert.Bool(request.OTA).IsTrue() assert.Bytes(request.UDPPayload.Value).Equals([]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) }
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 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) }