login_limiter_test.go 6.8 KB

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