func TestSwitchAccount(t *testing.T) { assert := assert.On(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } buffer := buf.New() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2)) assert.Error(err).IsNil() sa2, ok := cmd.(*protocol.CommandSwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() assert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID.String()).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
func TestCmdSegment(t *testing.T) { assert := assert.On(t) seg := &CmdOnlySegment{ Conv: 1, Cmd: CommandPing, Option: SegmentOptionClose, SendingNext: 11, ReceivinNext: 13, PeerRTO: 15, } nBytes := seg.ByteSize() bytes := make([]byte, nBytes) seg.Bytes()(bytes) assert.Int(len(bytes)).Equals(nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*CmdOnlySegment) assert.Uint16(seg2.Conv).Equals(seg.Conv) assert.Byte(byte(seg2.Command())).Equals(byte(seg.Command())) assert.Byte(byte(seg2.Option)).Equals(byte(seg.Option)) assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) assert.Uint32(seg2.ReceivinNext).Equals(seg.ReceivinNext) assert.Uint32(seg2.PeerRTO).Equals(seg.PeerRTO) }
func TestAuthenticationRequestRead(t *testing.T) { assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes( 0x05, // version 0x01, // nMethods 0x02, // methods ) request, _, err := ReadAuthentication(buffer) assert.Error(err).IsNil() assert.Byte(request.version).Equals(0x05) assert.Byte(request.nMethods).Equals(0x01) assert.Byte(request.authMethods[0]).Equals(0x02) }
func TestSocks4AuthenticationRequestRead(t *testing.T) { assert := assert.On(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).Equals(0x04) assert.Byte(request4.Command).Equals(0x01) assert.Port(request4.Port).Equals(v2net.Port(53)) assert.Bytes(request4.IP[:]).Equals([]byte{0x72, 0x72, 0x72, 0x72}) }
func TestACKSegment(t *testing.T) { assert := assert.On(t) seg := &AckSegment{ Conv: 1, ReceivingWindow: 2, ReceivingNext: 3, Timestamp: 10, Count: 5, NumberList: []uint32{1, 3, 5, 7, 9}, } nBytes := seg.ByteSize() bytes := make([]byte, nBytes) seg.Bytes()(bytes) assert.Int(len(bytes)).Equals(nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*AckSegment) assert.Uint16(seg2.Conv).Equals(seg.Conv) assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow) assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext) assert.Byte(seg2.Count).Equals(seg.Count) assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp) for i := byte(0); i < seg2.Count; i++ { assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i]) } }
func TestIPv6Request(t *testing.T) { assert := assert.On(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}) assert.Port(request.Port).Equals(8) }
func TestRequestRead(t *testing.T) { assert := assert.On(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).Equals(0x05) assert.Byte(request.Command).Equals(0x01) assert.Byte(request.AddrType).Equals(0x01) assert.Bytes(request.IPv4[:]).Equals([]byte{0x72, 0x72, 0x72, 0x72}) assert.Port(request.Port).Equals(v2net.Port(53)) }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := &protocol.User{ Level: 0, Email: "*****@*****.**", } account := &vmess.Account{ Id: uuid.New().String(), AlterId: 0, } user.Account = serial.ToTypedMessage(account) expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOptionConnectionReuse, Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), Security: protocol.Security(protocol.SecurityType_AES128_GCM), } buffer := buf.New() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := vmess.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)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security)) }
func TestUserParsing(t *testing.T) { assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{ "id": "96edb838-6d68-42ef-a933-25f7ac3a9d09", "email": "*****@*****.**", "level": 1, "alterId": 100 }`), user) assert.Error(err).IsNil() assert.Byte(byte(user.Level)).Equals(1) assert.String(user.Email).Equals("*****@*****.**") }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := protocol.NewUser( protocol.UserLevelUntrusted, "*****@*****.**") user.Account = &vmess.Account{ ID: protocol.NewID(uuid.New()), AlterIDs: nil, } 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 := vmess.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)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) }
func TestDomainAddressRequest(t *testing.T) { assert := assert.On(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.Address(request.Address).EqualsString("v2ray.com") assert.Port(request.Port).Equals(v2net.Port(80)) assert.String(request.Data.String()).Equals("Actual payload") }