func TestReceiverUser(t *testing.T) { assert := assert.On(t) id := protocol.NewID(uuid.New()) alters := protocol.NewAlterIDs(id, 100) account := &protocol.VMessAccount{ ID: id, AlterIDs: alters, } user := protocol.NewUser(account, protocol.UserLevel(0), "") rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := protocol.NewID(uuid.New()) alters2 := protocol.NewAlterIDs(id2, 100) account2 := &protocol.VMessAccount{ ID: id2, AlterIDs: alters2, } user2 := protocol.NewUser(account2, protocol.UserLevel(0), "") assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
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 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 TestReceiverUser(t *testing.T) { v2testing.Current(t) id := proto.NewID(uuid.New()) user := proto.NewUser(id, proto.UserLevel(0), 100, "") rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := proto.NewID(uuid.New()) user2 := proto.NewUser(id2, proto.UserLevel(0), 100, "") assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
func (this *userByEmail) Get(email string) (*proto.User, bool) { var user *proto.User var found bool this.RLock() user, found = this.cache[email] this.RUnlock() if !found { this.Lock() user, found = this.cache[email] if !found { id := proto.NewID(uuid.New()) user = proto.NewUser(id, this.defaultLevel, this.defaultAlterIDs, email) this.cache[email] = user } this.Unlock() } return user, found }
func (this *userByEmail) Get(email string) (*protocol.User, bool) { var user *protocol.User var found bool this.RLock() user, found = this.cache[email] this.RUnlock() if !found { this.Lock() user, found = this.cache[email] if !found { id := protocol.NewID(uuid.New()) alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs) account := &protocol.VMessAccount{ ID: id, AlterIDs: alterIDs, } user = protocol.NewUser(account, this.defaultLevel, email) this.cache[email] = user } this.Unlock() } return user, found }
func TestCmdKey(t *testing.T) { v2testing.Current(t) id := NewID(uuid.New()) assert.Bool(serial.BytesLiteral(id.CmdKey()).AllZero()).IsFalse() }
func TestCmdKey(t *testing.T) { assert := assert.On(t) id := NewID(uuid.New()) assert.Bool(predicate.BytesAll(id.CmdKey(), 0)).IsFalse() }