login_limiter_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/google/uuid"
  5. "testing"
  6. "time"
  7. )
  8. type MockCaptchaProvider struct{}
  9. func (p *MockCaptchaProvider) Generate() (string, string, string, error) {
  10. id := uuid.New().String()
  11. content := uuid.New().String()
  12. answer := uuid.New().String()
  13. return id, content, answer, nil
  14. }
  15. func (p *MockCaptchaProvider) Expiration() time.Duration {
  16. return 2 * time.Second
  17. }
  18. func (p *MockCaptchaProvider) Draw(content string) (string, error) {
  19. return "MOCK", nil
  20. }
  21. func TestSecurityWorkflow(t *testing.T) {
  22. policy := SecurityPolicy{
  23. CaptchaThreshold: 3,
  24. BanThreshold: 5,
  25. AttemptsWindow: 5 * time.Minute,
  26. BanDuration: 5 * time.Minute,
  27. }
  28. limiter := NewLoginLimiter(policy)
  29. ip := "192.168.1.100"
  30. // 测试正常失败记录
  31. for i := 0; i < 3; i++ {
  32. limiter.RecordFailedAttempt(ip)
  33. }
  34. isBanned, capRequired := limiter.CheckSecurityStatus(ip)
  35. fmt.Printf("IP: %s, Banned: %v, Captcha Required: %v\n", ip, isBanned, capRequired)
  36. if isBanned {
  37. t.Error("IP should not be banned yet")
  38. }
  39. if !capRequired {
  40. t.Error("Captcha should be required")
  41. }
  42. // 测试触发封禁
  43. for i := 0; i < 3; i++ {
  44. limiter.RecordFailedAttempt(ip)
  45. isBanned, capRequired = limiter.CheckSecurityStatus(ip)
  46. fmt.Printf("IP: %s, Banned: %v, Captcha Required: %v\n", ip, isBanned, capRequired)
  47. }
  48. // 测试封禁状态
  49. if isBanned, _ = limiter.CheckSecurityStatus(ip); !isBanned {
  50. t.Error("IP should be banned")
  51. }
  52. }
  53. func TestCaptchaFlow(t *testing.T) {
  54. policy := SecurityPolicy{CaptchaThreshold: 2}
  55. limiter := NewLoginLimiter(policy)
  56. limiter.RegisterProvider(&MockCaptchaProvider{})
  57. ip := "10.0.0.1"
  58. // 触发验证码要求
  59. limiter.RecordFailedAttempt(ip)
  60. limiter.RecordFailedAttempt(ip)
  61. // 检查状态
  62. if _, need := limiter.CheckSecurityStatus(ip); !need {
  63. t.Error("应该需要验证码")
  64. }
  65. // 生成验证码
  66. err, capc := limiter.RequireCaptcha()
  67. if err != nil {
  68. t.Fatalf("生成验证码失败: %v", err)
  69. }
  70. fmt.Printf("验证码内容: %#v\n", capc)
  71. // 验证成功
  72. if !limiter.VerifyCaptcha(capc.Id, capc.Answer) {
  73. t.Error("验证码应该验证成功")
  74. }
  75. // 验证已删除
  76. if limiter.VerifyCaptcha(capc.Id, capc.Answer) {
  77. t.Error("验证码应该已删除")
  78. }
  79. limiter.RemoveAttempts(ip)
  80. // 验证后状态
  81. if banned, need := limiter.CheckSecurityStatus(ip); banned || need {
  82. t.Error("验证成功后应该重置状态")
  83. }
  84. }
  85. func TestCaptchaMustFlow(t *testing.T) {
  86. policy := SecurityPolicy{CaptchaThreshold: 0}
  87. limiter := NewLoginLimiter(policy)
  88. limiter.RegisterProvider(&MockCaptchaProvider{})
  89. ip := "10.0.0.1"
  90. // 检查状态
  91. if _, need := limiter.CheckSecurityStatus(ip); !need {
  92. t.Error("应该需要验证码")
  93. }
  94. // 生成验证码
  95. err, capc := limiter.RequireCaptcha()
  96. if err != nil {
  97. t.Fatalf("生成验证码失败: %v", err)
  98. }
  99. fmt.Printf("验证码内容: %#v\n", capc)
  100. // 验证成功
  101. if !limiter.VerifyCaptcha(capc.Id, capc.Answer) {
  102. t.Error("验证码应该验证成功")
  103. }
  104. // 验证后状态
  105. if _, need := limiter.CheckSecurityStatus(ip); !need {
  106. t.Error("应该需要验证码")
  107. }
  108. }
  109. func TestAttemptTimeout(t *testing.T) {
  110. policy := SecurityPolicy{CaptchaThreshold: 2, AttemptsWindow: 1 * time.Second}
  111. limiter := NewLoginLimiter(policy)
  112. limiter.RegisterProvider(&MockCaptchaProvider{})
  113. ip := "10.0.0.1"
  114. // 触发验证码要求
  115. limiter.RecordFailedAttempt(ip)
  116. limiter.RecordFailedAttempt(ip)
  117. // 检查状态
  118. if _, need := limiter.CheckSecurityStatus(ip); !need {
  119. t.Error("应该需要验证码")
  120. }
  121. // 生成验证码
  122. err, _ := limiter.RequireCaptcha()
  123. if err != nil {
  124. t.Fatalf("生成验证码失败: %v", err)
  125. }
  126. // 等待超过 AttemptsWindow
  127. time.Sleep(2 * time.Second)
  128. // 触发验证码要求
  129. limiter.RecordFailedAttempt(ip)
  130. // 检查状态
  131. if _, need := limiter.CheckSecurityStatus(ip); need {
  132. t.Error("不应该需要验证码")
  133. }
  134. }
  135. func TestCaptchaTimeout(t *testing.T) {
  136. policy := SecurityPolicy{CaptchaThreshold: 2}
  137. limiter := NewLoginLimiter(policy)
  138. limiter.RegisterProvider(&MockCaptchaProvider{})
  139. ip := "10.0.0.1"
  140. // 触发验证码要求
  141. limiter.RecordFailedAttempt(ip)
  142. limiter.RecordFailedAttempt(ip)
  143. // 检查状态
  144. if _, need := limiter.CheckSecurityStatus(ip); !need {
  145. t.Error("应该需要验证码")
  146. }
  147. // 生成验证码
  148. err, capc := limiter.RequireCaptcha()
  149. if err != nil {
  150. t.Fatalf("生成验证码失败: %v", err)
  151. }
  152. // 等待超过 CaptchaValidPeriod
  153. time.Sleep(3 * time.Second)
  154. // 验证成功
  155. if limiter.VerifyCaptcha(capc.Id, capc.Answer) {
  156. t.Error("验证码应该已过期")
  157. }
  158. }
  159. func TestBanFlow(t *testing.T) {
  160. policy := SecurityPolicy{BanThreshold: 5}
  161. limiter := NewLoginLimiter(policy)
  162. ip := "10.0.0.1"
  163. // 触发ban
  164. for i := 0; i < 5; i++ {
  165. limiter.RecordFailedAttempt(ip)
  166. }
  167. // 检查状态
  168. if banned, _ := limiter.CheckSecurityStatus(ip); !banned {
  169. t.Error("should be banned")
  170. }
  171. }
  172. func TestBanDisableFlow(t *testing.T) {
  173. policy := SecurityPolicy{BanThreshold: 0}
  174. limiter := NewLoginLimiter(policy)
  175. ip := "10.0.0.1"
  176. // 触发ban
  177. for i := 0; i < 5; i++ {
  178. limiter.RecordFailedAttempt(ip)
  179. }
  180. // 检查状态
  181. if banned, _ := limiter.CheckSecurityStatus(ip); banned {
  182. t.Error("should not be banned")
  183. }
  184. }
  185. func TestBanTimeout(t *testing.T) {
  186. policy := SecurityPolicy{BanThreshold: 5, BanDuration: 1 * time.Second}
  187. limiter := NewLoginLimiter(policy)
  188. ip := "10.0.0.1"
  189. // 触发ban
  190. // 触发ban
  191. for i := 0; i < 5; i++ {
  192. limiter.RecordFailedAttempt(ip)
  193. }
  194. time.Sleep(2 * time.Second)
  195. // 检查状态
  196. if banned, _ := limiter.CheckSecurityStatus(ip); banned {
  197. t.Error("should not be banned")
  198. }
  199. }
  200. func TestLimiterDisabled(t *testing.T) {
  201. policy := SecurityPolicy{BanThreshold: 0, CaptchaThreshold: -1}
  202. limiter := NewLoginLimiter(policy)
  203. ip := "10.0.0.1"
  204. // 触发ban
  205. for i := 0; i < 5; i++ {
  206. limiter.RecordFailedAttempt(ip)
  207. }
  208. // 检查状态
  209. if banned, capNeed := limiter.CheckSecurityStatus(ip); banned || capNeed {
  210. fmt.Printf("IP: %s, Banned: %v, Captcha Required: %v\n", ip, banned, capNeed)
  211. t.Error("should not be banned or need captcha")
  212. }
  213. }
  214. func TestB64CaptchaFlow(t *testing.T) {
  215. limiter := NewLoginLimiter(defaultSecurityPolicy)
  216. limiter.RegisterProvider(B64StringCaptchaProvider{})
  217. ip := "10.0.0.1"
  218. // 触发验证码要求
  219. limiter.RecordFailedAttempt(ip)
  220. limiter.RecordFailedAttempt(ip)
  221. limiter.RecordFailedAttempt(ip)
  222. // 检查状态
  223. if _, need := limiter.CheckSecurityStatus(ip); !need {
  224. t.Error("应该需要验证码")
  225. }
  226. // 生成验证码
  227. err, capc := limiter.RequireCaptcha()
  228. if err != nil {
  229. t.Fatalf("生成验证码失败: %v", err)
  230. }
  231. fmt.Printf("验证码内容: %#v\n", capc)
  232. //draw
  233. err, b64 := limiter.DrawCaptcha(capc.Content)
  234. if err != nil {
  235. t.Fatalf("绘制验证码失败: %v", err)
  236. }
  237. fmt.Printf("验证码内容: %#v\n", b64)
  238. // 验证成功
  239. if !limiter.VerifyCaptcha(capc.Id, capc.Answer) {
  240. t.Error("验证码应该验证成功")
  241. }
  242. limiter.RemoveAttempts(ip)
  243. // 验证后状态
  244. if banned, need := limiter.CheckSecurityStatus(ip); banned || need {
  245. t.Error("验证成功后应该重置状态")
  246. }
  247. }