本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

76 lines
2.1KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package password
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestComplexity_IsComplexEnough(t *testing.T) {
  10. matchComplexityOnce.Do(func() {})
  11. testlist := []struct {
  12. complexity []string
  13. truevalues []string
  14. falsevalues []string
  15. }{
  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. }
  24. for _, test := range testlist {
  25. testComplextity(test.complexity)
  26. for _, val := range test.truevalues {
  27. assert.True(t, IsComplexEnough(val))
  28. }
  29. for _, val := range test.falsevalues {
  30. assert.False(t, IsComplexEnough(val))
  31. }
  32. }
  33. // Remove settings for other tests
  34. testComplextity([]string{"off"})
  35. }
  36. func TestComplexity_Generate(t *testing.T) {
  37. matchComplexityOnce.Do(func() {})
  38. const maxCount = 50
  39. const pwdLen = 50
  40. test := func(t *testing.T, modes []string) {
  41. testComplextity(modes)
  42. for i := 0; i < maxCount; i++ {
  43. pwd, err := Generate(pwdLen)
  44. assert.NoError(t, err)
  45. assert.Equal(t, pwdLen, len(pwd))
  46. assert.True(t, IsComplexEnough(pwd), "Failed complexities with modes %+v for generated: %s", modes, pwd)
  47. }
  48. }
  49. test(t, []string{"lower"})
  50. test(t, []string{"upper"})
  51. test(t, []string{"lower", "upper", "spec"})
  52. test(t, []string{"off"})
  53. test(t, []string{""})
  54. // Remove settings for other tests
  55. testComplextity([]string{"off"})
  56. }
  57. func testComplextity(values []string) {
  58. // Cleanup previous values
  59. validChars = ""
  60. requiredChars = make([]string, 0, len(values))
  61. setupComplexity(values)
  62. }
上海开阖软件有限公司 沪ICP备12045867号-1