github.com/xraypb/xray-core@v1.6.6/proxy/vmess/validator_test.go (about)

     1  package vmess_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/xraypb/xray-core/common"
     7  	"github.com/xraypb/xray-core/common/protocol"
     8  	"github.com/xraypb/xray-core/common/serial"
     9  	"github.com/xraypb/xray-core/common/uuid"
    10  	. "github.com/xraypb/xray-core/proxy/vmess"
    11  )
    12  
    13  func toAccount(a *Account) protocol.Account {
    14  	account, err := a.AsAccount()
    15  	common.Must(err)
    16  	return account
    17  }
    18  
    19  func TestUserValidator(t *testing.T) {
    20  	hasher := protocol.DefaultIDHash
    21  	v := NewTimedUserValidator(hasher)
    22  	defer common.Close(v)
    23  
    24  	id := uuid.New()
    25  	user := &protocol.MemoryUser{
    26  		Email: "test",
    27  		Account: toAccount(&Account{
    28  			Id: id.String(),
    29  		}),
    30  	}
    31  	common.Must(v.Add(user))
    32  
    33  	{
    34  		testSmallLag := func(lag int64) {
    35  			ts := int64(v.GetBaseTime()) + lag + 240
    36  			idHash := hasher(id.Bytes())
    37  			common.Must2(serial.WriteUint64(idHash, uint64(ts)))
    38  			userHash := idHash.Sum(nil)
    39  
    40  			euser, ets, found, _ := v.Get(userHash)
    41  			if !found {
    42  				t.Fatal("user not found")
    43  			}
    44  			if euser.Email != user.Email {
    45  				t.Error("unexpected user email: ", euser.Email, " want ", user.Email)
    46  			}
    47  			if int64(ets) != ts {
    48  				t.Error("unexpected timestamp: ", ets, " want ", ts)
    49  			}
    50  		}
    51  
    52  		testSmallLag(0)
    53  		testSmallLag(40)
    54  		testSmallLag(-40)
    55  		testSmallLag(80)
    56  		testSmallLag(-80)
    57  		testSmallLag(120)
    58  		testSmallLag(-120)
    59  	}
    60  
    61  	{
    62  		testBigLag := func(lag int64) {
    63  			ts := int64(v.GetBaseTime()) + lag + 240
    64  			idHash := hasher(id.Bytes())
    65  			common.Must2(serial.WriteUint64(idHash, uint64(ts)))
    66  			userHash := idHash.Sum(nil)
    67  
    68  			euser, _, found, _ := v.Get(userHash)
    69  			if found || euser != nil {
    70  				t.Error("unexpected user")
    71  			}
    72  		}
    73  
    74  		testBigLag(121)
    75  		testBigLag(-121)
    76  		testBigLag(310)
    77  		testBigLag(-310)
    78  		testBigLag(500)
    79  		testBigLag(-500)
    80  	}
    81  
    82  	if v := v.Remove(user.Email); !v {
    83  		t.Error("unable to remove user")
    84  	}
    85  	if v := v.Remove(user.Email); v {
    86  		t.Error("remove user twice")
    87  	}
    88  }
    89  
    90  func BenchmarkUserValidator(b *testing.B) {
    91  	for i := 0; i < b.N; i++ {
    92  		hasher := protocol.DefaultIDHash
    93  		v := NewTimedUserValidator(hasher)
    94  
    95  		for j := 0; j < 1500; j++ {
    96  			id := uuid.New()
    97  			v.Add(&protocol.MemoryUser{
    98  				Email: "test",
    99  				Account: toAccount(&Account{
   100  					Id: id.String(),
   101  				}),
   102  			})
   103  		}
   104  
   105  		common.Close(v)
   106  	}
   107  }