Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

password_test.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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{"off"}, []string{"1", "-", "a", "A", "ñ", "日本語"}, []string{}},
  17. {[]string{"lower"}, []string{"abc", "abc!"}, []string{"ABC", "123", "=!$", ""}},
  18. {[]string{"upper"}, []string{"ABC"}, []string{"abc", "123", "=!$", "abc!", ""}},
  19. {[]string{"digit"}, []string{"123"}, []string{"abc", "ABC", "=!$", "abc!", ""}},
  20. {[]string{"spec"}, []string{"=!$", "abc!"}, []string{"abc", "ABC", "123", ""}},
  21. {[]string{"off"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}, nil},
  22. {[]string{"lower", "spec"}, []string{"abc!"}, []string{"abc", "ABC", "123", "=!$", "abcABC123", ""}},
  23. {[]string{"lower", "upper", "digit"}, []string{"abcABC123"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}},
  24. {[]string{""}, []string{"abC=1", "abc!9D"}, []string{"ABC", "123", "=!$", ""}},
  25. }
  26. for _, test := range testlist {
  27. testComplextity(test.complexity)
  28. for _, val := range test.truevalues {
  29. assert.True(t, IsComplexEnough(val))
  30. }
  31. for _, val := range test.falsevalues {
  32. assert.False(t, IsComplexEnough(val))
  33. }
  34. }
  35. // Remove settings for other tests
  36. testComplextity([]string{"off"})
  37. }
  38. func TestComplexity_Generate(t *testing.T) {
  39. matchComplexityOnce.Do(func() {})
  40. const maxCount = 50
  41. const pwdLen = 50
  42. test := func(t *testing.T, modes []string) {
  43. testComplextity(modes)
  44. for i := 0; i < maxCount; i++ {
  45. pwd, err := Generate(pwdLen)
  46. assert.NoError(t, err)
  47. assert.Equal(t, pwdLen, len(pwd))
  48. assert.True(t, IsComplexEnough(pwd), "Failed complexities with modes %+v for generated: %s", modes, pwd)
  49. }
  50. }
  51. test(t, []string{"lower"})
  52. test(t, []string{"upper"})
  53. test(t, []string{"lower", "upper", "spec"})
  54. test(t, []string{"off"})
  55. test(t, []string{""})
  56. // Remove settings for other tests
  57. testComplextity([]string{"off"})
  58. }
  59. func testComplextity(values []string) {
  60. // Cleanup previous values
  61. validChars = ""
  62. requiredList = make([]complexity, 0, len(values))
  63. setupComplexity(values)
  64. }