Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

crypto_test.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package utils
  4. import (
  5. "testing"
  6. )
  7. const (
  8. storedToken = "1e82d113a59a874cccf82063ec603221"
  9. badToken = "1e82d113a59a874cccf82063ec603222"
  10. shortToken = "1e82d113a59a874cccf82063ec60322"
  11. longToken = "1e82d113a59a874cccf82063ec6032211"
  12. )
  13. func TestGenerateSecretToken(t *testing.T) {
  14. token := GenerateSecretToken()
  15. if len(token) != SecretTokenLength {
  16. t.Errorf("bad token: %v", token)
  17. }
  18. }
  19. func TestTokenCompare(t *testing.T) {
  20. if !SecretTokensMatch(storedToken, storedToken) {
  21. t.Error("matching tokens must match")
  22. }
  23. if SecretTokensMatch(storedToken, badToken) {
  24. t.Error("non-matching tokens must not match")
  25. }
  26. if SecretTokensMatch(storedToken, shortToken) {
  27. t.Error("non-matching tokens must not match")
  28. }
  29. if SecretTokensMatch(storedToken, longToken) {
  30. t.Error("non-matching tokens must not match")
  31. }
  32. if SecretTokensMatch("", "") {
  33. t.Error("the empty token should not match anything")
  34. }
  35. if SecretTokensMatch("", storedToken) {
  36. t.Error("the empty token should not match anything")
  37. }
  38. }
  39. func BenchmarkGenerateSecretToken(b *testing.B) {
  40. for i := 0; i < b.N; i++ {
  41. GenerateSecretToken()
  42. }
  43. }