You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. }
  44. func TestCertfpComparisons(t *testing.T) {
  45. opensslFP := "3D:6B:11:BF:B4:05:C3:F8:4B:38:CD:30:38:FB:EC:01:71:D5:03:54:79:04:07:88:4C:A5:5D:23:41:85:66:C9"
  46. oragonoFP := "3d6b11bfb405c3f84b38cd3038fbec0171d50354790407884ca55d23418566c9"
  47. badFP := "3d6b11bfb405c3f84b38cd3038fbec0171d50354790407884ca55d23418566c"
  48. badFP2 := "*"
  49. normalizedOpenssl, err := NormalizeCertfp(opensslFP)
  50. assertEqual(err, nil, t)
  51. assertEqual(normalizedOpenssl, oragonoFP, t)
  52. normalizedOragono, err := NormalizeCertfp(oragonoFP)
  53. assertEqual(err, nil, t)
  54. assertEqual(normalizedOragono, oragonoFP, t)
  55. _, err = NormalizeCertfp(badFP)
  56. if err == nil {
  57. t.Errorf("corrupt fp should fail normalization")
  58. }
  59. _, err = NormalizeCertfp(badFP2)
  60. if err == nil {
  61. t.Errorf("corrupt fp should fail normalization")
  62. }
  63. }