pkg.re/essentialkaos/ek.v3@v12.41.0+incompatible/passwd/passwd_test.go (about) 1 package passwd 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "testing" 12 13 . "pkg.re/essentialkaos/check.v1" 14 ) 15 16 // ////////////////////////////////////////////////////////////////////////////////// // 17 18 func Test(t *testing.T) { TestingT(t) } 19 20 type PasswdSuite struct{} 21 22 // ////////////////////////////////////////////////////////////////////////////////// // 23 24 var _ = Suite(&PasswdSuite{}) 25 26 // ////////////////////////////////////////////////////////////////////////////////// // 27 28 func (s *PasswdSuite) TestStrengthCheck(c *C) { 29 weakPass1 := "fgiaft" 30 weakPass2 := "FgA13" 31 weakPass3 := "FgaCCvfaD" 32 mediumPass := "AcDr123" 33 strongPass := "AbCDEf34%;" 34 35 c.Assert(GetPasswordStrength(weakPass1), Equals, STRENGTH_WEAK) 36 c.Assert(GetPasswordStrength(weakPass2), Equals, STRENGTH_WEAK) 37 c.Assert(GetPasswordStrength(weakPass3), Equals, STRENGTH_WEAK) 38 c.Assert(GetPasswordStrength(mediumPass), Equals, STRENGTH_MEDIUM) 39 c.Assert(GetPasswordStrength(strongPass), Equals, STRENGTH_STRONG) 40 c.Assert(GetPasswordStrength(""), Equals, STRENGTH_WEAK) 41 } 42 43 func (s *PasswdSuite) TestGenPassword(c *C) { 44 c.Assert(GenPassword(0, STRENGTH_WEAK), Equals, "") 45 c.Assert(GenPassword(16, STRENGTH_WEAK), HasLen, 16) 46 c.Assert(GetPasswordStrength(GenPassword(16, STRENGTH_WEAK)), Equals, STRENGTH_WEAK) 47 c.Assert(GetPasswordStrength(GenPassword(16, STRENGTH_MEDIUM)), Equals, STRENGTH_MEDIUM) 48 c.Assert(GetPasswordStrength(GenPassword(16, STRENGTH_STRONG)), Equals, STRENGTH_STRONG) 49 c.Assert(GetPasswordStrength(GenPassword(4, STRENGTH_STRONG)), Equals, STRENGTH_STRONG) 50 51 c.Assert(GetPasswordStrength(GenPassword(16, -100)), Equals, STRENGTH_WEAK) 52 c.Assert(GetPasswordStrength(GenPassword(4, 100)), Equals, STRENGTH_STRONG) 53 } 54 55 func (s *PasswdSuite) TestGenPasswordVariations(c *C) { 56 c.Assert(GenPasswordVariations(""), HasLen, 0) 57 c.Assert(GenPasswordVariations("test"), HasLen, 0) 58 c.Assert(GenPasswordVariations("password12345"), HasLen, 3) 59 c.Assert(GenPasswordVariations("passWORD12345"), DeepEquals, []string{ 60 "PASSword12345", "PassWORD12345", "passWORD1234", 61 }) 62 63 c.Assert(GenPasswordBytesVariations([]byte("")), HasLen, 0) 64 c.Assert(GenPasswordBytesVariations([]byte("test")), HasLen, 0) 65 c.Assert(GenPasswordBytesVariations([]byte("password12345")), HasLen, 3) 66 c.Assert(GenPasswordBytesVariations([]byte("passWORD12345")), DeepEquals, [][]byte{ 67 []byte("PASSword12345"), 68 []byte("PassWORD12345"), 69 []byte("passWORD1234"), 70 }) 71 } 72 73 func (s *PasswdSuite) TestHash(c *C) { 74 hp, err := Encrypt("Test123", "ABCD1234ABCD1234") 75 76 c.Assert(hp, NotNil) 77 c.Assert(err, IsNil) 78 79 hp, err = Hash("Test123", "ABCD1234ABCD1234") 80 81 c.Assert(hp, NotNil) 82 c.Assert(err, IsNil) 83 84 c.Assert(Check("Test123", "ABCD1234ABCD1234", hp), Equals, true) 85 c.Assert(Check("Test123", "ABCD1234ABCD1234", "A1236"), Equals, false) 86 c.Assert(Check("Test123", "ABCD1234ABCD1234", "VEVTdA"), Equals, false) 87 c.Assert(Check("Test123", "", hp), Equals, false) 88 c.Assert(Check("", "ABCD1234ABCD1234", hp), Equals, false) 89 c.Assert(Check("", "ABCD1234ABCD1234", hp), Equals, false) 90 91 c.Assert(Check("TEST", "ABCD1234ABCD1234", "\n\n\n\n"), Equals, false) 92 c.Assert(Check("TEST", "ABCD1234ABCD1234", "0000000000000000000000"), Equals, false) 93 } 94 95 func (s *PasswdSuite) TestHashErrors(c *C) { 96 var err error 97 98 _, err = Hash("", "ABCD1234ABCD1234") 99 100 c.Assert(err, NotNil) 101 c.Assert(err.Error(), Equals, "Password can't be empty") 102 103 _, err = Hash("Test123", "") 104 105 c.Assert(err, NotNil) 106 c.Assert(err.Error(), Equals, "Pepper can't be empty") 107 108 _, err = Hash("Test123", "ABCD1234ABCD12") 109 110 c.Assert(err, NotNil) 111 c.Assert(err.Error(), Equals, "Pepper have invalid size") 112 113 _, ok := unpadData([]byte("-")) 114 115 c.Assert(ok, Equals, false) 116 } 117 118 func (s *PasswdSuite) BenchmarkHash(c *C) { 119 for i := 0; i < c.N; i++ { 120 Hash("Test123", "ABCD1234ABCD1234") 121 } 122 } 123 124 func (s *PasswdSuite) BenchmarkCheck(c *C) { 125 for i := 0; i < c.N; i++ { 126 Check("Test123", "ABCD1234ABCD1234", "jXtzmneskO_ht9VNsuwq68O-jwj3PBxewGrr3YUKf8f7zPqNSlO-Eg7x2KlmoK-wOivvvdaiDpDH_3o5LdWP7ULf6K490KpoNhTZ5XOfaYc") 127 } 128 }