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 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 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 TestVMessSerialization(t *testing.T) { v2testing.Current(t) id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") assert.Error(err).IsNil() userId := vmess.NewID(id) testUser := &vmess.User{ ID: userId, } userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)} userSet.AddUser(testUser) request := new(VMessRequest) request.Version = byte(0x01) request.User = testUser randBytes := make([]byte, 36) _, err = rand.Read(randBytes) assert.Error(err).IsNil() request.RequestIV = randBytes[:16] request.RequestKey = randBytes[16:32] request.ResponseHeader = randBytes[32:] request.Command = byte(0x01) request.Address = v2net.DomainAddress("v2ray.com") request.Port = v2net.Port(80) mockTime := Timestamp(1823730) buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil) if err != nil { t.Fatal(err) } userSet.UserHashes[string(buffer.Value[:16])] = 0 userSet.Timestamps[string(buffer.Value[:16])] = mockTime requestReader := NewVMessRequestReader(&userSet) actualRequest, err := requestReader.Read(bytes.NewReader(buffer.Value)) if err != nil { t.Fatal(err) } assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01)) assert.String(actualRequest.User.ID).Named("UserId").Equals(request.User.ID.String()) assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:]) assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:]) assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:]) assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command) assert.String(actualRequest.Address).Named("Address").Equals(request.Address.String()) }
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 TestAuthenticationRequestRead(t *testing.T) { v2testing.Current(t) rawRequest := []byte{ 0x05, // version 0x01, // nMethods 0x02, // methods } request, _, err := ReadAuthentication(bytes.NewReader(rawRequest)) assert.Error(err).IsNil() assert.Byte(request.version).Named("Version").Equals(0x05) assert.Byte(request.nMethods).Named("#Methods").Equals(0x01) assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02) }
func TestACKSegment(t *testing.T) { assert := assert.On(t) seg := &AckSegment{ Conv: 1, ReceivingWindow: 2, ReceivingNext: 3, Count: 5, NumberList: []uint32{1, 3, 5, 7, 9}, TimestampList: []uint32{2, 4, 6, 8, 10}, } nBytes := seg.ByteSize() bytes := seg.Bytes(nil) 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) for i := byte(0); i < seg2.Count; i++ { assert.Uint32(seg2.TimestampList[i]).Equals(seg.TimestampList[i]) assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i]) } }
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 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 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 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) account, ok := user.Account.(*VMessAccount) assert.Bool(ok).IsTrue() assert.String(account.ID.String()).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09") }
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")) }