github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/vmess/validator_test.go (about)

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