diff --git a/internal/crypto/password_test.go b/internal/crypto/password_test.go index 8d1f41bf3..5fdbd438d 100644 --- a/internal/crypto/password_test.go +++ b/internal/crypto/password_test.go @@ -39,24 +39,13 @@ func TestGeneratePassword(t *testing.T) { requiredChars: []string{}, length: 8, wantErr: false, - - -func TestScrypt(t *testing.T) { - testCases := []scryptTestCase{ - { - name: "Firebase Scrypt: appropriate hash", - - hash: "$fbscrypt$v=1,n=16,r=8,p=1,ss=Bw==,sk=ou9tdYTGyYm8kuR6Dt0Bp0kDuAYoXrK16mbZO4yGwAn3oLspjnN0/c41v8xZnO1n14J3MjKj1b2g6AUCAlFwMw==$qV3N5ZhVttJ3YQ==$AwQW7J1yTqKV6neJmb1GbN9zTyNGfhotrOkPS+mtxarhNcLaB4ha49lJxVMsdV3BQx0G4XD0rOe3KaZN0dSyNg==", - password: "mytestpassword", - shouldPass: true, - }, - { - name: "Firebase Scrypt: incorrect hash", - hash: "$fbscrypt$v=1,n=16,r=8,p=1,ss=Bw==,sk=ou9tdYTGyYm8kuR6Dt0Bp0kDuAYoXrK16mbZO4yGwAn3oLspjnN0/c41v8xZnO1n14J3MjKj1b2g6AUCAlFwMw==$qV3N5ZhVttJ3YQ==$differenthash", - password: "mytestpassword", - shouldPass: false, }, } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GeneratePassword(tt.requiredChars, tt.length) + if (err != nil) != tt.wantErr { t.Errorf("GeneratePassword() error = %v, wantErr %v", err, tt.wantErr) return @@ -93,4 +82,41 @@ func TestScrypt(t *testing.T) { t.Errorf("GeneratePassword() generated duplicate password: %s", p) } passwords[p] = true + } +} + +type scryptTestCase struct { + name string + hash string + password string + shouldPass bool +} + +func TestScrypt(t *testing.T) { + testCases := []scryptTestCase{ + { + name: "Firebase Scrypt: appropriate hash", + + hash: "$fbscrypt$v=1,n=16,r=8,p=1,ss=Bw==,sk=ou9tdYTGyYm8kuR6Dt0Bp0kDuAYoXrK16mbZO4yGwAn3oLspjnN0/c41v8xZnO1n14J3MjKj1b2g6AUCAlFwMw==$qV3N5ZhVttJ3YQ==$AwQW7J1yTqKV6neJmb1GbN9zTyNGfhotrOkPS+mtxarhNcLaB4ha49lJxVMsdV3BQx0G4XD0rOe3KaZN0dSyNg==", + password: "mytestpassword", + shouldPass: true, + }, + { + name: "Firebase Scrypt: incorrect hash", + hash: "$fbscrypt$v=1,n=16,r=8,p=1,ss=Bw==,sk=ou9tdYTGyYm8kuR6Dt0Bp0kDuAYoXrK16mbZO4yGwAn3oLspjnN0/c41v8xZnO1n14J3MjKj1b2g6AUCAlFwMw==$qV3N5ZhVttJ3YQ==$differenthash", + password: "mytestpassword", + shouldPass: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := CompareHashAndPassword(context.Background(), tc.hash, tc.password) + if tc.shouldPass { + assert.NoError(t, err, "Expected test case to pass, but it failed") + } else { + assert.Error(t, err, "Expected test case to fail, but it passed") + } + }) + } }