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

     1  package aead
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestCreateAuthID(t *testing.T) {
    13  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    14  	authid := CreateAuthID(key, time.Now().Unix())
    15  
    16  	fmt.Println(key)
    17  	fmt.Println(authid)
    18  }
    19  
    20  func TestCreateAuthIDAndDecode(t *testing.T) {
    21  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    22  	authid := CreateAuthID(key, time.Now().Unix())
    23  
    24  	fmt.Println(key)
    25  	fmt.Println(authid)
    26  
    27  	AuthDecoder := NewAuthIDDecoderHolder()
    28  	var keyw [16]byte
    29  	copy(keyw[:], key)
    30  	AuthDecoder.AddUser(keyw, "Demo User")
    31  	res, err := AuthDecoder.Match(authid)
    32  	fmt.Println(res)
    33  	fmt.Println(err)
    34  	assert.Equal(t, "Demo User", res)
    35  	assert.Nil(t, err)
    36  }
    37  
    38  func TestCreateAuthIDAndDecode2(t *testing.T) {
    39  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    40  	authid := CreateAuthID(key, time.Now().Unix())
    41  
    42  	fmt.Println(key)
    43  	fmt.Println(authid)
    44  
    45  	AuthDecoder := NewAuthIDDecoderHolder()
    46  	var keyw [16]byte
    47  	copy(keyw[:], key)
    48  	AuthDecoder.AddUser(keyw, "Demo User")
    49  	res, err := AuthDecoder.Match(authid)
    50  	fmt.Println(res)
    51  	fmt.Println(err)
    52  	assert.Equal(t, "Demo User", res)
    53  	assert.Nil(t, err)
    54  
    55  	key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test")
    56  	authid2 := CreateAuthID(key2, time.Now().Unix())
    57  
    58  	res2, err2 := AuthDecoder.Match(authid2)
    59  	assert.EqualError(t, err2, "user do not exist")
    60  	assert.Nil(t, res2)
    61  }
    62  
    63  func TestCreateAuthIDAndDecodeMassive(t *testing.T) {
    64  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    65  	authid := CreateAuthID(key, time.Now().Unix())
    66  
    67  	fmt.Println(key)
    68  	fmt.Println(authid)
    69  
    70  	AuthDecoder := NewAuthIDDecoderHolder()
    71  	var keyw [16]byte
    72  	copy(keyw[:], key)
    73  	AuthDecoder.AddUser(keyw, "Demo User")
    74  	res, err := AuthDecoder.Match(authid)
    75  	fmt.Println(res)
    76  	fmt.Println(err)
    77  	assert.Equal(t, "Demo User", res)
    78  	assert.Nil(t, err)
    79  
    80  	for i := 0; i <= 10000; i++ {
    81  		key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i))
    82  		var keyw2 [16]byte
    83  		copy(keyw2[:], key2)
    84  		AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i))
    85  	}
    86  
    87  	authid3 := CreateAuthID(key, time.Now().Unix())
    88  
    89  	res2, err2 := AuthDecoder.Match(authid3)
    90  	assert.Equal(t, "Demo User", res2)
    91  	assert.Nil(t, err2)
    92  }
    93  
    94  func TestCreateAuthIDAndDecodeSuperMassive(t *testing.T) {
    95  	key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
    96  	authid := CreateAuthID(key, time.Now().Unix())
    97  
    98  	fmt.Println(key)
    99  	fmt.Println(authid)
   100  
   101  	AuthDecoder := NewAuthIDDecoderHolder()
   102  	var keyw [16]byte
   103  	copy(keyw[:], key)
   104  	AuthDecoder.AddUser(keyw, "Demo User")
   105  	res, err := AuthDecoder.Match(authid)
   106  	fmt.Println(res)
   107  	fmt.Println(err)
   108  	assert.Equal(t, "Demo User", res)
   109  	assert.Nil(t, err)
   110  
   111  	for i := 0; i <= 1000000; i++ {
   112  		key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i))
   113  		var keyw2 [16]byte
   114  		copy(keyw2[:], key2)
   115  		AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i))
   116  	}
   117  
   118  	authid3 := CreateAuthID(key, time.Now().Unix())
   119  
   120  	before := time.Now()
   121  	res2, err2 := AuthDecoder.Match(authid3)
   122  	after := time.Now()
   123  	assert.Equal(t, "Demo User", res2)
   124  	assert.Nil(t, err2)
   125  
   126  	fmt.Println(after.Sub(before).Seconds())
   127  }