gitea源码

password_test.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package password
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestComplexity_IsComplexEnough(t *testing.T) {
  9. matchComplexityOnce.Do(func() {})
  10. testlist := []struct {
  11. complexity []string
  12. truevalues []string
  13. falsevalues []string
  14. }{
  15. {[]string{"off"}, []string{"1", "-", "a", "A", "ñ", "日本語"}, []string{}},
  16. {[]string{"lower"}, []string{"abc", "abc!"}, []string{"ABC", "123", "=!$", ""}},
  17. {[]string{"upper"}, []string{"ABC"}, []string{"abc", "123", "=!$", "abc!", ""}},
  18. {[]string{"digit"}, []string{"123"}, []string{"abc", "ABC", "=!$", "abc!", ""}},
  19. {[]string{"spec"}, []string{"=!$", "abc!"}, []string{"abc", "ABC", "123", ""}},
  20. {[]string{"off"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}, nil},
  21. {[]string{"lower", "spec"}, []string{"abc!"}, []string{"abc", "ABC", "123", "=!$", "abcABC123", ""}},
  22. {[]string{"lower", "upper", "digit"}, []string{"abcABC123"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}},
  23. {[]string{""}, []string{"abC=1", "abc!9D"}, []string{"ABC", "123", "=!$", ""}},
  24. }
  25. for _, test := range testlist {
  26. testComplextity(test.complexity)
  27. for _, val := range test.truevalues {
  28. assert.True(t, IsComplexEnough(val))
  29. }
  30. for _, val := range test.falsevalues {
  31. assert.False(t, IsComplexEnough(val))
  32. }
  33. }
  34. // Remove settings for other tests
  35. testComplextity([]string{"off"})
  36. }
  37. func TestComplexity_Generate(t *testing.T) {
  38. matchComplexityOnce.Do(func() {})
  39. const maxCount = 50
  40. const pwdLen = 50
  41. test := func(t *testing.T, modes []string) {
  42. testComplextity(modes)
  43. for range maxCount {
  44. pwd, err := Generate(pwdLen)
  45. assert.NoError(t, err)
  46. assert.Len(t, pwd, pwdLen)
  47. assert.True(t, IsComplexEnough(pwd), "Failed complexities with modes %+v for generated: %s", modes, pwd)
  48. }
  49. }
  50. test(t, []string{"lower"})
  51. test(t, []string{"upper"})
  52. test(t, []string{"lower", "upper", "spec"})
  53. test(t, []string{"off"})
  54. test(t, []string{""})
  55. // Remove settings for other tests
  56. testComplextity([]string{"off"})
  57. }
  58. func testComplextity(values []string) {
  59. // Cleanup previous values
  60. validChars = ""
  61. requiredList = make([]complexity, 0, len(values))
  62. setupComplexity(values)
  63. }