github.com/goravel/framework@v1.13.9/hash/application_test.go (about)

     1  package hash
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/suite"
     7  
     8  	configmock "github.com/goravel/framework/contracts/config/mocks"
     9  	"github.com/goravel/framework/contracts/hash"
    10  )
    11  
    12  type ApplicationTestSuite struct {
    13  	suite.Suite
    14  	hashers map[string]hash.Hash
    15  }
    16  
    17  func TestApplicationTestSuite(t *testing.T) {
    18  	mockConfig := &configmock.Config{}
    19  	argon2idHasher := getArgon2idHasher(mockConfig)
    20  	bcryptHasher := getBcryptHasher(mockConfig)
    21  
    22  	suite.Run(t, &ApplicationTestSuite{
    23  		hashers: map[string]hash.Hash{
    24  			"argon2id": argon2idHasher,
    25  			"bcrypt":   bcryptHasher,
    26  		},
    27  	})
    28  	mockConfig.AssertExpectations(t)
    29  }
    30  
    31  func (s *ApplicationTestSuite) SetupTest() {
    32  
    33  }
    34  
    35  func (s *ApplicationTestSuite) TestMakeHash() {
    36  	for name, hasher := range s.hashers {
    37  		s.Run(name, func() {
    38  			s.NotEmpty(hasher.Make("password"))
    39  		})
    40  	}
    41  }
    42  
    43  func (s *ApplicationTestSuite) TestCheckHash() {
    44  	for name, hasher := range s.hashers {
    45  		s.Run(name, func() {
    46  			value, err := hasher.Make("password")
    47  			s.NoError(err)
    48  			s.True(hasher.Check("password", value))
    49  			s.False(hasher.Check("password1", value))
    50  			s.False(hasher.Check("password", "hash"))
    51  			s.False(hasher.Check("password", "hashhash"))
    52  			s.False(hasher.Check("password", "$argon2id$v=20$m=16,t=2,p=1$dTltTmtGb0JmNE9Zb0lTeQ$2lHJsAodBnV4u7j39gj7Uw"))
    53  			s.False(hasher.Check("password", "$argon2id$v=$m=16,t=2,p=1$dTltTmtGb0JmNE9Zb0lTeQ$2lHJsAodBnV4u7j39gj7Uw"))
    54  			s.False(hasher.Check("password", "$argon2id$v=19$m=16,t=2$dTltTmtGb0JmNE9Zb0lTeQ$2lHJsAodBnV4u7j39gj7Uw"))
    55  			s.False(hasher.Check("password", "$argon2id$v=19$m=16,t=2,p=1$dTltTmtGb0JmNE9Zb0lTeQ$123456"))
    56  			s.False(hasher.Check("password", "$argon2id$v=19$m=16,t=2,p=1$123456$2lHJsAodBnV4u7j39gj7xx"))
    57  		})
    58  	}
    59  }
    60  
    61  func (s *ApplicationTestSuite) TestConfigurationOverride() {
    62  	value := "$argon2id$v=19$m=65536,t=8,p=1$NlVjQm5PQUdWTHVTM1RBUg$Q5T7WfeCI7ucIdk6Na6AdQ"
    63  	s.True(s.hashers["argon2id"].Check("goravel", value))
    64  	s.True(s.hashers["argon2id"].NeedsRehash(value))
    65  }
    66  
    67  func (s *ApplicationTestSuite) TestNeedsRehash() {
    68  	for name, hasher := range s.hashers {
    69  		s.Run(name, func() {
    70  			value, err := hasher.Make("password")
    71  			s.NoError(err)
    72  			s.False(hasher.NeedsRehash(value))
    73  			s.True(hasher.NeedsRehash("hash"))
    74  			s.True(hasher.NeedsRehash("hashhash"))
    75  			s.True(hasher.NeedsRehash("$argon2id$v=$m=16,t=2,p=1$dTltTmtGb0JmNE9Zb0lTeQ$2lHJsAodBnV4u7j39gj7Uw"))
    76  			s.True(hasher.NeedsRehash("$argon2id$v=19$m=16,t=2$dTltTmtGb0JmNE9Zb0lTeQ$2lHJsAodBnV4u7j39gj7Uw"))
    77  		})
    78  	}
    79  }
    80  
    81  func getArgon2idHasher(mockConfig *configmock.Config) *Argon2id {
    82  	mockConfig.On("GetInt", "hashing.argon2id.memory", 65536).Return(65536).Once()
    83  	mockConfig.On("GetInt", "hashing.argon2id.time", 4).Return(4).Once()
    84  	mockConfig.On("GetInt", "hashing.argon2id.threads", 1).Return(1).Once()
    85  
    86  	return NewArgon2id(mockConfig)
    87  }
    88  
    89  func getBcryptHasher(mockConfig *configmock.Config) *Bcrypt {
    90  	mockConfig.On("GetInt", "hashing.bcrypt.rounds", 10).Return(10).Once()
    91  
    92  	return NewBcrypt(mockConfig)
    93  }