| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package aead
- import (
- "fmt"
- "strconv"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- )
- func TestCreateAuthID(t *testing.T) {
- key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
- authid := CreateAuthID(key, time.Now().Unix())
- fmt.Println(key)
- fmt.Println(authid)
- }
- func TestCreateAuthIDAndDecode(t *testing.T) {
- key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
- authid := CreateAuthID(key, time.Now().Unix())
- fmt.Println(key)
- fmt.Println(authid)
- AuthDecoder := NewAuthIDDecoderHolder()
- var keyw [16]byte
- copy(keyw[:], key)
- AuthDecoder.AddUser(keyw, "Demo User")
- res, err := AuthDecoder.Match(authid)
- fmt.Println(res)
- fmt.Println(err)
- assert.Equal(t, "Demo User", res)
- assert.Nil(t, err)
- }
- func TestCreateAuthIDAndDecode2(t *testing.T) {
- key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
- authid := CreateAuthID(key, time.Now().Unix())
- fmt.Println(key)
- fmt.Println(authid)
- AuthDecoder := NewAuthIDDecoderHolder()
- var keyw [16]byte
- copy(keyw[:], key)
- AuthDecoder.AddUser(keyw, "Demo User")
- res, err := AuthDecoder.Match(authid)
- fmt.Println(res)
- fmt.Println(err)
- assert.Equal(t, "Demo User", res)
- assert.Nil(t, err)
- key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test")
- authid2 := CreateAuthID(key2, time.Now().Unix())
- res2, err2 := AuthDecoder.Match(authid2)
- assert.EqualError(t, err2, "user do not exist")
- assert.Nil(t, res2)
- }
- func TestCreateAuthIDAndDecodeMassive(t *testing.T) {
- key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
- authid := CreateAuthID(key, time.Now().Unix())
- fmt.Println(key)
- fmt.Println(authid)
- AuthDecoder := NewAuthIDDecoderHolder()
- var keyw [16]byte
- copy(keyw[:], key)
- AuthDecoder.AddUser(keyw, "Demo User")
- res, err := AuthDecoder.Match(authid)
- fmt.Println(res)
- fmt.Println(err)
- assert.Equal(t, "Demo User", res)
- assert.Nil(t, err)
- for i := 0; i <= 10000; i++ {
- key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i))
- var keyw2 [16]byte
- copy(keyw2[:], key2)
- AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i))
- }
- authid3 := CreateAuthID(key, time.Now().Unix())
- res2, err2 := AuthDecoder.Match(authid3)
- assert.Equal(t, "Demo User", res2)
- assert.Nil(t, err2)
- }
- func TestCreateAuthIDAndDecodeSuperMassive(t *testing.T) {
- key := KDF16([]byte("Demo Key for Auth ID Test"), "Demo Path for Auth ID Test")
- authid := CreateAuthID(key, time.Now().Unix())
- fmt.Println(key)
- fmt.Println(authid)
- AuthDecoder := NewAuthIDDecoderHolder()
- var keyw [16]byte
- copy(keyw[:], key)
- AuthDecoder.AddUser(keyw, "Demo User")
- res, err := AuthDecoder.Match(authid)
- fmt.Println(res)
- fmt.Println(err)
- assert.Equal(t, "Demo User", res)
- assert.Nil(t, err)
- for i := 0; i <= 1000000; i++ {
- key2 := KDF16([]byte("Demo Key for Auth ID Test2"), "Demo Path for Auth ID Test", strconv.Itoa(i))
- var keyw2 [16]byte
- copy(keyw2[:], key2)
- AuthDecoder.AddUser(keyw2, "Demo User"+strconv.Itoa(i))
- }
- authid3 := CreateAuthID(key, time.Now().Unix())
- before := time.Now()
- res2, err2 := AuthDecoder.Match(authid3)
- after := time.Now()
- assert.Equal(t, "Demo User", res2)
- assert.Nil(t, err2)
- fmt.Println(after.Sub(before).Seconds())
- }
|