Skip to content

Commit

Permalink
util: add unit tests for password validation (#39540)
Browse files Browse the repository at this point in the history
ref #38928
  • Loading branch information
CbcWestwolf authored Dec 1, 2022
1 parent 38b0ab7 commit dd32b9e
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion util/password-validation/password_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestValidateDictionaryPassword(t *testing.T) {
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock

err := mock.SetGlobalSysVar(context.Background(), variable.ValidatePasswordDictionary, "1234;5678;HIJK")
err := mock.SetGlobalSysVar(context.Background(), variable.ValidatePasswordDictionary, "abc;123;1234;5678;HIJK;中文测试;。,;!")
require.NoError(t, err)
testcases := []struct {
pwd string
Expand All @@ -41,6 +41,8 @@ func TestValidateDictionaryPassword(t *testing.T) {
{"abcd12345efg", false},
{"abcd123efghij", true},
{"abcd123efghijk", false},
{"abcd123efghij中文测试", false},
{"abcd123。,;!", false},
}
for _, testcase := range testcases {
ok, err := ValidateDictionaryPassword(testcase.pwd, &vars.GlobalVarsAccessor)
Expand Down Expand Up @@ -135,3 +137,39 @@ func TestValidatePasswordMediumPolicy(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "", warn)
}

func TestValidatePassword(t *testing.T) {
sessionVars := variable.NewSessionVars(nil)
sessionVars.GlobalVarsAccessor = variable.NewMockGlobalAccessor4Tests()
sessionVars.GlobalVarsAccessor.(*variable.MockGlobalAccessor).SessionVars = sessionVars
sessionVars.User = &auth.UserIdentity{Username: "user", AuthUsername: "authuser"}

err := sessionVars.GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.ValidatePasswordPolicy, "LOW")
require.NoError(t, err)
err = ValidatePassword(sessionVars, "1234")
require.Error(t, err)
err = ValidatePassword(sessionVars, "user1234")
require.Error(t, err)
err = ValidatePassword(sessionVars, "authuser1234")
require.Error(t, err)
err = ValidatePassword(sessionVars, "User1234")
require.NoError(t, err)

err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.ValidatePasswordPolicy, "MEDIUM")
require.NoError(t, err)
err = ValidatePassword(sessionVars, "User1234")
require.Error(t, err)
err = ValidatePassword(sessionVars, "!User1234")
require.NoError(t, err)
err = ValidatePassword(sessionVars, "!User1234")
require.NoError(t, err)

err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.ValidatePasswordPolicy, "STRONG")
require.NoError(t, err)
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(context.Background(), variable.ValidatePasswordDictionary, "User")
require.NoError(t, err)
err = ValidatePassword(sessionVars, "!User1234")
require.Error(t, err)
err = ValidatePassword(sessionVars, "!ABcd1234")
require.NoError(t, err)
}

0 comments on commit dd32b9e

Please sign in to comment.