github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/pwlib/password_check_test.go (about)

     1  package pwlib
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TechProfile profile settings for technical users
     8  var TechProfile = PasswordProfile{
     9  	Length:    12,
    10  	Upper:     1,
    11  	Lower:     1,
    12  	Digits:    1,
    13  	Special:   1,
    14  	Firstchar: true,
    15  }
    16  
    17  // UserProfile profile settings for personal users
    18  var UserProfile = PasswordProfile{
    19  	Length:    10,
    20  	Upper:     1,
    21  	Lower:     1,
    22  	Digits:    1,
    23  	Special:   0,
    24  	Firstchar: true,
    25  }
    26  
    27  func TestDoPasswordCheck(t *testing.T) {
    28  	tests := []struct {
    29  		name    string
    30  		pass    string
    31  		valid   bool
    32  		profile PasswordProfile
    33  		special string
    34  	}{
    35  		{
    36  			"NoCharacterAtAll",
    37  			"",
    38  			false,
    39  			TechProfile,
    40  			"",
    41  		},
    42  		{
    43  			"JustEmptyStringAndWhitespace",
    44  			" \n\t\r\v\f xxxx",
    45  			false,
    46  			TechProfile,
    47  			"",
    48  		},
    49  		{
    50  			"MixtureOfEmptyStringAndWhitespace",
    51  			"U u\n1\t?\r1\v2\f34",
    52  			false,
    53  			TechProfile,
    54  			"",
    55  		},
    56  		{
    57  			"MissingUpperCaseString",
    58  			"uu1?1234aaaa",
    59  			false,
    60  			TechProfile,
    61  			"",
    62  		},
    63  		{
    64  			"MissingLowerCaseString",
    65  			"UU1?1234AAAA",
    66  			false,
    67  			TechProfile,
    68  			"",
    69  		},
    70  		{
    71  			"MissingNumber",
    72  			"Uua?aaaaxxxx",
    73  			false,
    74  			TechProfile,
    75  			"",
    76  		},
    77  		{
    78  			"MissingSymbol",
    79  			"Uu101234aaaa",
    80  			false,
    81  			TechProfile,
    82  			"",
    83  		},
    84  		{
    85  			"LessThanRequiredMinimumLength",
    86  			"Uu1?123",
    87  			false,
    88  			TechProfile,
    89  			"",
    90  		},
    91  		{
    92  			"ValidPassword",
    93  			"Uu1?1234aaaa",
    94  			true,
    95  			TechProfile,
    96  			"",
    97  		},
    98  		{
    99  			"InvalidSpecial",
   100  			"Uu1?1234aaaa",
   101  			false,
   102  			TechProfile,
   103  			"x!=@",
   104  		},
   105  	}
   106  
   107  	for _, c := range tests {
   108  		t.Run(c.name, func(t *testing.T) {
   109  			profile := c.profile
   110  			special := c.special
   111  			if special != "" {
   112  				SetSpecialChars(special)
   113  			}
   114  			if c.valid != DoPasswordCheck(c.pass, profile.Length, profile.Upper, profile.Lower, profile.Digits, profile.Special, profile.Firstchar, charset.AllChars) {
   115  				t.Fatal("invalid password")
   116  			}
   117  		})
   118  	}
   119  }