123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package datautil
- import (
- "fmt"
- "path"
- "testing"
- )
- func TestUserDB(t *testing.T) {
- // Create user DB instance and store a credential
- ud, err := NewUserDB(path.Join(testdbdir, "testuserdb"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- err = ud.AddUserEntry("fred", "s3cret", map[string]interface{}{
- "field1": "foo",
- "field2": 2,
- })
- if err != nil {
- t.Error(err)
- return
- }
- // Create a second user DB instance
- ud2, err := NewUserDB(path.Join(testdbdir, "testuserdb"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- // Check that the data was loaded
- if res := fmt.Sprint(ud2.AllUsers()); res != "[fred]" {
- t.Error("Unexpected result:", res)
- return
- }
- // Get the user data
- data, ok := ud2.UserData("fred")
- if !ok || data["field1"] != "foo" || data["field2"] != 2 {
- t.Error("Unexpected result:", ok, data)
- return
- }
- // Check user password
- if ok := ud2.CheckUserPassword("fred", "s3cret"); !ok || err != nil {
- t.Error("Unexpected result:", ok, err)
- return
- }
- if ok := ud2.CheckUserPassword("fred", "s4cret"); ok || err != nil {
- t.Error("Unexpected result:", ok, err)
- return
- }
- // Change data and password
- err = ud2.UpdateUserPassword("fred", "secret55")
- if err != nil {
- t.Error(err)
- return
- }
- err = ud2.UpdateUserData("fred", map[string]interface{}{
- "field5": "bar",
- "field2": 2,
- })
- if err != nil {
- t.Error(err)
- return
- }
- // ... and another instance
- ud3, err := NewUserDB(path.Join(testdbdir, "testuserdb"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- // Check that all updated information are correct
- data, ok = ud3.UserData("fred")
- if !ok || data["field5"] != "bar" || data["field2"] != 2 {
- t.Error("Unexpected result:", ok, data)
- return
- }
- // Check user password
- if ok := ud3.CheckUserPassword("fred", "s3cret"); ok || err != nil {
- t.Error("Unexpected result:", ok, err)
- return
- }
- if ok := ud3.CheckUserPassword("fred", "secret55"); !ok || err != nil {
- t.Error("Unexpected result:", ok, err)
- return
- }
- // Remove now the entry
- ud3.RemoveUserEntry("fred")
- ud4, err := NewUserDB(path.Join(testdbdir, "testuserdb"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- // Check that the data was removed
- if res := fmt.Sprint(ud4.AllUsers()); res != "[]" {
- t.Error("Unexpected result:", res)
- return
- }
- }
- func TestUserDBPasswordHistory(t *testing.T) {
- oldMaxPassHistory := MaxPassHistory
- MaxPassHistory = 3
- defer func() {
- MaxPassHistory = oldMaxPassHistory
- }()
- // Create user DB instance and store a credential
- ud, err := NewUserDB(path.Join(testdbdir, "testuserdbhistory"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- if err = ud.AddUserEntry("fred", "s3cret1", nil); err != nil {
- t.Error(err)
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 0 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- if err = ud.UpdateUserPassword("fred", "s3cret1"); err.Error() != "Cannot reuse current password" {
- t.Error(err)
- return
- }
- if err = ud.UpdateUserPassword("fred", "s3cret2"); err != nil {
- t.Error(err)
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 1 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret1"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret2"); ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- ud.UpdateUserPassword("fred", "s3cret3")
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret1"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret2"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 2 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- ud.UpdateUserPassword("fred", "s3cret4")
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret1"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret2"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret3"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 3 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- ud.UpdateUserPassword("fred", "s3cret5")
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret2"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret3"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret4"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 3 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret1"); ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- ud.UpdateUserPassword("fred", "s3cret6")
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret3"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret4"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret5"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if len(ud.Data["fred"].PasshashHistory) != 3 {
- t.Error("Unexpected result:", ud.Data["fred"].PasshashHistory)
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret2"); ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPasswordHistory("fred", "s3cret6"); ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- if ok := ud.CheckUserPassword("fred", "s3cret6"); !ok || err != nil {
- t.Error("Unexpected result")
- return
- }
- }
- func TestUserDBErrorCases(t *testing.T) {
- ud, err := NewUserDB(path.Join(testdbdir, invalidFileName), "test123")
- if err == nil || ud != nil {
- t.Error("Unexpected result:", err, ud)
- return
- }
- ud, err = NewUserDB(path.Join(testdbdir, "errtest"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- err = ud.AddUserEntry("foo", "bar", nil)
- if err != nil {
- t.Error(err)
- return
- }
- err = ud.AddUserEntry("foo", "bar", nil)
- if err == nil || err.Error() != "User foo already exists" {
- t.Error(err)
- return
- }
- err = ud.UpdateUserData("fred", nil)
- if err == nil || err.Error() != "Unknown user fred" {
- t.Error(err)
- return
- }
- err = ud.UpdateUserPassword("fred", "")
- if err == nil || err.Error() != "Unknown user fred" {
- t.Error(err)
- return
- }
- err = ud.RemoveUserEntry("fred")
- if err == nil || err.Error() != "Unknown user fred" {
- t.Error(err)
- return
- }
- }
- func TestEnforcedUserDB(t *testing.T) {
- // Create user DB instance and store a credential
- eud, err := NewEnforcedUserDB(path.Join(testdbdir, "testenforceuserdb"), "test123")
- if err != nil {
- t.Error(err)
- return
- }
- eud.SetPasswordCheckParam("NotContainSequence", false)
- if err := eud.AddUserEntry("fritz", "#Secr3taaa", nil); err != nil {
- t.Error(err)
- return
- }
- if eud.UserExists("foo") {
- t.Error("User foo should not exist")
- return
- }
- if !eud.UserExists("fritz") {
- t.Error("User fritz should exist")
- return
- }
- eud.SetPasswordCheckParam("NotContainSequence", true)
- if res := len(eud.PasswordCheckParams()); res != 8 {
- t.Error("Unexpected result:", res)
- return
- }
- if err := eud.UpdateUserPassword("fritz", "#Secr3tbbb"); err.Error() != "Password must not contain a same character sequence" {
- t.Error(err)
- return
- }
- if err := eud.UpdateUserPassword("fritz", "#Secr3tabc"); err != nil {
- t.Error(err)
- return
- }
- if err := eud.UpdateUserPassword("fritz", "#Secr3taaa"); err.Error() != "Password was used before within the last 10 changes; Password must not contain a same character sequence" {
- t.Error(err)
- return
- }
- if err := eud.AddUserEntry("hans", "aaa", nil); err.Error() != "Password matches a common dictionary password; Password must be at least 8 characters long; Password must contain an upper case character; Password must contain a number; Password must contain a special character; Password must not contain a same character sequence" {
- t.Error(err)
- return
- }
- // Test multiple errors
- if err := eud.UpdateUserPassword("fritz", "aaa"); err == nil || err.Error() != "Password matches a common dictionary password; Password must be at least 8 characters long; Password must contain an upper case character; Password must contain a number; Password must contain a special character; Password must not contain a same character sequence" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "#Secr3tabc"); err == nil || err.Error() != "Cannot reuse current password" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "AA1"); err == nil || err.Error() != "Password is too similar to the common dictionary password aa1234 (50% match); Password must be at least 8 characters long; Password must contain a lower case character; Password must contain a special character" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "xxx"); err == nil || err.Error() != "Password must be at least 8 characters long; Password must contain an upper case character; Password must contain a number; Password must contain a special character; Password must not contain a same character sequence" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "AA2"); err == nil || err.Error() != "Password is too similar to the common dictionary password aaa (66% match); Password must be at least 8 characters long; Password must contain a lower case character; Password must contain a special character" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "Test1234#"); err == nil || err.Error() != "Password is too similar to the common dictionary password test12345 (88% match)" {
- t.Error(err)
- return
- }
- if err := eud.IsAcceptablePassword("fritz", "#Test1234"); err == nil || err.Error() != "Password is too similar to the common dictionary password test1234 (88% match)" {
- t.Error(err)
- return
- }
- // Test EvalPasswordStrength
- if score, warn, err := eud.EvalPasswordStrength("fritz", "aaa"); fmt.Sprintf("%v#%v#%v", score, warn, err) != "0#[]#Password matches a common dictionary password; Password must be at least 8 characters long; Password must contain an upper case character; Password must contain a number; Password must contain a special character; Password must not contain a same character sequence" {
- t.Error("Unexpected result:", fmt.Sprintf("%v#%v#%v", score, warn, err))
- return
- }
- if score, warn, err := eud.EvalPasswordStrength("fritz", "#Secr3ttest"); fmt.Sprintf("%v#%v#%v", score, warn, err) != "1#[Password should be at least 12 characters long Password should contain at least 2 upper case characters Password should contain at least 2 numbers Password should contain at least 2 special characters Password is vaguely similar to the common dictionary password secre (45% match)]#<nil>" {
- t.Error("Unexpected result:", fmt.Sprintf("%v#%v#%v", score, warn, err))
- return
- }
- if score, warn, err := eud.EvalPasswordStrength("fritz", "#SECR3TTEsT"); fmt.Sprintf("%v#%v#%v", score, warn, err) != "1#[Password should be at least 12 characters long Password should contain at least 2 lower case characters Password should contain at least 2 numbers Password should contain at least 2 special characters Password is vaguely similar to the common dictionary password secre (45% match)]#<nil>" {
- t.Error("Unexpected result:", fmt.Sprintf("%v#%v#%v", score, warn, err))
- return
- }
- if score, warn, err := eud.EvalPasswordStrength("fritz", "#ArchBoo0815!"); fmt.Sprintf("%v#%v#%v", score, warn, err) != "10#[]#<nil>" {
- t.Error("Unexpected result:", fmt.Sprintf("%v#%v#%v", score, warn, err))
- return
- }
- }
- func TestDictPasswordDetection(t *testing.T) {
- // No match
- match, word, dist := CheckForDictPassword("ZYxzzyxzzy55xz#")
- if res := fmt.Sprintf("%v#%v#%v", match, word, dist); res != "false##-1" {
- t.Error("Unexpected result:", res)
- return
- }
- // Direct match
- match, word, dist = CheckForDictPassword("fireball")
- if res := fmt.Sprintf("%v#%v#%v", match, word, dist); res != "true#fireball#0" {
- t.Error("Unexpected result:", res)
- return
- }
- // Partial match
- match, word, dist = CheckForDictPassword("testfire")
- if res := fmt.Sprintf("%v#%v#%v", match, word, dist); res != "false#testibil#4" {
- t.Error("Unexpected result:", res)
- return
- }
- match, word, dist = CheckForDictPassword("tuberbla")
- if res := fmt.Sprintf("%v#%v#%v", match, word, dist); res != "false#erbol#5" {
- t.Error("Unexpected result:", res)
- return
- }
- }
|