Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

crypto_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 TestMunging(t *testing.T) {
  40. count := 131072
  41. set := make(map[string]bool)
  42. var token string
  43. for i := 0; i < count; i++ {
  44. token = GenerateSecretToken()
  45. set[token] = true
  46. }
  47. // all tokens generated thus far should be unique
  48. assertEqual(len(set), count, t)
  49. // iteratively munge the last generated token an additional `count` times
  50. mungedToken := token
  51. for i := 0; i < count; i++ {
  52. mungedToken = MungeSecretToken(mungedToken)
  53. assertEqual(len(mungedToken), len(token), t)
  54. set[mungedToken] = true
  55. }
  56. // munged tokens should not collide with generated tokens, or each other
  57. assertEqual(len(set), count*2, t)
  58. }
  59. func BenchmarkGenerateSecretToken(b *testing.B) {
  60. for i := 0; i < b.N; i++ {
  61. GenerateSecretToken()
  62. }
  63. }
  64. func BenchmarkMungeSecretToken(b *testing.B) {
  65. t := GenerateSecretToken()
  66. for i := 0; i < b.N; i++ {
  67. t = MungeSecretToken(t)
  68. }
  69. }
  70. func TestCertfpComparisons(t *testing.T) {
  71. 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"
  72. oragonoFP := "3d6b11bfb405c3f84b38cd3038fbec0171d50354790407884ca55d23418566c9"
  73. badFP := "3d6b11bfb405c3f84b38cd3038fbec0171d50354790407884ca55d23418566c"
  74. badFP2 := "*"
  75. normalizedOpenssl, err := NormalizeCertfp(opensslFP)
  76. assertEqual(err, nil, t)
  77. assertEqual(normalizedOpenssl, oragonoFP, t)
  78. normalizedOragono, err := NormalizeCertfp(oragonoFP)
  79. assertEqual(err, nil, t)
  80. assertEqual(normalizedOragono, oragonoFP, t)
  81. _, err = NormalizeCertfp(badFP)
  82. if err == nil {
  83. t.Errorf("corrupt fp should fail normalization")
  84. }
  85. _, err = NormalizeCertfp(badFP2)
  86. if err == nil {
  87. t.Errorf("corrupt fp should fail normalization")
  88. }
  89. }