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.

pwn_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pwn
  4. import (
  5. "errors"
  6. "math/rand"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. var client = New(WithHTTP(&http.Client{
  14. Timeout: time.Second * 2,
  15. }))
  16. func TestMain(m *testing.M) {
  17. rand.Seed(time.Now().Unix())
  18. os.Exit(m.Run())
  19. }
  20. func TestPassword(t *testing.T) {
  21. // Check input error
  22. _, err := client.CheckPassword("", false)
  23. if err == nil {
  24. t.Log("blank input should return an error")
  25. t.Fail()
  26. }
  27. if !errors.Is(err, ErrEmptyPassword) {
  28. t.Log("blank input should return ErrEmptyPassword")
  29. t.Fail()
  30. }
  31. // Should fail
  32. fail := "password1234"
  33. count, err := client.CheckPassword(fail, false)
  34. if err != nil {
  35. t.Log(err)
  36. t.Fail()
  37. }
  38. if count == 0 {
  39. t.Logf("%s should fail as a password\n", fail)
  40. t.Fail()
  41. }
  42. // Should fail (with padding)
  43. failPad := "administrator"
  44. count, err = client.CheckPassword(failPad, true)
  45. if err != nil {
  46. t.Log(err)
  47. t.Fail()
  48. }
  49. if count == 0 {
  50. t.Logf("%s should fail as a password\n", failPad)
  51. t.Fail()
  52. }
  53. // Checking for a "good" password isn't going to be perfect, but we can give it a good try
  54. // with hopefully minimal error. Try five times?
  55. var good bool
  56. var pw string
  57. for idx := 0; idx <= 5; idx++ {
  58. pw = testPassword()
  59. count, err = client.CheckPassword(pw, false)
  60. if err != nil {
  61. t.Log(err)
  62. t.Fail()
  63. }
  64. if count == 0 {
  65. good = true
  66. break
  67. }
  68. }
  69. if !good {
  70. t.Log("no generated passwords passed. there is a chance this is a fluke")
  71. t.Fail()
  72. }
  73. // Again, but with padded responses
  74. good = false
  75. for idx := 0; idx <= 5; idx++ {
  76. pw = testPassword()
  77. count, err = client.CheckPassword(pw, true)
  78. if err != nil {
  79. t.Log(err)
  80. t.Fail()
  81. }
  82. if count == 0 {
  83. good = true
  84. break
  85. }
  86. }
  87. if !good {
  88. t.Log("no generated passwords passed. there is a chance this is a fluke")
  89. t.Fail()
  90. }
  91. }
  92. // Credit to https://golangbyexample.com/generate-random-password-golang/
  93. // DO NOT USE THIS FOR AN ACTUAL PASSWORD GENERATOR
  94. var (
  95. lowerCharSet = "abcdedfghijklmnopqrst"
  96. upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  97. specialCharSet = "!@#$%&*"
  98. numberSet = "0123456789"
  99. allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
  100. )
  101. func testPassword() string {
  102. var password strings.Builder
  103. // Set special character
  104. for i := 0; i < 5; i++ {
  105. random := rand.Intn(len(specialCharSet))
  106. password.WriteString(string(specialCharSet[random]))
  107. }
  108. // Set numeric
  109. for i := 0; i < 5; i++ {
  110. random := rand.Intn(len(numberSet))
  111. password.WriteString(string(numberSet[random]))
  112. }
  113. // Set uppercase
  114. for i := 0; i < 5; i++ {
  115. random := rand.Intn(len(upperCharSet))
  116. password.WriteString(string(upperCharSet[random]))
  117. }
  118. for i := 0; i < 5; i++ {
  119. random := rand.Intn(len(allCharSet))
  120. password.WriteString(string(allCharSet[random]))
  121. }
  122. inRune := []rune(password.String())
  123. rand.Shuffle(len(inRune), func(i, j int) {
  124. inRune[i], inRune[j] = inRune[j], inRune[i]
  125. })
  126. return string(inRune)
  127. }