Personal book of passwords

main_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestGeneratePassphrase(t *testing.T) {
  7. profile := "guest"
  8. passphrase := "secret"
  9. site := Site{
  10. Host: "google.com",
  11. MinimumLength: 6,
  12. MaximumLength: 18,
  13. SpecialCharacters: " !@#$%^&*()_+-=<>,./|\\",
  14. NumberOfSpecialCharacters: 2,
  15. NumberOfDigits: 2,
  16. NumberOfUpperCase: 1,
  17. }
  18. i := 0
  19. expected := "Ce\\41ae|dc001da138"
  20. for i = 0; i < 2; i++ {
  21. b := site.generatePassphrase(profile, passphrase)
  22. actual := fmt.Sprintf("%s", string(b))
  23. if actual != expected {
  24. t.FailNow()
  25. }
  26. }
  27. }
  28. func TestContainsDigits(t *testing.T) {
  29. expected := true
  30. actual := containsDigits([]byte("blah0blah2"), 2)
  31. if actual != expected {
  32. t.FailNow()
  33. }
  34. expected = false
  35. actual = containsDigits([]byte("blah0blah"), 2)
  36. if actual != expected {
  37. t.FailNow()
  38. }
  39. expected = true
  40. actual = containsDigits([]byte("blah0blah12"), 2)
  41. if actual != expected {
  42. t.FailNow()
  43. }
  44. }
  45. func TestContainsUppercase(t *testing.T) {
  46. expected := true
  47. actual := containsUppercase([]byte("Blah0Blah2"), 2)
  48. if actual != expected {
  49. t.FailNow()
  50. }
  51. expected = false
  52. actual = containsUppercase([]byte("Blah0blah"), 2)
  53. if actual != expected {
  54. t.FailNow()
  55. }
  56. expected = true
  57. actual = containsUppercase([]byte("Blah0BLaH12"), 2)
  58. if actual != expected {
  59. t.FailNow()
  60. }
  61. }
  62. func TestContainsSpecialCharacters(t *testing.T) {
  63. special := "~!@#$%^&*()_+-= []{};':\",./<>?\\|"
  64. expected := true
  65. actual := containsSpecialCharacters([]byte("Blah!Blah&"), special, 2)
  66. if actual != expected {
  67. t.FailNow()
  68. }
  69. expected = false
  70. actual = containsSpecialCharacters([]byte("Blah!blah"), special, 2)
  71. if actual != expected {
  72. t.FailNow()
  73. }
  74. expected = true
  75. actual = containsSpecialCharacters([]byte("Blah!BLa l."), special, 2)
  76. if actual != expected {
  77. t.FailNow()
  78. }
  79. special = " "
  80. expected = true
  81. actual = containsSpecialCharacters([]byte("Blah BLa l "), special, 2)
  82. if actual != expected {
  83. t.FailNow()
  84. }
  85. }
  86. func TestLength(t *testing.T) {
  87. expected := true
  88. actual := validateLength([]byte("eightormore"), 8, -1)
  89. if actual != expected {
  90. t.FailNow()
  91. }
  92. expected = true
  93. actual = validateLength([]byte("eighttoten"), 8, 10)
  94. if actual != expected {
  95. t.FailNow()
  96. }
  97. expected = false
  98. actual = validateLength([]byte("eighttoten----"), 8, 10)
  99. if actual != expected {
  100. t.FailNow()
  101. }
  102. expected = true
  103. actual = validateLength([]byte("maxof8--"), -1, 8)
  104. if actual != expected {
  105. t.FailNow()
  106. }
  107. expected = false
  108. actual = validateLength([]byte("ei"), 8, -1)
  109. if actual != expected {
  110. t.FailNow()
  111. }
  112. }