github.com/verrazzano/verrazzano@v1.7.1/pkg/security/password/password_test.go (about) 1 // Copyright (c) 2021, 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package password 5 6 import ( 7 "strconv" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 // TestGeneratePassword tests generating random passwords 14 // GIVEN a call to GeneratePassword 15 // 16 // WHEN the deployment object does NOT have enough replicas available 17 // THEN false is returned 18 func TestGeneratePassword(t *testing.T) { 19 var tests = []struct { 20 length int 21 hasError bool 22 }{ 23 {-1, true}, 24 {10, false}, 25 {15, false}, 26 {31, false}, 27 {66, false}, 28 } 29 30 for _, tt := range tests { 31 t.Run(strconv.Itoa(tt.length), func(t *testing.T) { 32 pw, err := GeneratePassword(tt.length) 33 if tt.hasError { 34 assert.Error(t, err) 35 } else { 36 assert.NoError(t, err) 37 assert.Equal(t, tt.length, len(pw)) 38 } 39 }) 40 } 41 } 42 43 // TestMaskFunction tests creating and using the masking function 44 // GIVEN a call to MaskFunction 45 // 46 // WHEN the created function is invoked 47 // THEN it should mask all specified matches 48 func TestMaskFunction(t *testing.T) { 49 f := MaskFunction("password ") 50 var tests = []struct { 51 input string 52 output string 53 }{ 54 { 55 "blah blah password 123 blah blah", 56 "blah blah password ****** blah blah", 57 }, 58 { 59 "a password 123 another password 456 and the rest", 60 "a password ****** another password ****** and the rest", 61 }, 62 { 63 "password 123", 64 "password ******", 65 }, 66 { 67 "no change", 68 "no change", 69 }, 70 } 71 72 for _, tt := range tests { 73 t.Run(tt.input, func(t *testing.T) { 74 res := f(tt.input) 75 assert.Equal(t, tt.output, res) 76 }) 77 } 78 } 79 80 // TestGenerateRandomAlphaLower tests GenerateRandomAlphaLower 81 func TestGenerateRandomAlphaLower(t *testing.T) { 82 var tests = []struct { 83 length int 84 hasError bool 85 }{ 86 {-1, true}, 87 {0, true}, 88 {15, false}, 89 {31, false}, 90 {66, false}, 91 } 92 93 for _, tt := range tests { 94 t.Run(strconv.Itoa(tt.length), func(t *testing.T) { 95 pw, err := GenerateRandomAlphaLower(tt.length) 96 if tt.hasError { 97 assert.Error(t, err) 98 } else { 99 assert.NoError(t, err) 100 assert.Equal(t, tt.length, len(pw)) 101 } 102 }) 103 } 104 }