github.com/tommi2day/pwcli@v0.0.0-20240317203041-4d1177a5ab91/cmd/hash_test.go (about) 1 package cmd 2 3 import ( 4 "testing" 5 6 "golang.org/x/crypto/bcrypt" 7 8 "github.com/tommi2day/gomodules/common" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 const hashPassword = "testHashPassword" 15 const hashUsername = "testHashUsername" 16 17 func TestHash(t *testing.T) { 18 var out string 19 var err error 20 t.Run("TestHashMD5", func(t *testing.T) { 21 args := []string{ 22 "hash", 23 "--hash-method=md5", 24 "--username", hashUsername, 25 "--password", hashPassword, 26 "--info", 27 "--unit-test", 28 } 29 out, err = common.CmdRun(RootCmd, args) 30 require.NoErrorf(t, err, "hash md5 command should not return an error:%s", err) 31 assert.Contains(t, out, "md5ebcd", "Output should contain md5 prefix") 32 t.Logf(out) 33 }) 34 t.Run("TestHashScram", func(t *testing.T) { 35 args := []string{ 36 "hash", 37 "--hash-method=scram", 38 "--username", hashUsername, 39 "--password", hashPassword, 40 "--info", 41 "--unit-test", 42 } 43 out, err = common.CmdRun(RootCmd, args) 44 require.NoErrorf(t, err, "hash scram command should not return an error:%s", err) 45 assert.Contains(t, out, "SCRAM-SHA-256$4096:", "Output should contain SCRAM-SHA-256 header") 46 t.Logf(out) 47 }) 48 t.Run("TestHashBcryptCompare", func(t *testing.T) { 49 var hash string 50 hash, err = doBcrypt(hashPassword) 51 err = bcrypt.CompareHashAndPassword([]byte(hash), []byte(hashPassword)) 52 require.NoErrorf(t, err, "bcrypt compare should not return an error:%s", err) 53 }) 54 t.Run("TestHashBcrypt", func(t *testing.T) { 55 args := []string{ 56 "hash", 57 "--hash-method=bcrypt", 58 "--password", hashPassword, 59 "--info", 60 "--unit-test", 61 } 62 out, err = common.CmdRun(RootCmd, args) 63 require.NoErrorf(t, err, "hash bcrypt command should not return an error:%s", err) 64 assert.Contains(t, out, "$2a$", "Output should contain bcrypt header") 65 t.Logf(out) 66 }) 67 }