github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/proxy/vmess/aead/authid_test.go (about) 1 package aead 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "strconv" 7 "testing" 8 "time" 9 ) 10 11 func TestCreateAuthID(t *testing.T) { 12 key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") 13 authid := CreateAuthID(key, time.Now().Unix()) 14 15 fmt.Println(key) 16 fmt.Println(authid) 17 } 18 19 func TestCreateAuthIDAndDecode(t *testing.T) { 20 key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") 21 authid := CreateAuthID(key, time.Now().Unix()) 22 23 fmt.Println(key) 24 fmt.Println(authid) 25 26 AuthDecoder := NewAuthIDDecoderHolder() 27 var keyw [16]byte 28 copy(keyw[:], key) 29 AuthDecoder.AddUser(keyw, "Demo User") 30 res, err := AuthDecoder.Match(authid) 31 fmt.Println(res) 32 fmt.Println(err) 33 assert.Equal(t, "Demo User", res) 34 assert.Nil(t, err) 35 } 36 37 func TestCreateAuthIDAndDecode2(t *testing.T) { 38 key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") 39 authid := CreateAuthID(key, time.Now().Unix()) 40 41 fmt.Println(key) 42 fmt.Println(authid) 43 44 AuthDecoder := NewAuthIDDecoderHolder() 45 var keyw [16]byte 46 copy(keyw[:], key) 47 AuthDecoder.AddUser(keyw, "Demo User") 48 res, err := AuthDecoder.Match(authid) 49 fmt.Println(res) 50 fmt.Println(err) 51 assert.Equal(t, "Demo User", res) 52 assert.Nil(t, err) 53 54 key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test") 55 authid2 := CreateAuthID(key2, time.Now().Unix()) 56 57 res2, err2 := AuthDecoder.Match(authid2) 58 assert.EqualError(t, err2, "user do not exist") 59 assert.Nil(t, res2) 60 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 95 func TestCreateAuthIDAndDecodeSuperMassive(t *testing.T) { 96 key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test") 97 authid := CreateAuthID(key, time.Now().Unix()) 98 99 fmt.Println(key) 100 fmt.Println(authid) 101 102 AuthDecoder := NewAuthIDDecoderHolder() 103 var keyw [16]byte 104 copy(keyw[:], key) 105 AuthDecoder.AddUser(keyw, "Demo User") 106 res, err := AuthDecoder.Match(authid) 107 fmt.Println(res) 108 fmt.Println(err) 109 assert.Equal(t, "Demo User", res) 110 assert.Nil(t, err) 111 112 for i := 0; i <= 1000000; i++ { 113 key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i)) 114 var keyw2 [16]byte 115 copy(keyw2[:], key2) 116 AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i)) 117 } 118 119 authid3 := CreateAuthID(key, time.Now().Unix()) 120 121 before := time.Now() 122 res2, err2 := AuthDecoder.Match(authid3) 123 after := time.Now() 124 assert.Equal(t, "Demo User", res2) 125 assert.Nil(t, err2) 126 127 fmt.Println(after.Sub(before).Seconds()) 128 129 }