authid_test.go 3.2 KB

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