|
| 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 | + |
| 5 | +package password |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestComplexity_IsComplexEnough(t *testing.T) { |
| 14 | + matchComplexityOnce.Do(func() {}) |
| 15 | + |
| 16 | + testlist := []struct { |
| 17 | + complexity []string |
| 18 | + truevalues []string |
| 19 | + falsevalues []string |
| 20 | + }{ |
| 21 | + {[]string{"lower"}, []string{"abc", "abc!"}, []string{"ABC", "123", "=!$", ""}}, |
| 22 | + {[]string{"upper"}, []string{"ABC"}, []string{"abc", "123", "=!$", "abc!", ""}}, |
| 23 | + {[]string{"digit"}, []string{"123"}, []string{"abc", "ABC", "=!$", "abc!", ""}}, |
| 24 | + {[]string{"spec"}, []string{"=!$", "abc!"}, []string{"abc", "ABC", "123", ""}}, |
| 25 | + {[]string{"off"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}, nil}, |
| 26 | + {[]string{"lower", "spec"}, []string{"abc!"}, []string{"abc", "ABC", "123", "=!$", "abcABC123", ""}}, |
| 27 | + {[]string{"lower", "upper", "digit"}, []string{"abcABC123"}, []string{"abc", "ABC", "123", "=!$", "abc!", ""}}, |
| 28 | + } |
| 29 | + |
| 30 | + for _, test := range testlist { |
| 31 | + testComplextity(test.complexity) |
| 32 | + for _, val := range test.truevalues { |
| 33 | + assert.True(t, IsComplexEnough(val)) |
| 34 | + } |
| 35 | + for _, val := range test.falsevalues { |
| 36 | + assert.False(t, IsComplexEnough(val)) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Remove settings for other tests |
| 41 | + testComplextity([]string{"off"}) |
| 42 | +} |
| 43 | + |
| 44 | +func TestComplexity_Generate(t *testing.T) { |
| 45 | + matchComplexityOnce.Do(func() {}) |
| 46 | + |
| 47 | + const maxCount = 50 |
| 48 | + const pwdLen = 50 |
| 49 | + |
| 50 | + test := func(t *testing.T, modes []string) { |
| 51 | + testComplextity(modes) |
| 52 | + for i := 0; i < maxCount; i++ { |
| 53 | + pwd, err := Generate(pwdLen) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, pwdLen, len(pwd)) |
| 56 | + assert.True(t, IsComplexEnough(pwd), "Failed complexities with modes %+v for generated: %s", modes, pwd) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + test(t, []string{"lower"}) |
| 61 | + test(t, []string{"upper"}) |
| 62 | + test(t, []string{"lower", "upper", "spec"}) |
| 63 | + test(t, []string{"off"}) |
| 64 | + test(t, []string{""}) |
| 65 | + |
| 66 | + // Remove settings for other tests |
| 67 | + testComplextity([]string{"off"}) |
| 68 | +} |
| 69 | + |
| 70 | +func testComplextity(values []string) { |
| 71 | + // Cleanup previous values |
| 72 | + validChars = "" |
| 73 | + requiredChars = make([]string, 0, len(values)) |
| 74 | + setupComplexity(values) |
| 75 | +} |
0 commit comments